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