Fix auto-uninstall of legacy multi-install Chrome Frame.
[chromium-blink-merge.git] / content / renderer / service_worker / embedded_worker_context_client.cc
blobe2f64994a8f7f18527fbee2460f43ecce552b10c
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 #include "content/renderer/service_worker/embedded_worker_context_client.h"
7 #include "base/lazy_instance.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "base/pickle.h"
10 #include "base/threading/thread_local.h"
11 #include "content/child/request_extra_data.h"
12 #include "content/child/service_worker/service_worker_network_provider.h"
13 #include "content/child/thread_safe_sender.h"
14 #include "content/child/worker_task_runner.h"
15 #include "content/child/worker_thread_task_runner.h"
16 #include "content/common/devtools_messages.h"
17 #include "content/common/service_worker/embedded_worker_messages.h"
18 #include "content/common/service_worker/service_worker_types.h"
19 #include "content/public/renderer/document_state.h"
20 #include "content/renderer/render_thread_impl.h"
21 #include "content/renderer/service_worker/embedded_worker_dispatcher.h"
22 #include "content/renderer/service_worker/service_worker_script_context.h"
23 #include "ipc/ipc_message_macros.h"
24 #include "third_party/WebKit/public/platform/WebServiceWorkerResponse.h"
25 #include "third_party/WebKit/public/platform/WebString.h"
26 #include "third_party/WebKit/public/web/WebDataSource.h"
27 #include "third_party/WebKit/public/web/WebServiceWorkerNetworkProvider.h"
29 namespace content {
31 namespace {
33 // For now client must be a per-thread instance.
34 // TODO(kinuko): This needs to be refactored when we start using thread pool
35 // or having multiple clients per one thread.
36 base::LazyInstance<base::ThreadLocalPointer<EmbeddedWorkerContextClient> >::
37 Leaky g_worker_client_tls = LAZY_INSTANCE_INITIALIZER;
39 void CallWorkerContextDestroyedOnMainThread(int embedded_worker_id) {
40 if (!RenderThreadImpl::current() ||
41 !RenderThreadImpl::current()->embedded_worker_dispatcher())
42 return;
43 RenderThreadImpl::current()->embedded_worker_dispatcher()->
44 WorkerContextDestroyed(embedded_worker_id);
47 // We store an instance of this class in the "extra data" of the WebDataSource
48 // and attach a ServiceWorkerNetworkProvider to it as base::UserData.
49 // (see createServiceWorkerNetworkProvider).
50 class DataSourceExtraData
51 : public blink::WebDataSource::ExtraData,
52 public base::SupportsUserData {
53 public:
54 DataSourceExtraData() {}
55 virtual ~DataSourceExtraData() {}
58 // Called on the main thread only and blink owns it.
59 class WebServiceWorkerNetworkProviderImpl
60 : public blink::WebServiceWorkerNetworkProvider {
61 public:
62 // Blink calls this method for each request starting with the main script,
63 // we tag them with the provider id.
64 virtual void willSendRequest(
65 blink::WebDataSource* data_source,
66 blink::WebURLRequest& request) {
67 ServiceWorkerNetworkProvider* provider =
68 ServiceWorkerNetworkProvider::FromDocumentState(
69 static_cast<DataSourceExtraData*>(data_source->extraData()));
70 scoped_ptr<RequestExtraData> extra_data(new RequestExtraData);
71 extra_data->set_service_worker_provider_id(provider->provider_id());
72 request.setExtraData(extra_data.release());
76 } // namespace
78 EmbeddedWorkerContextClient*
79 EmbeddedWorkerContextClient::ThreadSpecificInstance() {
80 return g_worker_client_tls.Pointer()->Get();
83 EmbeddedWorkerContextClient::EmbeddedWorkerContextClient(
84 int embedded_worker_id,
85 int64 service_worker_version_id,
86 const GURL& service_worker_scope,
87 const GURL& script_url,
88 int worker_devtools_agent_route_id)
89 : embedded_worker_id_(embedded_worker_id),
90 service_worker_version_id_(service_worker_version_id),
91 service_worker_scope_(service_worker_scope),
92 script_url_(script_url),
93 worker_devtools_agent_route_id_(worker_devtools_agent_route_id),
94 sender_(ChildThread::current()->thread_safe_sender()),
95 main_thread_proxy_(base::MessageLoopProxy::current()),
96 weak_factory_(this) {
99 EmbeddedWorkerContextClient::~EmbeddedWorkerContextClient() {
100 // g_worker_client_tls.Pointer()->Get() could be NULL if this gets
101 // deleted before workerContextStarted() is called.
102 g_worker_client_tls.Pointer()->Set(NULL);
105 bool EmbeddedWorkerContextClient::OnMessageReceived(
106 const IPC::Message& msg) {
107 bool handled = true;
108 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerContextClient, msg)
109 IPC_MESSAGE_HANDLER(EmbeddedWorkerContextMsg_MessageToWorker,
110 OnMessageToWorker)
111 IPC_MESSAGE_UNHANDLED(handled = false)
112 IPC_END_MESSAGE_MAP()
113 return handled;
116 void EmbeddedWorkerContextClient::Send(IPC::Message* message) {
117 sender_->Send(message);
120 blink::WebURL EmbeddedWorkerContextClient::scope() const {
121 return service_worker_scope_;
124 void EmbeddedWorkerContextClient::getClients(
125 blink::WebServiceWorkerClientsCallbacks* callbacks) {
126 DCHECK(script_context_);
127 script_context_->GetClientDocuments(callbacks);
130 void EmbeddedWorkerContextClient::workerContextFailedToStart() {
131 DCHECK(main_thread_proxy_->RunsTasksOnCurrentThread());
132 DCHECK(!script_context_);
134 RenderThreadImpl::current()->embedded_worker_dispatcher()->
135 WorkerContextDestroyed(embedded_worker_id_);
138 void EmbeddedWorkerContextClient::workerContextStarted(
139 blink::WebServiceWorkerContextProxy* proxy) {
140 DCHECK(!worker_task_runner_);
141 worker_task_runner_ = new WorkerThreadTaskRunner(
142 WorkerTaskRunner::Instance()->CurrentWorkerId());
143 DCHECK_NE(0, WorkerTaskRunner::Instance()->CurrentWorkerId());
144 DCHECK(g_worker_client_tls.Pointer()->Get() == NULL);
145 DCHECK(!script_context_);
146 g_worker_client_tls.Pointer()->Set(this);
147 script_context_.reset(new ServiceWorkerScriptContext(this, proxy));
149 // Schedule a task to send back WorkerStarted asynchronously,
150 // so that at the time we send it we can be sure that the worker
151 // script has been evaluated and worker run loop has been started.
152 worker_task_runner_->PostTask(
153 FROM_HERE,
154 base::Bind(&EmbeddedWorkerContextClient::SendWorkerStarted,
155 weak_factory_.GetWeakPtr()));
158 void EmbeddedWorkerContextClient::willDestroyWorkerContext() {
159 // At this point OnWorkerRunLoopStopped is already called, so
160 // worker_task_runner_->RunsTasksOnCurrentThread() returns false
161 // (while we're still on the worker thread).
162 script_context_.reset();
164 #if !defined(HAS_SERVICE_WORKER_CONTEXT_DESTROYED)
165 // TODO(kinuko): Remove this after blink side is landed.
166 main_thread_proxy_->PostTask(
167 FROM_HERE,
168 base::Bind(&CallWorkerContextDestroyedOnMainThread,
169 embedded_worker_id_));
170 #endif
173 void EmbeddedWorkerContextClient::workerContextDestroyed() {
174 // TODO(kinuko): Remove this ifdef after blink side is landed.
175 #ifdef HAS_SERVICE_WORKER_CONTEXT_DESTROYED
176 // Now we should be able to free the WebEmbeddedWorker container on the
177 // main thread.
178 main_thread_proxy_->PostTask(
179 FROM_HERE,
180 base::Bind(&CallWorkerContextDestroyedOnMainThread,
181 embedded_worker_id_));
182 #endif
185 void EmbeddedWorkerContextClient::reportException(
186 const blink::WebString& error_message,
187 int line_number,
188 int column_number,
189 const blink::WebString& source_url) {
190 Send(new EmbeddedWorkerHostMsg_ReportException(
191 embedded_worker_id_, error_message, line_number,
192 column_number, GURL(source_url)));
195 void EmbeddedWorkerContextClient::reportConsoleMessage(
196 int source,
197 int level,
198 const blink::WebString& message,
199 int line_number,
200 const blink::WebString& source_url) {
201 EmbeddedWorkerHostMsg_ReportConsoleMessage_Params params;
202 params.source_identifier = source;
203 params.message_level = level;
204 params.message = message;
205 params.line_number = line_number;
206 params.source_url = GURL(source_url);
208 Send(new EmbeddedWorkerHostMsg_ReportConsoleMessage(
209 embedded_worker_id_, params));
212 void EmbeddedWorkerContextClient::dispatchDevToolsMessage(
213 const blink::WebString& message) {
214 sender_->Send(new DevToolsClientMsg_DispatchOnInspectorFrontend(
215 worker_devtools_agent_route_id_, message.utf8()));
218 void EmbeddedWorkerContextClient::saveDevToolsAgentState(
219 const blink::WebString& state) {
220 sender_->Send(new DevToolsHostMsg_SaveAgentRuntimeState(
221 worker_devtools_agent_route_id_, state.utf8()));
224 void EmbeddedWorkerContextClient::didHandleActivateEvent(
225 int request_id,
226 blink::WebServiceWorkerEventResult result) {
227 DCHECK(script_context_);
228 script_context_->DidHandleActivateEvent(request_id, result);
231 void EmbeddedWorkerContextClient::didHandleInstallEvent(
232 int request_id,
233 blink::WebServiceWorkerEventResult result) {
234 DCHECK(script_context_);
235 script_context_->DidHandleInstallEvent(request_id, result);
238 void EmbeddedWorkerContextClient::didHandleFetchEvent(int request_id) {
239 DCHECK(script_context_);
240 script_context_->DidHandleFetchEvent(
241 request_id,
242 SERVICE_WORKER_FETCH_EVENT_RESULT_FALLBACK,
243 ServiceWorkerResponse());
246 void EmbeddedWorkerContextClient::didHandleFetchEvent(
247 int request_id,
248 const blink::WebServiceWorkerResponse& web_response) {
249 DCHECK(script_context_);
250 ServiceWorkerResponse response(web_response.statusCode(),
251 web_response.statusText().utf8(),
252 web_response.method().utf8(),
253 std::map<std::string, std::string>());
254 script_context_->DidHandleFetchEvent(
255 request_id, SERVICE_WORKER_FETCH_EVENT_RESULT_RESPONSE, response);
258 void EmbeddedWorkerContextClient::didHandleSyncEvent(int request_id) {
259 DCHECK(script_context_);
260 script_context_->DidHandleSyncEvent(request_id);
263 blink::WebServiceWorkerNetworkProvider*
264 EmbeddedWorkerContextClient::createServiceWorkerNetworkProvider(
265 blink::WebDataSource* data_source) {
266 // Create a content::ServiceWorkerNetworkProvider for this data source so
267 // we can observe its requests.
268 scoped_ptr<ServiceWorkerNetworkProvider> provider(
269 new ServiceWorkerNetworkProvider());
271 // Tell the network provider about which version to load.
272 provider->SetServiceWorkerVersionId(service_worker_version_id_);
274 // The provider is kept around for the lifetime of the DataSource
275 // and ownership is transferred to the DataSource.
276 DataSourceExtraData* extra_data = new DataSourceExtraData();
277 data_source->setExtraData(extra_data);
278 ServiceWorkerNetworkProvider::AttachToDocumentState(
279 extra_data, provider.Pass());
281 // Blink is responsible for deleting the returned object.
282 return new WebServiceWorkerNetworkProviderImpl();
285 void EmbeddedWorkerContextClient::OnMessageToWorker(
286 int thread_id,
287 int embedded_worker_id,
288 const IPC::Message& message) {
289 if (!script_context_)
290 return;
291 DCHECK_EQ(embedded_worker_id_, embedded_worker_id);
292 script_context_->OnMessageReceived(message);
295 void EmbeddedWorkerContextClient::SendWorkerStarted() {
296 DCHECK(worker_task_runner_->RunsTasksOnCurrentThread());
297 Send(new EmbeddedWorkerHostMsg_WorkerStarted(
298 WorkerTaskRunner::Instance()->CurrentWorkerId(),
299 embedded_worker_id_));
302 } // namespace content