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 <vector> 21 22 #include "iremote_object.h" 23 #include "refbase.h" 24 25 namespace OHOS { 26 #define REGISTER_SYSTEM_ABILITY_BY_ID(abilityClassName, systemAbilityId, runOnCreate) \ 27 const bool abilityClassName##_##RegisterResult = \ 28 SystemAbility::MakeAndRegisterAbility(new abilityClassName(systemAbilityId, runOnCreate)); 29 30 #define DECLEAR_SYSTEM_ABILITY(className) \ 31 public: \ 32 virtual std::string GetClassName() override { \ 33 return #className; \ 34 } 35 36 #define DECLEAR_BASE_SYSTEM_ABILITY(className) \ 37 public: \ 38 virtual std::string GetClassName() = 0; 39 40 #define DECLARE_SYSTEM_ABILITY(className) \ 41 public: \ 42 virtual std::string GetClassName() override { \ 43 return #className; \ 44 } 45 46 #define DECLARE_BASE_SYSTEM_ABILITY(className) \ 47 public: \ 48 virtual std::string GetClassName() = 0; 49 50 51 #define DECLARE_SYSTEM_ABILITY(className) \ 52 public: \ 53 virtual std::string GetClassName() override { \ 54 return #className; \ 55 } 56 57 #define DECLARE_BASE_SYSTEM_ABILITY(className) \ 58 public: \ 59 virtual std::string GetClassName() = 0; 60 61 class SystemAbility { 62 DECLARE_BASE_SYSTEM_ABILITY(SystemAbility); 63 64 public: 65 static bool MakeAndRegisterAbility(SystemAbility* systemAbility); 66 bool AddSystemAbilityListener(int32_t systemAbilityId); 67 bool RemoveSystemAbilityListener(int32_t systemAbilityId); 68 69 protected: 70 virtual void OnDump(); 71 virtual void OnStart(); 72 virtual void OnStop(); 73 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId); 74 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId); 75 76 sptr<IRemoteObject> GetSystemAbility(int32_t systemAbilityId); 77 bool Publish(sptr<IRemoteObject> systemAbility); 78 void StopAbility(int32_t systemAbilityId); 79 80 SystemAbility(bool runOnCreate = false); 81 SystemAbility(int32_t systemAbilityId, bool runOnCreate = false); 82 virtual ~SystemAbility(); 83 84 private: 85 void Start(); 86 void Stop(); 87 void SADump(); 88 int32_t GetSystemAbilitId() const; 89 void SetLibPath(const std::u16string& libPath); 90 const std::u16string& GetLibPath() const; 91 void SetDependSa(const std::vector<std::u16string>& dependSa); 92 const std::vector<std::u16string>& GetDependSa() const; 93 void SetRunOnCreate(bool isRunOnCreate); 94 bool IsRunOnCreate() const; 95 void SetDistributed(bool isDistributed); 96 bool GetDistributed() const; 97 void SetDumpLevel(unsigned int dumpLevel); 98 unsigned int GetDumpLevel() const; 99 void SetDependTimeout(int dependTimeout); 100 int GetDependTimeout() const; 101 bool GetRunningStatus() const; 102 void SetCapability(const std::u16string& capability); 103 const std::u16string& GetCapability() const; 104 void SetPermission(const std::u16string& defPerm); 105 106 friend class LocalAbilityManager; 107 108 private: 109 int32_t saId_ = 0; 110 std::u16string libPath_; 111 std::vector<std::u16string> dependSa_; 112 bool isRunOnCreate_; 113 bool isDistributed_; 114 unsigned int dumpLevel_; 115 int dependTimeout_; 116 bool isRunning_; 117 std::u16string capability_; 118 sptr<IRemoteObject> publishObj_; 119 std::u16string permission_; 120 }; 121 } 122 123 #endif 124