11ebd3d54Sopenharmony_ci/* 21ebd3d54Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 31ebd3d54Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 41ebd3d54Sopenharmony_ci * you may not use this file except in compliance with the License. 51ebd3d54Sopenharmony_ci * You may obtain a copy of the License at 61ebd3d54Sopenharmony_ci * 71ebd3d54Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 81ebd3d54Sopenharmony_ci * 91ebd3d54Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 101ebd3d54Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 111ebd3d54Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 121ebd3d54Sopenharmony_ci * See the License for the specific language governing permissions and 131ebd3d54Sopenharmony_ci * limitations under the License. 141ebd3d54Sopenharmony_ci */ 151ebd3d54Sopenharmony_ci 161ebd3d54Sopenharmony_ci#include "watchdog.h" 171ebd3d54Sopenharmony_ci 181ebd3d54Sopenharmony_ci#include "iservice_registry.h" 191ebd3d54Sopenharmony_ci#include "system_ability_definition.h" 201ebd3d54Sopenharmony_ci 211ebd3d54Sopenharmony_ci#include "background_task_mgr_service.h" 221ebd3d54Sopenharmony_ci#include "transient_task_log.h" 231ebd3d54Sopenharmony_ci 241ebd3d54Sopenharmony_ciusing namespace std; 251ebd3d54Sopenharmony_ci 261ebd3d54Sopenharmony_cinamespace OHOS { 271ebd3d54Sopenharmony_cinamespace BackgroundTaskMgr { 281ebd3d54Sopenharmony_ci 291ebd3d54Sopenharmony_ciWatchdog::Watchdog(const wptr<BackgroundTaskMgrService>& service, const std::shared_ptr<DecisionMaker>& decision, 301ebd3d54Sopenharmony_ci const std::shared_ptr<AppExecFwk::EventRunner>& runner) : service_(service), decision_(decision) 311ebd3d54Sopenharmony_ci{ 321ebd3d54Sopenharmony_ci if (runner != nullptr) { 331ebd3d54Sopenharmony_ci SetEventRunner(runner); 341ebd3d54Sopenharmony_ci } 351ebd3d54Sopenharmony_ci} 361ebd3d54Sopenharmony_ci 371ebd3d54Sopenharmony_cibool Watchdog::AddWatchdog(int32_t requestId, const std::shared_ptr<KeyInfo>& info, int32_t interval) 381ebd3d54Sopenharmony_ci{ 391ebd3d54Sopenharmony_ci BGTASK_LOGD("AddWatchdog %{public}d", requestId); 401ebd3d54Sopenharmony_ci return SendEvent(requestId, info, interval); 411ebd3d54Sopenharmony_ci} 421ebd3d54Sopenharmony_ci 431ebd3d54Sopenharmony_civoid Watchdog::RemoveWatchdog(int32_t requestId) 441ebd3d54Sopenharmony_ci{ 451ebd3d54Sopenharmony_ci BGTASK_LOGD("RemoveWatchdog %{public}d", requestId); 461ebd3d54Sopenharmony_ci RemoveEvent(requestId); 471ebd3d54Sopenharmony_ci} 481ebd3d54Sopenharmony_ci 491ebd3d54Sopenharmony_civoid Watchdog::ProcessEvent(const AppExecFwk::InnerEvent::Pointer& event) 501ebd3d54Sopenharmony_ci{ 511ebd3d54Sopenharmony_ci if (event == nullptr) { 521ebd3d54Sopenharmony_ci return; 531ebd3d54Sopenharmony_ci } 541ebd3d54Sopenharmony_ci int32_t requestId = event->GetInnerEventId(); 551ebd3d54Sopenharmony_ci const shared_ptr<KeyInfo>& info = event->GetSharedObject<KeyInfo>(); 561ebd3d54Sopenharmony_ci if (info == nullptr) { 571ebd3d54Sopenharmony_ci return; 581ebd3d54Sopenharmony_ci } 591ebd3d54Sopenharmony_ci if (decision_->IsFrontApp(info->GetPkg(), info->GetUid())) { 601ebd3d54Sopenharmony_ci auto bgTask = service_.promote(); 611ebd3d54Sopenharmony_ci if (bgTask == nullptr) { 621ebd3d54Sopenharmony_ci BGTASK_LOGE("fail to get bgTask service."); 631ebd3d54Sopenharmony_ci return; 641ebd3d54Sopenharmony_ci } 651ebd3d54Sopenharmony_ci BGTASK_LOGI("handle watchdog, force cancel requestId: %{public}d", requestId); 661ebd3d54Sopenharmony_ci bgTask->ForceCancelSuspendDelay(requestId); 671ebd3d54Sopenharmony_ci } else { 681ebd3d54Sopenharmony_ci BGTASK_LOGI("handle application background, kill it, requestId: %{public}d", requestId); 691ebd3d54Sopenharmony_ci // do kill 701ebd3d54Sopenharmony_ci if (!KillApplicationByUid(info->GetPkg(), info->GetUid())) { 711ebd3d54Sopenharmony_ci BGTASK_LOGE("fail to kill running application"); 721ebd3d54Sopenharmony_ci } 731ebd3d54Sopenharmony_ci } 741ebd3d54Sopenharmony_ci} 751ebd3d54Sopenharmony_ci 761ebd3d54Sopenharmony_cibool Watchdog::KillApplicationByUid(const std::string &bundleName, const int32_t uid) 771ebd3d54Sopenharmony_ci{ 781ebd3d54Sopenharmony_ci BGTASK_LOGI("kill running application, app name is %{public}s, uid is %{public}d", 791ebd3d54Sopenharmony_ci bundleName.c_str(), uid); 801ebd3d54Sopenharmony_ci if (appMgrClient_ == nullptr) { 811ebd3d54Sopenharmony_ci appMgrClient_ = std::make_unique<AppExecFwk::AppMgrClient>(); 821ebd3d54Sopenharmony_ci if (appMgrClient_ == nullptr) { 831ebd3d54Sopenharmony_ci BGTASK_LOGE("failed to get appMgrClient"); 841ebd3d54Sopenharmony_ci return false; 851ebd3d54Sopenharmony_ci } 861ebd3d54Sopenharmony_ci } 871ebd3d54Sopenharmony_ci int32_t ret = (int32_t)appMgrClient_->KillApplicationByUid(bundleName, uid); 881ebd3d54Sopenharmony_ci if (ret != ERR_OK) { 891ebd3d54Sopenharmony_ci BGTASK_LOGE("Fail to kill application by uid."); 901ebd3d54Sopenharmony_ci return false; 911ebd3d54Sopenharmony_ci } 921ebd3d54Sopenharmony_ci return true; 931ebd3d54Sopenharmony_ci} 941ebd3d54Sopenharmony_ci} // namespace BackgroundTaskMgr 951ebd3d54Sopenharmony_ci} // namespace OHOS