1 /* 2 * Copyright (c) 2023 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 OHOS_ABILITY_RUNTIME_ABILITY_AUTO_STARTUP_SERVICE_H 17 #define OHOS_ABILITY_RUNTIME_ABILITY_AUTO_STARTUP_SERVICE_H 18 19 #include <map> 20 #include <mutex> 21 #include <vector> 22 23 #include "auto_startup_info.h" 24 #include "bundle_mgr_client.h" 25 #include "bundle_mgr_helper.h" 26 #include "iremote_object.h" 27 #include "singleton.h" 28 29 namespace OHOS { 30 namespace AbilityRuntime { 31 class AbilityAutoStartupService : public std::enable_shared_from_this<AbilityAutoStartupService> { 32 public: 33 explicit AbilityAutoStartupService(); 34 35 virtual ~AbilityAutoStartupService(); 36 37 /** 38 * @brief Register auto start up callback for system api. 39 * @param callback The point of JsAbilityAutoStartupCallBack. 40 * @return Returns ERR_OK on success, others on failure. 41 */ 42 int32_t RegisterAutoStartupSystemCallback(const sptr<IRemoteObject> &callback); 43 44 /** 45 * @brief Unregister auto start up callback for system api. 46 * @param callback The point of JsAbilityAutoStartupCallBack. 47 * @return Returns ERR_OK on success, others on failure. 48 */ 49 int32_t UnregisterAutoStartupSystemCallback(const sptr<IRemoteObject> &callback); 50 51 /** 52 * @brief Set every application auto start up state. 53 * @param info The auto startup info,include bundle name, module name, ability name. 54 * @return Returns ERR_OK on success, others on failure. 55 */ 56 int32_t SetApplicationAutoStartup(const AutoStartupInfo &info); 57 58 /** 59 * @brief Cancel every application auto start up . 60 * @param info The auto startup info,include bundle name, module name, ability name. 61 * @return Returns ERR_OK on success, others on failure. 62 */ 63 int32_t CancelApplicationAutoStartup(const AutoStartupInfo &info); 64 65 /** 66 * @brief Query auto startup state all application. 67 * @param infoList Output parameters, return auto startup info list. 68 * @param infoList Input parameters, return userid. 69 * @return Returns ERR_OK on success, others on failure. 70 */ 71 int32_t QueryAllAutoStartupApplications(std::vector<AutoStartupInfo> &infoList, int32_t userId); 72 73 /** 74 * @brief Query auto startup state all application without permission. 75 * @param infoList Output parameters, return auto startup info list. 76 * @param infoList Input parameters, return userid. 77 * @return Returns ERR_OK on success, others on failure. 78 */ 79 int32_t QueryAllAutoStartupApplicationsWithoutPermission(std::vector<AutoStartupInfo> &infoList, int32_t userId); 80 81 /** 82 * @brief Delete current bundleName auto start up data. 83 * @param bundleName The current bundleName. 84 * @param accessTokenId The accessTokenId. 85 * @return Returns ERR_OK on success, others on failure. 86 */ 87 int32_t DeleteAutoStartupData(const std::string &bundleName, int32_t accessTokenId); 88 89 /** 90 * @brief Check current bundleName auto start up data. 91 * @param bundleName The current bundleName. 92 * @param uid The uid. 93 * @return Returns ERR_OK on success, others on failure. 94 */ 95 int32_t CheckAutoStartupData(const std::string &bundleName, int32_t uid); 96 97 /** 98 * @brief Set application auto start up state by EDM. 99 * @param info The auto startup info, include bundle name, module name, ability name. 100 * @param flag Indicate whether to allow the application to change the auto start up state. 101 * @return Returns ERR_OK on success, others on failure. 102 */ 103 int32_t SetApplicationAutoStartupByEDM(const AutoStartupInfo &info, bool flag); 104 105 /** 106 * @brief Cancel application auto start up state by EDM. 107 * @param info The auto startup info, include bundle name, module name, ability name. 108 * @param flag Indicate whether to allow the application to change the auto start up state. 109 * @return Returns ERR_OK on success, others on failure. 110 */ 111 int32_t CancelApplicationAutoStartupByEDM(const AutoStartupInfo &info, bool flag); 112 113 /** 114 * @class ClientDeathRecipient 115 * notices IRemoteBroker died. 116 */ 117 class ClientDeathRecipient : public IRemoteObject::DeathRecipient { 118 public: 119 /** 120 * @brief Constructor 121 */ 122 explicit ClientDeathRecipient(const std::weak_ptr<AbilityAutoStartupService> &weakPtr); 123 virtual ~ClientDeathRecipient() = default; 124 /** 125 * @brief handle remote object died event. 126 * @param remote remote object. 127 */ 128 void OnRemoteDied(const wptr<IRemoteObject> &remote) override; 129 130 private: 131 std::weak_ptr<AbilityAutoStartupService> weakPtr_; 132 }; 133 134 private: 135 int32_t InnerSetApplicationAutoStartup(const AutoStartupInfo &info); 136 int32_t InnerCancelApplicationAutoStartup(const AutoStartupInfo &info); 137 void ExecuteCallbacks(bool isCallOn, const AutoStartupInfo &info); 138 void SetDeathRecipient( 139 const sptr<IRemoteObject> &callback, const sptr<IRemoteObject::DeathRecipient> &deathRecipient); 140 void CleanResource(const wptr<IRemoteObject> &remote); 141 std::string GetSelfApplicationBundleName(); 142 bool CheckSelfApplication(const std::string &bundleName); 143 bool GetBundleInfo(const std::string &bundleName, AppExecFwk::BundleInfo &bundleInfo, int32_t uid, 144 int32_t &userId, int32_t appIndex); 145 bool GetAbilityData(const AutoStartupInfo &info, bool &isVisible, 146 std::string &abilityTypeName, std::string &accessTokenId, int32_t &userId); 147 std::string GetAbilityTypeName(AppExecFwk::AbilityInfo abilityInfo); 148 std::string GetExtensionTypeName(AppExecFwk::ExtensionAbilityInfo extensionInfo); 149 std::shared_ptr<AppExecFwk::BundleMgrClient> GetBundleMgrClient(); 150 int32_t CheckPermissionForSystem(); 151 int32_t CheckPermissionForSelf(const std::string &bundleName); 152 int32_t CheckPermissionForEDM(); 153 int32_t InnerApplicationAutoStartupByEDM(const AutoStartupInfo &info, bool isSet, bool flag); 154 int32_t GetAbilityInfo(const AutoStartupInfo &info, std::string &abilityTypeName, std::string &accessTokenId); 155 int32_t GetAbilityInfo(const AutoStartupInfo &info, std::string &abilityTypeName, 156 std::string &accessTokenId, int32_t &userId); 157 158 mutable std::mutex autoStartUpMutex_; 159 mutable std::mutex deathRecipientsMutex_; 160 std::vector<sptr<IRemoteObject>> callbackVector_; 161 std::map<std::string, sptr<IRemoteObject>> callbackMaps_; 162 std::map<sptr<IRemoteObject>, sptr<IRemoteObject::DeathRecipient>> deathRecipients_; 163 std::shared_ptr<AppExecFwk::BundleMgrClient> bundleMgrClient_; 164 }; 165 } // namespace AbilityRuntime 166 } // namespace OHOS 167 #endif // OHOS_ABILITY_RUNTIME_ABILITY_AUTO_STARTUP_SERVICE_H