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