1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_CONTEXT_CLIENT_H_
6 #define CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_CONTEXT_CLIENT_H_
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/strings/string16.h"
11 #include "content/common/service_worker/service_worker_types.h"
12 #include "ipc/ipc_listener.h"
13 #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
14 #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h"
15 #include "third_party/WebKit/public/platform/WebURL.h"
16 #include "third_party/WebKit/public/web/WebServiceWorkerContextClient.h"
20 class MessageLoopProxy
;
30 class ServiceWorkerScriptContext
;
31 class ThreadSafeSender
;
33 // This class provides access to/from an embedded worker's WorkerGlobalScope.
34 // All methods other than the constructor (it's created on the main thread)
35 // and createServiceWorkerNetworkProvider (also called on the main thread)
36 // are called on the worker thread.
38 // TODO(kinuko): Currently EW/SW separation is made a little hazily.
39 // This should implement WebEmbeddedWorkerContextClient
40 // or sort of it (which doesn't exist yet) rather than
41 // WebServiceWorkerContextClient if we want to separate them more cleanly,
42 // or ServiceWorkerScriptContext should be merged into this class
43 // if we consider EW == SW script context.
44 class EmbeddedWorkerContextClient
45 : public blink::WebServiceWorkerContextClient
{
47 // Returns a thread-specific client instance. This does NOT create a
49 static EmbeddedWorkerContextClient
* ThreadSpecificInstance();
51 EmbeddedWorkerContextClient(int embedded_worker_id
,
52 int64 service_worker_version_id
,
53 const GURL
& service_worker_scope
,
54 const GURL
& script_url
,
55 int worker_devtools_agent_route_id
);
57 virtual ~EmbeddedWorkerContextClient();
59 bool OnMessageReceived(const IPC::Message
& msg
);
61 void Send(IPC::Message
* message
);
63 // WebServiceWorkerContextClient overrides, some of them are just dispatched
64 // on to script_context_.
65 virtual blink::WebURL
scope() const;
66 virtual blink::WebServiceWorkerCacheStorage
* cacheStorage();
67 virtual void didPauseAfterDownload();
68 virtual void getClients(blink::WebServiceWorkerClientsCallbacks
*);
69 virtual void workerReadyForInspection();
70 virtual void workerContextFailedToStart();
71 virtual void workerContextStarted(blink::WebServiceWorkerContextProxy
* proxy
);
72 virtual void willDestroyWorkerContext();
73 virtual void workerContextDestroyed();
74 virtual void reportException(const blink::WebString
& error_message
,
77 const blink::WebString
& source_url
);
78 virtual void reportConsoleMessage(int source
,
80 const blink::WebString
& message
,
82 const blink::WebString
& source_url
);
83 virtual void dispatchDevToolsMessage(const blink::WebString
&);
84 virtual void saveDevToolsAgentState(const blink::WebString
&);
85 virtual void didHandleActivateEvent(int request_id
,
86 blink::WebServiceWorkerEventResult
);
87 virtual void didHandleInstallEvent(int request_id
,
88 blink::WebServiceWorkerEventResult result
);
89 virtual void didHandleFetchEvent(int request_id
);
90 virtual void didHandleFetchEvent(
92 const blink::WebServiceWorkerResponse
& response
);
93 virtual void didHandleSyncEvent(int request_id
);
94 virtual blink::WebServiceWorkerNetworkProvider
*
95 createServiceWorkerNetworkProvider(blink::WebDataSource
* data_source
);
96 virtual void postMessageToClient(
98 const blink::WebString
& message
,
99 blink::WebMessagePortChannelArray
* channels
);
101 // TODO: Implement DevTools related method overrides.
103 int embedded_worker_id() const { return embedded_worker_id_
; }
104 base::MessageLoopProxy
* main_thread_proxy() const {
105 return main_thread_proxy_
.get();
107 ThreadSafeSender
* thread_safe_sender() { return sender_
.get(); }
110 void OnMessageToWorker(int thread_id
,
111 int embedded_worker_id
,
112 const IPC::Message
& message
);
113 void SendWorkerStarted();
115 const int embedded_worker_id_
;
116 const int64 service_worker_version_id_
;
117 const GURL service_worker_scope_
;
118 const GURL script_url_
;
119 const int worker_devtools_agent_route_id_
;
120 scoped_refptr
<ThreadSafeSender
> sender_
;
121 scoped_refptr
<base::MessageLoopProxy
> main_thread_proxy_
;
122 scoped_refptr
<base::TaskRunner
> worker_task_runner_
;
124 scoped_ptr
<ServiceWorkerScriptContext
> script_context_
;
126 base::WeakPtrFactory
<EmbeddedWorkerContextClient
> weak_factory_
;
128 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerContextClient
);
131 } // namespace content
133 #endif // CONTENT_RENDERER_SERVICE_WORKER_EMBEDDED_WORKER_CONTEXT_CLIENT_H_