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