1 // Copyright (C) 2024 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 //! trait for build a system ability
15 
16 use std::pin::Pin;
17 
18 use ipc::remote::RemoteStub;
19 
20 use crate::exts::SystemAbility;
21 use crate::wrapper::{
22     AbilityStub, AbilityWrapper, BuildSystemAbility, SystemAbilityOnDemandReason,
23     SystemAbilityWrapper,
24 };
25 
26 #[allow(unused)]
27 pub trait Ability {
on_start(&self, handler: Handler)28     fn on_start(&self, handler: Handler) {}
29 
on_stop(&self)30     fn on_stop(&self) {}
31 
on_dump(&self)32     fn on_dump(&self) {}
33 
on_idle(&self, reason: SystemAbilityOnDemandReason) -> i3234     fn on_idle(&self, reason: SystemAbilityOnDemandReason) -> i32 {
35         0
36     }
37 
on_active(&self, reason: SystemAbilityOnDemandReason)38     fn on_active(&self, reason: SystemAbilityOnDemandReason) {}
39 
on_start_with_reason(&self, reason: SystemAbilityOnDemandReason, handler: Handler)40     fn on_start_with_reason(&self, reason: SystemAbilityOnDemandReason, handler: Handler) {
41         self.on_start(handler);
42     }
43 
on_stop_with_reason(&self, reason: SystemAbilityOnDemandReason)44     fn on_stop_with_reason(&self, reason: SystemAbilityOnDemandReason) {
45         self.on_stop();
46     }
47 
on_system_ability_load_event(&self, said: i32, device_id: String)48     fn on_system_ability_load_event(&self, said: i32, device_id: String) {}
49 
on_system_ability_remove_event(&self, said: i32, device_id: String)50     fn on_system_ability_remove_event(&self, said: i32, device_id: String) {}
51 
on_device_level_changed(&self, change_type: i32, level: i32, action: String)52     fn on_device_level_changed(&self, change_type: i32, level: i32, action: String) {}
53 
build_system_ability(self, said: i32, run_on_create: bool) -> Option<SystemAbility> where Self: Sized + 'static,54     fn build_system_ability(self, said: i32, run_on_create: bool) -> Option<SystemAbility>
55     where
56         Self: Sized + 'static,
57     {
58         info!("build system ability");
59         let ability = Box::new(AbilityWrapper {
60             inner: Box::new(self),
61         });
62         let wrapper = BuildSystemAbility(ability, said, run_on_create);
63         if wrapper.is_null() {
64             None
65         } else {
66             Some(SystemAbility::new(wrapper))
67         }
68     }
69 }
70 
71 #[derive(Clone)]
72 pub struct Handler {
73     pub(crate) inner: *mut SystemAbilityWrapper,
74 }
75 
76 impl Handler {
publish<A: RemoteStub + 'static>(&self, ability: A) -> bool77     pub fn publish<A: RemoteStub + 'static>(&self, ability: A) -> bool {
78         let ability = Box::new(AbilityStub::new(ability));
79         let system_ability = unsafe { Pin::new_unchecked(&mut *self.inner) };
80         system_ability.PublishWrapper(ability)
81     }
82 
cancel_idle(&self) -> bool83     pub fn cancel_idle(&self) -> bool {
84         let system_ability = unsafe { Pin::new_unchecked(&mut *self.inner) };
85         system_ability.CancelIdleWrapper()
86     }
87 
add_system_ability_listen(&self, system_ability_id: i32) -> bool88     pub fn add_system_ability_listen(&self, system_ability_id: i32) -> bool {
89         let system_ability = unsafe { Pin::new_unchecked(&mut *self.inner) };
90         system_ability.AddSystemAbilityListener(system_ability_id)
91     }
92 
remove_system_ability_listen(&self, system_ability_id: i32) -> bool93     pub fn remove_system_ability_listen(&self, system_ability_id: i32) -> bool {
94         let system_ability = unsafe { Pin::new_unchecked(&mut *self.inner) };
95         system_ability.RemoveSystemAbilityListener(system_ability_id)
96     }
97 }
98