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