StorageAreaMap.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Copyright (C) 2013 Apple Inc. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
  14. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  15. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  16. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
  17. * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  18. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  19. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  20. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  21. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  22. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  23. * THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "config.h"
  26. #include "StorageAreaMap.h"
  27. #include "SecurityOriginData.h"
  28. #include "StorageAreaImpl.h"
  29. #include "StorageAreaMapMessages.h"
  30. #include "StorageManagerMessages.h"
  31. #include "StorageNamespaceImpl.h"
  32. #include "WebPage.h"
  33. #include "WebPageGroupProxy.h"
  34. #include "WebProcess.h"
  35. #include <WebCore/DOMWindow.h>
  36. #include <WebCore/Frame.h>
  37. #include <WebCore/Page.h>
  38. #include <WebCore/PageGroup.h>
  39. #include <WebCore/Storage.h>
  40. #include <WebCore/StorageEventDispatcher.h>
  41. #include <WebCore/StorageMap.h>
  42. using namespace WebCore;
  43. namespace WebKit {
  44. static uint64_t generateStorageMapID()
  45. {
  46. static uint64_t storageMapID;
  47. return ++storageMapID;
  48. }
  49. PassRefPtr<StorageAreaMap> StorageAreaMap::create(StorageNamespaceImpl* storageNamespace, PassRefPtr<WebCore::SecurityOrigin> securityOrigin)
  50. {
  51. return adoptRef(new StorageAreaMap(storageNamespace, securityOrigin));
  52. }
  53. StorageAreaMap::StorageAreaMap(StorageNamespaceImpl* storageNamespace, PassRefPtr<WebCore::SecurityOrigin> securityOrigin)
  54. : m_storageMapID(generateStorageMapID())
  55. , m_storageType(storageNamespace->storageType())
  56. , m_storageNamespaceID(storageNamespace->storageNamespaceID())
  57. , m_quotaInBytes(storageNamespace->quotaInBytes())
  58. , m_securityOrigin(securityOrigin)
  59. , m_currentSeed(0)
  60. , m_hasPendingClear(false)
  61. , m_hasPendingGetValues(false)
  62. {
  63. if (m_storageType == LocalStorage)
  64. WebProcess::shared().parentProcessConnection()->send(Messages::StorageManager::CreateLocalStorageMap(m_storageMapID, storageNamespace->storageNamespaceID(), SecurityOriginData::fromSecurityOrigin(m_securityOrigin.get())), 0);
  65. else
  66. WebProcess::shared().parentProcessConnection()->send(Messages::StorageManager::CreateSessionStorageMap(m_storageMapID, storageNamespace->storageNamespaceID(), SecurityOriginData::fromSecurityOrigin(m_securityOrigin.get())), 0);
  67. WebProcess::shared().addMessageReceiver(Messages::StorageAreaMap::messageReceiverName(), m_storageMapID, this);
  68. }
  69. StorageAreaMap::~StorageAreaMap()
  70. {
  71. WebProcess::shared().parentProcessConnection()->send(Messages::StorageManager::DestroyStorageMap(m_storageMapID), 0);
  72. WebProcess::shared().removeMessageReceiver(Messages::StorageAreaMap::messageReceiverName(), m_storageMapID);
  73. }
  74. unsigned StorageAreaMap::length()
  75. {
  76. loadValuesIfNeeded();
  77. return m_storageMap->length();
  78. }
  79. String StorageAreaMap::key(unsigned index)
  80. {
  81. loadValuesIfNeeded();
  82. return m_storageMap->key(index);
  83. }
  84. String StorageAreaMap::item(const String& key)
  85. {
  86. loadValuesIfNeeded();
  87. return m_storageMap->getItem(key);
  88. }
  89. void StorageAreaMap::setItem(Frame* sourceFrame, StorageAreaImpl* sourceArea, const String& key, const String& value, bool& quotaException)
  90. {
  91. loadValuesIfNeeded();
  92. ASSERT(m_storageMap->hasOneRef());
  93. String oldValue;
  94. quotaException = false;
  95. m_storageMap->setItem(key, value, oldValue, quotaException);
  96. if (quotaException)
  97. return;
  98. if (oldValue == value)
  99. return;
  100. m_pendingValueChanges.add(key);
  101. WebProcess::shared().parentProcessConnection()->send(Messages::StorageManager::SetItem(m_storageMapID, sourceArea->storageAreaID(), m_currentSeed, key, value, sourceFrame->document()->url()), 0);
  102. }
  103. void StorageAreaMap::removeItem(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea, const String& key)
  104. {
  105. loadValuesIfNeeded();
  106. ASSERT(m_storageMap->hasOneRef());
  107. String oldValue;
  108. m_storageMap->removeItem(key, oldValue);
  109. if (oldValue.isNull())
  110. return;
  111. m_pendingValueChanges.add(key);
  112. WebProcess::shared().parentProcessConnection()->send(Messages::StorageManager::RemoveItem(m_storageMapID, sourceArea->storageAreaID(), m_currentSeed, key, sourceFrame->document()->url()), 0);
  113. }
  114. void StorageAreaMap::clear(WebCore::Frame* sourceFrame, StorageAreaImpl* sourceArea)
  115. {
  116. resetValues();
  117. m_hasPendingClear = true;
  118. m_storageMap = StorageMap::create(m_quotaInBytes);
  119. WebProcess::shared().parentProcessConnection()->send(Messages::StorageManager::Clear(m_storageMapID, sourceArea->storageAreaID(), m_currentSeed, sourceFrame->document()->url()), 0);
  120. }
  121. bool StorageAreaMap::contains(const String& key)
  122. {
  123. loadValuesIfNeeded();
  124. return m_storageMap->contains(key);
  125. }
  126. void StorageAreaMap::resetValues()
  127. {
  128. m_storageMap = nullptr;
  129. m_pendingValueChanges.clear();
  130. m_hasPendingClear = false;
  131. m_hasPendingGetValues = false;
  132. m_currentSeed++;
  133. }
  134. void StorageAreaMap::loadValuesIfNeeded()
  135. {
  136. if (m_storageMap)
  137. return;
  138. HashMap<String, String> values;
  139. // FIXME: This should use a special sendSync flag to indicate that we don't want to process incoming messages while waiting for a reply.
  140. // (This flag does not yet exist). Since loadValuesIfNeeded() ends up being called from within JavaScript code, processing incoming synchronous messages
  141. // could lead to weird reentrency bugs otherwise.
  142. WebProcess::shared().parentProcessConnection()->sendSync(Messages::StorageManager::GetValues(m_storageMapID, m_currentSeed), Messages::StorageManager::GetValues::Reply(values), 0);
  143. m_storageMap = StorageMap::create(m_quotaInBytes);
  144. m_storageMap->importItems(values);
  145. // We want to ignore all changes until we get the DidGetValues message.
  146. m_hasPendingGetValues = true;
  147. }
  148. void StorageAreaMap::didGetValues(uint64_t storageMapSeed)
  149. {
  150. if (m_currentSeed != storageMapSeed)
  151. return;
  152. ASSERT(m_hasPendingGetValues);
  153. m_hasPendingGetValues = false;
  154. }
  155. void StorageAreaMap::didSetItem(uint64_t storageMapSeed, const String& key, bool quotaError)
  156. {
  157. if (m_currentSeed != storageMapSeed)
  158. return;
  159. ASSERT(m_pendingValueChanges.contains(key));
  160. if (quotaError) {
  161. resetValues();
  162. return;
  163. }
  164. m_pendingValueChanges.remove(key);
  165. }
  166. void StorageAreaMap::didRemoveItem(uint64_t storageMapSeed, const String& key)
  167. {
  168. if (m_currentSeed != storageMapSeed)
  169. return;
  170. ASSERT(m_pendingValueChanges.contains(key));
  171. m_pendingValueChanges.remove(key);
  172. }
  173. void StorageAreaMap::didClear(uint64_t storageMapSeed)
  174. {
  175. if (m_currentSeed != storageMapSeed)
  176. return;
  177. ASSERT(m_hasPendingClear);
  178. m_hasPendingClear = false;
  179. }
  180. bool StorageAreaMap::shouldApplyChangeForKey(const String& key) const
  181. {
  182. // We have not yet loaded anything from this storage map.
  183. if (!m_storageMap)
  184. return false;
  185. // Check if this storage area is currently waiting for the storage manager to update the given key.
  186. // If that is the case, we don't want to apply any changes made by other storage areas, since
  187. // our change was made last.
  188. if (m_pendingValueChanges.contains(key))
  189. return false;
  190. return true;
  191. }
  192. void StorageAreaMap::applyChange(const String& key, const String& newValue)
  193. {
  194. ASSERT(!m_storageMap || m_storageMap->hasOneRef());
  195. // There's a clear pending or getValues pending we don't want to apply any changes until we get the corresponding DidClear/DidGetValues messages.
  196. if (m_hasPendingClear || m_hasPendingGetValues)
  197. return;
  198. if (!key) {
  199. // A null key means clear.
  200. RefPtr<StorageMap> newStorageMap = StorageMap::create(m_quotaInBytes);
  201. // Any changes that were made locally after the clear must still be kept around in the new map.
  202. for (auto it = m_pendingValueChanges.begin().keys(), end = m_pendingValueChanges.end().keys(); it != end; ++it) {
  203. const String& key = *it;
  204. String value = m_storageMap->getItem(key);
  205. if (!value) {
  206. // This change must have been a pending remove, ignore it.
  207. continue;
  208. }
  209. String oldValue;
  210. newStorageMap->setItemIgnoringQuota(key, oldValue);
  211. }
  212. m_storageMap = newStorageMap.release();
  213. return;
  214. }
  215. if (!shouldApplyChangeForKey(key))
  216. return;
  217. if (!newValue) {
  218. // A null new value means that the item should be removed.
  219. String oldValue;
  220. m_storageMap->removeItem(key, oldValue);
  221. return;
  222. }
  223. m_storageMap->setItemIgnoringQuota(key, newValue);
  224. }
  225. void StorageAreaMap::dispatchStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString)
  226. {
  227. if (!sourceStorageAreaID) {
  228. // This storage event originates from another process so we need to apply the change to our storage area map.
  229. applyChange(key, newValue);
  230. }
  231. if (storageType() == SessionStorage)
  232. dispatchSessionStorageEvent(sourceStorageAreaID, key, oldValue, newValue, urlString);
  233. else
  234. dispatchLocalStorageEvent(sourceStorageAreaID, key, oldValue, newValue, urlString);
  235. }
  236. void StorageAreaMap::clearCache()
  237. {
  238. resetValues();
  239. }
  240. void StorageAreaMap::dispatchSessionStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString)
  241. {
  242. ASSERT(storageType() == SessionStorage);
  243. // Namespace IDs for session storage namespaces are equivalent to web page IDs
  244. // so we can get the right page here.
  245. WebPage* webPage = WebProcess::shared().webPage(m_storageNamespaceID);
  246. if (!webPage)
  247. return;
  248. Vector<RefPtr<Frame>> frames;
  249. Page* page = webPage->corePage();
  250. for (Frame* frame = page->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
  251. Document* document = frame->document();
  252. if (!document->securityOrigin()->equal(m_securityOrigin.get()))
  253. continue;
  254. Storage* storage = document->domWindow()->optionalSessionStorage();
  255. if (!storage)
  256. continue;
  257. StorageAreaImpl& storageArea = static_cast<StorageAreaImpl&>(storage->area());
  258. if (storageArea.storageAreaID() == sourceStorageAreaID) {
  259. // This is the storage area that caused the event to be dispatched.
  260. continue;
  261. }
  262. frames.append(frame);
  263. }
  264. StorageEventDispatcher::dispatchLocalStorageEventsToFrames(page->group(), frames, key, oldValue, newValue, urlString, m_securityOrigin.get());
  265. }
  266. void StorageAreaMap::dispatchLocalStorageEvent(uint64_t sourceStorageAreaID, const String& key, const String& oldValue, const String& newValue, const String& urlString)
  267. {
  268. ASSERT(storageType() == LocalStorage);
  269. Vector<RefPtr<Frame>> frames;
  270. PageGroup& pageGroup = *WebProcess::shared().webPageGroup(m_storageNamespaceID)->corePageGroup();
  271. const HashSet<Page*>& pages = pageGroup.pages();
  272. for (HashSet<Page*>::const_iterator it = pages.begin(), end = pages.end(); it != end; ++it) {
  273. for (Frame* frame = (*it)->mainFrame(); frame; frame = frame->tree()->traverseNext()) {
  274. Document* document = frame->document();
  275. if (!document->securityOrigin()->equal(m_securityOrigin.get()))
  276. continue;
  277. Storage* storage = document->domWindow()->optionalLocalStorage();
  278. if (!storage)
  279. continue;
  280. StorageAreaImpl& storageArea = static_cast<StorageAreaImpl&>(storage->area());
  281. if (storageArea.storageAreaID() == sourceStorageAreaID) {
  282. // This is the storage area that caused the event to be dispatched.
  283. continue;
  284. }
  285. frames.append(frame);
  286. }
  287. }
  288. StorageEventDispatcher::dispatchLocalStorageEventsToFrames(pageGroup, frames, key, oldValue, newValue, urlString, m_securityOrigin.get());
  289. }
  290. } // namespace WebKit