Web Inspector: Remove InspectorState
[WebKit-https.git] / Source / WebCore / inspector / InspectorDOMStorageAgent.cpp
1 /*
2  * Copyright (C) 2010 Google Inc. All rights reserved.
3  * Copyright (C) 2013 Samsung Electronics. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1.  Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  * 2.  Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
15  *     its contributors may be used to endorse or promote products derived
16  *     from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include "config.h"
31
32 #if ENABLE(INSPECTOR)
33
34 #include "InspectorDOMStorageAgent.h"
35
36 #include "Database.h"
37 #include "DOMWindow.h"
38 #include "Document.h"
39 #include "ExceptionCode.h"
40 #include "ExceptionCodeDescription.h"
41 #include "Frame.h"
42 #include "InspectorFrontend.h"
43 #include "InspectorPageAgent.h"
44 #include "InspectorValues.h"
45 #include "InstrumentingAgents.h"
46 #include "Page.h"
47 #include "PageGroup.h"
48 #include "SecurityOrigin.h"
49 #include "Storage.h"
50 #include "StorageArea.h"
51 #include "StorageNamespace.h"
52 #include "VoidCallback.h"
53
54 #include <wtf/Vector.h>
55
56 namespace WebCore {
57
58 InspectorDOMStorageAgent::InspectorDOMStorageAgent(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent)
59     : InspectorBaseAgent<InspectorDOMStorageAgent>("DOMStorage", instrumentingAgents)
60     , m_pageAgent(pageAgent)
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     m_frontend = 0;
81     disable(0);
82 }
83
84 void InspectorDOMStorageAgent::enable(ErrorString*)
85 {
86     m_enabled = true;
87 }
88
89 void InspectorDOMStorageAgent::disable(ErrorString*)
90 {
91     m_enabled = false;
92 }
93
94 void InspectorDOMStorageAgent::getDOMStorageItems(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, RefPtr<TypeBuilder::Array<TypeBuilder::Array<String>>>& items)
95 {
96     Frame* frame;
97     RefPtr<StorageArea> storageArea = findStorageArea(errorString, storageId, frame);
98     if (!storageArea) {
99         if (errorString)
100             *errorString = "No StorageArea for given storageId";
101         return;
102     }
103
104     RefPtr<TypeBuilder::Array<TypeBuilder::Array<String>>> storageItems = TypeBuilder::Array<TypeBuilder::Array<String>>::create();
105
106     for (unsigned i = 0; i < storageArea->length(); ++i) {
107         String key = storageArea->key(i);
108         String value = storageArea->item(key);
109
110         RefPtr<TypeBuilder::Array<String>> entry = TypeBuilder::Array<String>::create();
111         entry->addItem(key);
112         entry->addItem(value);
113         storageItems->addItem(entry.release());
114     }
115
116     items = storageItems.release();
117 }
118
119 void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, const String& key, const String& value)
120 {
121     Frame* frame;
122     RefPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
123     if (!storageArea) {
124         *errorString = "Storage not found";
125         return;
126     }
127
128     bool quotaException = false;
129     storageArea->setItem(frame, key, value, quotaException);
130     if (quotaException)
131         *errorString = ExceptionCodeDescription(QUOTA_EXCEEDED_ERR).name;
132 }
133
134 void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, const String& key)
135 {
136     Frame* frame;
137     RefPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
138     if (!storageArea) {
139         *errorString = "Storage not found";
140         return;
141     }
142
143     storageArea->removeItem(frame, key);
144 }
145
146 String InspectorDOMStorageAgent::storageId(Storage* storage)
147 {
148     ASSERT(storage);
149     Document* document = storage->frame()->document();
150     ASSERT(document);
151     DOMWindow* window = document->domWindow();
152     ASSERT(window);
153     RefPtr<SecurityOrigin> securityOrigin = document->securityOrigin();
154     bool isLocalStorage = window->optionalLocalStorage() == storage;
155     return storageId(securityOrigin.get(), isLocalStorage)->toJSONString();
156 }
157
158 PassRefPtr<TypeBuilder::DOMStorage::StorageId> InspectorDOMStorageAgent::storageId(SecurityOrigin* securityOrigin, bool isLocalStorage)
159 {
160     return TypeBuilder::DOMStorage::StorageId::create()
161         .setSecurityOrigin(securityOrigin->toRawString())
162         .setIsLocalStorage(isLocalStorage).release();
163 }
164
165 void InspectorDOMStorageAgent::didDispatchDOMStorageEvent(const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin, Page*)
166 {
167     if (!m_frontend || !m_enabled)
168         return;
169
170     RefPtr<TypeBuilder::DOMStorage::StorageId> id = storageId(securityOrigin, storageType == LocalStorage);
171
172     if (key.isNull())
173         m_frontend->domstorage()->domStorageItemsCleared(id);
174     else if (newValue.isNull())
175         m_frontend->domstorage()->domStorageItemRemoved(id, key);
176     else if (oldValue.isNull())
177         m_frontend->domstorage()->domStorageItemAdded(id, key, newValue);
178     else
179         m_frontend->domstorage()->domStorageItemUpdated(id, key, oldValue, newValue);
180 }
181
182 PassRefPtr<StorageArea> InspectorDOMStorageAgent::findStorageArea(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, Frame*& targetFrame)
183 {
184     String securityOrigin;
185     bool isLocalStorage = false;
186     bool success = storageId->getString("securityOrigin", &securityOrigin);
187     if (success)
188         success = storageId->getBoolean("isLocalStorage", &isLocalStorage);
189     if (!success) {
190         if (errorString)
191             *errorString = "Invalid storageId format";
192         targetFrame = 0;
193         return 0;
194     }
195
196     targetFrame = m_pageAgent->findFrameWithSecurityOrigin(securityOrigin);
197     if (!targetFrame) {
198         if (errorString)
199             *errorString = "Frame not found for the given security origin";
200         return 0;
201     }
202
203     Page* page = m_pageAgent->page();
204     if (isLocalStorage)
205         return page->group().localStorage()->storageArea(targetFrame->document()->securityOrigin());
206     return page->sessionStorage()->storageArea(targetFrame->document()->securityOrigin());
207 }
208
209 } // namespace WebCore
210
211 #endif // ENABLE(INSPECTOR)