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 #ifdef BGTASKMGR_CONTINUOUS_TASK_ENABLE
16 #include "background_task_observer.h"
17 #include "hilog_wrapper.h"
18 #include <unistd.h>
19 #include "sa_mgr_client.h"
20 #include "system_ability_definition.h"
21
22 namespace OHOS {
23 namespace AAFwk {
BackgroundTaskObserver()24 BackgroundTaskObserver::BackgroundTaskObserver()
25 {}
26
~BackgroundTaskObserver()27 BackgroundTaskObserver::~BackgroundTaskObserver()
28 {}
29
OnContinuousTaskStart(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)30 void BackgroundTaskObserver::OnContinuousTaskStart(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>
31 &continuousTaskCallbackInfo)
32 {
33 HILOG_DEBUG("OnContinuousTaskStart, uid:%{public}d", continuousTaskCallbackInfo->GetCreatorUid());
34 std::lock_guard<std::mutex> lock(bgTaskMutex_);
35 bgTaskUids_.push_front(continuousTaskCallbackInfo->GetCreatorUid());
36 if (appManager_ == nullptr) {
37 GetAppManager();
38 }
39 if (appManager_ != nullptr) {
40 appManager_->SetContinuousTaskProcess(continuousTaskCallbackInfo->GetCreatorPid(), true);
41 }
42 }
43
OnContinuousTaskStop(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo> & continuousTaskCallbackInfo)44 void BackgroundTaskObserver::OnContinuousTaskStop(const std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>
45 &continuousTaskCallbackInfo)
46 {
47 HILOG_DEBUG("OnContinuousTaskStop, uid:%{public}d", continuousTaskCallbackInfo->GetCreatorUid());
48 std::lock_guard<std::mutex> lock(bgTaskMutex_);
49 bgTaskUids_.remove(continuousTaskCallbackInfo->GetCreatorUid());
50 if (appManager_ == nullptr) {
51 GetAppManager();
52 }
53 if (appManager_ != nullptr) {
54 appManager_->SetContinuousTaskProcess(continuousTaskCallbackInfo->GetCreatorPid(), false);
55 }
56 }
57
GetContinuousTaskApps()58 void BackgroundTaskObserver::GetContinuousTaskApps()
59 {
60 std::vector<std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>> continuousTasks;
61 ErrCode result = BackgroundTaskMgr::BackgroundTaskMgrHelper::GetContinuousTaskApps(continuousTasks);
62 if (result != ERR_OK) {
63 HILOG_ERROR("failed to GetContinuousTaskApps, ErrCode: %{public}d", result);
64 return;
65 }
66 std::lock_guard<std::mutex> lock(bgTaskMutex_);
67 bgTaskUids_.clear();
68 for (size_t index = 0; index < continuousTasks.size(); index++) {
69 bgTaskUids_.push_front(continuousTasks[index]->GetCreatorUid());
70 }
71 }
72
IsBackgroundTaskUid(const int uid)73 bool BackgroundTaskObserver::IsBackgroundTaskUid(const int uid)
74 {
75 std::lock_guard<std::mutex> lock(bgTaskMutex_);
76 auto iter = find(bgTaskUids_.begin(), bgTaskUids_.end(), uid);
77 if (iter != bgTaskUids_.end()) {
78 return true;
79 }
80 return false;
81 }
82
GetAppManager()83 sptr<AppExecFwk::IAppMgr> BackgroundTaskObserver::GetAppManager()
84 {
85 if (appManager_ == nullptr) {
86 auto appObj =
87 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
88 if (appObj == nullptr) {
89 HILOG_ERROR("Failed to get app manager service.");
90 return nullptr;
91 }
92 appManager_ = iface_cast<AppExecFwk::IAppMgr>(appObj);
93 }
94 return appManager_;
95 }
96 } // namespace AAFwk
97 } // namespace OHOS
98 #endif
99