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