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_dispatcher.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/strings/string16.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "content/child/child_process.h"
11 #include "content/child/scoped_child_process_reference.h"
12 #include "content/child/thread_safe_sender.h"
13 #include "content/child/worker_task_runner.h"
14 #include "content/common/devtools_messages.h"
15 #include "content/common/service_worker/embedded_worker_messages.h"
16 #include "content/public/common/content_client.h"
17 #include "content/renderer/render_thread_impl.h"
18 #include "content/renderer/service_worker/embedded_worker_devtools_agent.h"
19 #include "content/renderer/service_worker/service_worker_context_client.h"
20 #include "third_party/WebKit/public/platform/WebString.h"
21 #include "third_party/WebKit/public/platform/WebURL.h"
22 #include "third_party/WebKit/public/web/WebEmbeddedWorker.h"
23 #include "third_party/WebKit/public/web/WebEmbeddedWorkerStartData.h"
27 // A thin wrapper of WebEmbeddedWorker which also adds and releases process
28 // references automatically.
29 class EmbeddedWorkerDispatcher::WorkerWrapper
{
31 WorkerWrapper(blink::WebEmbeddedWorker
* worker
, int devtools_agent_route_id
)
34 new EmbeddedWorkerDevToolsAgent(worker
, devtools_agent_route_id
)) {}
37 blink::WebEmbeddedWorker
* worker() { return worker_
.get(); }
40 ScopedChildProcessReference process_ref_
;
41 scoped_ptr
<blink::WebEmbeddedWorker
> worker_
;
42 scoped_ptr
<EmbeddedWorkerDevToolsAgent
> dev_tools_agent_
;
45 EmbeddedWorkerDispatcher::EmbeddedWorkerDispatcher() : weak_factory_(this) {}
47 EmbeddedWorkerDispatcher::~EmbeddedWorkerDispatcher() {}
49 bool EmbeddedWorkerDispatcher::OnMessageReceived(
50 const IPC::Message
& message
) {
52 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDispatcher
, message
)
53 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker
, OnStartWorker
)
54 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker
, OnStopWorker
)
55 IPC_MESSAGE_UNHANDLED(handled
= false)
60 void EmbeddedWorkerDispatcher::WorkerContextDestroyed(
61 int embedded_worker_id
) {
62 RenderThreadImpl::current()->thread_safe_sender()->Send(
63 new EmbeddedWorkerHostMsg_WorkerStopped(embedded_worker_id
));
64 workers_
.Remove(embedded_worker_id
);
67 void EmbeddedWorkerDispatcher::OnStartWorker(
68 const EmbeddedWorkerMsg_StartWorker_Params
& params
) {
69 DCHECK(!workers_
.Lookup(params
.embedded_worker_id
));
70 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerDispatcher::OnStartWorker");
71 RenderThread::Get()->EnsureWebKitInitialized();
72 scoped_ptr
<WorkerWrapper
> wrapper(
73 new WorkerWrapper(blink::WebEmbeddedWorker::create(
74 new ServiceWorkerContextClient(
75 params
.embedded_worker_id
,
76 params
.service_worker_version_id
,
79 params
.worker_devtools_agent_route_id
),
81 params
.worker_devtools_agent_route_id
));
83 blink::WebEmbeddedWorkerStartData start_data
;
84 start_data
.scriptURL
= params
.script_url
;
85 start_data
.userAgent
= base::UTF8ToUTF16(GetContentClient()->GetUserAgent());
86 start_data
.waitForDebuggerMode
=
87 params
.wait_for_debugger
?
88 blink::WebEmbeddedWorkerStartData::WaitForDebugger
:
89 blink::WebEmbeddedWorkerStartData::DontWaitForDebugger
;
90 start_data
.v8CacheOptions
=
91 static_cast<blink::WebSettings::V8CacheOptions
>(params
.v8_cache_options
);
93 wrapper
->worker()->startWorkerContext(start_data
);
94 workers_
.AddWithID(wrapper
.release(), params
.embedded_worker_id
);
97 void EmbeddedWorkerDispatcher::OnStopWorker(int embedded_worker_id
) {
98 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerDispatcher::OnStopWorker");
99 WorkerWrapper
* wrapper
= workers_
.Lookup(embedded_worker_id
);
101 LOG(WARNING
) << "Got OnStopWorker for nonexistent worker";
105 // This should eventually call WorkerContextDestroyed. (We may need to post
106 // a delayed task to forcibly abort the worker context if we find it
108 wrapper
->worker()->terminateWorkerContext();
111 } // namespace content