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