b811fba86d21864dfd0f0a76630b40400cc71343
[WebKit-https.git] / Source / WebCore / inspector / InspectorDOMStorageAgent.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 APPLE 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
31 #include "InspectorDOMStorageAgent.h"
32
33 #if ENABLE(INSPECTOR)
34
35 #include "Database.h"
36 #include "DOMWindow.h"
37 #include "ExceptionCode.h"
38 #include "Frame.h"
39 #include "InspectorDOMStorageResource.h"
40 #include "InspectorFrontend.h"
41 #include "InspectorState.h"
42 #include "InspectorValues.h"
43 #include "InstrumentingAgents.h"
44 #include "Storage.h"
45 #include "StorageArea.h"
46 #include "VoidCallback.h"
47
48 #include <wtf/Vector.h>
49
50 namespace WebCore {
51
52 namespace DOMStorageAgentState {
53 static const char domStorageAgentEnabled[] = "domStorageAgentEnabled";
54 };
55
56 typedef HashMap<int, RefPtr<InspectorDOMStorageResource> > DOMStorageResourcesMap;
57
58 InspectorDOMStorageAgent::InspectorDOMStorageAgent(InstrumentingAgents* instrumentingAgents, InspectorState* state)
59     : InspectorBaseAgent<InspectorDOMStorageAgent>("DOMStorage", instrumentingAgents, state)
60     , m_frontend(0)
61     , m_enabled(false)
62 {
63     m_instrumentingAgents->setInspectorDOMStorageAgent(this);
64 }
65
66 InspectorDOMStorageAgent::~InspectorDOMStorageAgent()
67 {
68     m_instrumentingAgents->setInspectorDOMStorageAgent(0);
69     m_instrumentingAgents = 0;
70 }
71
72 void InspectorDOMStorageAgent::setFrontend(InspectorFrontend* frontend)
73 {
74     m_frontend = frontend;
75 }
76
77 void InspectorDOMStorageAgent::clearFrontend()
78 {
79     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
80     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it)
81         it->second->unbind();
82     m_frontend = 0;
83     disable(0);
84 }
85
86 void InspectorDOMStorageAgent::restore()
87 {
88     m_enabled =  m_state->getBoolean(DOMStorageAgentState::domStorageAgentEnabled);
89 }
90
91 void InspectorDOMStorageAgent::enable(ErrorString*)
92 {
93     if (m_enabled)
94         return;
95     m_enabled = true;
96     m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, m_enabled);
97
98     DOMStorageResourcesMap::iterator resourcesEnd = m_resources.end();
99     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != resourcesEnd; ++it)
100         it->second->bind(m_frontend);
101 }
102
103 void InspectorDOMStorageAgent::disable(ErrorString*)
104 {
105     if (!m_enabled)
106         return;
107     m_enabled = false;
108     m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, m_enabled);
109 }
110
111 void InspectorDOMStorageAgent::getDOMStorageEntries(ErrorString*, int storageId, RefPtr<InspectorArray>* entries)
112 {
113     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
114     if (storageResource) {
115         storageResource->startReportingChangesToFrontend();
116         Storage* domStorage = storageResource->domStorage();
117         for (unsigned i = 0; i < domStorage->length(); ++i) {
118             String name(domStorage->key(i));
119             String value(domStorage->getItem(name));
120             RefPtr<InspectorArray> entry = InspectorArray::create();
121             entry->pushString(name);
122             entry->pushString(value);
123             (*entries)->pushArray(entry);
124         }
125     }
126 }
127
128 void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString*, int storageId, const String& key, const String& value, bool* success)
129 {
130     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
131     if (storageResource) {
132         ExceptionCode exception = 0;
133         storageResource->domStorage()->setItem(key, value, exception);
134         *success = !exception;
135     }
136 }
137
138 void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString*, int storageId, const String& key, bool* success)
139 {
140     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
141     if (storageResource) {
142         storageResource->domStorage()->removeItem(key);
143         *success = true;
144     }
145 }
146
147 int InspectorDOMStorageAgent::storageId(Storage* storage)
148 {
149     ASSERT(storage);
150     Frame* frame = storage->frame();
151     ExceptionCode ec = 0;
152     bool isLocalStorage = (frame->domWindow()->localStorage(ec) == storage && !ec);
153     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
154     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
155         if (it->second->isSameHostAndType(frame, isLocalStorage))
156             return it->first;
157     }
158     return 0;
159 }
160
161 InspectorDOMStorageResource* InspectorDOMStorageAgent::getDOMStorageResourceForId(int storageId)
162 {
163     DOMStorageResourcesMap::iterator it = m_resources.find(storageId);
164     if (it == m_resources.end())
165         return 0;
166     return it->second.get();
167 }
168
169 void InspectorDOMStorageAgent::didUseDOMStorage(StorageArea* storageArea, bool isLocalStorage, Frame* frame)
170 {
171     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
172     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
173         if (it->second->isSameHostAndType(frame, isLocalStorage))
174             return;
175     }
176
177     RefPtr<Storage> domStorage = Storage::create(frame, storageArea);
178     RefPtr<InspectorDOMStorageResource> resource = InspectorDOMStorageResource::create(domStorage.get(), isLocalStorage, frame);
179
180     m_resources.set(resource->id(), resource);
181
182     // Resources are only bound while visible.
183     if (m_enabled)
184         resource->bind(m_frontend);
185 }
186
187 void InspectorDOMStorageAgent::clearResources()
188 {
189     m_resources.clear();
190 }
191
192
193 } // namespace WebCore
194
195 #endif // ENABLE(INSPECTOR)