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 #ifdef BGTASKMGR_ENABLE
17 #include "time_service_client.h"
18
19 #include "bundle_active_log.h"
20 #include "bundle_active_app_state_observer.h"
21 #include "bundle_active_report_handler.h"
22 #include "bundle_active_event.h"
23 #include "bundle_active_account_helper.h"
24 #include "bundle_active_continuous_task_observer.h"
25 #include "bundle_active_report_controller.h"
26
27 namespace OHOS {
28 namespace DeviceUsageStats {
29
OnContinuousTaskStart(const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)30 void BundleActiveContinuousTaskObserver::OnContinuousTaskStart(
31 const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo>& continuousTaskCallbackInfo)
32 {
33 ReportContinuousTaskEvent(continuousTaskCallbackInfo, true);
34 }
35
OnContinuousTaskStop(const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)36 void BundleActiveContinuousTaskObserver::OnContinuousTaskStop(
37 const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo> &continuousTaskCallbackInfo)
38 {
39 ReportContinuousTaskEvent(continuousTaskCallbackInfo, false);
40 }
41
OnRemoteDied(const wptr<IRemoteObject> & object)42 void BundleActiveContinuousTaskObserver::OnRemoteDied(const wptr<IRemoteObject> &object)
43 {
44 isRemoteDied_.store(true);
45 }
46
GetBundleMgr()47 bool BundleActiveContinuousTaskObserver::GetBundleMgr()
48 {
49 if (!bundleMgr_) {
50 sptr<ISystemAbilityManager> systemAbilityManager =
51 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
52 if (!systemAbilityManager) {
53 BUNDLE_ACTIVE_LOGE("Failed to get system ability mgr.");
54 return false;
55 }
56 sptr<IRemoteObject> remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
57 if (!remoteObject) {
58 BUNDLE_ACTIVE_LOGE("Failed to get bundle manager service.");
59 return false;
60 }
61 bundleMgr_ = iface_cast<IBundleMgr>(remoteObject);
62 if (!bundleMgr_) {
63 BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability, bundleMgr_");
64 return false;
65 }
66 auto object = bundleMgr_->AsObject();
67 if (!object) {
68 BUNDLE_ACTIVE_LOGE("Failed to get system bundle manager services ability, bundleMgr_->AsObject()");
69 return false;
70 }
71 }
72 return true;
73 }
74
ReportContinuousTaskEvent(const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo,const bool isStart)75 void BundleActiveContinuousTaskObserver::ReportContinuousTaskEvent(
76 const std::shared_ptr<OHOS::BackgroundTaskMgr::ContinuousTaskCallbackInfo>& continuousTaskCallbackInfo,
77 const bool isStart)
78 {
79 int32_t uid = continuousTaskCallbackInfo->GetCreatorUid();
80 pid_t pid = continuousTaskCallbackInfo->GetCreatorPid();
81 std::string continuousTaskAbilityName_ = continuousTaskCallbackInfo->GetAbilityName();
82 int32_t userId = -1;
83 std::string bundleName = "";
84 if (GetBundleMgr()) {
85 bundleMgr_->GetNameForUid(uid, bundleName);
86 } else {
87 BUNDLE_ACTIVE_LOGE("Get bundle mgr failed!");
88 return;
89 }
90 OHOS::ErrCode ret = BundleActiveAccountHelper::GetUserId(uid, userId);
91 if (ret == ERR_OK && userId != -1 && !bundleName.empty()) {
92 BundleActiveReportHandlerObject tmpHandlerObject(userId, "");
93 BundleActiveEvent event(bundleName, continuousTaskAbilityName_, uid);
94 sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
95 tmpHandlerObject.event_ = event;
96 tmpHandlerObject.event_.timeStamp_ = timer->GetBootTimeMs();
97 if (isStart) {
98 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::LONG_TIME_TASK_STARTTED;
99 } else {
100 tmpHandlerObject.event_.eventId_ = BundleActiveEvent::LONG_TIME_TASK_ENDED;
101 }
102 BUNDLE_ACTIVE_LOGI("OnContinuousTaskStart id is %{public}d, bundle name is %{public}s, "
103 "ability name is %{public}s, event id is %{public}d,"
104 "uid is %{public}d, pid is %{public}d",
105 tmpHandlerObject.userId_, tmpHandlerObject.event_.bundleName_.c_str(),
106 tmpHandlerObject.event_.continuousTaskAbilityName_.c_str(), tmpHandlerObject.event_.eventId_,
107 uid, pid);
108 auto reportHandler = BundleActiveReportController::GetInstance().GetBundleReportHandler();
109 if (reportHandler != nullptr) {
110 BUNDLE_ACTIVE_LOGI("BundleActiveAppStateObserver::OnAbilityStateChanged handler not null, SEND");
111 std::shared_ptr<BundleActiveReportHandlerObject> handlerobjToPtr =
112 std::make_shared<BundleActiveReportHandlerObject>(tmpHandlerObject);
113 reportHandler->SendEvent(BundleActiveReportHandler::MSG_REPORT_EVENT, handlerobjToPtr);
114 }
115 }
116 }
117 } // namespace DeviceUsageStats
118 } // namespace OHOS
119
120 #endif