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 "bg_task_observer.h"
17 #include "memmgr_log.h"
18 #include "reclaim_priority_manager.h"
19 
20 namespace OHOS {
21 namespace Memory {
22 namespace {
23 const std::string TAG = "BgTaskObserver";
24 }
25 
OnTransientTaskStart(const std::shared_ptr<BackgroundTaskMgr::TransientTaskAppInfo> & ttInfo)26 void BgTaskObserver::OnTransientTaskStart(
27     const std::shared_ptr<BackgroundTaskMgr::TransientTaskAppInfo>& ttInfo)
28 {
29     if (ttInfo == nullptr) {
30         HILOGE("called with null TransientTaskAppInfo");
31         return;
32     }
33     std::string pkgName = ttInfo->GetPackageName();
34     int uid = ttInfo->GetUid();
35     int pid = ttInfo->GetPid();
36     HILOGD("called, pkg=%{public}s, uid=%{public}d, pid=%{public}d", pkgName.c_str(), uid, pid);
37 
38     ReclaimPriorityManager::GetInstance().UpdateReclaimPriority(
39         SingleRequest({pid, uid, pkgName}, AppStateUpdateReason::SUSPEND_DELAY_START));
40 }
41 
OnTransientTaskEnd(const std::shared_ptr<BackgroundTaskMgr::TransientTaskAppInfo> & ttInfo)42 void BgTaskObserver::OnTransientTaskEnd(
43     const std::shared_ptr<BackgroundTaskMgr::TransientTaskAppInfo>& ttInfo)
44 {
45     if (ttInfo == nullptr) {
46         HILOGE("called with null TransientTaskAppInfo");
47         return;
48     }
49     std::string pkgName = ttInfo->GetPackageName();
50     int uid = ttInfo->GetUid();
51     int pid = ttInfo->GetPid();
52     HILOGD("called, pkg=%{public}s, uid=%{public}d, pid=%{public}d", pkgName.c_str(), uid, pid);
53 
54     ReclaimPriorityManager::GetInstance().UpdateReclaimPriority(
55         SingleRequest({pid, uid, pkgName}, AppStateUpdateReason::SUSPEND_DELAY_END));
56 }
57 
OnContinuousTaskStart(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo> & ctInfo)58 void BgTaskObserver::OnContinuousTaskStart(
59     const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>& ctInfo)
60 {
61     if (ctInfo == nullptr) {
62         HILOGE("called with null ContinuousTaskCallbackInfo");
63         return;
64     }
65     int32_t type = ctInfo->GetTypeId();
66     uid_t uid = ctInfo->GetCreatorUid();
67     pid_t pid = ctInfo->GetCreatorPid();
68     std::string abilityName = ctInfo->GetAbilityName();
69     HILOGD("called, abilityName=%{public}s, type=%{public}d uid=%{public}d, pid=%{public}d",
70         abilityName.c_str(), type, uid, pid);
71 
72     ReclaimPriorityManager::GetInstance().UpdateReclaimPriority(
73         SingleRequest({pid, uid, abilityName}, AppStateUpdateReason::BACKGROUND_RUNNING_START));
74 }
75 
OnContinuousTaskStop(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo> & ctInfo)76 void BgTaskObserver::OnContinuousTaskStop(
77     const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>& ctInfo)
78 {
79     if (ctInfo == nullptr) {
80         HILOGE("called with null ContinuousTaskCallbackInfo");
81         return;
82     }
83     int32_t type = ctInfo->GetTypeId();
84     uid_t uid = ctInfo->GetCreatorUid();
85     pid_t pid = ctInfo->GetCreatorPid();
86     std::string abilityName = ctInfo->GetAbilityName();
87     HILOGD("called, abilityName=%{public}s, type=%{public}d uid=%{public}d, pid=%{public}d",
88         abilityName.c_str(), type, uid, pid);
89 
90     ReclaimPriorityManager::GetInstance().UpdateReclaimPriority(
91         SingleRequest({pid, uid, abilityName}, AppStateUpdateReason::BACKGROUND_RUNNING_END));
92 }
93 
OnRemoteDied(const wptr<IRemoteObject> & object)94 void BgTaskObserver::OnRemoteDied(const wptr<IRemoteObject> &object)
95 {
96     HILOGE("called");
97 }
98 } // namespace Memory
99 } // namespace OHOS
100