2 * Copyright (C) 2009 Google Inc. All rights reserved.
3 * Copyright (C) 2014 University of Washington. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following disclaimer
13 * in the documentation and/or other materials provided with the
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #ifndef InspectorValues_h
33 #define InspectorValues_h
35 #include <wtf/Assertions.h>
36 #include <wtf/HashMap.h>
37 #include <wtf/RefCounted.h>
38 #include <wtf/Vector.h>
39 #include <wtf/text/StringHash.h>
40 #include <wtf/text/WTFString.h>
45 class InspectorArrayBase
;
46 class InspectorObject
;
47 class InspectorObjectBase
;
49 class JS_EXPORT_PRIVATE InspectorValue
: public RefCounted
<InspectorValue
> {
51 static const int maxDepth
= 1000;
54 : m_type(Type::Null
) { }
55 virtual ~InspectorValue() { }
57 static Ref
<InspectorValue
> null();
69 Type
type() const { return m_type
; }
71 bool isNull() const { return m_type
== Type::Null
; }
73 virtual bool asBoolean(bool&) const;
74 virtual bool asInteger(int&) const;
75 virtual bool asInteger(unsigned&) const;
76 virtual bool asInteger(long&) const;
77 virtual bool asInteger(long long&) const;
78 virtual bool asInteger(unsigned long&) const;
79 virtual bool asInteger(unsigned long long&) const;
80 virtual bool asDouble(double&) const;
81 virtual bool asDouble(float&) const;
82 virtual bool asString(String
&) const;
83 virtual bool asValue(RefPtr
<InspectorValue
>&);
84 virtual bool asObject(RefPtr
<InspectorObject
>&);
85 virtual bool asArray(RefPtr
<InspectorArray
>&);
87 static bool parseJSON(const String
& jsonInput
, RefPtr
<InspectorValue
>& output
);
89 String
toJSONString() const;
90 virtual void writeJSON(StringBuilder
& output
) const;
93 explicit InspectorValue(Type type
) : m_type(type
) { }
99 class JS_EXPORT_PRIVATE InspectorBasicValue
: public InspectorValue
{
102 static Ref
<InspectorBasicValue
> create(bool);
103 static Ref
<InspectorBasicValue
> create(int);
104 static Ref
<InspectorBasicValue
> create(double);
106 virtual bool asBoolean(bool&) const override
;
107 // Numbers from the frontend are always parsed as doubles, so we allow
108 // clients to convert to integral values with this function.
109 virtual bool asInteger(int&) const override
;
110 virtual bool asInteger(unsigned&) const override
;
111 virtual bool asInteger(long&) const override
;
112 virtual bool asInteger(long long&) const override
;
113 virtual bool asInteger(unsigned long&) const override
;
114 virtual bool asInteger(unsigned long long&) const override
;
115 virtual bool asDouble(double&) const override
;
116 virtual bool asDouble(float&) const override
;
118 virtual void writeJSON(StringBuilder
& output
) const override
;
121 explicit InspectorBasicValue(bool value
)
122 : InspectorValue(Type::Boolean
)
123 , m_booleanValue(value
) { }
125 explicit InspectorBasicValue(int value
)
126 : InspectorValue(Type::Integer
)
127 , m_doubleValue(static_cast<double>(value
)) { }
129 explicit InspectorBasicValue(double value
)
130 : InspectorValue(Type::Double
)
131 , m_doubleValue(value
) { }
135 double m_doubleValue
;
139 class JS_EXPORT_PRIVATE InspectorString
: public InspectorValue
{
141 static Ref
<InspectorString
> create(const String
&);
142 static Ref
<InspectorString
> create(const char*);
144 virtual bool asString(String
& output
) const override
;
146 virtual void writeJSON(StringBuilder
& output
) const override
;
149 explicit InspectorString(const String
& value
)
150 : InspectorValue(Type::String
)
151 , m_stringValue(value
) { }
153 explicit InspectorString(const char* value
)
154 : InspectorValue(Type::String
)
155 , m_stringValue(value
) { }
157 String m_stringValue
;
160 class JS_EXPORT_PRIVATE InspectorObjectBase
: public InspectorValue
{
162 typedef HashMap
<String
, RefPtr
<InspectorValue
>> Dictionary
;
165 typedef Dictionary::iterator iterator
;
166 typedef Dictionary::const_iterator const_iterator
;
168 InspectorObject
* openAccessors();
171 virtual ~InspectorObjectBase();
173 virtual bool asObject(RefPtr
<InspectorObject
>& output
) override
;
175 // FIXME: use templates to reduce the amount of duplicated set*() methods.
176 void setBoolean(const String
& name
, bool);
177 void setInteger(const String
& name
, int);
178 void setDouble(const String
& name
, double);
179 void setString(const String
& name
, const String
&);
180 void setValue(const String
& name
, RefPtr
<InspectorValue
>&&);
181 void setObject(const String
& name
, RefPtr
<InspectorObjectBase
>&&);
182 void setArray(const String
& name
, RefPtr
<InspectorArrayBase
>&&);
184 iterator
find(const String
& name
);
185 const_iterator
find(const String
& name
) const;
187 // FIXME: use templates to reduce the amount of duplicated get*() methods.
188 bool getBoolean(const String
& name
, bool& output
) const;
189 template<class T
> bool getDouble(const String
& name
, T
& output
) const
191 RefPtr
<InspectorValue
> value
;
192 if (!getValue(name
, value
))
195 return value
->asDouble(output
);
197 template<class T
> bool getInteger(const String
& name
, T
& output
) const
199 RefPtr
<InspectorValue
> value
;
200 if (!getValue(name
, value
))
203 return value
->asInteger(output
);
206 bool getString(const String
& name
, String
& output
) const;
207 bool getObject(const String
& name
, RefPtr
<InspectorObject
>&) const;
208 bool getArray(const String
& name
, RefPtr
<InspectorArray
>&) const;
209 bool getValue(const String
& name
, RefPtr
<InspectorValue
>&) const;
211 void remove(const String
& name
);
213 virtual void writeJSON(StringBuilder
& output
) const override
;
215 iterator
begin() { return m_data
.begin(); }
216 iterator
end() { return m_data
.end(); }
217 const_iterator
begin() const { return m_data
.begin(); }
218 const_iterator
end() const { return m_data
.end(); }
220 int size() const { return m_data
.size(); }
223 InspectorObjectBase();
227 Vector
<String
> m_order
;
230 class InspectorObject
: public InspectorObjectBase
{
232 static JS_EXPORT_PRIVATE Ref
<InspectorObject
> create();
234 using InspectorObjectBase::asObject
;
236 using InspectorObjectBase::setBoolean
;
237 using InspectorObjectBase::setInteger
;
238 using InspectorObjectBase::setDouble
;
239 using InspectorObjectBase::setString
;
240 using InspectorObjectBase::setValue
;
241 using InspectorObjectBase::setObject
;
242 using InspectorObjectBase::setArray
;
244 using InspectorObjectBase::find
;
245 using InspectorObjectBase::getBoolean
;
246 using InspectorObjectBase::getInteger
;
247 using InspectorObjectBase::getDouble
;
248 using InspectorObjectBase::getString
;
249 using InspectorObjectBase::getObject
;
250 using InspectorObjectBase::getArray
;
251 using InspectorObjectBase::getValue
;
253 using InspectorObjectBase::remove
;
255 using InspectorObjectBase::begin
;
256 using InspectorObjectBase::end
;
258 using InspectorObjectBase::size
;
262 class JS_EXPORT_PRIVATE InspectorArrayBase
: public InspectorValue
{
264 typedef Vector
<RefPtr
<InspectorValue
>>::iterator iterator
;
265 typedef Vector
<RefPtr
<InspectorValue
>>::const_iterator const_iterator
;
267 unsigned length() const { return static_cast<unsigned>(m_data
.size()); }
270 virtual ~InspectorArrayBase();
272 virtual bool asArray(RefPtr
<InspectorArray
>&) override
;
274 void pushBoolean(bool);
275 void pushInteger(int);
276 void pushDouble(double);
277 void pushString(const String
&);
278 void pushValue(RefPtr
<InspectorValue
>&&);
279 void pushObject(RefPtr
<InspectorObjectBase
>&&);
280 void pushArray(RefPtr
<InspectorArrayBase
>&&);
282 RefPtr
<InspectorValue
> get(size_t index
) const;
284 virtual void writeJSON(StringBuilder
& output
) const override
;
286 iterator
begin() { return m_data
.begin(); }
287 iterator
end() { return m_data
.end(); }
288 const_iterator
begin() const { return m_data
.begin(); }
289 const_iterator
end() const { return m_data
.end(); }
292 InspectorArrayBase();
295 Vector
<RefPtr
<InspectorValue
>> m_data
;
298 class InspectorArray
: public InspectorArrayBase
{
300 static JS_EXPORT_PRIVATE Ref
<InspectorArray
> create();
302 using InspectorArrayBase::asArray
;
304 using InspectorArrayBase::pushBoolean
;
305 using InspectorArrayBase::pushInteger
;
306 using InspectorArrayBase::pushDouble
;
307 using InspectorArrayBase::pushString
;
308 using InspectorArrayBase::pushValue
;
309 using InspectorArrayBase::pushObject
;
310 using InspectorArrayBase::pushArray
;
312 using InspectorArrayBase::get
;
314 using InspectorArrayBase::begin
;
315 using InspectorArrayBase::end
;
319 inline InspectorObjectBase::iterator
InspectorObjectBase::find(const String
& name
)
321 return m_data
.find(name
);
324 inline InspectorObjectBase::const_iterator
InspectorObjectBase::find(const String
& name
) const
326 return m_data
.find(name
);
329 inline void InspectorObjectBase::setBoolean(const String
& name
, bool value
)
331 setValue(name
, InspectorBasicValue::create(value
));
334 inline void InspectorObjectBase::setInteger(const String
& name
, int value
)
336 setValue(name
, InspectorBasicValue::create(value
));
339 inline void InspectorObjectBase::setDouble(const String
& name
, double value
)
341 setValue(name
, InspectorBasicValue::create(value
));
344 inline void InspectorObjectBase::setString(const String
& name
, const String
& value
)
346 setValue(name
, InspectorString::create(value
));
349 inline void InspectorObjectBase::setValue(const String
& name
, RefPtr
<InspectorValue
>&& value
)
352 if (m_data
.set(name
, WTF::move(value
)).isNewEntry
)
353 m_order
.append(name
);
356 inline void InspectorObjectBase::setObject(const String
& name
, RefPtr
<InspectorObjectBase
>&& value
)
359 if (m_data
.set(name
, WTF::move(value
)).isNewEntry
)
360 m_order
.append(name
);
363 inline void InspectorObjectBase::setArray(const String
& name
, RefPtr
<InspectorArrayBase
>&& value
)
366 if (m_data
.set(name
, WTF::move(value
)).isNewEntry
)
367 m_order
.append(name
);
370 inline void InspectorArrayBase::pushBoolean(bool value
)
372 m_data
.append(InspectorBasicValue::create(value
));
375 inline void InspectorArrayBase::pushInteger(int value
)
377 m_data
.append(InspectorBasicValue::create(value
));
380 inline void InspectorArrayBase::pushDouble(double value
)
382 m_data
.append(InspectorBasicValue::create(value
));
385 inline void InspectorArrayBase::pushString(const String
& value
)
387 m_data
.append(InspectorString::create(value
));
390 inline void InspectorArrayBase::pushValue(RefPtr
<InspectorValue
>&& value
)
393 m_data
.append(WTF::move(value
));
396 inline void InspectorArrayBase::pushObject(RefPtr
<InspectorObjectBase
>&& value
)
399 m_data
.append(WTF::move(value
));
402 inline void InspectorArrayBase::pushArray(RefPtr
<InspectorArrayBase
>&& value
)
405 m_data
.append(WTF::move(value
));
408 } // namespace Inspector
410 #endif // !defined(InspectorValues_h)