1 /* 2 * Copyright (c) 2024 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 #ifndef CLOUD_DATA_JS_STRATEGY_CONTEXT_H 17 #define CLOUD_DATA_JS_STRATEGY_CONTEXT_H 18 19 #include <cmath> 20 #include <string> 21 #include <vector> 22 23 #include "cloud_types.h" 24 #include "common_types.h" 25 #include "js_cloud_utils.h" 26 #include "js_error_utils.h" 27 #include "napi_queue.h" 28 29 namespace OHOS { 30 namespace CloudData { 31 struct CloudStrategyContext : public ContextBase { 32 Strategy strategy; 33 std::vector<CommonType::Value> param; SetDefaultCloudStrategyContext34 void SetDefault() 35 { 36 switch (strategy) { 37 case Strategy::STRATEGY_NETWORK: 38 param = {}; 39 return; 40 default: 41 param = {}; 42 return; 43 } 44 } 45 CheckParamCloudStrategyContext46 std::pair<Status, std::string> CheckParam() 47 { 48 switch (strategy) { 49 case Strategy::STRATEGY_NETWORK: 50 if (!ConvertNetworkParam()) { 51 return { Status::INVALID_ARGUMENT, "member of param must be of type NetWorkStrategy" }; 52 } 53 break; 54 default: 55 return { Status::ERROR, "strategy must be of type StrategyType" }; 56 } 57 return { Status::SUCCESS, "" }; 58 } 59 60 private: ConvertNetworkParamCloudStrategyContext61 bool ConvertNetworkParam() 62 { 63 std::vector<CommonType::Value> tmp = { 0 }; 64 for (auto &value : param) { 65 if (std::get_if<double>(&value) == nullptr) { 66 return false; 67 } 68 auto val = static_cast<int64_t>(std::round(std::get<double>(value))); 69 if (val < 0 || val > NetWorkStrategy::NETWORK_STRATEGY_BUTT) { 70 return false; 71 } 72 tmp.push_back(val); 73 } 74 param = tmp; 75 return true; 76 } 77 }; 78 } 79 } 80 #endif //CLOUD_DATA_JS_STRATEGY_CONTEXT_H 81