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 QueryBundleStatsInfoByInterval, query all bundle usage statistics in specific time span for calling user.
76      *
77      * @param PackageStats .
78      * @param intervalType .
79      * @param beginTime .
80      * @param endTime .
81      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
82      * @return errCode.
83      */
84     ErrCode QueryBundleStatsInfoByInterval(std::vector<BundleActivePackageStats>& PackageStats,
85         const int32_t intervalType, const int64_t beginTime, const int64_t endTime, int32_t userId) override;
86 
87     /**
88      * @brief QueryBundleEvents, query all events in specific time span for calling user.
89      *
90      * @param bundleActiveEvents .
91      * @param beginTime .
92      * @param endTime .
93      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
94      * @return errCode.
95      */
96     ErrCode QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents, const int64_t beginTime,
97         const int64_t endTime, int32_t userId) override;
98 
99     /**
100      * @brief SetAppGroup, set specific bundle of specific user to a priority group.
101      *
102      * @param bundleName .
103      * @param newGroup .
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 SetAppGroup(const std::string& bundleName, int32_t newGroup, int32_t userId) override;
108 
109     /**
110      * @brief QueryBundleStatsInfos, query bundle usage statistics in specific time span for calling bundle.
111      *
112      * @param bundleActivePackageStats, The result of QueryBundleStatsInfos.
113      * @param intervalType .
114      * @param beginTime .
115      * @param endTime .
116      * @return errCode.
117      */
118     ErrCode QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& bundleActivePackageStats,
119         const int32_t intervalType, const int64_t beginTime, const int64_t endTime) override;
120 
121     /**
122      * @brief QueryCurrentBundleEvents, query bundle usage statistics in specific time span for calling bundle.
123      *
124      * @param bundleActiveEvents the std::vector<BundleActiveEvent>, as the result of QueryCurrentBundleEvents.
125      * @param beginTime .
126      * @param endTime .
127      * @return errCode.
128      */
129     ErrCode QueryCurrentBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvents,
130         const int64_t beginTime, const int64_t endTime) override;
131 
132     /**
133      * @brief QueryAppGroup, query appGroup by bundleName and userId.
134      *
135      * @param appGroup as the result of QueryAppGroup.
136      * @param bundleName .
137      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
138      * @return errCode.
139      */
140     ErrCode QueryAppGroup(int32_t& appGroup, std::string& bundleName, const int32_t userId) override;
141 
142     /**
143      * @brief QueryModuleUsageRecords, query all from usage statistics in specific time span for calling user.
144      *
145      * @param maxNum .
146      * @param results .
147      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
148      * @return errCode.
149      */
150     ErrCode QueryModuleUsageRecords(int32_t maxNum, std::vector<BundleActiveModuleRecord>& results,
151         int32_t userId = -1) override;
152 
153     /**
154      * @brief QueryDeviceEventStats, query all from event stats in specific time span for calling user.
155      *
156      * @param beginTime .
157      * @param endTime .
158      * @param eventStats .
159      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
160      * @return errCode.
161      */
162     ErrCode QueryDeviceEventStats(int64_t beginTime, int64_t endTime,
163         std::vector<BundleActiveEventStats>& eventStats, int32_t userId) override;
164 
165     /**
166      * @brief QueryNotificationEventStats, query all app notification number in specific time span for calling user.
167      *
168      * @param beginTime .
169      * @param endTime .
170      * @param eventStats .
171      * @param userId default userId is -1 for JS API, if other SAs call this API, they should explicit define userId.
172      * @return errCode.
173      */
174     ErrCode QueryNotificationEventStats(int64_t beginTime, int64_t endTime,
175         std::vector<BundleActiveEventStats>& eventStats, int32_t userId) override;
176 
177     /**
178      * @brief BundleActiveService, default constructor.
179      *
180      * @param systemAbilityId .
181      * @param runOnCreate .
182      */
183     BundleActiveService(const int32_t systemAbilityId, bool runOnCreate);
184 
185     /**
186      * @brief RegisterAppGroupCallBack, observe bundle group change event.
187      *
188      * @param observer .
189      * @return errCode.
190      */
191     int32_t RegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer) override;
192 
193     /**
194      * @brief UnRegisterAppGroupCallBack, unobserve bundle group change event.
195      *
196      * @param observer .
197      * @return errCode.
198      */
199     int32_t UnRegisterAppGroupCallBack(const sptr<IAppGroupCallback> &observer) override;
200 
201     int32_t Dump(int32_t fd, const std::vector<std::u16string> &args) override;
202 
203 protected:
204     /**
205      * @brief The OnStart callback.
206      */
207     void OnStart() override;
208     /**
209      * @brief The OnStop callback.
210      */
211     void OnStop() override;
212 
213 private:
214     std::shared_ptr<BundleActiveCore> bundleActiveCore_;
215     std::shared_ptr<BundleActiveReportHandler> reportHandler_;
216     sptr<BundleActiveAppStateObserver> appStateObserver_;
217 #ifdef BGTASKMGR_ENABLE
218     std::shared_ptr<BundleActiveContinuousTaskObserver> continuousTaskObserver_;
219 #endif
220 #ifdef DEVICE_USAGES_STATISTICS_POWERMANGER_ENABLE
221     sptr<PowerMgr::IAsyncShutdownCallback> shutdownCallback_;
222     sptr<PowerMgr::IPowerStateCallback> powerStateCallback_;
223 #endif
224     bool ready_ {false};
225     int32_t ConvertIntervalType(const int32_t intervalType);
226     void InitNecessaryState();
227     void InitService();
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     bool AllowDump();
242     int32_t ShellDump(const std::vector<std::string> &dumpOption, std::vector<std::string> &dumpInfo);
243 };
244 }  // namespace DeviceUsageStats
245 }  // namespace OHOS
246 #endif  // BUNDLE_ACTIVE_SERVICE_H
247 
248