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