1 /* 2 * Copyright (c) 2025 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 WEBVIEW_VALUE_H 17 #define WEBVIEW_VALUE_H 18 19 #include "nweb_rom_value.h" 20 21 namespace OHOS::NWeb { 22 23 class WebViewValue : public NWebRomValue { 24 public: 25 union data_union { 26 int n; 27 bool b; 28 double f; 29 }; 30 31 WebViewValue() = default; 32 virtual ~WebViewValue() = default; 33 explicit WebViewValue(NWebRomValue::Type type); 34 35 NWebRomValue::Type GetType() override; 36 37 void SetType(NWebRomValue::Type type) override; 38 39 int GetInt() override; 40 41 void SetInt(int value) override; 42 43 bool GetBool() override; 44 45 void SetBool(bool value) override; 46 47 double GetDouble() override; 48 49 void SetDouble(double value) override; 50 51 std::string GetString() override; 52 53 void SetString(const std::string& value) override; 54 55 const char* GetBinary(int& length) override; 56 57 void SetBinary(int length, const char* value) override; 58 59 std::map<std::string, std::shared_ptr<NWebRomValue>> GetDictValue() override; 60 61 std::vector<std::shared_ptr<NWebRomValue>> GetListValue() override; 62 63 std::shared_ptr<NWebRomValue> NewChildValue() override; 64 65 void SaveDictChildValue(const std::string& key) override; 66 67 void SaveListChildValue() override; 68 GetInt64()69 int64_t GetInt64() override 70 { 71 return value_; 72 } 73 SetInt64(int64_t value)74 void SetInt64(int64_t value) override 75 { 76 value_ = value; 77 } 78 SetBinary(const std::vector<uint8_t> & binary_data)79 void SetBinary(const std::vector<uint8_t>& binary_data) override 80 { 81 binary_data_.reserve(binary_data.size()); 82 binary_data_ = binary_data; 83 } 84 GetBinary()85 std::vector<uint8_t> GetBinary() override 86 { 87 return binary_data_; 88 } 89 GetErrName()90 std::string GetErrName() override 91 { 92 return err_name_; 93 } 94 SetErrName(const std::string & name)95 void SetErrName(const std::string& name) override 96 { 97 err_name_ = name; 98 } 99 GetErrMsg()100 std::string GetErrMsg() override 101 { 102 return err_msg_; 103 } 104 SetErrMsg(const std::string & msg)105 void SetErrMsg(const std::string& msg) override 106 { 107 err_msg_ = msg; 108 } 109 GetStringArray()110 std::vector<std::string> GetStringArray() override 111 { 112 return string_arr_; 113 } 114 SetStringArray(const std::vector<std::string> & string_arr)115 void SetStringArray(const std::vector<std::string>& string_arr) override 116 { 117 string_arr_ = string_arr; 118 } 119 GetBoolArray()120 std::vector<bool> GetBoolArray() override 121 { 122 return bool_arr_; 123 } 124 SetBoolArray(const std::vector<bool> & bool_arr)125 void SetBoolArray(const std::vector<bool>& bool_arr) override 126 { 127 bool_arr_ = bool_arr; 128 } 129 GetDoubleArray()130 std::vector<double> GetDoubleArray() override 131 { 132 return double_arr_; 133 } 134 SetDoubleArray(const std::vector<double> & double_arr)135 void SetDoubleArray(const std::vector<double>& double_arr) override 136 { 137 double_arr_ = double_arr; 138 } 139 GetInt64Array()140 std::vector<int64_t> GetInt64Array() override 141 { 142 return int64_arr_; 143 } 144 SetInt64Array(const std::vector<int64_t> & int64_arr)145 void SetInt64Array(const std::vector<int64_t>& int64_arr) override 146 { 147 int64_arr_ = int64_arr; 148 } 149 150 private: 151 void CheckType(NWebRomValue::Type type); 152 153 private: 154 NWebRomValue::Type type_ = NWebRomValue::Type::NONE; 155 156 data_union data_; 157 std::string str_; 158 std::shared_ptr<NWebRomValue> child_node_ = nullptr; 159 std::vector<std::shared_ptr<NWebRomValue>> list_value_; 160 std::map<std::string, std::shared_ptr<NWebRomValue>> dict_value_; 161 162 std::vector<uint8_t> binary_data_; 163 std::string err_name_; 164 std::string err_msg_; 165 int64_t value_ = -1; 166 std::vector<std::string> string_arr_; 167 std::vector<bool> bool_arr_; 168 std::vector<double> double_arr_; 169 std::vector<int64_t> int64_arr_; 170 }; 171 172 } // namespace OHOS::NWeb 173 174 #endif // WEBVIEW_VALUE_H 175