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 JSONObject::iterator it = m_properties->find(propertyName); 88 long value = 0; 89 if (it != m_properties->end()) 90 it->value->asNumber(&value); 91 return value; 92 } 93 94 double InspectorState::getDouble(const String& propertyName) 95 { 96 JSONObject::iterator it = m_properties->find(propertyName); 97 double value = 0; 98 if (it != m_properties->end()) 99 it->value->asNumber(&value); 100 return value; 101 } 102 103 PassRefPtr<JSONObject> InspectorState::getObject(const String& propertyName) 104 { 105 JSONObject::iterator it = m_properties->find(propertyName); 106 if (it == m_properties->end()) { 107 m_properties->setObject(propertyName, JSONObject::create()); 108 it = m_properties->find(propertyName); 109 } 110 return it->value->asObject(); 111 } 112 113 InspectorState* InspectorCompositeState::createAgentState(const String& agentName) 114 { 115 ASSERT(m_stateObject->find(agentName) == m_stateObject->end()); 116 ASSERT(m_inspectorStateMap.find(agentName) == m_inspectorStateMap.end()); 117 RefPtr<JSONObject> stateProperties = JSONObject::create(); 118 m_stateObject->setObject(agentName, stateProperties); 119 OwnPtr<InspectorState> statePtr = adoptPtr(new InspectorState(this, stateProperties)); 120 InspectorState* state = statePtr.get(); 121 m_inspectorStateMap.add(agentName, statePtr.release()); 122 return state; 123 } 124 125 void InspectorCompositeState::loadFromCookie(const String& inspectorCompositeStateCookie) 126 { 127 RefPtr<JSONValue> cookie = parseJSON(inspectorCompositeStateCookie); 128 if (cookie) 129 m_stateObject = cookie->asObject(); 130 if (!m_stateObject) 131 m_stateObject = JSONObject::create(); 132 133 InspectorStateMap::iterator end = m_inspectorStateMap.end(); 134 for (InspectorStateMap::iterator it = m_inspectorStateMap.begin(); it != end; ++it) { 135 RefPtr<JSONObject> agentStateObject = m_stateObject->getObject(it->key); 136 if (!agentStateObject) { 137 agentStateObject = JSONObject::create(); 138 m_stateObject->setObject(it->key, agentStateObject); 139 } 140 it->value->setFromCookie(agentStateObject); 141 } 142 } 143 144 void InspectorCompositeState::mute() 145 { 146 m_isMuted = true; 147 } 148 149 void InspectorCompositeState::unmute() 150 { 151 m_isMuted = false; 152 } 153 154 void InspectorCompositeState::inspectorStateUpdated() 155 { 156 if (m_client && !m_isMuted) 157 m_client->updateInspectorStateCookie(m_stateObject->toJSONString()); 158 } 159 160 } // namespace WebCore 161 162