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 WebServiceWorkerContextClient_h 32 #define WebServiceWorkerContextClient_h 33 34 #include "WebWorkerPermissionClientProxy.h" 35 #include "public/platform/WebMessagePortChannel.h" 36 #include "public/platform/WebServiceWorkerClientsInfo.h" 37 #include "public/platform/WebServiceWorkerEventResult.h" 38 #include "public/platform/WebURL.h" 39 40 namespace blink { 41 42 class WebDataSource; 43 class WebServiceWorkerContextProxy; 44 class WebServiceWorkerNetworkProvider; 45 class WebServiceWorkerResponse; 46 class WebString; 47 48 // This interface is implemented by the client. It is supposed to be created 49 // on the main thread and then passed on to the worker thread. 50 // by a newly created WorkerGlobalScope. All methods of this class, except 51 // for createServiceWorkerNetworkProvider() and workerContextFailedToStart(), 52 // are called on the worker thread. 53 // FIXME: Split this into EmbeddedWorkerContextClient and 54 // ServiceWorkerScriptContextClient when we decide to use EmbeddedWorker 55 // framework for other implementation (like SharedWorker). 56 class WebServiceWorkerContextClient { 57 public: ~WebServiceWorkerContextClient()58 virtual ~WebServiceWorkerContextClient() { } 59 60 // ServiceWorker specific method. Called when script accesses the 61 // the |scope| attribute of the ServiceWorkerGlobalScope. Immutable per spec. scope()62 virtual WebURL scope() const { return WebURL(); } 63 64 // If the worker was started with WebEmbeddedWorkerStartData indicating to pause 65 // after download, this method is called after the main script resource has been 66 // downloaded. The scope will not be created and the script will not be loaded until 67 // WebEmbeddedWorker.resumeAfterDownload() is invoked. didPauseAfterDownload()68 virtual void didPauseAfterDownload() { } 69 70 // A new WorkerGlobalScope is created and started to run on the 71 // worker thread. 72 // This also gives back a proxy to the client to talk to the 73 // newly created WorkerGlobalScope. The proxy is held by WorkerGlobalScope 74 // and should not be held by the caller. No proxy methods should be called 75 // after willDestroyWorkerContext() is called. workerContextStarted(WebServiceWorkerContextProxy *)76 virtual void workerContextStarted(WebServiceWorkerContextProxy*) { } 77 78 // WorkerGlobalScope is about to be destroyed. The client should clear 79 // the WebServiceWorkerGlobalScopeProxy when this is called. willDestroyWorkerContext()80 virtual void willDestroyWorkerContext() { } 81 82 // WorkerGlobalScope is destroyed and the worker is ready to be terminated. workerContextDestroyed()83 virtual void workerContextDestroyed() { } 84 85 // Starting worker context is failed. This could happen when loading 86 // worker script fails, or is asked to terminated before the context starts. 87 // This is called on the main thread. workerContextFailedToStart()88 virtual void workerContextFailedToStart() { } 89 90 // Called when the WorkerGlobalScope had an error or an exception. reportException(const WebString & errorMessage,int lineNumber,int columnNumber,const WebString & sourceURL)91 virtual void reportException(const WebString& errorMessage, int lineNumber, int columnNumber, const WebString& sourceURL) { } 92 93 // Called when the console message is reported. reportConsoleMessage(int source,int level,const WebString & message,int lineNumber,const WebString & sourceURL)94 virtual void reportConsoleMessage(int source, int level, const WebString& message, int lineNumber, const WebString& sourceURL) { } 95 96 // Inspector related messages. dispatchDevToolsMessage(const WebString &)97 virtual void dispatchDevToolsMessage(const WebString&) { } saveDevToolsAgentState(const WebString &)98 virtual void saveDevToolsAgentState(const WebString&) { } 99 100 // ServiceWorker specific method. didHandleActivateEvent(int eventID,blink::WebServiceWorkerEventResult result)101 virtual void didHandleActivateEvent(int eventID, blink::WebServiceWorkerEventResult result) { } 102 103 // ServiceWorker specific method. Called after InstallEvent (dispatched 104 // via WebServiceWorkerContextProxy) is handled by the ServiceWorker's 105 // script context. didHandleInstallEvent(int installEventID,blink::WebServiceWorkerEventResult result)106 virtual void didHandleInstallEvent(int installEventID, blink::WebServiceWorkerEventResult result) { } 107 108 // ServiceWorker specific methods. Called after FetchEvent is handled by the 109 // ServiceWorker's script context. When no response is provided, the browser 110 // should fallback to native fetch. didHandleFetchEvent(int fetchEventID)111 virtual void didHandleFetchEvent(int fetchEventID) { } didHandleFetchEvent(int fetchEventID,const WebServiceWorkerResponse & response)112 virtual void didHandleFetchEvent(int fetchEventID, const WebServiceWorkerResponse& response) { } 113 114 // ServiceWorker specific method. Called after SyncEvent (dispatched via 115 // WebServiceWorkerContextProxy) is handled by the ServiceWorker's script 116 // context. didHandleSyncEvent(int syncEventID)117 virtual void didHandleSyncEvent(int syncEventID) { } 118 119 // Ownership of the returned object is transferred to the caller. createServiceWorkerNetworkProvider(blink::WebDataSource *)120 virtual WebServiceWorkerNetworkProvider* createServiceWorkerNetworkProvider(blink::WebDataSource*) { return 0; } 121 122 // Ownership of the passed callbacks is transferred to the callee, callee 123 // should delete the callbacks after calling either onSuccess or onError. 124 // WebServiceWorkerClientsInfo and WebServiceWorkerError ownerships are 125 // passed to the WebServiceWorkerClientsCallbacks implementation. getClients(WebServiceWorkerClientsCallbacks *)126 virtual void getClients(WebServiceWorkerClientsCallbacks*) { BLINK_ASSERT_NOT_REACHED(); } 127 128 // Callee receives ownership of the passed vector. 129 // FIXME: Blob refs should be passed to maintain ref counts. crbug.com/351753 postMessageToClient(int clientID,const WebString &,WebMessagePortChannelArray *)130 virtual void postMessageToClient(int clientID, const WebString&, WebMessagePortChannelArray*) { BLINK_ASSERT_NOT_REACHED(); } 131 }; 132 133 } // namespace blink 134 135 #endif // WebWorkerContextClient_h 136