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