1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef SYSTEM_ABILITY_H 17 #define SYSTEM_ABILITY_H 18 19 #include <string> 20 #include <unordered_map> 21 #include <vector> 22 23 #include "iremote_object.h" 24 #include "refbase.h" 25 #include "system_ability_ondemand_reason.h" 26 27 class CSystemAbilityInnerService; 28 namespace OHOS { 29 #define REGISTER_SYSTEM_ABILITY_BY_ID(abilityClassName, systemAbilityId, runOnCreate) \ 30 const bool abilityClassName##_##RegisterResult = \ 31 SystemAbility::MakeAndRegisterAbility(new abilityClassName(systemAbilityId, runOnCreate)); 32 33 #define DECLEAR_SYSTEM_ABILITY(className) \ 34 public: \ 35 virtual std::string GetClassName() override { \ 36 return #className; \ 37 } 38 39 #define DECLEAR_BASE_SYSTEM_ABILITY(className) \ 40 public: \ 41 virtual std::string GetClassName() = 0 42 43 #define DECLARE_SYSTEM_ABILITY(className) \ 44 public: \ 45 virtual std::string GetClassName() override { \ 46 return #className; \ 47 } 48 49 #define DECLARE_BASE_SYSTEM_ABILITY(className) \ 50 public: \ 51 virtual std::string GetClassName() = 0 52 53 enum class SystemAbilityState { 54 NOT_LOADED = 0, 55 ACTIVE, 56 IDLE, 57 }; 58 /** 59 * @class SystemAbility. 60 * SystemAbility provides a base class for each SA. 61 */ 62 class SystemAbility { 63 DECLARE_BASE_SYSTEM_ABILITY(SystemAbility); 64 65 public: 66 /** 67 * MakeAndRegisterAbility, Register SA into the localAbilityManager. 68 * 69 * @param systemAbility, SA required to register. 70 * @return Returns true on success. 71 */ 72 static bool MakeAndRegisterAbility(SystemAbility* systemAbility); 73 74 /** 75 * AddSystemAbilityListener, Provide the function of SA monitoring and waiting for other SA. 76 * 77 * @param systemAbilityId, said that needs to be monitored. 78 * @return Returns true on success. 79 */ 80 bool AddSystemAbilityListener(int32_t systemAbilityId); 81 82 /** 83 * RemoveSystemAbilityListener, Remove the SA you are listening to. 84 * 85 * @param systemAbilityId, The listening said that needs to be removed. 86 * @return Returns true on success. 87 */ 88 bool RemoveSystemAbilityListener(int32_t systemAbilityId); 89 90 protected: 91 /** 92 * OnDump, Dump sa. 93 * 94 * @return Returns void. 95 */ 96 virtual void OnDump(); 97 98 /** 99 * OnStart, The user needs to override onstart, initialize and publish SA. 100 * 101 * @return Returns void. 102 */ 103 virtual void OnStart(); 104 105 /** 106 * OnStart, The user needs to override onstart, initialize and publish SA. 107 * 108 * @param startReason, The reason for start SA. 109 * @return Returns void. 110 */ 111 virtual void OnStart(const SystemAbilityOnDemandReason& startReason); 112 113 /** 114 * OnIdle, The user needs to override OnIdle, OnIdle is a callback function when uninstalling SA. 115 * 116 * @param idleReason, The reason for unload SA. 117 * @return Returns void. 118 */ 119 virtual int32_t OnIdle(const SystemAbilityOnDemandReason& idleReason); 120 121 /** 122 * OnActive, The user needs to override OnActive, OnActive is a callback function when CancelIdle. 123 * 124 * @param activeReason, The reason for active SA. 125 * @return Returns void. 126 */ 127 virtual void OnActive(const SystemAbilityOnDemandReason& activeReason); 128 129 /** 130 * OnStop, The user needs to override OnStop, Onstop is called when the SA is uninstalled. 131 * 132 * @return Returns void. 133 */ 134 virtual void OnStop(); 135 136 /** 137 * OnStop, The user needs to override OnStop, Onstop is called when the SA is uninstalled. 138 * 139 * @param stopReason, The reason for stop SA. 140 * @return Returns void. 141 */ 142 virtual void OnStop(const SystemAbilityOnDemandReason& stopReason); 143 144 /** 145 * OnAddSystemAbility, OnAddSystemAbility will be called when the listening SA starts. 146 * 147 * @param systemAbilityId, The said being listened to. 148 * @param deviceId, deviceId is empty. 149 * @return void. 150 */ 151 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId); 152 153 /** 154 * OnRemoveSystemAbility, OnRemoveSystemAbility will be called when the listening SA Uninstall. 155 * 156 * @param systemAbilityId, The said being listened to. 157 * @param deviceId, deviceId is empty. 158 * @return void. 159 */ 160 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId); 161 162 /** 163 * GetSystemAbility, get systemability. 164 * 165 * @param systemAbilityId, The said number that needs to be obtained. 166 * @return nullptr indicates that sa does not exist. 167 */ 168 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId); 169 170 /** 171 * Publish, Register functions for sa. 172 * 173 * @param systemAbility, SA that need to be registered. 174 * @return True indicates successful registration. 175 */ 176 bool Publish(sptr<IRemoteObject> systemAbility); 177 178 /** 179 * CancelIdle, Cancel Uninstall, Calling the function from idle to active. 180 * 181 * @return True indicates Cancel succeeded. 182 */ 183 bool CancelIdle(); 184 185 /** 186 * StopAbility, Remove sa from samgr. 187 * 188 * @param systemAbilityId, Said to be deleted. 189 * @return void. 190 */ 191 static void StopAbility(int32_t systemAbilityId); 192 193 SystemAbility(bool runOnCreate = false); 194 SystemAbility(int32_t systemAbilityId, bool runOnCreate = false); 195 virtual ~SystemAbility(); 196 197 /** 198 * GetAbilityState, Obtain status information of sa. 199 * 200 * @return status information of sa. 201 */ 202 SystemAbilityState GetAbilityState(); 203 204 /** 205 * OnDeviceLevelChanged, OnDeviceLevelChanged will be called when listen strategy send. 206 * 207 * @param type, type is a certain device status type. 208 * @param level, level is level of a certain device status type. 209 * @param action, action is scheduling strategy. 210 * @return void. 211 */ 212 virtual void OnDeviceLevelChanged(int32_t type, int32_t level, std::string& action); 213 214 /** 215 * OnExtension, OnExtension will be called when extension is send 216 * 217 * @param extension, the system ability extension name. 218 * @param data, extension data. 219 * @param reply, extension reply. 220 * @return int32_t, error code. 221 */ 222 virtual int32_t OnExtension(const std::string& extension, MessageParcel& data, MessageParcel& reply); 223 224 private: 225 void Start(); 226 void Idle(SystemAbilityOnDemandReason& idleReason, int32_t& delayTime); 227 void Active(SystemAbilityOnDemandReason& activeReason); 228 void Stop(); 229 void SADump(); 230 int32_t GetSystemAbilitId() const; 231 void SetLibPath(const std::string& libPath); 232 const std::string& GetLibPath() const; 233 void SetDependSa(const std::vector<int32_t>& dependSa); 234 const std::vector<int32_t>& GetDependSa() const; 235 void SetRunOnCreate(bool isRunOnCreate); 236 bool IsRunOnCreate() const; 237 void SetDistributed(bool isDistributed); 238 bool GetDistributed() const; 239 void SetDumpLevel(unsigned int dumpLevel); 240 unsigned int GetDumpLevel() const; 241 void SetDependTimeout(int dependTimeout); 242 int GetDependTimeout() const; 243 bool GetRunningStatus() const; 244 void SetCapability(const std::u16string& capability); 245 const std::u16string& GetCapability() const; 246 void SetPermission(const std::u16string& defPerm); 247 void GetOnDemandReasonExtraData(SystemAbilityOnDemandReason& onDemandStartReason); 248 249 friend class LocalAbilityManager; 250 friend class ::CSystemAbilityInnerService; 251 252 private: 253 int32_t saId_ = 0; 254 std::string libPath_; 255 std::vector<int32_t> dependSa_; 256 bool isRunOnCreate_; 257 bool isDistributed_; 258 unsigned int dumpLevel_; 259 int dependTimeout_; 260 bool isRunning_; 261 SystemAbilityState abilityState_; 262 std::u16string capability_; 263 sptr<IRemoteObject> publishObj_; 264 std::u16string permission_; 265 std::recursive_mutex abilityLock; 266 }; 267 } 268 269 #endif 270