| 1 | /* |
| 2 | * Copyright (C) 2017 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 | #pragma once |
| 27 | |
| 28 | #if ENABLE(SERVICE_WORKER) |
| 29 | |
| 30 | #include "CacheStorageConnection.h" |
| 31 | #include "Document.h" |
| 32 | #include "FetchIdentifier.h" |
| 33 | #include "Page.h" |
| 34 | #include "SecurityOrigin.h" |
| 35 | #include "ServiceWorkerDebuggable.h" |
| 36 | #include "ServiceWorkerIdentifier.h" |
| 37 | #include "ServiceWorkerInspectorProxy.h" |
| 38 | #include "ServiceWorkerThread.h" |
| 39 | #include "WorkerDebuggerProxy.h" |
| 40 | #include "WorkerLoaderProxy.h" |
| 41 | #include <wtf/HashMap.h> |
| 42 | |
| 43 | namespace WebCore { |
| 44 | |
| 45 | class CacheStorageProvider; |
| 46 | class FetchLoader; |
| 47 | class FetchLoaderClient; |
| 48 | class PageConfiguration; |
| 49 | class ServiceWorkerInspectorProxy; |
| 50 | struct ServiceWorkerContextData; |
| 51 | |
| 52 | class ServiceWorkerThreadProxy final : public ThreadSafeRefCounted<ServiceWorkerThreadProxy>, public WorkerLoaderProxy, public WorkerDebuggerProxy { |
| 53 | public: |
| 54 | template<typename... Args> static Ref<ServiceWorkerThreadProxy> create(Args&&... args) |
| 55 | { |
| 56 | return adoptRef(*new ServiceWorkerThreadProxy(std::forward<Args>(args)...)); |
| 57 | } |
| 58 | ~ServiceWorkerThreadProxy(); |
| 59 | |
| 60 | ServiceWorkerIdentifier identifier() const { return m_serviceWorkerThread->identifier(); } |
| 61 | ServiceWorkerThread& thread() { return m_serviceWorkerThread.get(); } |
| 62 | ServiceWorkerInspectorProxy& inspectorProxy() { return m_inspectorProxy; } |
| 63 | |
| 64 | bool isTerminatingOrTerminated() const { return m_isTerminatingOrTerminated; } |
| 65 | void setAsTerminatingOrTerminated() { m_isTerminatingOrTerminated = true; } |
| 66 | |
| 67 | WEBCORE_EXPORT std::unique_ptr<FetchLoader> createBlobLoader(FetchLoaderClient&, const URL&); |
| 68 | |
| 69 | const URL& scriptURL() const { return m_document->url(); } |
| 70 | |
| 71 | // Public only for testing purposes. |
| 72 | WEBCORE_TESTSUPPORT_EXPORT void notifyNetworkStateChange(bool isOnline); |
| 73 | |
| 74 | WEBCORE_EXPORT void startFetch(SWServerConnectionIdentifier, FetchIdentifier, Ref<ServiceWorkerFetch::Client>&&, Optional<ServiceWorkerClientIdentifier>&&, ResourceRequest&&, String&& referrer, FetchOptions&&); |
| 75 | WEBCORE_EXPORT void cancelFetch(SWServerConnectionIdentifier, FetchIdentifier); |
| 76 | WEBCORE_EXPORT void continueDidReceiveFetchResponse(SWServerConnectionIdentifier, FetchIdentifier); |
| 77 | WEBCORE_EXPORT void removeFetch(SWServerConnectionIdentifier, FetchIdentifier); |
| 78 | |
| 79 | private: |
| 80 | WEBCORE_EXPORT ServiceWorkerThreadProxy(PageConfiguration&&, const ServiceWorkerContextData&, PAL::SessionID, String&& userAgent, CacheStorageProvider&, SecurityOrigin::StorageBlockingPolicy); |
| 81 | |
| 82 | WEBCORE_EXPORT static void networkStateChanged(bool isOnLine); |
| 83 | |
| 84 | // WorkerLoaderProxy |
| 85 | bool postTaskForModeToWorkerGlobalScope(ScriptExecutionContext::Task&&, const String& mode) final; |
| 86 | void postTaskToLoader(ScriptExecutionContext::Task&&) final; |
| 87 | Ref<CacheStorageConnection> createCacheStorageConnection() final; |
| 88 | |
| 89 | // WorkerDebuggerProxy |
| 90 | void postMessageToDebugger(const String&) final; |
| 91 | void setResourceCachingDisabled(bool) final; |
| 92 | |
| 93 | UniqueRef<Page> m_page; |
| 94 | Ref<Document> m_document; |
| 95 | Ref<ServiceWorkerThread> m_serviceWorkerThread; |
| 96 | CacheStorageProvider& m_cacheStorageProvider; |
| 97 | RefPtr<CacheStorageConnection> m_cacheStorageConnection; |
| 98 | PAL::SessionID m_sessionID; |
| 99 | bool m_isTerminatingOrTerminated { false }; |
| 100 | |
| 101 | ServiceWorkerInspectorProxy m_inspectorProxy; |
| 102 | #if ENABLE(REMOTE_INSPECTOR) |
| 103 | std::unique_ptr<ServiceWorkerDebuggable> m_remoteDebuggable; |
| 104 | #endif |
| 105 | HashMap<std::pair<SWServerConnectionIdentifier, FetchIdentifier>, Ref<ServiceWorkerFetch::Client>> m_ongoingFetchTasks; |
| 106 | }; |
| 107 | |
| 108 | } // namespace WebKit |
| 109 | |
| 110 | #endif // ENABLE(SERVICE_WORKER) |
| 111 | |