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