1 /*
2 * Copyright (c) 2023 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 #define LOG_TAG "LifeCycleManager"
16
17 #include "lifecycle_manager.h"
18
19 #include <algorithm>
20 #include <cinttypes>
21
22 #include "log_print.h"
23
24 namespace OHOS {
25 namespace UDMF {
26 using CleanAfterGet = LifeCyclePolicy;
27 std::unordered_map<std::string, std::shared_ptr<LifeCyclePolicy>> LifeCycleManager::intentionPolicy_ = {
28 { UD_INTENTION_MAP.at(UD_INTENTION_DRAG), std::make_shared<CleanAfterGet>() },
29 { UD_INTENTION_MAP.at(UD_INTENTION_DATA_HUB), std::make_shared<CleanAfterGet>() }
30 };
31
GetInstance()32 LifeCycleManager &LifeCycleManager::GetInstance()
33 {
34 static LifeCycleManager instance;
35 return instance;
36 }
37
OnGot(const UnifiedKey & key)38 Status LifeCycleManager::OnGot(const UnifiedKey &key)
39 {
40 auto findPolicy = intentionPolicy_.find(key.intention);
41 if (findPolicy == intentionPolicy_.end()) {
42 ZLOGE("Invalid intention, intention: %{public}s.", key.intention.c_str());
43 return E_INVALID_PARAMETERS;
44 }
45 auto policy = findPolicy->second;
46 ExecutorPool::TaskId taskId = executors_->Execute([=] {
47 policy->OnGot(key);
48 });
49 if (taskId == ExecutorPool::INVALID_TASK_ID) {
50 ZLOGE("OnGot task execute failed.");
51 return E_ERROR;
52 }
53 return E_OK;
54 }
55
OnStart()56 Status LifeCycleManager::OnStart()
57 {
58 Status status = E_OK;
59 std::string errorInfo;
60 for (auto &[intention, lifeCyclePolicy] : intentionPolicy_) {
61 if (lifeCyclePolicy == nullptr) {
62 continue;
63 }
64 Status delStatus = lifeCyclePolicy->OnStart(intention);
65 if (delStatus != E_OK) {
66 status = delStatus;
67 errorInfo += intention + " ";
68 }
69 }
70 if (status != E_OK) {
71 ZLOGW("fail, status = %{public}d, intention = [%{public}s].", status, errorInfo.c_str());
72 }
73 return status;
74 }
75
StartLifeCycleTimer()76 Status LifeCycleManager::StartLifeCycleTimer()
77 {
78 if (executors_ == nullptr) {
79 ZLOGE("Executors_ is nullptr.");
80 return E_ERROR;
81 }
82 ExecutorPool::TaskId taskId = executors_->Schedule(GetTask(), LifeCyclePolicy::INTERVAL);
83 if (taskId == ExecutorPool::INVALID_TASK_ID) {
84 ZLOGE("ExecutorPool Schedule failed.");
85 return E_ERROR;
86 }
87 ZLOGI("ScheduleTask start, TaskId: %{public}" PRIu64 ".", taskId);
88 return E_OK;
89 }
90
GetTask()91 ExecutorPool::Task LifeCycleManager::GetTask()
92 {
93 return [this] {
94 Status status = E_OK;
95 std::string errorInfo;
96 for (auto &[intention, lifeCyclePolicy] : intentionPolicy_) {
97 if (lifeCyclePolicy == nullptr) {
98 continue;
99 }
100 Status delStatus = lifeCyclePolicy->OnTimeout(intention);
101 if (delStatus != E_OK) {
102 status = delStatus;
103 errorInfo += intention + " ";
104 }
105 }
106 if (status != E_OK) {
107 ZLOGW("fail, status = %{public}d, intention = [%{public}s].", status, errorInfo.c_str());
108 }
109 return status;
110 };
111 }
112
SetThreadPool(std::shared_ptr<ExecutorPool> executors)113 void LifeCycleManager::SetThreadPool(std::shared_ptr<ExecutorPool> executors)
114 {
115 executors_ = executors;
116 }
117 } // namespace UDMF
118 } // namespace OHOS
119