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 "bindings/core/v8/ExceptionState.h"
34 #include "core/InspectorFrontend.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/LocalDOMWindow.h"
42 #include "core/frame/LocalFrame.h"
43 #include "core/page/Page.h"
44 #include "core/storage/Storage.h"
45 #include "core/storage/StorageNamespace.h"
46 #include "platform/JSONValues.h"
47 #include "platform/weborigin/SecurityOrigin.h"
51 namespace DOMStorageAgentState {
52 static const char domStorageAgentEnabled[] = "domStorageAgentEnabled";
55 static bool hadException(ExceptionState& exceptionState, ErrorString* errorString)
57 if (!exceptionState.hadException())
60 switch (exceptionState.code()) {
62 *errorString = "Security error";
65 *errorString = "Unknown DOM storage error";
70 InspectorDOMStorageAgent::InspectorDOMStorageAgent(InspectorPageAgent* pageAgent)
71 : InspectorBaseAgent<InspectorDOMStorageAgent>("DOMStorage")
72 , m_pageAgent(pageAgent)
77 InspectorDOMStorageAgent::~InspectorDOMStorageAgent()
81 void InspectorDOMStorageAgent::trace(Visitor* visitor)
83 visitor->trace(m_pageAgent);
84 InspectorBaseAgent::trace(visitor);
87 void InspectorDOMStorageAgent::setFrontend(InspectorFrontend* frontend)
89 m_frontend = frontend->domstorage();
92 void InspectorDOMStorageAgent::clearFrontend()
98 void InspectorDOMStorageAgent::restore()
104 bool InspectorDOMStorageAgent::isEnabled() const
106 return m_state->getBoolean(DOMStorageAgentState::domStorageAgentEnabled);
109 void InspectorDOMStorageAgent::enable(ErrorString*)
111 m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, true);
112 m_instrumentingAgents->setInspectorDOMStorageAgent(this);
115 void InspectorDOMStorageAgent::disable(ErrorString*)
117 m_instrumentingAgents->setInspectorDOMStorageAgent(0);
118 m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, false);
121 void InspectorDOMStorageAgent::getDOMStorageItems(ErrorString* errorString, const RefPtr<JSONObject>& storageId, RefPtr<TypeBuilder::Array<TypeBuilder::Array<String> > >& items)
124 OwnPtrWillBeRawPtr<StorageArea> storageArea = findStorageArea(errorString, storageId, frame);
128 RefPtr<TypeBuilder::Array<TypeBuilder::Array<String> > > storageItems = TypeBuilder::Array<TypeBuilder::Array<String> >::create();
130 TrackExceptionState exceptionState;
131 for (unsigned i = 0; i < storageArea->length(exceptionState, frame); ++i) {
132 String name(storageArea->key(i, exceptionState, frame));
133 if (hadException(exceptionState, errorString))
135 String value(storageArea->getItem(name, exceptionState, frame));
136 if (hadException(exceptionState, errorString))
138 RefPtr<TypeBuilder::Array<String> > entry = TypeBuilder::Array<String>::create();
139 entry->addItem(name);
140 entry->addItem(value);
141 storageItems->addItem(entry);
143 items = storageItems.release();
146 static String toErrorString(ExceptionState& exceptionState)
148 if (exceptionState.hadException())
149 return DOMException::getErrorName(exceptionState.code());
153 void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString* errorString, const RefPtr<JSONObject>& storageId, const String& key, const String& value)
156 OwnPtrWillBeRawPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
158 *errorString = "Storage not found";
162 TrackExceptionState exceptionState;
163 storageArea->setItem(key, value, exceptionState, frame);
164 *errorString = toErrorString(exceptionState);
167 void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString* errorString, const RefPtr<JSONObject>& storageId, const String& key)
170 OwnPtrWillBeRawPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
172 *errorString = "Storage not found";
176 TrackExceptionState exceptionState;
177 storageArea->removeItem(key, exceptionState, frame);
178 *errorString = toErrorString(exceptionState);
181 PassRefPtr<TypeBuilder::DOMStorage::StorageId> InspectorDOMStorageAgent::storageId(SecurityOrigin* securityOrigin, bool isLocalStorage)
183 return TypeBuilder::DOMStorage::StorageId::create()
184 .setSecurityOrigin(securityOrigin->toRawString())
185 .setIsLocalStorage(isLocalStorage).release();
188 void InspectorDOMStorageAgent::didDispatchDOMStorageEvent(const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin)
190 if (!m_frontend || !isEnabled())
193 RefPtr<TypeBuilder::DOMStorage::StorageId> id = storageId(securityOrigin, storageType == LocalStorage);
196 m_frontend->domStorageItemsCleared(id);
197 else if (newValue.isNull())
198 m_frontend->domStorageItemRemoved(id, key);
199 else if (oldValue.isNull())
200 m_frontend->domStorageItemAdded(id, key, newValue);
202 m_frontend->domStorageItemUpdated(id, key, oldValue, newValue);
205 PassOwnPtrWillBeRawPtr<StorageArea> InspectorDOMStorageAgent::findStorageArea(ErrorString* errorString, const RefPtr<JSONObject>& storageId, LocalFrame*& targetFrame)
207 String securityOrigin;
208 bool isLocalStorage = false;
209 bool success = storageId->getString("securityOrigin", &securityOrigin);
211 success = storageId->getBoolean("isLocalStorage", &isLocalStorage);
214 *errorString = "Invalid storageId format";
218 LocalFrame* frame = m_pageAgent->findFrameWithSecurityOrigin(securityOrigin);
221 *errorString = "LocalFrame not found for the given security origin";
227 return StorageNamespace::localStorageArea(frame->document()->securityOrigin());
228 return m_pageAgent->page()->sessionStorage()->storageArea(frame->document()->securityOrigin());