Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2009 Google Inc. All rights reserved.
      3  *
      4  * Redistribution and use in source and binary forms, with or without
      5  * modification, are permitted provided that the following conditions are
      6  * met:
      7  *
      8  *     * Redistributions of source code must retain the above copyright
      9  * notice, this list of conditions and the following disclaimer.
     10  *     * Redistributions in binary form must reproduce the above
     11  * copyright notice, this list of conditions and the following disclaimer
     12  * in the documentation and/or other materials provided with the
     13  * distribution.
     14  *     * Neither the name of Google Inc. nor the names of its
     15  * contributors may be used to endorse or promote products derived from
     16  * this software without specific prior written permission.
     17  *
     18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29  */
     30 
     31 #ifndef InspectorValues_h
     32 #define InspectorValues_h
     33 
     34 #if ENABLE(INSPECTOR)
     35 
     36 #include "PlatformString.h"
     37 
     38 #include <wtf/Forward.h>
     39 #include <wtf/HashMap.h>
     40 #include <wtf/RefCounted.h>
     41 #include <wtf/Vector.h>
     42 #include <wtf/text/StringHash.h>
     43 
     44 namespace WebCore {
     45 
     46 class InspectorArray;
     47 class InspectorObject;
     48 
     49 class InspectorValue : public RefCounted<InspectorValue> {
     50 public:
     51     InspectorValue() : m_type(TypeNull) { }
     52     virtual ~InspectorValue() { }
     53 
     54     static PassRefPtr<InspectorValue> null()
     55     {
     56         return adoptRef(new InspectorValue());
     57     }
     58 
     59     typedef enum {
     60         TypeNull = 0,
     61         TypeBoolean,
     62         TypeNumber,
     63         TypeString,
     64         TypeObject,
     65         TypeArray
     66     } Type;
     67 
     68     Type type() const { return m_type; }
     69 
     70     bool isNull() const { return m_type == TypeNull; }
     71 
     72     virtual bool asBoolean(bool* output) const;
     73     virtual bool asNumber(double* output) const;
     74     virtual bool asNumber(long* output) const;
     75     virtual bool asNumber(int* output) const;
     76     virtual bool asNumber(unsigned long* output) const;
     77     virtual bool asNumber(unsigned int* output) const;
     78     virtual bool asString(String* output) const;
     79     virtual bool asValue(RefPtr<InspectorValue>* output);
     80     virtual bool asObject(RefPtr<InspectorObject>* output);
     81     virtual bool asArray(RefPtr<InspectorArray>* output);
     82     virtual PassRefPtr<InspectorObject> asObject();
     83     virtual PassRefPtr<InspectorArray> asArray();
     84 
     85     static PassRefPtr<InspectorValue> parseJSON(const String& json);
     86 
     87     String toJSONString() const;
     88     virtual void writeJSON(Vector<UChar>* output) const;
     89 
     90 protected:
     91     explicit InspectorValue(Type type) : m_type(type) { }
     92 
     93 private:
     94     Type m_type;
     95 };
     96 
     97 class InspectorBasicValue : public InspectorValue {
     98 public:
     99 
    100     static PassRefPtr<InspectorBasicValue> create(bool value)
    101     {
    102         return adoptRef(new InspectorBasicValue(value));
    103     }
    104 
    105     static PassRefPtr<InspectorBasicValue> create(int value)
    106     {
    107         return adoptRef(new InspectorBasicValue(value));
    108     }
    109 
    110     static PassRefPtr<InspectorBasicValue> create(double value)
    111     {
    112         return adoptRef(new InspectorBasicValue(value));
    113     }
    114 
    115     virtual bool asBoolean(bool* output) const;
    116     virtual bool asNumber(double* output) const;
    117     virtual bool asNumber(long* output) const;
    118     virtual bool asNumber(int* output) const;
    119     virtual bool asNumber(unsigned long* output) const;
    120     virtual bool asNumber(unsigned int* output) const;
    121 
    122     virtual void writeJSON(Vector<UChar>* output) const;
    123 
    124 private:
    125     explicit InspectorBasicValue(bool value) : InspectorValue(TypeBoolean), m_boolValue(value) { }
    126     explicit InspectorBasicValue(int value) : InspectorValue(TypeNumber), m_doubleValue((double)value) { }
    127     explicit InspectorBasicValue(double value) : InspectorValue(TypeNumber), m_doubleValue(value) { }
    128 
    129     union {
    130         bool m_boolValue;
    131         double m_doubleValue;
    132     };
    133 };
    134 
    135 class InspectorString : public InspectorValue {
    136 public:
    137     static PassRefPtr<InspectorString> create(const String& value)
    138     {
    139         return adoptRef(new InspectorString(value));
    140     }
    141 
    142     static PassRefPtr<InspectorString> create(const char* value)
    143     {
    144         return adoptRef(new InspectorString(value));
    145     }
    146 
    147     virtual bool asString(String* output) const;
    148 
    149     virtual void writeJSON(Vector<UChar>* output) const;
    150 
    151 private:
    152     explicit InspectorString(const String& value) : InspectorValue(TypeString), m_stringValue(value) { }
    153     explicit InspectorString(const char* value) : InspectorValue(TypeString), m_stringValue(value) { }
    154 
    155     String m_stringValue;
    156 };
    157 
    158 class InspectorObject : public InspectorValue {
    159 private:
    160     typedef HashMap<String, RefPtr<InspectorValue> > Dictionary;
    161 
    162 public:
    163     typedef Dictionary::iterator iterator;
    164     typedef Dictionary::const_iterator const_iterator;
    165 
    166 public:
    167     static PassRefPtr<InspectorObject> create()
    168     {
    169         return adoptRef(new InspectorObject());
    170     }
    171     ~InspectorObject();
    172 
    173     virtual bool asObject(RefPtr<InspectorObject>* output);
    174     virtual PassRefPtr<InspectorObject> asObject();
    175 
    176     void setBoolean(const String& name, bool);
    177     void setNumber(const String& name, double);
    178     void setString(const String& name, const String&);
    179     void setValue(const String& name, PassRefPtr<InspectorValue>);
    180     void setObject(const String& name, PassRefPtr<InspectorObject>);
    181     void setArray(const String& name, PassRefPtr<InspectorArray>);
    182 
    183     iterator find(const String& name);
    184     const_iterator find(const String& name) const;
    185     bool getBoolean(const String& name, bool* output) const;
    186     template<class T> bool getNumber(const String& name, T* output) const
    187     {
    188         RefPtr<InspectorValue> value = get(name);
    189         if (!value)
    190             return false;
    191         return value->asNumber(output);
    192     }
    193     bool getString(const String& name, String* output) const;
    194     PassRefPtr<InspectorObject> getObject(const String& name) const;
    195     PassRefPtr<InspectorArray> getArray(const String& name) const;
    196     PassRefPtr<InspectorValue> get(const String& name) const;
    197 
    198     void remove(const String& name);
    199 
    200     virtual void writeJSON(Vector<UChar>* output) const;
    201 
    202     iterator begin() { return m_data.begin(); }
    203     iterator end() { return m_data.end(); }
    204     const_iterator begin() const { return m_data.begin(); }
    205     const_iterator end() const { return m_data.end(); }
    206 
    207 private:
    208     InspectorObject();
    209     Dictionary m_data;
    210     Vector<String> m_order;
    211 };
    212 
    213 class InspectorArray : public InspectorValue {
    214 public:
    215     static PassRefPtr<InspectorArray> create()
    216     {
    217         return adoptRef(new InspectorArray());
    218     }
    219     ~InspectorArray();
    220 
    221     virtual bool asArray(RefPtr<InspectorArray>* output);
    222     virtual PassRefPtr<InspectorArray> asArray();
    223 
    224     void pushBoolean(bool);
    225     void pushNumber(double);
    226     void pushString(const String&);
    227     void pushValue(PassRefPtr<InspectorValue>);
    228     void pushObject(PassRefPtr<InspectorObject>);
    229     void pushArray(PassRefPtr<InspectorArray>);
    230     unsigned length() const { return m_data.size(); }
    231 
    232     PassRefPtr<InspectorValue> get(size_t index);
    233 
    234     virtual void writeJSON(Vector<UChar>* output) const;
    235 
    236 private:
    237     InspectorArray();
    238     Vector<RefPtr<InspectorValue> > m_data;
    239 };
    240 
    241 inline InspectorObject::iterator InspectorObject::find(const String& name)
    242 {
    243     return m_data.find(name);
    244 }
    245 
    246 inline InspectorObject::const_iterator InspectorObject::find(const String& name) const
    247 {
    248     return m_data.find(name);
    249 }
    250 
    251 inline void InspectorObject::setBoolean(const String& name, bool value)
    252 {
    253     setValue(name, InspectorBasicValue::create(value));
    254 }
    255 
    256 inline void InspectorObject::setNumber(const String& name, double value)
    257 {
    258     setValue(name, InspectorBasicValue::create(value));
    259 }
    260 
    261 inline void InspectorObject::setString(const String& name, const String& value)
    262 {
    263     setValue(name, InspectorString::create(value));
    264 }
    265 
    266 inline void InspectorObject::setValue(const String& name, PassRefPtr<InspectorValue> value)
    267 {
    268     if (m_data.set(name, value).second)
    269         m_order.append(name);
    270 }
    271 
    272 inline void InspectorObject::setObject(const String& name, PassRefPtr<InspectorObject> value)
    273 {
    274     if (m_data.set(name, value).second)
    275         m_order.append(name);
    276 }
    277 
    278 inline void InspectorObject::setArray(const String& name, PassRefPtr<InspectorArray> value)
    279 {
    280     if (m_data.set(name, value).second)
    281         m_order.append(name);
    282 }
    283 
    284 inline void InspectorArray::pushBoolean(bool value)
    285 {
    286     m_data.append(InspectorBasicValue::create(value));
    287 }
    288 
    289 inline void InspectorArray::pushNumber(double value)
    290 {
    291     m_data.append(InspectorBasicValue::create(value));
    292 }
    293 
    294 inline void InspectorArray::pushString(const String& value)
    295 {
    296     m_data.append(InspectorString::create(value));
    297 }
    298 
    299 inline void InspectorArray::pushValue(PassRefPtr<InspectorValue> value)
    300 {
    301     m_data.append(value);
    302 }
    303 
    304 inline void InspectorArray::pushObject(PassRefPtr<InspectorObject> value)
    305 {
    306     m_data.append(value);
    307 }
    308 
    309 inline void InspectorArray::pushArray(PassRefPtr<InspectorArray> value)
    310 {
    311     m_data.append(value);
    312 }
    313 
    314 } // namespace WebCore
    315 
    316 #endif // ENABLE(INSPECTOR)
    317 #endif // !defined(InspectorValues_h)
    318