InspectorDOMStorageAgent.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. #include "config.h"
  30. #if ENABLE(INSPECTOR)
  31. #include "InspectorDOMStorageAgent.h"
  32. #include "Database.h"
  33. #include "DOMWindow.h"
  34. #include "Document.h"
  35. #include "ExceptionCode.h"
  36. #include "ExceptionCodeDescription.h"
  37. #include "Frame.h"
  38. #include "InspectorFrontend.h"
  39. #include "InspectorPageAgent.h"
  40. #include "InspectorState.h"
  41. #include "InspectorValues.h"
  42. #include "InstrumentingAgents.h"
  43. #include "Page.h"
  44. #include "PageGroup.h"
  45. #include "SecurityOrigin.h"
  46. #include "Storage.h"
  47. #include "StorageArea.h"
  48. #include "StorageNamespace.h"
  49. #include "VoidCallback.h"
  50. #include <wtf/Vector.h>
  51. namespace WebCore {
  52. namespace DOMStorageAgentState {
  53. static const char domStorageAgentEnabled[] = "domStorageAgentEnabled";
  54. };
  55. InspectorDOMStorageAgent::InspectorDOMStorageAgent(InstrumentingAgents* instrumentingAgents, InspectorPageAgent* pageAgent, InspectorCompositeState* state)
  56. : InspectorBaseAgent<InspectorDOMStorageAgent>("DOMStorage", instrumentingAgents, state)
  57. , m_pageAgent(pageAgent)
  58. , m_frontend(0)
  59. {
  60. m_instrumentingAgents->setInspectorDOMStorageAgent(this);
  61. }
  62. InspectorDOMStorageAgent::~InspectorDOMStorageAgent()
  63. {
  64. m_instrumentingAgents->setInspectorDOMStorageAgent(0);
  65. m_instrumentingAgents = 0;
  66. }
  67. void InspectorDOMStorageAgent::setFrontend(InspectorFrontend* frontend)
  68. {
  69. m_frontend = frontend;
  70. }
  71. void InspectorDOMStorageAgent::clearFrontend()
  72. {
  73. m_frontend = 0;
  74. disable(0);
  75. }
  76. bool InspectorDOMStorageAgent::isEnabled() const
  77. {
  78. return m_state->getBoolean(DOMStorageAgentState::domStorageAgentEnabled);
  79. }
  80. void InspectorDOMStorageAgent::enable(ErrorString*)
  81. {
  82. m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, true);
  83. }
  84. void InspectorDOMStorageAgent::disable(ErrorString*)
  85. {
  86. m_state->setBoolean(DOMStorageAgentState::domStorageAgentEnabled, false);
  87. }
  88. void InspectorDOMStorageAgent::getDOMStorageItems(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, RefPtr<TypeBuilder::Array<TypeBuilder::Array<String> > >& items)
  89. {
  90. Frame* frame;
  91. RefPtr<StorageArea> storageArea = findStorageArea(errorString, storageId, frame);
  92. if (!storageArea) {
  93. if (errorString)
  94. *errorString = "No StorageArea for given storageId";
  95. return;
  96. }
  97. RefPtr<TypeBuilder::Array<TypeBuilder::Array<String> > > storageItems = TypeBuilder::Array<TypeBuilder::Array<String> >::create();
  98. for (unsigned i = 0; i < storageArea->length(); ++i) {
  99. String key = storageArea->key(i);
  100. String value = storageArea->item(key);
  101. RefPtr<TypeBuilder::Array<String> > entry = TypeBuilder::Array<String>::create();
  102. entry->addItem(key);
  103. entry->addItem(value);
  104. storageItems->addItem(entry.release());
  105. }
  106. items = storageItems.release();
  107. }
  108. void InspectorDOMStorageAgent::setDOMStorageItem(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, const String& key, const String& value)
  109. {
  110. Frame* frame;
  111. RefPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
  112. if (!storageArea) {
  113. *errorString = "Storage not found";
  114. return;
  115. }
  116. bool quotaException = false;
  117. storageArea->setItem(frame, key, value, quotaException);
  118. if (quotaException)
  119. *errorString = ExceptionCodeDescription(QUOTA_EXCEEDED_ERR).name;
  120. }
  121. void InspectorDOMStorageAgent::removeDOMStorageItem(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, const String& key)
  122. {
  123. Frame* frame;
  124. RefPtr<StorageArea> storageArea = findStorageArea(0, storageId, frame);
  125. if (!storageArea) {
  126. *errorString = "Storage not found";
  127. return;
  128. }
  129. storageArea->removeItem(frame, key);
  130. }
  131. String InspectorDOMStorageAgent::storageId(Storage* storage)
  132. {
  133. ASSERT(storage);
  134. Document* document = storage->frame()->document();
  135. ASSERT(document);
  136. DOMWindow* window = document->domWindow();
  137. ASSERT(window);
  138. RefPtr<SecurityOrigin> securityOrigin = document->securityOrigin();
  139. bool isLocalStorage = window->optionalLocalStorage() == storage;
  140. return storageId(securityOrigin.get(), isLocalStorage)->toJSONString();
  141. }
  142. PassRefPtr<TypeBuilder::DOMStorage::StorageId> InspectorDOMStorageAgent::storageId(SecurityOrigin* securityOrigin, bool isLocalStorage)
  143. {
  144. return TypeBuilder::DOMStorage::StorageId::create()
  145. .setSecurityOrigin(securityOrigin->toRawString())
  146. .setIsLocalStorage(isLocalStorage).release();
  147. }
  148. void InspectorDOMStorageAgent::didDispatchDOMStorageEvent(const String& key, const String& oldValue, const String& newValue, StorageType storageType, SecurityOrigin* securityOrigin, Page*)
  149. {
  150. if (!m_frontend || !isEnabled())
  151. return;
  152. RefPtr<TypeBuilder::DOMStorage::StorageId> id = storageId(securityOrigin, storageType == LocalStorage);
  153. if (key.isNull())
  154. m_frontend->domstorage()->domStorageItemsCleared(id);
  155. else if (newValue.isNull())
  156. m_frontend->domstorage()->domStorageItemRemoved(id, key);
  157. else if (oldValue.isNull())
  158. m_frontend->domstorage()->domStorageItemAdded(id, key, newValue);
  159. else
  160. m_frontend->domstorage()->domStorageItemUpdated(id, key, oldValue, newValue);
  161. }
  162. PassRefPtr<StorageArea> InspectorDOMStorageAgent::findStorageArea(ErrorString* errorString, const RefPtr<InspectorObject>& storageId, Frame*& targetFrame)
  163. {
  164. String securityOrigin;
  165. bool isLocalStorage = false;
  166. bool success = storageId->getString("securityOrigin", &securityOrigin);
  167. if (success)
  168. success = storageId->getBoolean("isLocalStorage", &isLocalStorage);
  169. if (!success) {
  170. if (errorString)
  171. *errorString = "Invalid storageId format";
  172. targetFrame = 0;
  173. return 0;
  174. }
  175. targetFrame = m_pageAgent->findFrameWithSecurityOrigin(securityOrigin);
  176. if (!targetFrame) {
  177. if (errorString)
  178. *errorString = "Frame not found for the given security origin";
  179. return 0;
  180. }
  181. Page* page = m_pageAgent->page();
  182. if (isLocalStorage)
  183. return page->group().localStorage()->storageArea(targetFrame->document()->securityOrigin());
  184. return page->sessionStorage()->storageArea(targetFrame->document()->securityOrigin());
  185. }
  186. } // namespace WebCore
  187. #endif // ENABLE(INSPECTOR)