• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (decision_->IsFrontApp(info->GetPkg(), info->GetUid())) {
57         auto bgTask = service_.promote();
58         if (bgTask == nullptr) {
59             BGTASK_LOGE("fail to get bgTask service.");
60             return;
61         }
62         BGTASK_LOGI("handle watchdog, force cancel requestId: %{public}d", requestId);
63         bgTask->ForceCancelSuspendDelay(requestId);
64     } else {
65         BGTASK_LOGI("handle application background, kill it, requestId: %{public}d", requestId);
66         // do kill
67         if (!KillApplicationByUid(info->GetPkg(), info->GetUid())) {
68             BGTASK_LOGE("fail to kill running application");
69         }
70     }
71 }
72 
KillApplicationByUid(const std::string & bundleName,const int32_t uid)73 bool Watchdog::KillApplicationByUid(const std::string &bundleName, const int32_t uid)
74 {
75     BGTASK_LOGI("kill running application, app name is %{public}s, uid is %{public}d",
76         bundleName.c_str(), uid);
77     if (appMgrClient_ == nullptr) {
78         appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>();
79         if (appMgrClient_ == nullptr) {
80             BGTASK_LOGE("failed to get appMgrClient");
81             return false;
82         }
83         int32_t result = static_cast<int32_t>(appMgrClient_->ConnectAppMgrService());
84         if (result != ERR_OK) {
85             BGTASK_LOGE("failed to ConnectAppMgrService");
86             return false;
87         }
88     }
89     int32_t ret = (int32_t)appMgrClient_->KillApplicationByUid(bundleName, uid);
90     if (ret != ERR_OK) {
91         BGTASK_LOGE("Fail to kill application by uid.");
92         return false;
93     }
94 
95     return true;
96 }
97 }  // namespace BackgroundTaskMgr
98 }  // namespace OHOS