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