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 #include "watchdog.h"
17
18 #include "iservice_registry.h"
19 #include "system_ability_definition.h"
20
21 #include "background_task_mgr_service.h"
22 #include "transient_task_log.h"
23
24 using namespace std;
25
26 namespace OHOS {
27 namespace BackgroundTaskMgr {
28 const std::string BACKGROUND_WATCHDOG = "BgTaskWatchdog";
29
Watchdog(const wptr<BackgroundTaskMgrService> & service,const std::shared_ptr<DecisionMaker> & decision)30 Watchdog::Watchdog(const wptr<BackgroundTaskMgrService>& service, const std::shared_ptr<DecisionMaker>& decision)
31 : service_(service), decision_(decision)
32 {
33 std::shared_ptr<AppExecFwk::EventRunner> eventRunner = AppExecFwk::EventRunner::Create(BACKGROUND_WATCHDOG);
34 if (eventRunner.get() != nullptr) {
35 SetEventRunner(eventRunner);
36 }
37 }
38
AddWatchdog(int32_t requestId,const std::shared_ptr<KeyInfo> & info,int32_t interval)39 bool Watchdog::AddWatchdog(int32_t requestId, const std::shared_ptr<KeyInfo>& info, int32_t interval)
40 {
41 BGTASK_LOGD("AddWatchdog %{public}d", requestId);
42 return SendEvent(requestId, info, interval);
43 }
44
RemoveWatchdog(int32_t requestId)45 void Watchdog::RemoveWatchdog(int32_t requestId)
46 {
47 BGTASK_LOGD("RemoveWatchdog %{public}d", requestId);
48 RemoveEvent(requestId);
49 }
50
ProcessEvent(const AppExecFwk::InnerEvent::Pointer & event)51 void Watchdog::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event)
52 {
53 if (event == nullptr) {
54 return;
55 }
56 int32_t requestId = event->GetInnerEventId();
57 const shared_ptr<KeyInfo>& info = event->GetSharedObject<KeyInfo>();
58 if (decision_->IsFrontApp(info->GetPkg(), info->GetUid())) {
59 auto bgTask = service_.promote();
60 if (bgTask == nullptr) {
61 BGTASK_LOGE("fail to get bgTask service.");
62 return;
63 }
64 BGTASK_LOGI("handle watchdog, force cancel requestId: %{public}d", requestId);
65 bgTask->ForceCancelSuspendDelay(requestId);
66 } else {
67 BGTASK_LOGI("handle application background, kill it, requestId: %{public}d", requestId);
68 // do kill
69 if (!KillApplicationByUid(info->GetPkg(), info->GetUid())) {
70 BGTASK_LOGE("fail to kill running application");
71 }
72 }
73 }
74
KillApplicationByUid(const std::string & bundleName,const int32_t uid)75 bool Watchdog::KillApplicationByUid(const std::string &bundleName, const int32_t uid)
76 {
77 BGTASK_LOGI("kill running application, app name is %{public}s, uid is %{public}d",
78 bundleName.c_str(), uid);
79 if (appMgrClient_ == nullptr) {
80 appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
81 if (appMgrClient_ == nullptr) {
82 BGTASK_LOGE("failed to get appMgrClient");
83 return false;
84 }
85 int32_t result = static_cast<int32_t>(appMgrClient_->ConnectAppMgrService());
86 if (result != ERR_OK) {
87 BGTASK_LOGE("failed to ConnectAppMgrService");
88 return false;
89 }
90 }
91 int32_t ret = (int32_t)appMgrClient_->KillApplicationByUid(bundleName, uid);
92 if (ret != ERR_OK) {
93 BGTASK_LOGE("Fail to kill application by uid.");
94 return false;
95 }
96
97 return true;
98 }
99 } // namespace BackgroundTaskMgr
100 } // namespace OHOS