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