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 36namespace blink { 37 38InspectorState::InspectorState(InspectorStateUpdateListener* listener, PassRefPtr<JSONObject> properties) 39 : m_listener(listener) 40 , m_properties(properties) 41{ 42} 43 44void InspectorState::updateCookie() 45{ 46 if (m_listener) 47 m_listener->inspectorStateUpdated(); 48} 49 50void InspectorState::setFromCookie(PassRefPtr<JSONObject> properties) 51{ 52 m_properties = properties; 53} 54 55void InspectorState::setValue(const String& propertyName, PassRefPtr<JSONValue> value) 56{ 57 m_properties->setValue(propertyName, value); 58 updateCookie(); 59} 60 61void InspectorState::remove(const String& propertyName) 62{ 63 m_properties->remove(propertyName); 64 updateCookie(); 65} 66 67bool 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 76String 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 85long InspectorState::getLong(const String& propertyName) 86{ 87 return getLong(propertyName, 0); 88} 89 90 91long 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 100double InspectorState::getDouble(const String& propertyName) 101{ 102 return getDouble(propertyName, 0); 103} 104 105double 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 114PassRefPtr<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 124void InspectorState::trace(Visitor* visitor) 125{ 126 visitor->trace(m_listener); 127} 128 129InspectorState* InspectorCompositeState::createAgentState(const String& agentName) 130{ 131 ASSERT(m_stateObject->find(agentName) == m_stateObject->end()); 132 ASSERT(m_inspectorStateMap.find(agentName) == m_inspectorStateMap.end()); 133 RefPtr<JSONObject> stateProperties = JSONObject::create(); 134 m_stateObject->setObject(agentName, stateProperties); 135 OwnPtrWillBeRawPtr<InspectorState> statePtr = adoptPtrWillBeNoop(new InspectorState(this, stateProperties)); 136 InspectorState* state = statePtr.get(); 137 m_inspectorStateMap.add(agentName, statePtr.release()); 138 return state; 139} 140 141void InspectorCompositeState::loadFromCookie(const String& inspectorCompositeStateCookie) 142{ 143 RefPtr<JSONValue> cookie = parseJSON(inspectorCompositeStateCookie); 144 if (cookie) 145 m_stateObject = cookie->asObject(); 146 if (!m_stateObject) 147 m_stateObject = JSONObject::create(); 148 149 InspectorStateMap::iterator end = m_inspectorStateMap.end(); 150 for (InspectorStateMap::iterator it = m_inspectorStateMap.begin(); it != end; ++it) { 151 RefPtr<JSONObject> agentStateObject = m_stateObject->getObject(it->key); 152 if (!agentStateObject) { 153 agentStateObject = JSONObject::create(); 154 m_stateObject->setObject(it->key, agentStateObject); 155 } 156 it->value->setFromCookie(agentStateObject); 157 } 158} 159 160void InspectorCompositeState::mute() 161{ 162 m_isMuted = true; 163} 164 165void InspectorCompositeState::unmute() 166{ 167 m_isMuted = false; 168} 169 170void InspectorCompositeState::inspectorStateUpdated() 171{ 172 if (m_client && !m_isMuted) 173 m_client->updateInspectorStateCookie(m_stateObject->toJSONString()); 174} 175 176void InspectorCompositeState::trace(Visitor* visitor) 177{ 178#if ENABLE(OILPAN) 179 visitor->trace(m_inspectorStateMap); 180#endif 181} 182 183} // namespace blink 184 185