Web Inspector: Generate Individual InspectorBackendDispatchers, add base InspectorBac...
[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(ASCIILiteral("DOMStorage"), instrumentingAgents)
60     , m_pageAgent(pageAgent)
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::didCreateFrontendAndBackend(InspectorFrontendChannel* frontendChannel, InspectorBackendDispatcher* backendDispatcher)
73 {
74     m_frontendDispatcher = std::make_unique<InspectorDOMStorageFrontendDispatcher>(frontendChannel);
75     m_backendDispatcher = InspectorDOMStorageBackendDispatcher::create(backendDispatcher, this);
76 }
77
78 void InspectorDOMStorageAgent::willDestroyFrontendAndBackend()
79 {
80     m_frontendDispatcher = nullptr;
81     m_backendDispatcher.clear();
82
83     disable(nullptr);
84 }
85
86 void InspectorDOMStorageAgent::enable(ErrorString*)
87 {
88     m_enabled = true;
89 }
90
91 void InspectorDOMStorageAgent::disable(ErrorString*)
92 {
93     m_enabled = false;
94 }
95
96 void InspectorDOMStorageAgent::getDOMStorageItems(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, RefPtr<TypeBuilder::Array<TypeBuilder::Array<String>>>& items)
97 {
98     Frame* frame;
99     RefPtr<StorageArea> storageArea = findStorageArea(errorString, storageId, frame);
100     if (!storageArea) {
101         if (errorString)
102             *errorString = "No StorageArea for given storageId";
103         return;
104     }
105
106     RefPtr<TypeBuilder::Array<TypeBuilder::Array<String>>> storageItems = TypeBuilder::Array<TypeBuilder::Array<String>>::create();
107
108     for (unsigned i = 0; i < storageArea->length(); ++i) {
109         String key = storageArea->key(i);
110         String value = storageArea->item(key);
111
112         RefPtr<TypeBuilder::Array<String>> entry = TypeBuilder::Array<String>::create();
113         entry->addItem(key);
114         entry->addItem(value);
115         storageItems->addItem(entry.release());
116     }
117
118     items = storageItems.release();
119 }
120
121 void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, const String& key, const String& value)
122 {
123     Frame* frame;
124     RefPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
125     if (!storageArea) {
126         *errorString = "Storage not found";
127         return;
128     }
129
130     bool quotaException = false;
131     storageArea->setItem(frame, key, value, quotaException);
132     if (quotaException)
133         *errorString = ExceptionCodeDescription(QUOTA_EXCEEDED_ERR).name;
134 }
135
136 void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, const String& key)
137 {
138     Frame* frame;
139     RefPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
140     if (!storageArea) {
141         *errorString = "Storage not found";
142         return;
143     }
144
145     storageArea->removeItem(frame, key);
146 }
147
148 String InspectorDOMStorageAgent::storageId(Storage* storage)
149 {
150     ASSERT(storage);
151     Document* document = storage->frame()->document();
152     ASSERT(document);
153     DOMWindow* window = document->domWindow();
154     ASSERT(window);
155     RefPtr<SecurityOrigin> securityOrigin = document->securityOrigin();
156     bool isLocalStorage = window->optionalLocalStorage() == storage;
157     return storageId(securityOrigin.get(), isLocalStorage)->toJSONString();
158 }
159
160 PassRefPtr<TypeBuilder::DOMStorage::StorageId> InspectorDOMStorageAgent::storageId(SecurityOrigin* securityOrigin, bool isLocalStorage)
161 {
162     return TypeBuilder::DOMStorage::StorageId::create()
163         .setSecurityOrigin(securityOrigin->toRawString())
164         .setIsLocalStorage(isLocalStorage).release();
165 }
166
167 void InspectorDOMStorageAgent::didDispatchDOMStorageEvent(const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin, Page*)
168 {
169     if (!m_frontendDispatcher || !m_enabled)
170         return;
171
172     RefPtr<TypeBuilder::DOMStorage::StorageId> id = storageId(securityOrigin, storageType == LocalStorage);
173
174     if (key.isNull())
175         m_frontendDispatcher->domStorageItemsCleared(id);
176     else if (newValue.isNull())
177         m_frontendDispatcher->domStorageItemRemoved(id, key);
178     else if (oldValue.isNull())
179         m_frontendDispatcher->domStorageItemAdded(id, key, newValue);
180     else
181         m_frontendDispatcher->domStorageItemUpdated(id, key, oldValue, newValue);
182 }
183
184 PassRefPtr<StorageArea> InspectorDOMStorageAgent::findStorageArea(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, Frame*& targetFrame)
185 {
186     String securityOrigin;
187     bool isLocalStorage = false;
188     bool success = storageId->getString("securityOrigin", &securityOrigin);
189     if (success)
190         success = storageId->getBoolean("isLocalStorage", &isLocalStorage);
191     if (!success) {
192         if (errorString)
193             *errorString = "Invalid storageId format";
194         targetFrame = 0;
195         return 0;
196     }
197
198     targetFrame = m_pageAgent->findFrameWithSecurityOrigin(securityOrigin);
199     if (!targetFrame) {
200         if (errorString)
201             *errorString = "Frame not found for the given security origin";
202         return 0;
203     }
204
205     Page* page = m_pageAgent->page();
206     if (isLocalStorage)
207         return page->group().localStorage()->storageArea(targetFrame->document()->securityOrigin());
208     return page->sessionStorage()->storageArea(targetFrame->document()->securityOrigin());
209 }
210
211 } // namespace WebCore
212
213 #endif // ENABLE(INSPECTOR)