https://bugs.webkit.org/show_bug.cgi?id=80903 InspectorDOMStorageAgent creates a...
[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         return;
116     Frame* frame = storageResource->frame();
117     if (!frame)
118         return;
119         
120     storageResource->startReportingChangesToFrontend();
121     StorageArea* storageArea = storageResource->storageArea();
122     for (unsigned i = 0; i < storageArea->length(frame); ++i) {
123         String name(storageArea->key(i, frame));
124         String value(storageArea->getItem(name, frame));
125         RefPtr<InspectorArray> entry = InspectorArray::create();
126         entry->pushString(name);
127         entry->pushString(value);
128         entries->pushArray(entry);
129     }
130 }
131
132 void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString*, int storageId, const String& key, const String& value, bool* success)
133 {
134     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
135     if (storageResource) {
136         ExceptionCode exception = 0;
137         storageResource->storageArea()->setItem(key, value, exception, storageResource->frame());
138         *success = !exception;
139     }
140 }
141
142 void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString*, int storageId, const String& key, bool* success)
143 {
144     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
145     if (storageResource) {
146         storageResource->storageArea()->removeItem(key, storageResource->frame());
147         *success = true;
148     }
149 }
150
151 int InspectorDOMStorageAgent::storageId(Storage* storage)
152 {
153     ASSERT(storage);
154     Frame* frame = storage->frame();
155     ExceptionCode ec = 0;
156     bool isLocalStorage = (frame->domWindow()->localStorage(ec) == storage && !ec);
157     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
158     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
159         if (it->second->isSameHostAndType(frame, isLocalStorage))
160             return it->first;
161     }
162     return 0;
163 }
164
165 InspectorDOMStorageResource* InspectorDOMStorageAgent::getDOMStorageResourceForId(int storageId)
166 {
167     DOMStorageResourcesMap::iterator it = m_resources.find(storageId);
168     if (it == m_resources.end())
169         return 0;
170     return it->second.get();
171 }
172
173 void InspectorDOMStorageAgent::didUseDOMStorage(StorageArea* storageArea, bool isLocalStorage, Frame* frame)
174 {
175     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
176     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
177         if (it->second->isSameHostAndType(frame, isLocalStorage))
178             return;
179     }
180
181     RefPtr<InspectorDOMStorageResource> resource = InspectorDOMStorageResource::create(storageArea, isLocalStorage, frame);
182
183     m_resources.set(resource->id(), resource);
184
185     // Resources are only bound while visible.
186     if (m_enabled)
187         resource->bind(m_frontend);
188 }
189
190 void InspectorDOMStorageAgent::clearResources()
191 {
192     m_resources.clear();
193 }
194
195
196 } // namespace WebCore
197
198 #endif // ENABLE(INSPECTOR)