| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/storage/DOMWindowStorage.h" | 5 #include "modules/storage/DOMWindowStorage.h" |
| 6 | 6 |
| 7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
| 8 #include "core/frame/FrameHost.h" | 8 #include "core/frame/FrameHost.h" |
| 9 #include "core/frame/LocalDOMWindow.h" | 9 #include "core/frame/LocalDOMWindow.h" |
| 10 #include "core/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
| 11 #include "core/frame/Settings.h" | 11 #include "core/frame/Settings.h" |
| 12 #include "core/page/Page.h" | 12 #include "core/page/Page.h" |
| 13 #include "modules/storage/Storage.h" | 13 #include "modules/storage/Storage.h" |
| 14 #include "modules/storage/StorageNamespace.h" | 14 #include "modules/storage/StorageNamespace.h" |
| 15 #include "modules/storage/StorageNamespaceController.h" | 15 #include "modules/storage/StorageNamespaceController.h" |
| 16 #include "wtf/PassRefPtr.h" | 16 #include "wtf/PassRefPtr.h" |
| 17 | 17 |
| 18 namespace blink { | 18 namespace blink { |
| 19 | 19 |
| 20 DOMWindowStorage::DOMWindowStorage(LocalDOMWindow& window) | 20 DOMWindowStorage::DOMWindowStorage(LocalDOMWindow& window) |
| 21 : ContextClient(window.frame()), m_window(&window) {} | 21 : Supplement<LocalDOMWindow>(window) {} |
| 22 | 22 |
| 23 DEFINE_TRACE(DOMWindowStorage) { | 23 DEFINE_TRACE(DOMWindowStorage) { |
| 24 visitor->trace(m_window); | |
| 25 visitor->trace(m_sessionStorage); | 24 visitor->trace(m_sessionStorage); |
| 26 visitor->trace(m_localStorage); | 25 visitor->trace(m_localStorage); |
| 27 Supplement<LocalDOMWindow>::trace(visitor); | 26 Supplement<LocalDOMWindow>::trace(visitor); |
| 28 ContextClient::trace(visitor); | |
| 29 } | 27 } |
| 30 | 28 |
| 31 // static | 29 // static |
| 32 const char* DOMWindowStorage::supplementName() { | 30 const char* DOMWindowStorage::supplementName() { |
| 33 return "DOMWindowStorage"; | 31 return "DOMWindowStorage"; |
| 34 } | 32 } |
| 35 | 33 |
| 36 // static | 34 // static |
| 37 DOMWindowStorage& DOMWindowStorage::from(LocalDOMWindow& window) { | 35 DOMWindowStorage& DOMWindowStorage::from(LocalDOMWindow& window) { |
| 38 DOMWindowStorage* supplement = static_cast<DOMWindowStorage*>( | 36 DOMWindowStorage* supplement = static_cast<DOMWindowStorage*>( |
| (...skipping 12 matching lines...) Expand all Loading... |
| 51 } | 49 } |
| 52 | 50 |
| 53 // static | 51 // static |
| 54 Storage* DOMWindowStorage::localStorage(DOMWindow& window, | 52 Storage* DOMWindowStorage::localStorage(DOMWindow& window, |
| 55 ExceptionState& exceptionState) { | 53 ExceptionState& exceptionState) { |
| 56 return from(toLocalDOMWindow(window)).localStorage(exceptionState); | 54 return from(toLocalDOMWindow(window)).localStorage(exceptionState); |
| 57 } | 55 } |
| 58 | 56 |
| 59 Storage* DOMWindowStorage::sessionStorage( | 57 Storage* DOMWindowStorage::sessionStorage( |
| 60 ExceptionState& exceptionState) const { | 58 ExceptionState& exceptionState) const { |
| 61 if (!m_window->isCurrentlyDisplayedInFrame()) | 59 if (!host()->frame()) |
| 62 return nullptr; | 60 return nullptr; |
| 63 | 61 |
| 64 Document* document = m_window->document(); | 62 Document* document = host()->frame()->document(); |
| 65 if (!document) | 63 DCHECK(document); |
| 66 return nullptr; | |
| 67 | |
| 68 String accessDeniedMessage = "Access is denied for this document."; | 64 String accessDeniedMessage = "Access is denied for this document."; |
| 69 if (!document->getSecurityOrigin()->canAccessLocalStorage()) { | 65 if (!document->getSecurityOrigin()->canAccessLocalStorage()) { |
| 70 if (document->isSandboxed(SandboxOrigin)) | 66 if (document->isSandboxed(SandboxOrigin)) |
| 71 exceptionState.throwSecurityError( | 67 exceptionState.throwSecurityError( |
| 72 "The document is sandboxed and lacks the 'allow-same-origin' flag."); | 68 "The document is sandboxed and lacks the 'allow-same-origin' flag."); |
| 73 else if (document->url().protocolIs("data")) | 69 else if (document->url().protocolIs("data")) |
| 74 exceptionState.throwSecurityError( | 70 exceptionState.throwSecurityError( |
| 75 "Storage is disabled inside 'data:' URLs."); | 71 "Storage is disabled inside 'data:' URLs."); |
| 76 else | 72 else |
| 77 exceptionState.throwSecurityError(accessDeniedMessage); | 73 exceptionState.throwSecurityError(accessDeniedMessage); |
| 78 return nullptr; | 74 return nullptr; |
| 79 } | 75 } |
| 80 | 76 |
| 81 if (m_sessionStorage) { | 77 if (m_sessionStorage) { |
| 82 if (!m_sessionStorage->area()->canAccessStorage(m_window->frame())) { | 78 if (!m_sessionStorage->area()->canAccessStorage(document->frame())) { |
| 83 exceptionState.throwSecurityError(accessDeniedMessage); | 79 exceptionState.throwSecurityError(accessDeniedMessage); |
| 84 return nullptr; | 80 return nullptr; |
| 85 } | 81 } |
| 86 return m_sessionStorage; | 82 return m_sessionStorage; |
| 87 } | 83 } |
| 88 | 84 |
| 89 Page* page = document->page(); | 85 Page* page = document->page(); |
| 90 if (!page) | 86 if (!page) |
| 91 return nullptr; | 87 return nullptr; |
| 92 | 88 |
| 93 StorageArea* storageArea = | 89 StorageArea* storageArea = |
| 94 StorageNamespaceController::from(page)->sessionStorage()->storageArea( | 90 StorageNamespaceController::from(page)->sessionStorage()->storageArea( |
| 95 document->getSecurityOrigin()); | 91 document->getSecurityOrigin()); |
| 96 if (!storageArea->canAccessStorage(m_window->frame())) { | 92 if (!storageArea->canAccessStorage(document->frame())) { |
| 97 exceptionState.throwSecurityError(accessDeniedMessage); | 93 exceptionState.throwSecurityError(accessDeniedMessage); |
| 98 return nullptr; | 94 return nullptr; |
| 99 } | 95 } |
| 100 | 96 |
| 101 m_sessionStorage = Storage::create(m_window->frame(), storageArea); | 97 m_sessionStorage = Storage::create(document->frame(), storageArea); |
| 102 return m_sessionStorage; | 98 return m_sessionStorage; |
| 103 } | 99 } |
| 104 | 100 |
| 105 Storage* DOMWindowStorage::localStorage(ExceptionState& exceptionState) const { | 101 Storage* DOMWindowStorage::localStorage(ExceptionState& exceptionState) const { |
| 106 if (!m_window->isCurrentlyDisplayedInFrame()) | 102 if (!host()->frame()) |
| 107 return nullptr; | 103 return nullptr; |
| 108 Document* document = m_window->document(); | 104 |
| 109 if (!document) | 105 Document* document = host()->frame()->document(); |
| 110 return nullptr; | 106 DCHECK(document); |
| 111 String accessDeniedMessage = "Access is denied for this document."; | 107 String accessDeniedMessage = "Access is denied for this document."; |
| 112 if (!document->getSecurityOrigin()->canAccessLocalStorage()) { | 108 if (!document->getSecurityOrigin()->canAccessLocalStorage()) { |
| 113 if (document->isSandboxed(SandboxOrigin)) | 109 if (document->isSandboxed(SandboxOrigin)) |
| 114 exceptionState.throwSecurityError( | 110 exceptionState.throwSecurityError( |
| 115 "The document is sandboxed and lacks the 'allow-same-origin' flag."); | 111 "The document is sandboxed and lacks the 'allow-same-origin' flag."); |
| 116 else if (document->url().protocolIs("data")) | 112 else if (document->url().protocolIs("data")) |
| 117 exceptionState.throwSecurityError( | 113 exceptionState.throwSecurityError( |
| 118 "Storage is disabled inside 'data:' URLs."); | 114 "Storage is disabled inside 'data:' URLs."); |
| 119 else | 115 else |
| 120 exceptionState.throwSecurityError(accessDeniedMessage); | 116 exceptionState.throwSecurityError(accessDeniedMessage); |
| 121 return nullptr; | 117 return nullptr; |
| 122 } | 118 } |
| 123 if (m_localStorage) { | 119 if (m_localStorage) { |
| 124 if (!m_localStorage->area()->canAccessStorage(m_window->frame())) { | 120 if (!m_localStorage->area()->canAccessStorage(document->frame())) { |
| 125 exceptionState.throwSecurityError(accessDeniedMessage); | 121 exceptionState.throwSecurityError(accessDeniedMessage); |
| 126 return nullptr; | 122 return nullptr; |
| 127 } | 123 } |
| 128 return m_localStorage; | 124 return m_localStorage; |
| 129 } | 125 } |
| 130 // FIXME: Seems this check should be much higher? | 126 // FIXME: Seems this check should be much higher? |
| 131 FrameHost* host = document->frameHost(); | 127 FrameHost* host = document->frameHost(); |
| 132 if (!host || !host->settings().getLocalStorageEnabled()) | 128 if (!host || !host->settings().getLocalStorageEnabled()) |
| 133 return nullptr; | 129 return nullptr; |
| 134 StorageArea* storageArea = | 130 StorageArea* storageArea = |
| 135 StorageNamespace::localStorageArea(document->getSecurityOrigin()); | 131 StorageNamespace::localStorageArea(document->getSecurityOrigin()); |
| 136 if (!storageArea->canAccessStorage(m_window->frame())) { | 132 if (!storageArea->canAccessStorage(document->frame())) { |
| 137 exceptionState.throwSecurityError(accessDeniedMessage); | 133 exceptionState.throwSecurityError(accessDeniedMessage); |
| 138 return nullptr; | 134 return nullptr; |
| 139 } | 135 } |
| 140 m_localStorage = Storage::create(m_window->frame(), storageArea); | 136 m_localStorage = Storage::create(document->frame(), storageArea); |
| 141 return m_localStorage; | 137 return m_localStorage; |
| 142 } | 138 } |
| 143 | 139 |
| 144 } // namespace blink | 140 } // namespace blink |
| OLD | NEW |