• 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 
16 #include "strategy_manager_adapter.h"
17 
18 #include "ibase_strategy.h"
19 #include "standby_service_log.h"
20 #include "network_strategy.h"
21 #include "standby_config_manager.h"
22 #include "running_lock_strategy.h"
23 
24 namespace OHOS {
25 namespace DevStandbyMgr {
26 namespace {
27 const std::map<std::string, std::shared_ptr<IBaseStrategy>> strategyMap_ {
28     { "NETWORK", std::make_shared<NetworkStrategy>() },
29     { "RUNNING_LOCK", std::make_shared<RunningLockStrategy>() },
30 };
31 }
32 
Init()33 bool StrategyManagerAdapter::Init()
34 {
35     if (StandbyConfigManager::GetInstance() == nullptr) {
36         STANDBYSERVICE_LOGE("standby service ptr is nullptr, init failed");
37         return false;
38     }
39     const auto& strategyConfigList = StandbyConfigManager::GetInstance()->GetStrategyConfigList();
40     if (strategyConfigList.empty()) {
41         STANDBYSERVICE_LOGI("strategies is disabled");
42         return true;
43     }
44     RegisterPolicy(strategyConfigList);
45     STANDBYSERVICE_LOGI("strategy manager plugin initialization succeed");
46     return true;
47 }
48 
UnInit()49 bool StrategyManagerAdapter::UnInit()
50 {
51     for (const auto& strategy : strategyList_) {
52         strategy->OnDestroy();
53     }
54     strategyList_.clear();
55     return true;
56 }
57 
RegisterPolicy(const std::vector<std::string> & strategies)58 void StrategyManagerAdapter::RegisterPolicy(const std::vector<std::string>& strategies)
59 {
60     for (const auto& item : strategies) {
61         auto strategy = strategyMap_.find(item);
62         if (strategy == strategyMap_.end()) {
63             continue;
64         }
65         STANDBYSERVICE_LOGI("strategy manager init %{public}s", item.c_str());
66         auto strategyPtr = strategy->second;
67         if (!strategyPtr) {
68             continue;
69         }
70         if (strategyPtr->OnCreated() == ERR_OK) {
71             strategyList_.emplace_back(strategyPtr);
72         }
73     }
74 }
75 
HandleEvent(const StandbyMessage & message)76 void StrategyManagerAdapter::HandleEvent(const StandbyMessage& message)
77 {
78     STANDBYSERVICE_LOGD("StrategyManagerAdapter revceive message %{public}u, action: %{public}s",
79         message.eventId_, message.action_.c_str());
80     for (const auto &strategy : strategyList_) {
81         strategy->HandleEvent(message);
82     }
83 }
84 
ShellDump(const std::vector<std::string> & argsInStr,std::string & result)85 void StrategyManagerAdapter::ShellDump(const std::vector<std::string>& argsInStr, std::string& result)
86 {
87     for (const auto &strategy : strategyList_) {
88         strategy->ShellDump(argsInStr, result);
89     }
90 }
91 }  // namespace DevStandbyMgr
92 }  // namespace OHOS