1 | /* |
2 | * Copyright (C) 2019 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 | |
26 | #include "config.h" |
27 | #include "DocumentStorageAccess.h" |
28 | |
29 | #if ENABLE(RESOURCE_LOAD_STATISTICS) |
30 | |
31 | #include "Chrome.h" |
32 | #include "ChromeClient.h" |
33 | #include "Document.h" |
34 | #include "Frame.h" |
35 | #include "FrameLoader.h" |
36 | #include "FrameLoaderClient.h" |
37 | #include "JSDOMPromiseDeferred.h" |
38 | #include "Microtasks.h" |
39 | #include "Page.h" |
40 | #include "RegistrableDomain.h" |
41 | #include "SecurityOrigin.h" |
42 | #include "Settings.h" |
43 | #include "UserGestureIndicator.h" |
44 | |
45 | namespace WebCore { |
46 | |
47 | DocumentStorageAccess* DocumentStorageAccess::from(Document& document) |
48 | { |
49 | auto* supplement = static_cast<DocumentStorageAccess*>(Supplement<Document>::from(&document, supplementName())); |
50 | if (!supplement) { |
51 | auto newSupplement = std::make_unique<DocumentStorageAccess>(document); |
52 | supplement = newSupplement.get(); |
53 | provideTo(&document, supplementName(), WTFMove(newSupplement)); |
54 | } |
55 | return supplement; |
56 | } |
57 | |
58 | const char* DocumentStorageAccess::supplementName() |
59 | { |
60 | return "DocumentStorageAccess" ; |
61 | } |
62 | |
63 | void DocumentStorageAccess::hasStorageAccess(Document& document, Ref<DeferredPromise>&& promise) |
64 | { |
65 | DocumentStorageAccess::from(document)->hasStorageAccess(WTFMove(promise)); |
66 | } |
67 | |
68 | void DocumentStorageAccess::requestStorageAccess(Document& document, Ref<DeferredPromise>&& promise) |
69 | { |
70 | DocumentStorageAccess::from(document)->requestStorageAccess(WTFMove(promise)); |
71 | } |
72 | |
73 | void DocumentStorageAccess::hasStorageAccess(Ref<DeferredPromise>&& promise) |
74 | { |
75 | ASSERT(m_document.settings().storageAccessAPIEnabled()); |
76 | |
77 | if (m_document.frame() && hasFrameSpecificStorageAccess()) { |
78 | promise->resolve<IDLBoolean>(true); |
79 | return; |
80 | } |
81 | |
82 | if (!m_document.frame() || m_document.securityOrigin().isUnique()) { |
83 | promise->resolve<IDLBoolean>(false); |
84 | return; |
85 | } |
86 | |
87 | if (m_document.frame()->isMainFrame()) { |
88 | promise->resolve<IDLBoolean>(true); |
89 | return; |
90 | } |
91 | |
92 | auto& securityOrigin = m_document.securityOrigin(); |
93 | auto& topSecurityOrigin = m_document.topDocument().securityOrigin(); |
94 | if (securityOrigin.equal(&topSecurityOrigin)) { |
95 | promise->resolve<IDLBoolean>(true); |
96 | return; |
97 | } |
98 | |
99 | auto frameID = m_document.frame()->loader().client().frameID(); |
100 | auto pageID = m_document.frame()->loader().client().pageID(); |
101 | if (!frameID || !pageID) { |
102 | promise->reject(); |
103 | return; |
104 | } |
105 | |
106 | if (Page* page = m_document.page()) { |
107 | auto subFrameDomain = RegistrableDomain::uncheckedCreateFromHost(securityOrigin.host()); |
108 | auto topFrameDomain = RegistrableDomain::uncheckedCreateFromHost(topSecurityOrigin.host()); |
109 | page->chrome().client().hasStorageAccess(WTFMove(subFrameDomain), WTFMove(topFrameDomain), frameID.value(), pageID.value(), [weakThis = makeWeakPtr(*this), promise = WTFMove(promise)] (bool hasAccess) { |
110 | DocumentStorageAccess* document = weakThis.get(); |
111 | if (!document) |
112 | return; |
113 | |
114 | promise->resolve<IDLBoolean>(hasAccess); |
115 | }); |
116 | return; |
117 | } |
118 | |
119 | promise->reject(); |
120 | } |
121 | |
122 | void DocumentStorageAccess::requestStorageAccess(Ref<DeferredPromise>&& promise) |
123 | { |
124 | ASSERT(m_document.settings().storageAccessAPIEnabled()); |
125 | |
126 | if (m_document.frame() && hasFrameSpecificStorageAccess()) { |
127 | promise->resolve(); |
128 | return; |
129 | } |
130 | |
131 | if (!m_document.frame() || m_document.securityOrigin().isUnique()) { |
132 | promise->reject(); |
133 | return; |
134 | } |
135 | |
136 | if (m_document.frame()->isMainFrame()) { |
137 | promise->resolve(); |
138 | return; |
139 | } |
140 | |
141 | auto& topDocument = m_document.topDocument(); |
142 | auto& topSecurityOrigin = topDocument.securityOrigin(); |
143 | auto& securityOrigin = m_document.securityOrigin(); |
144 | if (securityOrigin.equal(&topSecurityOrigin)) { |
145 | promise->resolve(); |
146 | return; |
147 | } |
148 | |
149 | // If there is a sandbox, it has to allow the storage access API to be called. |
150 | if (m_document.sandboxFlags() != SandboxNone && m_document.isSandboxed(SandboxStorageAccessByUserActivation)) { |
151 | promise->reject(); |
152 | return; |
153 | } |
154 | |
155 | // The iframe has to be a direct child of the top document. |
156 | if (&topDocument != m_document.parentDocument()) { |
157 | promise->reject(); |
158 | return; |
159 | } |
160 | |
161 | if (!UserGestureIndicator::processingUserGesture()) { |
162 | promise->reject(); |
163 | return; |
164 | } |
165 | |
166 | Page* page = m_document.page(); |
167 | auto frameID = m_document.frame()->loader().client().frameID(); |
168 | auto pageID = m_document.frame()->loader().client().pageID(); |
169 | if (!page || !frameID || !pageID) { |
170 | promise->reject(); |
171 | return; |
172 | } |
173 | |
174 | auto subFrameDomain = RegistrableDomain::uncheckedCreateFromHost(securityOrigin.host()); |
175 | auto topFrameDomain = RegistrableDomain::uncheckedCreateFromHost(topSecurityOrigin.host()); |
176 | |
177 | page->chrome().client().requestStorageAccess(WTFMove(subFrameDomain), WTFMove(topFrameDomain), frameID.value(), pageID.value(), [documentReference = makeWeakPtr(*this), promise = WTFMove(promise)] (StorageAccessWasGranted wasGranted, StorageAccessPromptWasShown promptWasShown) mutable { |
178 | DocumentStorageAccess* document = documentReference.get(); |
179 | if (!document) |
180 | return; |
181 | |
182 | // Consume the user gesture only if the user explicitly denied access. |
183 | bool shouldPreserveUserGesture = wasGranted == StorageAccessWasGranted::Yes || promptWasShown == StorageAccessPromptWasShown::No; |
184 | |
185 | if (shouldPreserveUserGesture) { |
186 | MicrotaskQueue::mainThreadQueue().append(std::make_unique<VoidMicrotask>([documentReference = makeWeakPtr(*document)] () { |
187 | if (auto* document = documentReference.get()) |
188 | document->enableTemporaryTimeUserGesture(); |
189 | })); |
190 | } |
191 | |
192 | if (wasGranted == StorageAccessWasGranted::Yes) { |
193 | document->setHasFrameSpecificStorageAccess(true); |
194 | promise->resolve(); |
195 | } else |
196 | promise->reject(); |
197 | |
198 | if (shouldPreserveUserGesture) { |
199 | MicrotaskQueue::mainThreadQueue().append(std::make_unique<VoidMicrotask>([documentReference = WTFMove(documentReference)] () { |
200 | if (auto* document = documentReference.get()) |
201 | document->consumeTemporaryTimeUserGesture(); |
202 | })); |
203 | } |
204 | }); |
205 | } |
206 | |
207 | void DocumentStorageAccess::enableTemporaryTimeUserGesture() |
208 | { |
209 | m_temporaryUserGesture = std::make_unique<UserGestureIndicator>(ProcessingUserGesture, &m_document); |
210 | } |
211 | |
212 | void DocumentStorageAccess::consumeTemporaryTimeUserGesture() |
213 | { |
214 | m_temporaryUserGesture = nullptr; |
215 | } |
216 | |
217 | bool DocumentStorageAccess::hasFrameSpecificStorageAccess() const |
218 | { |
219 | auto* frame = m_document.frame(); |
220 | return frame && frame->loader().client().hasFrameSpecificStorageAccess(); |
221 | } |
222 | |
223 | void DocumentStorageAccess::setHasFrameSpecificStorageAccess(bool value) |
224 | { |
225 | if (auto* frame = m_document.frame()) |
226 | frame->loader().client().setHasFrameSpecificStorageAccess(value); |
227 | } |
228 | |
229 | } // namespace WebCore |
230 | |
231 | #endif // ENABLE(RESOURCE_LOAD_STATISTICS) |
232 | |