1 /* 2 * Copyright (C) 2013 Google 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 are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_SERVICE_WORKER_SERVICE_WORKER_GLOBAL_SCOPE_PROXY_H_ 32 #define THIRD_PARTY_BLINK_RENDERER_MODULES_SERVICE_WORKER_SERVICE_WORKER_GLOBAL_SCOPE_PROXY_H_ 33 34 #include <memory> 35 36 #include "base/macros.h" 37 #include "base/threading/thread_checker.h" 38 #include "third_party/blink/public/mojom/service_worker/controller_service_worker.mojom-blink.h" 39 #include "third_party/blink/public/mojom/service_worker/dispatch_fetch_event_params.mojom-blink.h" 40 #include "third_party/blink/public/mojom/service_worker/service_worker.mojom-blink.h" 41 #include "third_party/blink/public/platform/web_string.h" 42 #include "third_party/blink/public/web/modules/service_worker/web_service_worker_context_proxy.h" 43 #include "third_party/blink/renderer/core/workers/worker_reporting_proxy.h" 44 #include "third_party/blink/renderer/platform/heap/persistent.h" 45 #include "third_party/blink/renderer/platform/weborigin/kurl.h" 46 #include "third_party/blink/renderer/platform/wtf/casting.h" 47 #include "third_party/blink/renderer/platform/wtf/forward.h" 48 49 namespace blink { 50 51 class ServiceWorkerGlobalScope; 52 class WebEmbeddedWorkerImpl; 53 class WebServiceWorkerContextClient; 54 55 // This class is created and destructed on the main thread, but live most 56 // of its time as a resident of the worker thread. All methods other than its 57 // ctor/dtor and Detach() are called on the worker thread. 58 // 59 // This implements WebServiceWorkerContextProxy, which connects ServiceWorker's 60 // WorkerGlobalScope and embedder/chrome, and implements ServiceWorker-specific 61 // events/upcall methods that are to be called by embedder/chromium, 62 // e.g. onfetch. 63 // 64 // An instance of this class is supposed to outlive until 65 // workerThreadTerminated() is called by its corresponding 66 // WorkerGlobalScope. 67 // 68 // TODO(bashi): Update the above comment and method comments once we move 69 // creation of this class off the main thread. 70 class ServiceWorkerGlobalScopeProxy final : public WebServiceWorkerContextProxy, 71 public WorkerReportingProxy { 72 public: 73 ServiceWorkerGlobalScopeProxy(WebEmbeddedWorkerImpl&, 74 WebServiceWorkerContextClient&, 75 scoped_refptr<base::SingleThreadTaskRunner> 76 parent_thread_default_task_runner); 77 ~ServiceWorkerGlobalScopeProxy() override; 78 79 // WebServiceWorkerContextProxy overrides: 80 void BindServiceWorker( 81 CrossVariantMojoReceiver<mojom::blink::ServiceWorkerInterfaceBase> 82 receiver) override; 83 void BindControllerServiceWorker( 84 CrossVariantMojoReceiver< 85 mojom::blink::ControllerServiceWorkerInterfaceBase> receiver) 86 override; 87 void OnNavigationPreloadResponse( 88 int fetch_event_id, 89 std::unique_ptr<WebURLResponse>, 90 mojo::ScopedDataPipeConsumerHandle data_pipe) override; 91 void OnNavigationPreloadError( 92 int fetch_event_id, 93 std::unique_ptr<WebServiceWorkerError>) override; 94 void OnNavigationPreloadComplete(int fetch_event_id, 95 base::TimeTicks completion_time, 96 int64_t encoded_data_length, 97 int64_t encoded_body_length, 98 int64_t decoded_body_length) override; 99 bool IsWindowInteractionAllowed() override; 100 void PauseEvaluation() override; 101 void ResumeEvaluation() override; 102 bool HasFetchHandler() override; 103 104 // WorkerReportingProxy overrides: 105 void CountFeature(WebFeature) override; 106 void ReportException(const String& error_message, 107 std::unique_ptr<SourceLocation>, 108 int exception_id) override; 109 void ReportConsoleMessage(mojom::ConsoleMessageSource, 110 mojom::ConsoleMessageLevel, 111 const String& message, 112 SourceLocation*) override; 113 void WillInitializeWorkerContext() override; 114 void DidCreateWorkerGlobalScope(WorkerOrWorkletGlobalScope*) override; 115 void DidLoadClassicScript() override; 116 void DidFetchScript() override; 117 void DidFailToFetchClassicScript() override; 118 void DidFailToFetchModuleScript() override; 119 void WillEvaluateClassicScript(size_t script_size, 120 size_t cached_metadata_size) override; 121 void WillEvaluateImportedClassicScript(size_t script_size, 122 size_t cached_metadata_size) override; 123 void WillEvaluateModuleScript() override; 124 void DidEvaluateTopLevelScript(bool success) override; 125 void DidCloseWorkerGlobalScope() override; 126 void WillDestroyWorkerGlobalScope() override; 127 void DidTerminateWorkerThread() override; 128 bool IsServiceWorkerGlobalScopeProxy() const override; 129 130 // Called from ServiceWorkerGlobalScope. 131 void SetupNavigationPreload( 132 int fetch_event_id, 133 const KURL& url, 134 mojom::blink::FetchEventPreloadHandlePtr preload_handle); 135 void RequestTermination(WTF::CrossThreadOnceFunction<void(bool)> callback); 136 137 // Detaches this proxy object entirely from the outside world, clearing out 138 // all references. 139 // 140 // It is called on the main thread during WebEmbeddedWorkerImpl finalization 141 // _after_ the worker thread using the proxy has been terminated. 142 void Detach(); 143 144 void TerminateWorkerContext(); 145 146 private: 147 WebServiceWorkerContextClient& Client() const; 148 ServiceWorkerGlobalScope* WorkerGlobalScope() const; 149 150 // Non-null until the WebEmbeddedWorkerImpl explicitly detach()es 151 // as part of its finalization. 152 WebEmbeddedWorkerImpl* embedded_worker_; 153 154 scoped_refptr<base::SingleThreadTaskRunner> 155 parent_thread_default_task_runner_; 156 157 WebServiceWorkerContextClient* client_; 158 159 CrossThreadPersistent<ServiceWorkerGlobalScope> worker_global_scope_; 160 161 THREAD_CHECKER(worker_thread_checker_); 162 163 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerGlobalScopeProxy); 164 }; 165 166 // TODO(leonhsl): This is only used by ServiceWorkerGlobalScope for calling 167 // WebServiceWorkerContextClient::{SetupNavigationPreload,RequestTermination}(), 168 // which will be Onion Soupped eventually, at that time we'd remove this. 169 template <> 170 struct DowncastTraits<ServiceWorkerGlobalScopeProxy> { 171 static bool AllowFrom(const WorkerReportingProxy& proxy) { 172 return proxy.IsServiceWorkerGlobalScopeProxy(); 173 } 174 }; 175 176 } // namespace blink 177 178 #endif // THIRD_PARTY_BLINK_RENDERER_MODULES_SERVICE_WORKER_SERVICE_WORKER_GLOBAL_SCOPE_PROXY_H_ 179