2011-01-21 Pavel Podivilov <podivilov@chromium.org>
[WebKit-https.git] / Source / WebCore / inspector / InspectorState.cpp
1 /*
2  * Copyright (C) 2010 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 "InspectorState.h"
31
32 #if ENABLE(INSPECTOR)
33
34 #include "InspectorClient.h"
35
36 namespace WebCore {
37
38 InspectorState::InspectorState(InspectorClient* client)
39     : m_client(client)
40 {
41     // Pure reload state
42     registerBoolean(userInitiatedProfiling, false);
43     registerBoolean(timelineProfilerEnabled, false);
44     registerBoolean(searchingForNode, false);
45     registerObject(browserBreakpoints);
46     registerBoolean(consoleMessagesEnabled, false);
47     registerBoolean(monitoringXHR, false);
48     registerBoolean(resourceAgentEnabled, false);
49 }
50
51 void InspectorState::restoreFromInspectorCookie(const String& json)
52 {
53     RefPtr<InspectorValue> jsonValue = InspectorValue::parseJSON(json);
54     if (!jsonValue)
55         return;
56
57     RefPtr<InspectorObject> jsonObject = jsonValue->asObject();
58     if (!jsonObject)
59         return;
60
61     for (InspectorObject::iterator i = jsonObject->begin(); i != jsonObject->end(); ++i) {
62         InspectorPropertyId id = (InspectorPropertyId)i->first.toInt();
63         ASSERT(id > 0 && id < lastPropertyId);
64         PropertyMap::iterator j = m_properties.find(id);
65         ASSERT(j != m_properties.end());
66         ASSERT(j->second.m_value->type() == i->second->type());
67         j->second.m_value = i->second;
68     }
69 }
70
71 void InspectorState::updateCookie()
72 {
73     RefPtr<InspectorObject> cookieObject = InspectorObject::create();
74     for (PropertyMap::iterator i = m_properties.begin(); i != m_properties.end(); ++i)
75         cookieObject->setValue(String::number(i->first), i->second.m_value);
76     m_client->updateInspectorStateCookie(cookieObject->toJSONString());
77 }
78
79 void InspectorState::setValue(InspectorPropertyId id, PassRefPtr<InspectorValue> value)
80 {
81     PropertyMap::iterator i = m_properties.find(id);
82     ASSERT(i != m_properties.end());
83     i->second.m_value = value;
84     updateCookie();
85 }
86
87 bool InspectorState::getBoolean(InspectorPropertyId id)
88 {
89     PropertyMap::iterator i = m_properties.find(id);
90     ASSERT(i != m_properties.end());
91     bool value = false;
92     i->second.m_value->asBoolean(&value);
93     return value;
94 }
95
96 String InspectorState::getString(InspectorPropertyId id)
97 {
98     PropertyMap::iterator i = m_properties.find(id);
99     ASSERT(i != m_properties.end());
100     String value;
101     i->second.m_value->asString(&value);
102     return value;
103 }
104
105 long InspectorState::getLong(InspectorPropertyId id)
106 {
107     PropertyMap::iterator i = m_properties.find(id);
108     ASSERT(i != m_properties.end());
109     long value = 0;
110     i->second.m_value->asNumber(&value);
111     return value;
112 }
113
114 PassRefPtr<InspectorObject> InspectorState::getObject(InspectorPropertyId id)
115 {
116     PropertyMap::iterator i = m_properties.find(id);
117     ASSERT(i != m_properties.end());
118     return i->second.m_value->asObject();
119 }
120
121 void InspectorState::setObject(InspectorPropertyId id, PassRefPtr<InspectorObject> value)
122 {
123     PropertyMap::iterator i = m_properties.find(id);
124     ASSERT(i != m_properties.end());
125     Property& property = i->second;
126     property.m_value = value;
127     updateCookie();
128 }
129
130 void InspectorState::registerBoolean(InspectorPropertyId propertyId, bool value)
131 {
132     m_properties.set(propertyId, Property::create(InspectorBasicValue::create(value)));
133 }
134
135 void InspectorState::registerString(InspectorPropertyId propertyId, const String& value)
136 {
137     m_properties.set(propertyId, Property::create(InspectorString::create(value)));
138 }
139
140 void InspectorState::registerLong(InspectorPropertyId propertyId, long value)
141 {
142     m_properties.set(propertyId, Property::create(InspectorBasicValue::create((double)value)));
143 }
144
145 void InspectorState::registerObject(InspectorPropertyId propertyId)
146 {
147     m_properties.set(propertyId, Property::create(InspectorObject::create()));
148 }
149
150 InspectorState::Property InspectorState::Property::create(PassRefPtr<InspectorValue> value)
151 {
152     Property property;
153     property.m_value = value;
154     return property;
155 }
156
157 } // namespace WebCore
158
159 #endif // ENABLE(INSPECTOR)