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