8433a6d1767ea033d5e95b91c4f335980edba96b
[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 InspectorDOMStorageAgent::InspectorDOMStorageAgent(InstrumentingAgents* instrumentingAgents, InspectorCompositeState* state)
60     : InspectorBaseAgent<InspectorDOMStorageAgent>("DOMStorage", instrumentingAgents, state)
61     , m_frontend(0)
62     , m_enabled(false)
63 {
64     m_instrumentingAgents->setInspectorDOMStorageAgent(this);
65 }
66
67 InspectorDOMStorageAgent::~InspectorDOMStorageAgent()
68 {
69     m_instrumentingAgents->setInspectorDOMStorageAgent(0);
70     m_instrumentingAgents = 0;
71 }
72
73 void InspectorDOMStorageAgent::setFrontend(InspectorFrontend* frontend)
74 {
75     m_frontend = frontend;
76 }
77
78 void InspectorDOMStorageAgent::clearFrontend()
79 {
80     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
81     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it)
82         it->value->unbind();
83     m_frontend = 0;
84     disable(0);
85 }
86
87 void InspectorDOMStorageAgent::restore()
88 {
89     m_enabled =  m_state->getBoolean(DOMStorageAgentState::domStorageAgentEnabled);
90 }
91
92 void InspectorDOMStorageAgent::enable(ErrorString*)
93 {
94     if (m_enabled)
95         return;
96     m_enabled = true;
97     m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, m_enabled);
98
99     DOMStorageResourcesMap::iterator resourcesEnd = m_resources.end();
100     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != resourcesEnd; ++it)
101         it->value->bind(m_frontend);
102 }
103
104 void InspectorDOMStorageAgent::disable(ErrorString*)
105 {
106     if (!m_enabled)
107         return;
108     m_enabled = false;
109     m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, m_enabled);
110 }
111
112 void InspectorDOMStorageAgent::getDOMStorageEntries(ErrorString*, const String& storageId, RefPtr<TypeBuilder::Array<TypeBuilder::Array<String> > >& entries)
113 {
114     // FIXME: consider initializing this array after 2 checks below. The checks should return error messages in this case.
115     entries = TypeBuilder::Array<TypeBuilder::Array<String> >::create();
116
117     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
118     if (!storageResource)
119         return;
120     Frame* frame = storageResource->frame();
121     if (!frame)
122         return;
123
124     // FIXME: Exceptions are not reported here.
125     ExceptionCode ec = 0;
126     StorageArea* storageArea = storageResource->storageArea();
127     for (unsigned i = 0; i < storageArea->length(ec, frame); ++i) {
128         String name(storageArea->key(i, ec, frame));
129         if (ec)
130             return;
131         String value(storageArea->getItem(name, ec, frame));
132         if (ec)
133             return;
134         RefPtr<TypeBuilder::Array<String> > entry = TypeBuilder::Array<String>::create();
135         entry->addItem(name);
136         entry->addItem(value);
137         entries->addItem(entry);
138     }
139 }
140
141 void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString*, const String& storageId, const String& key, const String& value, bool* success)
142 {
143     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
144     if (storageResource) {
145         ExceptionCode exception = 0;
146         storageResource->storageArea()->setItem(key, value, exception, storageResource->frame());
147         *success = !exception;
148     } else
149         *success = false;
150 }
151
152 void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString*, const String& storageId, const String& key, bool* success)
153 {
154     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
155     if (storageResource) {
156         ExceptionCode exception = 0;
157         storageResource->storageArea()->removeItem(key, exception, storageResource->frame());
158         *success = !exception;
159     } else
160         *success = false;
161 }
162
163 String InspectorDOMStorageAgent::storageId(Storage* storage)
164 {
165     ASSERT(storage);
166     Frame* frame = storage->frame();
167     ExceptionCode ec = 0;
168     bool isLocalStorage = (frame->document()->domWindow()->localStorage(ec) == storage && !ec);
169     return storageId(frame->document()->securityOrigin(), isLocalStorage);
170 }
171
172 String InspectorDOMStorageAgent::storageId(SecurityOrigin* securityOrigin, bool isLocalStorage)
173 {
174     ASSERT(securityOrigin);
175     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
176     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
177         if (it->value->isSameOriginAndType(securityOrigin, isLocalStorage))
178             return it->key;
179     }
180     return String();
181 }
182
183 InspectorDOMStorageResource* InspectorDOMStorageAgent::getDOMStorageResourceForId(const String& storageId)
184 {
185     DOMStorageResourcesMap::iterator it = m_resources.find(storageId);
186     if (it == m_resources.end())
187         return 0;
188     return it->value.get();
189 }
190
191 void InspectorDOMStorageAgent::didUseDOMStorage(StorageArea* storageArea, bool isLocalStorage, Frame* frame)
192 {
193     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
194     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
195         if (it->value->isSameOriginAndType(frame->document()->securityOrigin(), isLocalStorage))
196             return;
197     }
198
199     RefPtr<InspectorDOMStorageResource> resource = InspectorDOMStorageResource::create(storageArea, isLocalStorage, frame);
200
201     m_resources.set(resource->id(), resource);
202
203     // Resources are only bound while visible.
204     if (m_enabled)
205         resource->bind(m_frontend);
206 }
207
208 void InspectorDOMStorageAgent::didDispatchDOMStorageEvent(const String&, const String&, const String&, StorageType storageType, SecurityOrigin* securityOrigin, Page*)
209 {
210     if (!m_frontend || !m_enabled)
211         return;
212
213     String id = storageId(securityOrigin, storageType == LocalStorage);
214
215     if (id.isEmpty())
216         return;
217
218     m_frontend->domstorage()->domStorageUpdated(id);
219 }
220
221 void InspectorDOMStorageAgent::clearResources()
222 {
223     m_resources.clear();
224 }
225
226 void InspectorDOMStorageAgent::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
227 {
228     MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::InspectorDOMStorageAgent);
229     InspectorBaseAgent<InspectorDOMStorageAgent>::reportMemoryUsage(memoryObjectInfo);
230     info.addMember(m_resources, "resources");
231     info.addWeakPointer(m_frontend);
232 }
233
234 } // namespace WebCore
235
236 #endif // ENABLE(INSPECTOR)