Home | History | Annotate | Download | only in inspector
      1 /*
      2  * Copyright (C) 2011 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
      6  * are met:
      7  *
      8  * 1.  Redistributions of source code must retain the above copyright
      9  *     notice, this list of conditions and the following disclaimer.
     10  * 2.  Redistributions in binary form must reproduce the above copyright
     11  *     notice, this list of conditions and the following disclaimer in the
     12  *     documentation and/or other materials provided with the distribution.
     13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     14  *     its contributors may be used to endorse or promote products derived
     15  *     from this software without specific prior written permission.
     16  *
     17  * THIS SOFTWARE IS PROVIDED BY GOOGLE AND ITS CONTRIBUTORS "AS IS" AND ANY
     18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
     21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 #include "core/inspector/InspectorState.h"
     31 
     32 #include "core/inspector/InspectorStateClient.h"
     33 #include "core/inspector/JSONParser.h"
     34 #include "wtf/PassOwnPtr.h"
     35 
     36 namespace WebCore {
     37 
     38 InspectorState::InspectorState(InspectorStateUpdateListener* listener, PassRefPtr<JSONObject> properties)
     39     : m_listener(listener)
     40     , m_properties(properties)
     41 {
     42 }
     43 
     44 void InspectorState::updateCookie()
     45 {
     46     if (m_listener)
     47         m_listener->inspectorStateUpdated();
     48 }
     49 
     50 void InspectorState::setFromCookie(PassRefPtr<JSONObject> properties)
     51 {
     52     m_properties = properties;
     53 }
     54 
     55 void InspectorState::setValue(const String& propertyName, PassRefPtr<JSONValue> value)
     56 {
     57     m_properties->setValue(propertyName, value);
     58     updateCookie();
     59 }
     60 
     61 void InspectorState::remove(const String& propertyName)
     62 {
     63     m_properties->remove(propertyName);
     64     updateCookie();
     65 }
     66 
     67 bool InspectorState::getBoolean(const String& propertyName)
     68 {
     69     JSONObject::iterator it = m_properties->find(propertyName);
     70     bool value = false;
     71     if (it != m_properties->end())
     72         it->value->asBoolean(&value);
     73     return value;
     74 }
     75 
     76 String InspectorState::getString(const String& propertyName)
     77 {
     78     JSONObject::iterator it = m_properties->find(propertyName);
     79     String value;
     80     if (it != m_properties->end())
     81         it->value->asString(&value);
     82     return value;
     83 }
     84 
     85 long InspectorState::getLong(const String& propertyName)
     86 {
     87     return getLong(propertyName, 0);
     88 }
     89 
     90 
     91 long InspectorState::getLong(const String& propertyName, long defaultValue)
     92 {
     93     JSONObject::iterator it = m_properties->find(propertyName);
     94     long value = defaultValue;
     95     if (it != m_properties->end())
     96         it->value->asNumber(&value);
     97     return value;
     98 }
     99 
    100 double InspectorState::getDouble(const String& propertyName)
    101 {
    102     return getDouble(propertyName, 0);
    103 }
    104 
    105 double InspectorState::getDouble(const String& propertyName, double defaultValue)
    106 {
    107     JSONObject::iterator it = m_properties->find(propertyName);
    108     double value = defaultValue;
    109     if (it != m_properties->end())
    110         it->value->asNumber(&value);
    111     return value;
    112 }
    113 
    114 PassRefPtr<JSONObject> InspectorState::getObject(const String& propertyName)
    115 {
    116     JSONObject::iterator it = m_properties->find(propertyName);
    117     if (it == m_properties->end()) {
    118         m_properties->setObject(propertyName, JSONObject::create());
    119         it = m_properties->find(propertyName);
    120     }
    121     return it->value->asObject();
    122 }
    123 
    124 InspectorState* InspectorCompositeState::createAgentState(const String& agentName)
    125 {
    126     ASSERT(m_stateObject->find(agentName) == m_stateObject->end());
    127     ASSERT(m_inspectorStateMap.find(agentName) == m_inspectorStateMap.end());
    128     RefPtr<JSONObject> stateProperties = JSONObject::create();
    129     m_stateObject->setObject(agentName, stateProperties);
    130     OwnPtr<InspectorState> statePtr = adoptPtr(new InspectorState(this, stateProperties));
    131     InspectorState* state = statePtr.get();
    132     m_inspectorStateMap.add(agentName, statePtr.release());
    133     return state;
    134 }
    135 
    136 void InspectorCompositeState::loadFromCookie(const String& inspectorCompositeStateCookie)
    137 {
    138     RefPtr<JSONValue> cookie = parseJSON(inspectorCompositeStateCookie);
    139     if (cookie)
    140         m_stateObject = cookie->asObject();
    141     if (!m_stateObject)
    142         m_stateObject = JSONObject::create();
    143 
    144     InspectorStateMap::iterator end = m_inspectorStateMap.end();
    145     for (InspectorStateMap::iterator it = m_inspectorStateMap.begin(); it != end; ++it) {
    146         RefPtr<JSONObject> agentStateObject = m_stateObject->getObject(it->key);
    147         if (!agentStateObject) {
    148             agentStateObject = JSONObject::create();
    149             m_stateObject->setObject(it->key, agentStateObject);
    150         }
    151         it->value->setFromCookie(agentStateObject);
    152     }
    153 }
    154 
    155 void InspectorCompositeState::mute()
    156 {
    157     m_isMuted = true;
    158 }
    159 
    160 void InspectorCompositeState::unmute()
    161 {
    162     m_isMuted = false;
    163 }
    164 
    165 void InspectorCompositeState::inspectorStateUpdated()
    166 {
    167     if (m_client && !m_isMuted)
    168         m_client->updateInspectorStateCookie(m_stateObject->toJSONString());
    169 }
    170 
    171 } // namespace WebCore
    172 
    173