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_CHILD_SERVICE_WORKER_EMBEDDED_WORKER_CLIENT_H_
6 #define CONTENT_CHILD_SERVICE_WORKER_EMBEDDED_WORKER_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 // TODO(kinuko): Deprecate this.
64 void SendReplyToBrowser(int request_id
, const IPC::Message
& message
);
66 // WebServiceWorkerContextClient overrides, some of them are just dispatched
67 // on to script_context_.
68 virtual blink::WebURL
scope() const;
69 virtual void getClients(blink::WebServiceWorkerClientsCallbacks
*);
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
);
97 // TODO: Implement DevTools related method overrides.
99 int embedded_worker_id() const { return embedded_worker_id_
; }
100 base::MessageLoopProxy
* main_thread_proxy() const {
101 return main_thread_proxy_
;
105 void OnMessageToWorker(int thread_id
,
106 int embedded_worker_id
,
107 const IPC::Message
& message
);
108 void SendWorkerStarted();
110 const int embedded_worker_id_
;
111 const int64 service_worker_version_id_
;
112 const GURL service_worker_scope_
;
113 const GURL script_url_
;
114 const int worker_devtools_agent_route_id_
;
115 scoped_refptr
<ThreadSafeSender
> sender_
;
116 scoped_refptr
<base::MessageLoopProxy
> main_thread_proxy_
;
117 scoped_refptr
<base::TaskRunner
> worker_task_runner_
;
119 scoped_ptr
<ServiceWorkerScriptContext
> script_context_
;
121 base::WeakPtrFactory
<EmbeddedWorkerContextClient
> weak_factory_
;
123 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerContextClient
);
126 } // namespace content
128 #endif // CONTENT_CHILD_SERVICE_WORKER_EMBEDDED_WORKER_CLIENT_H_