Chromium Code Reviews| 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/Parser.h" | |
| 5 #include "platform/inspector_protocol/Values.h" | 6 #include "platform/inspector_protocol/Values.h" |
| 6 | 7 |
| 7 #include "platform/Decimal.h" | 8 #include "platform/Decimal.h" |
| 8 #include "wtf/MathExtras.h" | 9 #include "wtf/MathExtras.h" |
| 9 #include "wtf/text/StringBuilder.h" | 10 #include "wtf/text/StringBuilder.h" |
| 10 | 11 |
| 11 namespace blink { | 12 namespace blink { |
| 12 namespace protocol { | 13 namespace protocol { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 writeJSON(&result); | 89 writeJSON(&result); |
| 89 return result.toString(); | 90 return result.toString(); |
| 90 } | 91 } |
| 91 | 92 |
| 92 void Value::writeJSON(StringBuilder* output) const | 93 void Value::writeJSON(StringBuilder* output) const |
| 93 { | 94 { |
| 94 ASSERT(m_type == TypeNull); | 95 ASSERT(m_type == TypeNull); |
| 95 output->append(nullString, 4); | 96 output->append(nullString, 4); |
| 96 } | 97 } |
| 97 | 98 |
| 99 PassOwnPtr<Value> Value::clone() const | |
| 100 { | |
| 101 return Value::null(); | |
| 102 } | |
| 103 | |
| 98 bool FundamentalValue::asBoolean(bool* output) const | 104 bool FundamentalValue::asBoolean(bool* output) const |
| 99 { | 105 { |
| 100 if (type() != TypeBoolean) | 106 if (type() != TypeBoolean) |
| 101 return false; | 107 return false; |
| 102 *output = m_boolValue; | 108 *output = m_boolValue; |
| 103 return true; | 109 return true; |
| 104 } | 110 } |
| 105 | 111 |
| 106 bool FundamentalValue::asNumber(double* output) const | 112 bool FundamentalValue::asNumber(double* output) const |
| 107 { | 113 { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 129 output->append(falseString, 5); | 135 output->append(falseString, 5); |
| 130 } else if (type() == TypeNumber) { | 136 } else if (type() == TypeNumber) { |
| 131 if (!std::isfinite(m_doubleValue)) { | 137 if (!std::isfinite(m_doubleValue)) { |
| 132 output->append(nullString, 4); | 138 output->append(nullString, 4); |
| 133 return; | 139 return; |
| 134 } | 140 } |
| 135 output->append(Decimal::fromDouble(m_doubleValue).toString()); | 141 output->append(Decimal::fromDouble(m_doubleValue).toString()); |
| 136 } | 142 } |
| 137 } | 143 } |
| 138 | 144 |
| 145 PassOwnPtr<Value> FundamentalValue::clone() const | |
| 146 { | |
| 147 return type() == TypeNumber ? FundamentalValue::create(m_doubleValue) : Fund amentalValue::create(m_boolValue); | |
| 148 } | |
| 149 | |
| 139 bool StringValue::asString(String* output) const | 150 bool StringValue::asString(String* output) const |
| 140 { | 151 { |
| 141 *output = m_stringValue; | 152 *output = m_stringValue; |
| 142 return true; | 153 return true; |
| 143 } | 154 } |
| 144 | 155 |
| 145 void StringValue::writeJSON(StringBuilder* output) const | 156 void StringValue::writeJSON(StringBuilder* output) const |
| 146 { | 157 { |
| 147 ASSERT(type() == TypeString); | 158 ASSERT(type() == TypeString); |
| 148 doubleQuoteStringForJSON(m_stringValue, output); | 159 doubleQuoteStringForJSON(m_stringValue, output); |
| 149 } | 160 } |
| 150 | 161 |
| 162 PassOwnPtr<Value> StringValue::clone() const | |
| 163 { | |
| 164 return StringValue::create(m_stringValue); | |
| 165 } | |
| 166 | |
| 151 DictionaryValue::~DictionaryValue() | 167 DictionaryValue::~DictionaryValue() |
| 152 { | 168 { |
| 153 } | 169 } |
| 154 | 170 |
| 155 void DictionaryValue::setBoolean(const String& name, bool value) | 171 void DictionaryValue::setBoolean(const String& name, bool value) |
| 156 { | 172 { |
| 157 setValue(name, FundamentalValue::create(value)); | 173 setValue(name, FundamentalValue::create(value)); |
| 158 } | 174 } |
| 159 | 175 |
| 160 void DictionaryValue::setNumber(const String& name, double value) | 176 void DictionaryValue::setNumber(const String& name, double value) |
| 161 { | 177 { |
| 162 setValue(name, FundamentalValue::create(value)); | 178 setValue(name, FundamentalValue::create(value)); |
| 163 } | 179 } |
| 164 | 180 |
| 165 void DictionaryValue::setString(const String& name, const String& value) | 181 void DictionaryValue::setString(const String& name, const String& value) |
| 166 { | 182 { |
| 167 setValue(name, StringValue::create(value)); | 183 setValue(name, StringValue::create(value)); |
| 168 } | 184 } |
| 169 | 185 |
| 170 void DictionaryValue::setValue(const String& name, PassRefPtr<Value> value) | 186 void DictionaryValue::setValue(const String& name, PassOwnPtr<Value> value) |
| 171 { | 187 { |
| 172 ASSERT(value); | 188 ASSERT(value); |
| 173 if (m_data.set(name, value).isNewEntry) | 189 if (m_data.set(name, value).isNewEntry) |
| 174 m_order.append(name); | 190 m_order.append(name); |
| 175 } | 191 } |
| 176 | 192 |
| 177 void DictionaryValue::setObject(const String& name, PassRefPtr<DictionaryValue> value) | 193 void DictionaryValue::setObject(const String& name, PassOwnPtr<DictionaryValue> value) |
| 178 { | 194 { |
| 179 ASSERT(value); | 195 ASSERT(value); |
| 180 if (m_data.set(name, value).isNewEntry) | 196 if (m_data.set(name, value).isNewEntry) |
| 181 m_order.append(name); | 197 m_order.append(name); |
| 182 } | 198 } |
| 183 | 199 |
| 184 void DictionaryValue::setArray(const String& name, PassRefPtr<ListValue> value) | 200 void DictionaryValue::setArray(const String& name, PassOwnPtr<ListValue> value) |
| 185 { | 201 { |
| 186 ASSERT(value); | 202 ASSERT(value); |
| 187 if (m_data.set(name, value).isNewEntry) | 203 if (m_data.set(name, value).isNewEntry) |
| 188 m_order.append(name); | 204 m_order.append(name); |
| 189 } | 205 } |
| 190 | 206 |
| 191 DictionaryValue::iterator DictionaryValue::find(const String& name) | 207 DictionaryValue::iterator DictionaryValue::find(const String& name) |
| 192 { | 208 { |
| 193 return m_data.find(name); | 209 return m_data.find(name); |
| 194 } | 210 } |
| 195 | 211 |
| 196 DictionaryValue::const_iterator DictionaryValue::find(const String& name) const | 212 DictionaryValue::const_iterator DictionaryValue::find(const String& name) const |
| 197 { | 213 { |
| 198 return m_data.find(name); | 214 return m_data.find(name); |
| 199 } | 215 } |
| 200 | 216 |
| 201 bool DictionaryValue::getBoolean(const String& name, bool* output) const | 217 bool DictionaryValue::getBoolean(const String& name, bool* output) const |
| 202 { | 218 { |
| 203 RefPtr<protocol::Value> value = get(name); | 219 protocol::Value* value = get(name); |
| 204 if (!value) | 220 if (!value) |
| 205 return false; | 221 return false; |
| 206 return value->asBoolean(output); | 222 return value->asBoolean(output); |
| 207 } | 223 } |
| 208 | 224 |
| 209 bool DictionaryValue::getString(const String& name, String* output) const | 225 bool DictionaryValue::getString(const String& name, String* output) const |
| 210 { | 226 { |
| 211 RefPtr<protocol::Value> value = get(name); | 227 protocol::Value* value = get(name); |
| 212 if (!value) | 228 if (!value) |
| 213 return false; | 229 return false; |
| 214 return value->asString(output); | 230 return value->asString(output); |
| 215 } | 231 } |
| 216 | 232 |
| 217 PassRefPtr<DictionaryValue> DictionaryValue::getObject(const String& name) const | 233 DictionaryValue* DictionaryValue::getObject(const String& name) const |
| 218 { | 234 { |
| 219 return DictionaryValue::cast(get(name)); | 235 return DictionaryValue::cast(get(name)); |
| 220 } | 236 } |
| 221 | 237 |
| 222 PassRefPtr<protocol::ListValue> DictionaryValue::getArray(const String& name) co nst | 238 protocol::ListValue* DictionaryValue::getArray(const String& name) const |
| 223 { | 239 { |
| 224 return ListValue::cast(get(name)); | 240 return ListValue::cast(get(name)); |
| 225 } | 241 } |
| 226 | 242 |
| 227 PassRefPtr<protocol::Value> DictionaryValue::get(const String& name) const | 243 protocol::Value* DictionaryValue::get(const String& name) const |
| 228 { | 244 { |
| 229 Dictionary::const_iterator it = m_data.find(name); | 245 Dictionary::const_iterator it = m_data.find(name); |
| 230 if (it == m_data.end()) | 246 if (it == m_data.end()) |
| 231 return nullptr; | 247 return nullptr; |
| 232 return it->value; | 248 return it->value.get(); |
| 233 } | 249 } |
| 234 | 250 |
| 235 bool DictionaryValue::booleanProperty(const String& name, bool defaultValue) con st | 251 bool DictionaryValue::booleanProperty(const String& name, bool defaultValue) con st |
| 236 { | 252 { |
| 237 bool result = defaultValue; | 253 bool result = defaultValue; |
| 238 getBoolean(name, &result); | 254 getBoolean(name, &result); |
| 239 return result; | 255 return result; |
| 240 } | 256 } |
| 241 | 257 |
| 242 void DictionaryValue::remove(const String& name) | 258 void DictionaryValue::remove(const String& name) |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 258 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end()); | 274 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end()); |
| 259 if (i) | 275 if (i) |
| 260 output->append(','); | 276 output->append(','); |
| 261 doubleQuoteStringForJSON(it->key, output); | 277 doubleQuoteStringForJSON(it->key, output); |
| 262 output->append(':'); | 278 output->append(':'); |
| 263 it->value->writeJSON(output); | 279 it->value->writeJSON(output); |
| 264 } | 280 } |
| 265 output->append('}'); | 281 output->append('}'); |
| 266 } | 282 } |
| 267 | 283 |
| 284 PassOwnPtr<Value> DictionaryValue::clone() const | |
| 285 { | |
| 286 OwnPtr<DictionaryValue> result = DictionaryValue::create(); | |
| 287 for (size_t i = 0; i < m_order.size(); ++i) { | |
| 288 Dictionary::const_iterator it = m_data.find(m_order[i]); | |
| 289 ASSERT_WITH_SECURITY_IMPLICATION(it != m_data.end()); | |
|
dgozman
2016/03/01 01:45:05
Security implication? Why not just assert?
| |
| 290 result->setValue(it->key, it->value->clone()); | |
| 291 } | |
| 292 return result.release(); | |
| 293 } | |
| 294 | |
| 268 DictionaryValue::DictionaryValue() | 295 DictionaryValue::DictionaryValue() |
| 269 : Value(TypeObject) | 296 : Value(TypeObject) |
| 270 , m_data() | 297 , m_data() |
| 271 , m_order() | 298 , m_order() |
| 272 { | 299 { |
| 273 } | 300 } |
| 274 | 301 |
| 275 ListValue::~ListValue() | 302 ListValue::~ListValue() |
| 276 { | 303 { |
| 277 } | 304 } |
| 278 | 305 |
| 279 void ListValue::writeJSON(StringBuilder* output) const | 306 void ListValue::writeJSON(StringBuilder* output) const |
| 280 { | 307 { |
| 281 output->append('['); | 308 output->append('['); |
| 282 for (Vector<RefPtr<protocol::Value>>::const_iterator it = m_data.begin(); it != m_data.end(); ++it) { | 309 for (Vector<OwnPtr<protocol::Value>>::const_iterator it = m_data.begin(); it != m_data.end(); ++it) { |
| 283 if (it != m_data.begin()) | 310 if (it != m_data.begin()) |
| 284 output->append(','); | 311 output->append(','); |
| 285 (*it)->writeJSON(output); | 312 (*it)->writeJSON(output); |
| 286 } | 313 } |
| 287 output->append(']'); | 314 output->append(']'); |
| 288 } | 315 } |
| 289 | 316 |
| 317 PassOwnPtr<Value> ListValue::clone() const | |
| 318 { | |
| 319 OwnPtr<ListValue> result = ListValue::create(); | |
| 320 for (Vector<OwnPtr<protocol::Value>>::const_iterator it = m_data.begin(); it != m_data.end(); ++it) | |
| 321 result->pushValue((*it)->clone()); | |
| 322 return result.release(); | |
| 323 } | |
| 324 | |
| 290 ListValue::ListValue() | 325 ListValue::ListValue() |
| 291 : Value(TypeArray) | 326 : Value(TypeArray) |
| 292 , m_data() | 327 , m_data() |
| 293 { | 328 { |
| 294 } | 329 } |
| 295 | 330 |
| 296 void ListValue::pushValue(PassRefPtr<protocol::Value> value) | 331 void ListValue::pushValue(PassOwnPtr<protocol::Value> value) |
| 297 { | 332 { |
| 298 ASSERT(value); | 333 ASSERT(value); |
| 299 m_data.append(value); | 334 m_data.append(value); |
| 300 } | 335 } |
| 301 | 336 |
| 302 PassRefPtr<protocol::Value> ListValue::get(size_t index) | 337 protocol::Value* ListValue::get(size_t index) |
| 303 { | 338 { |
| 304 ASSERT_WITH_SECURITY_IMPLICATION(index < m_data.size()); | 339 ASSERT_WITH_SECURITY_IMPLICATION(index < m_data.size()); |
| 305 return m_data[index]; | 340 return m_data[index].get(); |
| 306 } | 341 } |
| 307 | 342 |
| 308 } // namespace protocol | 343 } // namespace protocol |
| 309 } // namespace blink | 344 } // namespace blink |
| OLD | NEW |