| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2013 Google Inc. All rights reserved. | 2 * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 #include "modules/serviceworkers/ForeignFetchEvent.h" | 53 #include "modules/serviceworkers/ForeignFetchEvent.h" |
| 54 #include "modules/serviceworkers/InstallEvent.h" | 54 #include "modules/serviceworkers/InstallEvent.h" |
| 55 #include "modules/serviceworkers/ServiceWorkerClient.h" | 55 #include "modules/serviceworkers/ServiceWorkerClient.h" |
| 56 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" | 56 #include "modules/serviceworkers/ServiceWorkerGlobalScope.h" |
| 57 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" | 57 #include "modules/serviceworkers/ServiceWorkerWindowClient.h" |
| 58 #include "modules/serviceworkers/WaitUntilObserver.h" | 58 #include "modules/serviceworkers/WaitUntilObserver.h" |
| 59 #include "platform/RuntimeEnabledFeatures.h" | 59 #include "platform/RuntimeEnabledFeatures.h" |
| 60 #include "public/platform/modules/notifications/WebNotificationData.h" | 60 #include "public/platform/modules/notifications/WebNotificationData.h" |
| 61 #include "public/platform/modules/serviceworker/WebServiceWorkerEventResult.h" | 61 #include "public/platform/modules/serviceworker/WebServiceWorkerEventResult.h" |
| 62 #include "public/platform/modules/serviceworker/WebServiceWorkerRequest.h" | 62 #include "public/platform/modules/serviceworker/WebServiceWorkerRequest.h" |
| 63 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" |
| 63 #include "public/web/WebSerializedScriptValue.h" | 64 #include "public/web/WebSerializedScriptValue.h" |
| 64 #include "public/web/modules/serviceworker/WebServiceWorkerContextClient.h" | 65 #include "public/web/modules/serviceworker/WebServiceWorkerContextClient.h" |
| 65 #include "web/WebEmbeddedWorkerImpl.h" | 66 #include "web/WebEmbeddedWorkerImpl.h" |
| 66 #include "wtf/Assertions.h" | 67 #include "wtf/Assertions.h" |
| 67 #include "wtf/Functional.h" | 68 #include "wtf/Functional.h" |
| 68 #include "wtf/PtrUtil.h" | 69 #include "wtf/PtrUtil.h" |
| 69 #include <memory> | 70 #include <memory> |
| 70 #include <utility> | 71 #include <utility> |
| 71 | 72 |
| 72 namespace blink { | 73 namespace blink { |
| 73 | 74 |
| 74 ServiceWorkerGlobalScopeProxy* ServiceWorkerGlobalScopeProxy::create( | 75 ServiceWorkerGlobalScopeProxy* ServiceWorkerGlobalScopeProxy::create( |
| 75 WebEmbeddedWorkerImpl& embeddedWorker, | 76 WebEmbeddedWorkerImpl& embeddedWorker, |
| 76 Document& document, | 77 Document& document, |
| 77 WebServiceWorkerContextClient& client) { | 78 WebServiceWorkerContextClient& client) { |
| 78 return new ServiceWorkerGlobalScopeProxy(embeddedWorker, document, client); | 79 return new ServiceWorkerGlobalScopeProxy(embeddedWorker, document, client); |
| 79 } | 80 } |
| 80 | 81 |
| 81 ServiceWorkerGlobalScopeProxy::~ServiceWorkerGlobalScopeProxy() { | 82 ServiceWorkerGlobalScopeProxy::~ServiceWorkerGlobalScopeProxy() { |
| 82 // Verify that the proxy has been detached. | 83 // Verify that the proxy has been detached. |
| 83 DCHECK(!m_embeddedWorker); | 84 DCHECK(!m_embeddedWorker); |
| 84 } | 85 } |
| 85 | 86 |
| 86 DEFINE_TRACE(ServiceWorkerGlobalScopeProxy) { | 87 DEFINE_TRACE(ServiceWorkerGlobalScopeProxy) { |
| 87 visitor->trace(m_document); | 88 visitor->trace(m_document); |
| 89 visitor->trace(m_pendingPreloadFetchEvents); |
| 88 } | 90 } |
| 89 | 91 |
| 90 void ServiceWorkerGlobalScopeProxy::setRegistration( | 92 void ServiceWorkerGlobalScopeProxy::setRegistration( |
| 91 std::unique_ptr<WebServiceWorkerRegistration::Handle> handle) { | 93 std::unique_ptr<WebServiceWorkerRegistration::Handle> handle) { |
| 92 workerGlobalScope()->setRegistration(std::move(handle)); | 94 workerGlobalScope()->setRegistration(std::move(handle)); |
| 93 } | 95 } |
| 94 | 96 |
| 95 void ServiceWorkerGlobalScopeProxy::dispatchActivateEvent(int eventID) { | 97 void ServiceWorkerGlobalScopeProxy::dispatchActivateEvent(int eventID) { |
| 96 WaitUntilObserver* observer = WaitUntilObserver::create( | 98 WaitUntilObserver* observer = WaitUntilObserver::create( |
| 97 workerGlobalScope(), WaitUntilObserver::Activate, eventID); | 99 workerGlobalScope(), WaitUntilObserver::Activate, eventID); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 WaitUntilObserver* observer = WaitUntilObserver::create( | 146 WaitUntilObserver* observer = WaitUntilObserver::create( |
| 145 workerGlobalScope(), WaitUntilObserver::Message, eventID); | 147 workerGlobalScope(), WaitUntilObserver::Message, eventID); |
| 146 | 148 |
| 147 Event* event = | 149 Event* event = |
| 148 ExtendableMessageEvent::create(value, origin, ports, source, observer); | 150 ExtendableMessageEvent::create(value, origin, ports, source, observer); |
| 149 workerGlobalScope()->dispatchExtendableEvent(event, observer); | 151 workerGlobalScope()->dispatchExtendableEvent(event, observer); |
| 150 } | 152 } |
| 151 | 153 |
| 152 void ServiceWorkerGlobalScopeProxy::dispatchFetchEvent( | 154 void ServiceWorkerGlobalScopeProxy::dispatchFetchEvent( |
| 153 int fetchEventID, | 155 int fetchEventID, |
| 154 const WebServiceWorkerRequest& webRequest) { | 156 const WebServiceWorkerRequest& webRequest, |
| 157 bool navigationPreloadSent) { |
| 155 ScriptState::Scope scope( | 158 ScriptState::Scope scope( |
| 156 workerGlobalScope()->scriptController()->getScriptState()); | 159 workerGlobalScope()->scriptController()->getScriptState()); |
| 157 WaitUntilObserver* waitUntilObserver = WaitUntilObserver::create( | 160 WaitUntilObserver* waitUntilObserver = WaitUntilObserver::create( |
| 158 workerGlobalScope(), WaitUntilObserver::Fetch, fetchEventID); | 161 workerGlobalScope(), WaitUntilObserver::Fetch, fetchEventID); |
| 159 RespondWithObserver* respondWithObserver = RespondWithObserver::create( | 162 RespondWithObserver* respondWithObserver = RespondWithObserver::create( |
| 160 workerGlobalScope(), fetchEventID, webRequest.url(), webRequest.mode(), | 163 workerGlobalScope(), fetchEventID, webRequest.url(), webRequest.mode(), |
| 161 webRequest.frameType(), webRequest.requestContext(), waitUntilObserver); | 164 webRequest.frameType(), webRequest.requestContext(), waitUntilObserver); |
| 162 Request* request = Request::create( | 165 Request* request = Request::create( |
| 163 workerGlobalScope()->scriptController()->getScriptState(), webRequest); | 166 workerGlobalScope()->scriptController()->getScriptState(), webRequest); |
| 164 request->getHeaders()->setGuard(Headers::ImmutableGuard); | 167 request->getHeaders()->setGuard(Headers::ImmutableGuard); |
| 165 FetchEventInit eventInit; | 168 FetchEventInit eventInit; |
| 166 eventInit.setCancelable(true); | 169 eventInit.setCancelable(true); |
| 167 eventInit.setRequest(request); | 170 eventInit.setRequest(request); |
| 168 eventInit.setClientId( | 171 eventInit.setClientId( |
| 169 webRequest.isMainResourceLoad() ? WebString() : webRequest.clientId()); | 172 webRequest.isMainResourceLoad() ? WebString() : webRequest.clientId()); |
| 170 eventInit.setIsReload(webRequest.isReload()); | 173 eventInit.setIsReload(webRequest.isReload()); |
| 174 ScriptState* scriptState = |
| 175 workerGlobalScope()->scriptController()->getScriptState(); |
| 171 FetchEvent* fetchEvent = FetchEvent::create( | 176 FetchEvent* fetchEvent = FetchEvent::create( |
| 172 workerGlobalScope()->scriptController()->getScriptState(), | 177 scriptState, EventTypeNames::fetch, eventInit, respondWithObserver, |
| 173 EventTypeNames::fetch, eventInit, respondWithObserver, waitUntilObserver); | 178 waitUntilObserver, navigationPreloadSent); |
| 179 if (navigationPreloadSent) { |
| 180 // Keep |fetchEvent| until onNavigationPreloadResponse() or |
| 181 // onNavigationPreloadError() will be called. |
| 182 m_pendingPreloadFetchEvents.add(fetchEventID, fetchEvent); |
| 183 } |
| 174 waitUntilObserver->willDispatchEvent(); | 184 waitUntilObserver->willDispatchEvent(); |
| 175 respondWithObserver->willDispatchEvent(); | 185 respondWithObserver->willDispatchEvent(); |
| 176 DispatchEventResult dispatchResult = | 186 DispatchEventResult dispatchResult = |
| 177 workerGlobalScope()->dispatchEvent(fetchEvent); | 187 workerGlobalScope()->dispatchEvent(fetchEvent); |
| 178 respondWithObserver->didDispatchEvent(dispatchResult); | 188 respondWithObserver->didDispatchEvent(dispatchResult); |
| 179 // false is okay because waitUntil for fetch event doesn't care about the | 189 // false is okay because waitUntil for fetch event doesn't care about the |
| 180 // promise rejection or an uncaught runtime script error. | 190 // promise rejection or an uncaught runtime script error. |
| 181 waitUntilObserver->didDispatchEvent(false /* errorOccurred */); | 191 waitUntilObserver->didDispatchEvent(false /* errorOccurred */); |
| 182 } | 192 } |
| 183 | 193 |
| 194 void ServiceWorkerGlobalScopeProxy::onNavigationPreloadResponse( |
| 195 int fetchEventID, |
| 196 std::unique_ptr<WebServiceWorkerResponse> response, |
| 197 std::unique_ptr<WebDataConsumerHandle> dataConsumeHandle) { |
| 198 FetchEvent* fetchEvent = m_pendingPreloadFetchEvents.take(fetchEventID); |
| 199 DCHECK(fetchEvent); |
| 200 fetchEvent->onNavigationPreloadResponse(std::move(response), |
| 201 std::move(dataConsumeHandle)); |
| 202 } |
| 203 |
| 204 void ServiceWorkerGlobalScopeProxy::onNavigationPreloadError( |
| 205 int fetchEventID, |
| 206 std::unique_ptr<WebServiceWorkerError> error) { |
| 207 FetchEvent* fetchEvent = m_pendingPreloadFetchEvents.take(fetchEventID); |
| 208 DCHECK(fetchEvent); |
| 209 fetchEvent->onNavigationPreloadError(std::move(error)); |
| 210 } |
| 211 |
| 184 void ServiceWorkerGlobalScopeProxy::dispatchForeignFetchEvent( | 212 void ServiceWorkerGlobalScopeProxy::dispatchForeignFetchEvent( |
| 185 int fetchEventID, | 213 int fetchEventID, |
| 186 const WebServiceWorkerRequest& webRequest) { | 214 const WebServiceWorkerRequest& webRequest) { |
| 187 if (!OriginTrials::foreignFetchEnabled(workerGlobalScope())) { | 215 if (!OriginTrials::foreignFetchEnabled(workerGlobalScope())) { |
| 188 // If origin trial tokens have expired, or are otherwise no longer valid | 216 // If origin trial tokens have expired, or are otherwise no longer valid |
| 189 // no events should be dispatched. | 217 // no events should be dispatched. |
| 190 // TODO(mek): Ideally the browser wouldn't even start the service worker | 218 // TODO(mek): Ideally the browser wouldn't even start the service worker |
| 191 // if its tokens have expired. | 219 // if its tokens have expired. |
| 192 ServiceWorkerGlobalScopeClient::from(workerGlobalScope()) | 220 ServiceWorkerGlobalScopeClient::from(workerGlobalScope()) |
| 193 ->respondToFetchEvent(fetchEventID, WTF::currentTime()); | 221 ->respondToFetchEvent(fetchEventID, WTF::currentTime()); |
| (...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 407 return *m_document; | 435 return *m_document; |
| 408 } | 436 } |
| 409 | 437 |
| 410 ServiceWorkerGlobalScope* ServiceWorkerGlobalScopeProxy::workerGlobalScope() | 438 ServiceWorkerGlobalScope* ServiceWorkerGlobalScopeProxy::workerGlobalScope() |
| 411 const { | 439 const { |
| 412 DCHECK(m_workerGlobalScope); | 440 DCHECK(m_workerGlobalScope); |
| 413 return m_workerGlobalScope; | 441 return m_workerGlobalScope; |
| 414 } | 442 } |
| 415 | 443 |
| 416 } // namespace blink | 444 } // namespace blink |
| OLD | NEW |