| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "platform/inspector_protocol/Values.h" | 5 #include "platform/inspector_protocol/Values.h" |
| 6 | 6 |
| 7 #include "platform/inspector_protocol/Parser.h" | 7 #include "platform/inspector_protocol/Parser.h" |
| 8 #include "platform/inspector_protocol/String16.h" | 8 #include "platform/inspector_protocol/String16.h" |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 dst->append('"'); | 70 dst->append('"'); |
| 71 } | 71 } |
| 72 | 72 |
| 73 } // anonymous namespace | 73 } // anonymous namespace |
| 74 | 74 |
| 75 bool Value::asBoolean(bool*) const | 75 bool Value::asBoolean(bool*) const |
| 76 { | 76 { |
| 77 return false; | 77 return false; |
| 78 } | 78 } |
| 79 | 79 |
| 80 bool Value::asNumber(double*) const | 80 bool Value::asDouble(double*) const |
| 81 { | 81 { |
| 82 return false; | 82 return false; |
| 83 } | 83 } |
| 84 | 84 |
| 85 bool Value::asNumber(int*) const | 85 bool Value::asInteger(int*) const |
| 86 { | 86 { |
| 87 return false; | 87 return false; |
| 88 } | 88 } |
| 89 | 89 |
| 90 bool Value::asString(String16*) const | 90 bool Value::asString(String16*) const |
| 91 { | 91 { |
| 92 return false; | 92 return false; |
| 93 } | 93 } |
| 94 | 94 |
| 95 String16 Value::toJSONString() const | 95 String16 Value::toJSONString() const |
| (...skipping 16 matching lines...) Expand all Loading... |
| 112 } | 112 } |
| 113 | 113 |
| 114 bool FundamentalValue::asBoolean(bool* output) const | 114 bool FundamentalValue::asBoolean(bool* output) const |
| 115 { | 115 { |
| 116 if (type() != TypeBoolean) | 116 if (type() != TypeBoolean) |
| 117 return false; | 117 return false; |
| 118 *output = m_boolValue; | 118 *output = m_boolValue; |
| 119 return true; | 119 return true; |
| 120 } | 120 } |
| 121 | 121 |
| 122 bool FundamentalValue::asNumber(double* output) const | 122 bool FundamentalValue::asDouble(double* output) const |
| 123 { | 123 { |
| 124 if (type() != TypeNumber) | 124 if (type() == TypeDouble) { |
| 125 return false; | 125 *output = m_doubleValue; |
| 126 *output = m_doubleValue; | 126 return true; |
| 127 return true; | 127 } |
| 128 if (type() == TypeInteger) { |
| 129 *output = m_integerValue; |
| 130 return true; |
| 131 } |
| 132 return false; |
| 128 } | 133 } |
| 129 | 134 |
| 130 bool FundamentalValue::asNumber(int* output) const | 135 bool FundamentalValue::asInteger(int* output) const |
| 131 { | 136 { |
| 132 if (type() != TypeNumber) | 137 if (type() != TypeInteger) |
| 133 return false; | 138 return false; |
| 134 *output = static_cast<int>(m_doubleValue); | 139 *output = m_integerValue; |
| 135 return true; | 140 return true; |
| 136 } | 141 } |
| 137 | 142 |
| 138 void FundamentalValue::writeJSON(String16Builder* output) const | 143 void FundamentalValue::writeJSON(String16Builder* output) const |
| 139 { | 144 { |
| 140 DCHECK(type() == TypeBoolean || type() == TypeNumber); | 145 DCHECK(type() == TypeBoolean || type() == TypeInteger || type() == TypeDoubl
e); |
| 141 if (type() == TypeBoolean) { | 146 if (type() == TypeBoolean) { |
| 142 if (m_boolValue) | 147 if (m_boolValue) |
| 143 output->append(trueString, 4); | 148 output->append(trueString, 4); |
| 144 else | 149 else |
| 145 output->append(falseString, 5); | 150 output->append(falseString, 5); |
| 146 } else if (type() == TypeNumber) { | 151 } else if (type() == TypeDouble) { |
| 147 if (!std::isfinite(m_doubleValue)) { | 152 if (!std::isfinite(m_doubleValue)) { |
| 148 output->append(nullString, 4); | 153 output->append(nullString, 4); |
| 149 return; | 154 return; |
| 150 } | 155 } |
| 151 output->append(String16::fromDouble(m_doubleValue)); | 156 output->append(String16::fromDouble(m_doubleValue)); |
| 157 } else if (type() == TypeInteger) { |
| 158 output->append(String16::fromInteger(m_integerValue)); |
| 152 } | 159 } |
| 153 } | 160 } |
| 154 | 161 |
| 155 std::unique_ptr<Value> FundamentalValue::clone() const | 162 std::unique_ptr<Value> FundamentalValue::clone() const |
| 156 { | 163 { |
| 157 return type() == TypeNumber ? FundamentalValue::create(m_doubleValue) : Fund
amentalValue::create(m_boolValue); | 164 switch (type()) { |
| 165 case TypeDouble: return FundamentalValue::create(m_doubleValue); |
| 166 case TypeInteger: return FundamentalValue::create(m_integerValue); |
| 167 case TypeBoolean: return FundamentalValue::create(m_boolValue); |
| 168 default: |
| 169 NOTREACHED(); |
| 170 } |
| 171 return nullptr; |
| 158 } | 172 } |
| 159 | 173 |
| 160 bool StringValue::asString(String16* output) const | 174 bool StringValue::asString(String16* output) const |
| 161 { | 175 { |
| 162 *output = m_stringValue; | 176 *output = m_stringValue; |
| 163 return true; | 177 return true; |
| 164 } | 178 } |
| 165 | 179 |
| 166 void StringValue::writeJSON(String16Builder* output) const | 180 void StringValue::writeJSON(String16Builder* output) const |
| 167 { | 181 { |
| 168 DCHECK(type() == TypeString); | 182 DCHECK(type() == TypeString); |
| 169 doubleQuoteStringForJSON(m_stringValue, output); | 183 doubleQuoteStringForJSON(m_stringValue, output); |
| 170 } | 184 } |
| 171 | 185 |
| 172 std::unique_ptr<Value> StringValue::clone() const | 186 std::unique_ptr<Value> StringValue::clone() const |
| 173 { | 187 { |
| 174 return StringValue::create(m_stringValue); | 188 return StringValue::create(m_stringValue); |
| 175 } | 189 } |
| 176 | 190 |
| 177 DictionaryValue::~DictionaryValue() | 191 DictionaryValue::~DictionaryValue() |
| 178 { | 192 { |
| 179 } | 193 } |
| 180 | 194 |
| 181 void DictionaryValue::setBoolean(const String16& name, bool value) | 195 void DictionaryValue::setBoolean(const String16& name, bool value) |
| 182 { | 196 { |
| 183 setValue(name, FundamentalValue::create(value)); | 197 setValue(name, FundamentalValue::create(value)); |
| 184 } | 198 } |
| 185 | 199 |
| 186 void DictionaryValue::setNumber(const String16& name, double value) | 200 void DictionaryValue::setInteger(const String16& name, int value) |
| 187 { | 201 { |
| 188 setValue(name, FundamentalValue::create(value)); | 202 setValue(name, FundamentalValue::create(value)); |
| 189 } | 203 } |
| 204 |
| 205 void DictionaryValue::setDouble(const String16& name, double value) |
| 206 { |
| 207 setValue(name, FundamentalValue::create(value)); |
| 208 } |
| 190 | 209 |
| 191 void DictionaryValue::setString(const String16& name, const String16& value) | 210 void DictionaryValue::setString(const String16& name, const String16& value) |
| 192 { | 211 { |
| 193 setValue(name, StringValue::create(value)); | 212 setValue(name, StringValue::create(value)); |
| 194 } | 213 } |
| 195 | 214 |
| 196 void DictionaryValue::setValue(const String16& name, std::unique_ptr<Value> valu
e) | 215 void DictionaryValue::setValue(const String16& name, std::unique_ptr<Value> valu
e) |
| 197 { | 216 { |
| 198 set(name, value); | 217 set(name, value); |
| 199 } | 218 } |
| 200 | 219 |
| 201 void DictionaryValue::setObject(const String16& name, std::unique_ptr<Dictionary
Value> value) | 220 void DictionaryValue::setObject(const String16& name, std::unique_ptr<Dictionary
Value> value) |
| 202 { | 221 { |
| 203 set(name, value); | 222 set(name, value); |
| 204 } | 223 } |
| 205 | 224 |
| 206 void DictionaryValue::setArray(const String16& name, std::unique_ptr<ListValue>
value) | 225 void DictionaryValue::setArray(const String16& name, std::unique_ptr<ListValue>
value) |
| 207 { | 226 { |
| 208 set(name, value); | 227 set(name, value); |
| 209 } | 228 } |
| 210 | 229 |
| 211 bool DictionaryValue::getBoolean(const String16& name, bool* output) const | 230 bool DictionaryValue::getBoolean(const String16& name, bool* output) const |
| 212 { | 231 { |
| 213 protocol::Value* value = get(name); | 232 protocol::Value* value = get(name); |
| 214 if (!value) | 233 if (!value) |
| 215 return false; | 234 return false; |
| 216 return value->asBoolean(output); | 235 return value->asBoolean(output); |
| 217 } | 236 } |
| 218 | 237 |
| 238 bool DictionaryValue::getInteger(const String16& name, int* output) const |
| 239 { |
| 240 Value* value = get(name); |
| 241 if (!value) |
| 242 return false; |
| 243 return value->asInteger(output); |
| 244 } |
| 245 |
| 246 bool DictionaryValue::getDouble(const String16& name, double* output) const |
| 247 { |
| 248 Value* value = get(name); |
| 249 if (!value) |
| 250 return false; |
| 251 return value->asDouble(output); |
| 252 } |
| 253 |
| 219 bool DictionaryValue::getString(const String16& name, String16* output) const | 254 bool DictionaryValue::getString(const String16& name, String16* output) const |
| 220 { | 255 { |
| 221 protocol::Value* value = get(name); | 256 protocol::Value* value = get(name); |
| 222 if (!value) | 257 if (!value) |
| 223 return false; | 258 return false; |
| 224 return value->asString(output); | 259 return value->asString(output); |
| 225 } | 260 } |
| 226 | 261 |
| 227 DictionaryValue* DictionaryValue::getObject(const String16& name) const | 262 DictionaryValue* DictionaryValue::getObject(const String16& name) const |
| 228 { | 263 { |
| (...skipping 19 matching lines...) Expand all Loading... |
| 248 return std::make_pair(key, m_data.find(key)->second.get()); | 283 return std::make_pair(key, m_data.find(key)->second.get()); |
| 249 } | 284 } |
| 250 | 285 |
| 251 bool DictionaryValue::booleanProperty(const String16& name, bool defaultValue) c
onst | 286 bool DictionaryValue::booleanProperty(const String16& name, bool defaultValue) c
onst |
| 252 { | 287 { |
| 253 bool result = defaultValue; | 288 bool result = defaultValue; |
| 254 getBoolean(name, &result); | 289 getBoolean(name, &result); |
| 255 return result; | 290 return result; |
| 256 } | 291 } |
| 257 | 292 |
| 258 double DictionaryValue::numberProperty(const String16& name, double defaultValue
) const | 293 int DictionaryValue::integerProperty(const String16& name, int defaultValue) con
st |
| 259 { | 294 { |
| 260 double result = defaultValue; | 295 int result = defaultValue; |
| 261 getNumber(name, &result); | 296 getInteger(name, &result); |
| 262 return result; | 297 return result; |
| 263 } | 298 } |
| 264 | 299 |
| 300 double DictionaryValue::doubleProperty(const String16& name, double defaultValue
) const |
| 301 { |
| 302 double result = defaultValue; |
| 303 getDouble(name, &result); |
| 304 return result; |
| 305 } |
| 306 |
| 265 void DictionaryValue::remove(const String16& name) | 307 void DictionaryValue::remove(const String16& name) |
| 266 { | 308 { |
| 267 m_data.erase(name); | 309 m_data.erase(name); |
| 268 m_order.erase(std::remove(m_order.begin(), m_order.end(), name), m_order.end
()); | 310 m_order.erase(std::remove(m_order.begin(), m_order.end(), name), m_order.end
()); |
| 269 } | 311 } |
| 270 | 312 |
| 271 void DictionaryValue::writeJSON(String16Builder* output) const | 313 void DictionaryValue::writeJSON(String16Builder* output) const |
| 272 { | 314 { |
| 273 output->append('{'); | 315 output->append('{'); |
| 274 for (size_t i = 0; i < m_order.size(); ++i) { | 316 for (size_t i = 0; i < m_order.size(); ++i) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 } | 379 } |
| 338 | 380 |
| 339 protocol::Value* ListValue::at(size_t index) | 381 protocol::Value* ListValue::at(size_t index) |
| 340 { | 382 { |
| 341 DCHECK_LT(index, m_data.size()); | 383 DCHECK_LT(index, m_data.size()); |
| 342 return m_data[index].get(); | 384 return m_data[index].get(); |
| 343 } | 385 } |
| 344 | 386 |
| 345 } // namespace protocol | 387 } // namespace protocol |
| 346 } // namespace blink | 388 } // namespace blink |
| OLD | NEW |