1 /* 2 * Copyright (c) 2022 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 "nweb_message_ext.h" 17 #include "securec.h" 18 19 namespace OHOS::NWeb { 20 ConvertNwebHap2NwebMessage(std::shared_ptr<NWebHapValue> hap)21std::shared_ptr<NWebMessage> ConvertNwebHap2NwebMessage(std::shared_ptr<NWebHapValue> hap) 22 { 23 std::shared_ptr<NWebMessage> message = std::make_shared<NWebMessage>(); 24 if (!hap) { 25 return message; 26 } 27 auto type = hap->GetType(); 28 switch (type) { 29 case NWebHapValue::Type::INTEGER: { 30 message->SetType(NWebValue::Type::INTEGER); 31 message->SetInt64(hap->GetInt()); 32 break; 33 } 34 case NWebHapValue::Type::DOUBLE: { 35 message->SetType(NWebValue::Type::DOUBLE); 36 message->SetDouble(hap->GetDouble()); 37 break; 38 } 39 case NWebHapValue::Type::BOOLEAN: { 40 message->SetType(NWebValue::Type::BOOLEAN); 41 message->SetBoolean(hap->GetBool()); 42 break; 43 } 44 case NWebHapValue::Type::STRING: { 45 message->SetType(NWebValue::Type::STRING); 46 message->SetString(hap->GetString()); 47 break; 48 } 49 case NWebHapValue::Type::LIST: { 50 auto list = hap->GetListValue(); 51 size_t len = list.size(); 52 std::vector<std::string> string_arr; 53 std::vector<bool> bool_arr; 54 std::vector<double> double_arr; 55 std::vector<int64_t> int64_arr; 56 NWebValue::Type elem_type = NWebValue::Type::NONE; 57 for (size_t i = 0; i < len; i++) { 58 if (!list[i]) { 59 continue; 60 } 61 if (list[i]->GetType() == NWebHapValue::Type::STRING) { 62 elem_type = NWebValue::Type::STRING; 63 string_arr.push_back(list[i]->GetString()); 64 } else if (list[i]->GetType() == NWebHapValue::Type::BOOLEAN) { 65 elem_type = NWebValue::Type::BOOLEAN; 66 bool_arr.push_back(list[i]->GetBool()); 67 } else if (list[i]->GetType() == NWebHapValue::Type::DOUBLE) { 68 elem_type = NWebValue::Type::DOUBLE; 69 double_arr.push_back(list[i]->GetDouble()); 70 } else if (list[i]->GetType() == NWebHapValue::Type::INTEGER) { 71 elem_type = NWebValue::Type::INTEGER; 72 int64_arr.push_back(list[i]->GetInt()); 73 } 74 } 75 if (elem_type == NWebValue::Type::STRING) { 76 message->SetType(NWebValue::Type::STRINGARRAY); 77 message->SetStringArray(string_arr); 78 } else if (elem_type == NWebValue::Type::BOOLEAN) { 79 message->SetType(NWebValue::Type::BOOLEANARRAY); 80 message->SetBooleanArray(bool_arr); 81 } else if (elem_type == NWebValue::Type::DOUBLE) { 82 message->SetType(NWebValue::Type::DOUBLEARRAY); 83 message->SetDoubleArray(double_arr); 84 } else if (elem_type == NWebValue::Type::INTEGER) { 85 message->SetType(NWebValue::Type::INT64ARRAY); 86 message->SetInt64Array(int64_arr); 87 } 88 break; 89 } 90 case NWebHapValue::Type::DICTIONARY: { 91 std::map<std::string, NWebValue> map; 92 auto dict = hap->GetDictValue(); 93 message->SetType(NWebValue::Type::ERROR); 94 std::string strErrName; 95 std::string strErrMsg; 96 if (dict.find("Error.name") != dict.end() && dict["Error.name"]) { 97 strErrName = dict["Error.name"]->GetString(); 98 } 99 if (dict.find("Error.message") != dict.end() && dict["Error.message"]) { 100 strErrMsg = dict["Error.message"]->GetString(); 101 } 102 message->SetErrName(strErrName); 103 message->SetErrMsg(strErrMsg); 104 break; 105 } 106 case NWebHapValue::Type::BINARY: { 107 auto length = 0; 108 auto buff = hap->GetBinary(length); 109 std::vector<uint8_t> arr(length); 110 memcpy_s(&arr[0], length, buff, length); 111 message->SetType(NWebValue::Type::BINARY); 112 message->SetBinary(arr); 113 break; 114 } 115 default: { 116 break; 117 } 118 } 119 return message; 120 } 121 122 } // namespace OHOS::NWeb 123