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