2 * Copyright (C) 2010 Google Inc. All rights reserved.
3 * Copyright (C) 2013 Samsung Electronics. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
31 #include "core/inspector/InspectorDOMStorageAgent.h"
33 #include "InspectorFrontend.h"
34 #include "bindings/v8/ExceptionState.h"
35 #include "core/dom/DOMException.h"
36 #include "core/dom/Document.h"
37 #include "core/dom/ExceptionCode.h"
38 #include "core/inspector/InspectorPageAgent.h"
39 #include "core/inspector/InspectorState.h"
40 #include "core/inspector/InstrumentingAgents.h"
41 #include "core/frame/DOMWindow.h"
42 #include "core/frame/Frame.h"
43 #include "core/page/Page.h"
44 #include "core/page/PageGroup.h"
45 #include "core/storage/Storage.h"
46 #include "core/storage/StorageNamespace.h"
47 #include "platform/JSONValues.h"
48 #include "platform/weborigin/SecurityOrigin.h"
52 namespace DOMStorageAgentState {
53 static const char domStorageAgentEnabled[] = "domStorageAgentEnabled";
56 static bool hadException(ExceptionState& exceptionState, ErrorString* errorString)
58 if (!exceptionState.hadException())
61 switch (exceptionState.code()) {
63 *errorString = "Security error";
66 *errorString = "Unknown DOM storage error";
71 InspectorDOMStorageAgent::InspectorDOMStorageAgent(InspectorPageAgent* pageAgent)
72 : InspectorBaseAgent<InspectorDOMStorageAgent>("DOMStorage")
73 , m_pageAgent(pageAgent)
78 InspectorDOMStorageAgent::~InspectorDOMStorageAgent()
82 void InspectorDOMStorageAgent::setFrontend(InspectorFrontend* frontend)
84 m_frontend = frontend;
87 void InspectorDOMStorageAgent::clearFrontend()
93 bool InspectorDOMStorageAgent::isEnabled() const
95 return m_state->getBoolean(DOMStorageAgentState::domStorageAgentEnabled);
98 void InspectorDOMStorageAgent::enable(ErrorString*)
100 m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, true);
101 m_instrumentingAgents->setInspectorDOMStorageAgent(this);
104 void InspectorDOMStorageAgent::disable(ErrorString*)
106 m_instrumentingAgents->setInspectorDOMStorageAgent(0);
107 m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, false);
110 void InspectorDOMStorageAgent::getDOMStorageItems(ErrorString* errorString, const RefPtr<JSONObject>& storageId, RefPtr<TypeBuilder::Array<TypeBuilder::Array<String> > >& items)
113 OwnPtrWillBeRawPtr<StorageArea> storageArea = findStorageArea(errorString, storageId, frame);
117 RefPtr<TypeBuilder::Array<TypeBuilder::Array<String> > > storageItems = TypeBuilder::Array<TypeBuilder::Array<String> >::create();
119 TrackExceptionState exceptionState;
120 for (unsigned i = 0; i < storageArea->length(exceptionState, frame); ++i) {
121 String name(storageArea->key(i, exceptionState, frame));
122 if (hadException(exceptionState, errorString))
124 String value(storageArea->getItem(name, exceptionState, frame));
125 if (hadException(exceptionState, errorString))
127 RefPtr<TypeBuilder::Array<String> > entry = TypeBuilder::Array<String>::create();
128 entry->addItem(name);
129 entry->addItem(value);
130 storageItems->addItem(entry);
132 items = storageItems.release();
135 static String toErrorString(ExceptionState& exceptionState)
137 if (exceptionState.hadException())
138 return DOMException::getErrorName(exceptionState.code());
142 void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString* errorString, const RefPtr<JSONObject>& storageId, const String& key, const String& value)
145 OwnPtrWillBeRawPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
147 *errorString = "Storage not found";
151 TrackExceptionState exceptionState;
152 storageArea->setItem(key, value, exceptionState, frame);
153 *errorString = toErrorString(exceptionState);
156 void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString* errorString, const RefPtr<JSONObject>& storageId, const String& key)
159 OwnPtrWillBeRawPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
161 *errorString = "Storage not found";
165 TrackExceptionState exceptionState;
166 storageArea->removeItem(key, exceptionState, frame);
167 *errorString = toErrorString(exceptionState);
170 PassRefPtr<TypeBuilder::DOMStorage::StorageId> InspectorDOMStorageAgent::storageId(SecurityOrigin* securityOrigin, bool isLocalStorage)
172 return TypeBuilder::DOMStorage::StorageId::create()
173 .setSecurityOrigin(securityOrigin->toRawString())
174 .setIsLocalStorage(isLocalStorage).release();
177 void InspectorDOMStorageAgent::didDispatchDOMStorageEvent(const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin)
179 if (!m_frontend || !isEnabled())
182 RefPtr<TypeBuilder::DOMStorage::StorageId> id = storageId(securityOrigin, storageType == LocalStorage);
185 m_frontend->domstorage()->domStorageItemsCleared(id);
186 else if (newValue.isNull())
187 m_frontend->domstorage()->domStorageItemRemoved(id, key);
188 else if (oldValue.isNull())
189 m_frontend->domstorage()->domStorageItemAdded(id, key, newValue);
191 m_frontend->domstorage()->domStorageItemUpdated(id, key, oldValue, newValue);
194 PassOwnPtrWillBeRawPtr<StorageArea> InspectorDOMStorageAgent::findStorageArea(ErrorString* errorString, const RefPtr<JSONObject>& storageId, Frame*& targetFrame)
196 String securityOrigin;
197 bool isLocalStorage = false;
198 bool success = storageId->getString("securityOrigin", &securityOrigin);
200 success = storageId->getBoolean("isLocalStorage", &isLocalStorage);
203 *errorString = "Invalid storageId format";
207 Frame* frame = m_pageAgent->findFrameWithSecurityOrigin(securityOrigin);
210 *errorString = "Frame not found for the given security origin";
216 return StorageNamespace::localStorageArea(frame->document()->securityOrigin());
217 return m_pageAgent->page()->sessionStorage()->storageArea(frame->document()->securityOrigin());
220 } // namespace WebCore