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