Web Inspector: Refactor InspectorDOMStorageAgent::getDOMStorageEntries to report...
[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 #if ENABLE(INSPECTOR)
32
33 #include "InspectorDOMStorageAgent.h"
34
35 #include "Database.h"
36 #include "DOMWindow.h"
37 #include "Document.h"
38 #include "ExceptionCode.h"
39 #include "Frame.h"
40 #include "InspectorDOMStorageResource.h"
41 #include "InspectorFrontend.h"
42 #include "InspectorState.h"
43 #include "InspectorValues.h"
44 #include "InstrumentingAgents.h"
45 #include "Storage.h"
46 #include "StorageArea.h"
47 #include "VoidCallback.h"
48 #include "WebCoreMemoryInstrumentation.h"
49
50 #include <wtf/MemoryInstrumentationHashMap.h>
51 #include <wtf/Vector.h>
52
53 namespace WebCore {
54
55 namespace DOMStorageAgentState {
56 static const char domStorageAgentEnabled[] = "domStorageAgentEnabled";
57 };
58
59 static bool hadException(ExceptionCode ec, ErrorString* errorString)
60 {
61     switch (ec) {
62     case 0:
63         return false;
64     case SECURITY_ERR:
65         *errorString = "Security error";
66         return true;
67     default:
68         *errorString = "Unknown DOM storage error";
69         return true;
70     }
71 }
72
73 InspectorDOMStorageAgent::InspectorDOMStorageAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state)
74     : InspectorBaseAgent<InspectorDOMStorageAgent>("DOMStorage", instrumentingAgents, state)
75     , m_frontend(0)
76     , m_enabled(false)
77 {
78     m_instrumentingAgents->setInspectorDOMStorageAgent(this);
79 }
80
81 InspectorDOMStorageAgent::~InspectorDOMStorageAgent()
82 {
83     m_instrumentingAgents->setInspectorDOMStorageAgent(0);
84     m_instrumentingAgents = 0;
85 }
86
87 void InspectorDOMStorageAgent::setFrontend(InspectorFrontend* frontend)
88 {
89     m_frontend = frontend;
90 }
91
92 void InspectorDOMStorageAgent::clearFrontend()
93 {
94     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
95     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it)
96         it->value->unbind();
97     m_frontend = 0;
98     disable(0);
99 }
100
101 void InspectorDOMStorageAgent::restore()
102 {
103     m_enabled =  m_state->getBoolean(DOMStorageAgentState::domStorageAgentEnabled);
104 }
105
106 void InspectorDOMStorageAgent::enable(ErrorString*)
107 {
108     if (m_enabled)
109         return;
110     m_enabled = true;
111     m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, m_enabled);
112
113     DOMStorageResourcesMap::iterator resourcesEnd = m_resources.end();
114     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != resourcesEnd; ++it)
115         it->value->bind(m_frontend);
116 }
117
118 void InspectorDOMStorageAgent::disable(ErrorString*)
119 {
120     if (!m_enabled)
121         return;
122     m_enabled = false;
123     m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, m_enabled);
124 }
125
126 void InspectorDOMStorageAgent::getDOMStorageEntries(ErrorString* errorString, const String& storageId, RefPtr<TypeBuilder::Array<TypeBuilder::Array<String> > >& entries)
127 {
128     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
129     if (!storageResource) {
130         *errorString = "Storage resource not found for the given storage identifier";
131         return;
132     }
133     Frame* frame = storageResource->frame();
134     if (!frame) {
135         *errorString = "Frame not found";
136         return;
137     }
138
139     RefPtr<TypeBuilder::Array<TypeBuilder::Array<String> > > storageEntries = TypeBuilder::Array<TypeBuilder::Array<String> >::create();
140
141     ExceptionCode ec = 0;
142     StorageArea* storageArea = storageResource->storageArea();
143     for (unsigned i = 0; i < storageArea->length(ec, frame); ++i) {
144         String name(storageArea->key(i, ec, frame));
145         if (hadException(ec, errorString))
146             return;
147         String value(storageArea->getItem(name, ec, frame));
148         if (hadException(ec, errorString))
149             return;
150         RefPtr<TypeBuilder::Array<String> > entry = TypeBuilder::Array<String>::create();
151         entry->addItem(name);
152         entry->addItem(value);
153         storageEntries->addItem(entry);
154     }
155     entries = storageEntries.release();
156 }
157
158 void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString*, const String& storageId, const String& key, const String& value, bool* success)
159 {
160     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
161     if (storageResource) {
162         ExceptionCode exception = 0;
163         storageResource->storageArea()->setItem(key, value, exception, storageResource->frame());
164         *success = !exception;
165     } else
166         *success = false;
167 }
168
169 void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString*, const String& storageId, const String& key, bool* success)
170 {
171     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
172     if (storageResource) {
173         ExceptionCode exception = 0;
174         storageResource->storageArea()->removeItem(key, exception, storageResource->frame());
175         *success = !exception;
176     } else
177         *success = false;
178 }
179
180 String InspectorDOMStorageAgent::storageId(Storage* storage)
181 {
182     ASSERT(storage);
183     Frame* frame = storage->frame();
184     ExceptionCode ec = 0;
185     bool isLocalStorage = (frame->document()->domWindow()->localStorage(ec) == storage && !ec);
186     return storageId(frame->document()->securityOrigin(), isLocalStorage);
187 }
188
189 String InspectorDOMStorageAgent::storageId(SecurityOrigin* securityOrigin, bool isLocalStorage)
190 {
191     ASSERT(securityOrigin);
192     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
193     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
194         if (it->value->isSameOriginAndType(securityOrigin, isLocalStorage))
195             return it->key;
196     }
197     return String();
198 }
199
200 InspectorDOMStorageResource* InspectorDOMStorageAgent::getDOMStorageResourceForId(const String& storageId)
201 {
202     DOMStorageResourcesMap::iterator it = m_resources.find(storageId);
203     if (it == m_resources.end())
204         return 0;
205     return it->value.get();
206 }
207
208 void InspectorDOMStorageAgent::didUseDOMStorage(StorageArea* storageArea, bool isLocalStorage, Frame* frame)
209 {
210     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
211     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
212         if (it->value->isSameOriginAndType(frame->document()->securityOrigin(), isLocalStorage))
213             return;
214     }
215
216     RefPtr<InspectorDOMStorageResource> resource = InspectorDOMStorageResource::create(storageArea, isLocalStorage, frame);
217
218     m_resources.set(resource->id(), resource);
219
220     // Resources are only bound while visible.
221     if (m_enabled)
222         resource->bind(m_frontend);
223 }
224
225 void InspectorDOMStorageAgent::didDispatchDOMStorageEvent(const String&, const String&, const String&, StorageType storageType, SecurityOrigin* securityOrigin, Page*)
226 {
227     if (!m_frontend || !m_enabled)
228         return;
229
230     String id = storageId(securityOrigin, storageType == LocalStorage);
231
232     if (id.isEmpty())
233         return;
234
235     m_frontend->domstorage()->domStorageUpdated(id);
236 }
237
238 void InspectorDOMStorageAgent::clearResources()
239 {
240     m_resources.clear();
241 }
242
243 void InspectorDOMStorageAgent::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
244 {
245     MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::InspectorDOMStorageAgent);
246     InspectorBaseAgent<InspectorDOMStorageAgent>::reportMemoryUsage(memoryObjectInfo);
247     info.addMember(m_resources, "resources");
248     info.addWeakPointer(m_frontend);
249 }
250
251 } // namespace WebCore
252
253 #endif // ENABLE(INSPECTOR)