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
OnRemoteDied(const wptr<IRemoteObject> & object)58 void BackgroundTaskObserver::OnRemoteDied(const wptr<IRemoteObject> &object)
59 {
60 HILOG_DEBUG("OnRemoteDied");
61 int attemptNums = 0;
62 while ((BackgroundTaskMgr::BackgroundTaskMgrHelper::SubscribeBackgroundTask(
63 *(shared_from_this()))) != ERR_OK) {
64 ++attemptNums;
65 if (attemptNums > SUBSCRIBE_BACKGROUND_TASK_TRY) {
66 HILOG_ERROR("subscribeBackgroundTask fail, attemptNums:%{public}d", attemptNums);
67 return;
68 }
69 usleep(REPOLL_TIME_MICRO_SECONDS);
70 }
71
72 std::vector<std::shared_ptr<BackgroundTaskMgr::ContinuousTaskCallbackInfo>> continuousTasks;
73 BackgroundTaskMgr::BackgroundTaskMgrHelper::GetContinuousTaskApps(continuousTasks);
74 std::lock_guard<std::mutex> lock(bgTaskMutex_);
75 bgTaskUids_.clear();
76 for (size_t index = 0; index < continuousTasks.size(); index++) {
77 bgTaskUids_.push_front(continuousTasks[index]->GetCreatorUid());
78 }
79 }
80
IsBackgroundTaskUid(const int uid)81 bool BackgroundTaskObserver::IsBackgroundTaskUid(const int uid)
82 {
83 std::lock_guard<std::mutex> lock(bgTaskMutex_);
84 auto iter = find(bgTaskUids_.begin(), bgTaskUids_.end(), uid);
85 if (iter != bgTaskUids_.end()) {
86 return true;
87 }
88 return false;
89 }
90
GetAppManager()91 sptr<AppExecFwk::IAppMgr> BackgroundTaskObserver::GetAppManager()
92 {
93 if (appManager_ == nullptr) {
94 auto appObj =
95 OHOS::DelayedSingleton<SaMgrClient>::GetInstance()->GetSystemAbility(APP_MGR_SERVICE_ID);
96 if (appObj == nullptr) {
97 HILOG_ERROR("Failed to get app manager service.");
98 return nullptr;
99 }
100 appManager_ = iface_cast<AppExecFwk::IAppMgr>(appObj);
101 }
102 return appManager_;
103 }
104 } // namespace AAFwk
105 } // namespace OHOS
106 #endif
107