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_CORE_H 17 #define BUNDLE_ACTIVE_CORE_H 18 19 #include <mutex> 20 21 #include "power_mgr_client.h" 22 #include "accesstoken_kit.h" 23 #ifdef OS_ACCOUNT_PART_ENABLED 24 #include "os_account_manager.h" 25 #endif // OS_ACCOUNT_PART_ENABLED 26 #include "ibundle_active_service.h" 27 #include "remote_death_recipient.h" 28 #include "iapp_group_callback.h" 29 #include "bundle_active_debug_mode.h" 30 #include "bundle_active_stats_update_listener.h" 31 #include "bundle_state_inner_errors.h" 32 #include "bundle_active_user_service.h" 33 #include "bundle_active_group_controller.h" 34 #include "bundle_active_group_handler.h" 35 #include "bundle_active_common_event_subscriber.h" 36 #include "bundle_active_constant.h" 37 38 namespace OHOS { 39 namespace DeviceUsageStats { 40 using namespace OHOS::Security; 41 42 class BundleActiveReportHandlerObject { 43 public: 44 BundleActiveEvent event_; 45 int32_t userId_; 46 std::string bundleName_; 47 BundleActiveReportHandlerObject(); 48 BundleActiveReportHandlerObject(const int32_t userId, const std::string bundleName); 49 BundleActiveReportHandlerObject(const BundleActiveReportHandlerObject& orig); ~BundleActiveReportHandlerObject()50 ~BundleActiveReportHandlerObject() {} 51 }; 52 53 class BundleActiveReportHandler; 54 55 class BundleActiveCore : public BundleActiveStatsUpdateListener, 56 public std::enable_shared_from_this<BundleActiveCore> { 57 public: 58 BundleActiveCore(); 59 virtual ~BundleActiveCore(); 60 61 /* 62 * function: ReportEvent, used to report ability fourground/background/destroy event. 63 * parameters: event, userId 64 */ 65 int32_t ReportEvent(BundleActiveEvent& event, int32_t userId); 66 67 /* 68 * function: ReportEventToAllUserId, report flush to disk, end_of_day event to service. 69 * parameters: event 70 */ 71 int32_t ReportEventToAllUserId(BundleActiveEvent& event); 72 73 /* 74 * function: OnStatsChanged, report flush to disk, end_of_day event to service. 75 * parameters: userId 76 */ 77 void OnStatsChanged(const int32_t userId) override; 78 79 /* 80 * function: OnStatsChanged, when device reboot after more than one day, BundleActiveUserService 81 * will use it to flush group info. 82 */ 83 void OnStatsReload() override; 84 85 /* 86 * function: OnSystemUpdate, now is emtpy, later will called when system is updated. 87 * parameters: userId 88 */ 89 void OnSystemUpdate(int32_t userId) override; 90 91 /* 92 * function: OnBundleUninstalled when received a PACKATE_REMOVED commen event, 93 * BundleActiveCommonEventSubscriber call it to remove data. 94 * parameters: userId, bundleName 95 */ 96 void OnBundleUninstalled(const int32_t userId, const std::string& bundleName); 97 98 /* 99 * function: Init, BundleAciveService call it to init systemTimeShot_, realTimeShot_, 100 * create bundleGroupController_ object. 101 */ 102 void Init(); 103 104 /* 105 * function: InitBundleGroupController, BundleAciveService call it to init bundleGroupController_ object, 106 * set its handler and subscribe needed common event. 107 * create bundleGroupController_ object. 108 */ 109 void InitBundleGroupController(); 110 111 /* 112 * function: SetHandler, BundleActiveService call it to set event report handler 113 * parameters: reportHandler 114 */ 115 void SetHandler(const std::shared_ptr<BundleActiveReportHandler>& reportHandler); 116 117 /* 118 * function: RestoreToDatabase, restore bundle usage data and form data to database 119 * parameters: userId 120 */ 121 void RestoreToDatabase(const int32_t userId); 122 123 /* 124 * function: RestoreToDatabaseLocked, flush database for one user data 125 * parameters: userId 126 */ 127 void RestoreToDatabaseLocked(const int32_t userId); 128 129 /* 130 * function: ShutDown, called when device shutdown, update the in-memory stat and flush the database. 131 */ 132 void ShutDown(); 133 /* 134 * function: PreservePowerStateInfo, called when device change power state, preserve power state info. 135 */ 136 void PreservePowerStateInfo(const int32_t eventId); 137 138 /* 139 * function: queryBundleStatsInfos, query the package stat for calling user. 140 * parameters: userId, intervalType, beginTime, endTime, bundleName 141 * return: vector of BundleActivePackageStats 142 */ 143 ErrCode QueryBundleStatsInfos(std::vector<BundleActivePackageStats>& packageStats, const int32_t userId, 144 const int32_t intervalType, const int64_t beginTime, const int64_t endTime, std::string bundleName); 145 146 // query the event stat for calling user. 147 ErrCode QueryBundleEvents(std::vector<BundleActiveEvent>& bundleActiveEvent, const int32_t userId, 148 const int64_t beginTime, const int64_t endTime, std::string bundleName); 149 150 // check the app idle state for calling user. 151 int32_t IsBundleIdle(const std::string& bundleName, const int32_t userId); 152 153 // query the app group for calling app. 154 ErrCode QueryAppGroup(int32_t& appGroup, const std::string& bundleName, const int32_t userId); 155 156 ErrCode QueryModuleUsageRecords(int32_t maxNum, std::vector<BundleActiveModuleRecord>& results, int32_t userId); 157 158 /* 159 * function: QueryDeviceEventStats, query all from event stats in specific time span for calling user. 160 * parameters: beginTime, endTime, eventStats, userId, default userId is -1 for JS API, 161 * if other SAs call this API, they should explicit define userId. 162 * return: errorcode. 163 */ 164 ErrCode QueryDeviceEventStats(int64_t beginTime, int64_t endTime, 165 std::vector<BundleActiveEventStats>& eventStats, int32_t userId); 166 167 /* 168 * function: QueryNotificationEventStats, query all app notification number in specific time span for calling user. 169 * parameters: beginTime, endTime, eventStats, userId, default userId is -1 for JS API, 170 * if other SAs call this API, they should explicit define userId. 171 * return: errorcode. 172 */ 173 ErrCode QueryNotificationEventStats(int64_t beginTime, int64_t endTime, 174 std::vector<BundleActiveEventStats>& eventStats, int32_t userId); 175 176 // get the wall time and check if the wall time is changed. 177 int64_t CheckTimeChangeAndGetWallTime(int32_t userId = 0); 178 179 // convert event timestamp from boot based time to wall time. 180 void ConvertToSystemTimeLocked(BundleActiveEvent& event); 181 182 // get or create BundleActiveUserService object for specifice user. 183 std::shared_ptr<BundleActiveUserService> GetUserDataAndInitializeIfNeeded(const int32_t userId, 184 const int64_t timeStamp, const bool debug); 185 186 // when received a USER_REMOVED commen event, call it to remove data. 187 void OnUserRemoved(const int32_t userId); 188 189 // when user switched, restore old userdata. 190 void OnUserSwitched(const int32_t userId); 191 192 /* 193 * function: SetAppGroup, change bundleGroup to the newGroup. 194 * parameters: bundleName, newGroup, userId, isFlush, 195 * return: errorcode. 196 */ 197 ErrCode SetAppGroup( 198 const std::string& bundleName, const int32_t newGroup, const int32_t userId, const bool isFlush); 199 200 // get all user in device. 201 void GetAllActiveUser(std::vector<int32_t>& activatedOsAccountIds); 202 203 // when service stop, call it to unregister commen event and shutdown call back. 204 void UnRegisterSubscriber(); 205 206 // get system time in MS. 207 int64_t GetSystemTimeMs(); 208 209 /* 210 * function: RegisterAppGroupCallBack, register the observer to groupObservers. 211 * parameters: observer 212 * return: errCode. 213 */ 214 ErrCode RegisterAppGroupCallBack(const AccessToken::AccessTokenID& tokenId, 215 const sptr<IAppGroupCallback> &observer); 216 217 /* 218 * function: UnRegisterAppGroupCallBack, remove the observer from groupObservers. 219 * parameters: observer 220 * return: errCode. 221 */ 222 ErrCode UnRegisterAppGroupCallBack(const AccessToken::AccessTokenID& tokenId, 223 const sptr<IAppGroupCallback> &observer); 224 225 int32_t currentUsedUser_; 226 void OnAppGroupChanged(const AppGroupCallbackInfo& callbackInfo); 227 228 private: 229 void AddObserverDeathRecipient(const sptr<IAppGroupCallback> &observer); 230 void RemoveObserverDeathRecipient(const sptr<IAppGroupCallback> &observer); 231 void OnObserverDied(const wptr<IRemoteObject> &remote); 232 void OnObserverDiedInner(const wptr<IRemoteObject> &remote); 233 int64_t flushInterval_; 234 static const int64_t TIME_CHANGE_THRESHOLD_MILLIS = TWO_SECONDS; 235 const int32_t DEFAULT_USER_ID = -1; 236 std::map<int32_t, std::string> visibleActivities_; 237 // use weak_ptr to avoid circulate reference of core and handler. 238 std::weak_ptr<BundleActiveReportHandler> handler_; 239 std::shared_ptr<BundleActiveGroupController> bundleGroupController_; 240 std::shared_ptr<BundleActiveGroupHandler> bundleGroupHandler_; 241 int64_t systemTimeShot_; 242 int64_t realTimeShot_; 243 std::mutex mutex_; 244 std::mutex callbackMutex_; 245 std::mutex deathRecipientMutex_; 246 std::map<int32_t, std::shared_ptr<BundleActiveUserService>> userStatServices_; 247 void RegisterSubscriber(); 248 std::shared_ptr<BundleActiveCommonEventSubscriber> commonEventSubscriber_; 249 void RestoreAllData(); 250 std::map<AccessToken::AccessTokenID, sptr<IAppGroupCallback>> groupChangeObservers_; 251 std::map<sptr<IRemoteObject>, sptr<RemoteDeathRecipient>> recipientMap_; 252 void ObtainSystemEventName(BundleActiveEvent& event); 253 bool debugCore_; 254 }; 255 } // namespace DeviceUsageStats 256 } // namespace OHOS 257 #endif // BUNDLE_ACTIVE_CORE_H 258 259