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) && ENABLE(DOM_STORAGE)
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 
InspectorDOMStorageAgent(InstrumentingAgents * instrumentingAgents,InspectorState * state)58 InspectorDOMStorageAgent::InspectorDOMStorageAgent(InstrumentingAgents* instrumentingAgents, InspectorState* state)
59     : m_instrumentingAgents(instrumentingAgents)
60     , m_inspectorState(state)
61     , m_frontend(0)
62     , m_enabled(false)
63 {
64     m_instrumentingAgents->setInspectorDOMStorageAgent(this);
65 }
66 
~InspectorDOMStorageAgent()67 InspectorDOMStorageAgent::~InspectorDOMStorageAgent()
68 {
69     m_instrumentingAgents->setInspectorDOMStorageAgent(0);
70     m_instrumentingAgents = 0;
71 }
72 
setFrontend(InspectorFrontend * frontend)73 void InspectorDOMStorageAgent::setFrontend(InspectorFrontend* frontend)
74 {
75     m_frontend = frontend;
76 }
77 
clearFrontend()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->second->unbind();
83     m_frontend = 0;
84     disable(0);
85 }
86 
restore()87 void InspectorDOMStorageAgent::restore()
88 {
89     m_enabled =  m_inspectorState->getBoolean(DOMStorageAgentState::domStorageAgentEnabled);
90 }
91 
enable(ErrorString *)92 void InspectorDOMStorageAgent::enable(ErrorString*)
93 {
94     if (m_enabled)
95         return;
96     m_enabled = true;
97     m_inspectorState->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->second->bind(m_frontend);
102 }
103 
disable(ErrorString *)104 void InspectorDOMStorageAgent::disable(ErrorString*)
105 {
106     if (!m_enabled)
107         return;
108     m_enabled = false;
109     m_inspectorState->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, m_enabled);
110 }
111 
getDOMStorageEntries(ErrorString *,int storageId,RefPtr<InspectorArray> * entries)112 void InspectorDOMStorageAgent::getDOMStorageEntries(ErrorString*, int storageId, RefPtr<InspectorArray>* entries)
113 {
114     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
115     if (storageResource) {
116         storageResource->startReportingChangesToFrontend();
117         Storage* domStorage = storageResource->domStorage();
118         for (unsigned i = 0; i < domStorage->length(); ++i) {
119             String name(domStorage->key(i));
120             String value(domStorage->getItem(name));
121             RefPtr<InspectorArray> entry = InspectorArray::create();
122             entry->pushString(name);
123             entry->pushString(value);
124             (*entries)->pushArray(entry);
125         }
126     }
127 }
128 
setDOMStorageItem(ErrorString *,int storageId,const String & key,const String & value,bool * success)129 void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString*, int storageId, const String& key, const String& value, bool* success)
130 {
131     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
132     if (storageResource) {
133         ExceptionCode exception = 0;
134         storageResource->domStorage()->setItem(key, value, exception);
135         *success = !exception;
136     }
137 }
138 
removeDOMStorageItem(ErrorString *,int storageId,const String & key,bool * success)139 void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString*, int storageId, const String& key, bool* success)
140 {
141     InspectorDOMStorageResource* storageResource = getDOMStorageResourceForId(storageId);
142     if (storageResource) {
143         storageResource->domStorage()->removeItem(key);
144         *success = true;
145     }
146 }
147 
storageId(Storage * storage)148 int InspectorDOMStorageAgent::storageId(Storage* storage)
149 {
150     ASSERT(storage);
151     Frame* frame = storage->frame();
152     ExceptionCode ec = 0;
153     bool isLocalStorage = (frame->domWindow()->localStorage(ec) == storage && !ec);
154     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
155     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
156         if (it->second->isSameHostAndType(frame, isLocalStorage))
157             return it->first;
158     }
159     return 0;
160 }
161 
getDOMStorageResourceForId(int storageId)162 InspectorDOMStorageResource* InspectorDOMStorageAgent::getDOMStorageResourceForId(int storageId)
163 {
164     DOMStorageResourcesMap::iterator it = m_resources.find(storageId);
165     if (it == m_resources.end())
166         return 0;
167     return it->second.get();
168 }
169 
didUseDOMStorage(StorageArea * storageArea,bool isLocalStorage,Frame * frame)170 void InspectorDOMStorageAgent::didUseDOMStorage(StorageArea* storageArea, bool isLocalStorage, Frame* frame)
171 {
172     DOMStorageResourcesMap::iterator domStorageEnd = m_resources.end();
173     for (DOMStorageResourcesMap::iterator it = m_resources.begin(); it != domStorageEnd; ++it) {
174         if (it->second->isSameHostAndType(frame, isLocalStorage))
175             return;
176     }
177 
178     RefPtr<Storage> domStorage = Storage::create(frame, storageArea);
179     RefPtr<InspectorDOMStorageResource> resource = InspectorDOMStorageResource::create(domStorage.get(), isLocalStorage, frame);
180 
181     m_resources.set(resource->id(), resource);
182 
183     // Resources are only bound while visible.
184     if (m_frontend)
185         resource->bind(m_frontend);
186 }
187 
clearResources()188 void InspectorDOMStorageAgent::clearResources()
189 {
190     m_resources.clear();
191 }
192 
193 
194 } // namespace WebCore
195 
196 #endif // ENABLE(INSPECTOR) && ENABLE(DOM_STORE)
197