• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 BUNDLE_ACTIVE_SERVICE_H
17 #define BUNDLE_ACTIVE_SERVICE_H
18 
19 #include "bundle_mgr_interface.h"
20 #include "singleton.h"
21 
22 #include "app_mgr_interface.h"
23 #include "ibundle_active_service.h"
24 #include "bundle_active_stub.h"
25 #include "bundle_active_core.h"
26 #include "bundle_active_report_handler.h"
27 #include "bundle_active_shutdown_callback_service.h"
28 #include "bundle_active_power_state_callback_service.h"
29 #include "bundle_active_app_state_observer.h"
30 #include "bundle_active_continuous_task_observer.h"
31 #include "bundle_active_account_helper.h"
32 #include "file_ex.h"
33 #include "string_ex.h"
34 #include "system_ability.h"
35 
36 namespace OHOS {
37 namespace DeviceUsageStats {
38 class BundleActiveService : public SystemAbility, public BundleActiveStub,
39     public std::enable_shared_from_this<BundleActiveService> {
40     DISALLOW_COPY_AND_MOVE(BundleActiveService);
41     DECLARE_SYSTEM_ABILITY(BundleActiveService);
42     DECLARE_DELAYED_SINGLETON(BundleActiveService);
43 public:
44     using IBundleMgr = OHOS::AppExecFwk::IBundleMgr;
45     using BundleInfo = OHOS::AppExecFwk::BundleInfo;
46     using BundleFlag = OHOS::AppExecFwk::BundleFlag;
47     using HapModuleInfo = OHOS::AppExecFwk::HapModuleInfo;
48     using AbilityInfo = OHOS::AppExecFwk::AbilityInfo;
49     using ApplicationInfo = OHOS::AppExecFwk::ApplicationInfo;
50     using ApplicationFlag = OHOS::AppExecFwk::ApplicationFlag;
51     using AbilityType = OHOS::AppExecFwk::AbilityType;
52 
53     /**
54      * @brief ReportEvent, used to report event.
55      *
56      * @param event .
57      * @param userId .
58      * @return errCode.
59      */
60     ErrCode ReportEvent(BundleActiveEvent& event, const int32_t userId) override;
61 
62     /**
63      * @brief IsBundleIdle, used to check whether specific bundle is idle.
64      *
65      * @param isBundleIdle The result of IsBundleIdle.
66      * @param bundleName .
67      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
68      * @return errCode.
69      */
70     ErrCode IsBundleIdle(bool& isBundleIdle, const std::string& bundleName, int32_t userId) override;
71 
72     /**
73      * @brief QueryBundleStatsInfoByInterval, query all bundle usage statistics in specific time span for calling user.
74      *
75      * @param PackageStats .
76      * @param intervalType .
77      * @param beginTime .
78      * @param endTime .
79      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
80      * @return errCode.
81      */
82     ErrCode QueryBundleStatsInfoByInterval(std::vector<BundleActivePackageStats>& PackageStats,
83         const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId) override;
84 
85     /**
86      * @brief QueryBundleEvents, query all events in specific time span for calling user.
87      *
88      * @param bundleActiveEvents .
89      * @param beginTime .
90      * @param endTime .
91      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
92      * @return errCode.
93      */
94     ErrCode QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents, const int64_t beginTime,
95         const int64_t endTime, int32_t userId) override;
96 
97     /**
98      * @brief SetAppGroup, set specific bundle of specific user to a priority group.
99      *
100      * @param bundleName .
101      * @param newGroup .
102      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
103      * @return errCode.
104      */
105     ErrCode SetAppGroup(const std::string& bundleName, int32_t newGroup, int32_t userId) override;
106 
107     /**
108      * @brief QueryBundleStatsInfos, query bundle usage statistics in specific time span for calling bundle.
109      *
110      * @param bundleActivePackageStats, The result of QueryBundleStatsInfos.
111      * @param intervalType .
112      * @param beginTime .
113      * @param endTime .
114      * @return errCode.
115      */
116     ErrCode QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& bundleActivePackageStats,
117         const int32_t intervalType, const int64_t beginTime, const int64_t endTime) override;
118 
119     /**
120      * @brief QueryCurrentBundleEvents, query bundle usage statistics in specific time span for calling bundle.
121      *
122      * @param bundleActiveEvents the std::vector<BundleActiveEvent>, as the result of QueryCurrentBundleEvents.
123      * @param beginTime .
124      * @param endTime .
125      * @return errCode.
126      */
127     ErrCode QueryCurrentBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents,
128         const int64_t beginTime, const int64_t endTime) override;
129 
130     /**
131      * @brief QueryAppGroup, query appGroup by bundleName and userId.
132      *
133      * @param appGroup as the result of QueryAppGroup.
134      * @param bundleName .
135      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
136      * @return errCode.
137      */
138     ErrCode QueryAppGroup(int32_t& appGroup, std::string& bundleName, const int32_t userId) override;
139 
140     /**
141      * @brief QueryModuleUsageRecords, query all from usage statistics in specific time span for calling user.
142      *
143      * @param maxNum .
144      * @param results .
145      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
146      * @return errCode.
147      */
148     ErrCode QueryModuleUsageRecords(int32_t maxNum, std::vector<BundleActiveModuleRecord>& results,
149         int32_t userId = -1) override;
150 
151     /**
152      * @brief QueryDeviceEventStats, query all from event stats in specific time span for calling user.
153      *
154      * @param beginTime .
155      * @param endTime .
156      * @param eventStats .
157      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
158      * @return errCode.
159      */
160     ErrCode QueryDeviceEventStats(int64_t beginTime, int64_t endTime,
161         std::vector<BundleActiveEventStats>& eventStats, int32_t userId) override;
162 
163     /**
164      * @brief QueryNotificationEventStats, query all app notification number in specific time span for calling user.
165      *
166      * @param beginTime .
167      * @param endTime .
168      * @param eventStats .
169      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
170      * @return errCode.
171      */
172     ErrCode QueryNotificationEventStats(int64_t beginTime, int64_t endTime,
173         std::vector<BundleActiveEventStats>& eventStats, int32_t userId) override;
174 
175     /**
176      * @brief BundleActiveService, default constructor.
177      *
178      * @param systemAbilityId .
179      * @param runOnCreate .
180      */
181     BundleActiveService(const int32_t systemAbilityId, bool runOnCreate);
182 
183     /**
184      * @brief RegisterAppGroupCallBack, observe bundle group change event.
185      *
186      * @param observer .
187      * @return errCode.
188      */
189     int32_t RegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer) override;
190 
191     /**
192      * @brief UnRegisterAppGroupCallBack, unobserve bundle group change event.
193      *
194      * @param observer .
195      * @return errCode.
196      */
197     int32_t UnRegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer) override;
198 
199     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
200 
201 protected:
202     /**
203      * @brief The OnStart callback.
204      */
205     void OnStart() override;
206     /**
207      * @brief The OnStop callback.
208      */
209     void OnStop() override;
210 
211 private:
212     std::shared_ptr<BundleActiveCore> bundleActiveCore_;
213     std::shared_ptr<BundleActiveReportHandler> reportHandler_;
214     sptr<BundleActiveAppStateObserver> appStateObserver_;
215 #ifdef BGTASKMGR_ENABLE
216     std::shared_ptr<BundleActiveContinuousTaskObserver> continuousTaskObserver_;
217 #endif
218     sptr<IBundleMgr> sptrBundleMgr_;
219     sptr<BundleActiveShutdownCallbackService> shutdownCallback_;
220     sptr<BundleActivePowerStateCallbackService> powerStateCallback_;
221     std::shared_ptr<AppExecFwk::EventRunner> runner_;
222     std::shared_ptr<AppExecFwk::EventHandler> handler_;
223     bool ready_ {false};
224     int32_t ConvertIntervalType(const int32_t intervalType);
225     void InitNecessaryState();
226     void InitService();
227     ErrCode GetBundleMgrProxy();
228     ErrCode CheckBundleIsSystemAppAndHasPermission(const int32_t uid,
229         OHOS::Security::AccessToken::AccessTokenID tokenId);
230     ErrCode CheckSystemAppOrNativePermission(const int32_t uid, OHOS::Security::AccessToken::AccessTokenID tokenId);
231     ErrCode CheckNativePermission(OHOS::Security::AccessToken::AccessTokenID tokenId);
232     void InitAppStateSubscriber(const std::shared_ptr<BundleActiveReportHandler>& reportHandler);
233     void InitContinuousSubscriber(const std::shared_ptr<BundleActiveReportHandler>& reportHandler);
234     bool SubscribeAppState();
235     bool SubscribeContinuousTask();
236     OHOS::sptr<OHOS::AppExecFwk::IAppMgr> GetAppManagerInstance();
237     void QueryModuleRecordInfos(BundleActiveModuleRecord& moduleRecord);
238     void SerModuleProperties(const HapModuleInfo& hapModuleInfo,
239     const ApplicationInfo& appInfo, const AbilityInfo& abilityInfo, BundleActiveModuleRecord& moduleRecord);
240     void DumpUsage(std::string &result);
241     int32_t ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
242 };
243 }  // namespace DeviceUsageStats
244 }  // namespace OHOS
245 #endif  // BUNDLE_ACTIVE_SERVICE_H
246 
247