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_context_client.h"
19 #include "content/renderer/service_worker/embedded_worker_devtools_agent.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_HANDLER(EmbeddedWorkerMsg_ResumeAfterDownload
,
56 OnResumeAfterDownload
)
57 IPC_MESSAGE_UNHANDLED(handled
= false)
62 void EmbeddedWorkerDispatcher::WorkerContextDestroyed(
63 int embedded_worker_id
) {
64 RenderThreadImpl::current()->thread_safe_sender()->Send(
65 new EmbeddedWorkerHostMsg_WorkerStopped(embedded_worker_id
));
66 workers_
.Remove(embedded_worker_id
);
69 void EmbeddedWorkerDispatcher::OnStartWorker(
70 const EmbeddedWorkerMsg_StartWorker_Params
& params
) {
71 DCHECK(!workers_
.Lookup(params
.embedded_worker_id
));
72 RenderThread::Get()->EnsureWebKitInitialized();
73 scoped_ptr
<WorkerWrapper
> wrapper(
74 new WorkerWrapper(blink::WebEmbeddedWorker::create(
75 new EmbeddedWorkerContextClient(
76 params
.embedded_worker_id
,
77 params
.service_worker_version_id
,
80 params
.worker_devtools_agent_route_id
),
82 params
.worker_devtools_agent_route_id
));
84 blink::WebEmbeddedWorkerStartData start_data
;
85 start_data
.scriptURL
= params
.script_url
;
86 start_data
.userAgent
= base::UTF8ToUTF16(GetContentClient()->GetUserAgent());
87 start_data
.pauseAfterDownloadMode
=
88 params
.pause_after_download
?
89 blink::WebEmbeddedWorkerStartData::PauseAfterDownload
:
90 blink::WebEmbeddedWorkerStartData::DontPauseAfterDownload
;
91 start_data
.waitForDebuggerMode
=
92 params
.wait_for_debugger
?
93 blink::WebEmbeddedWorkerStartData::WaitForDebugger
:
94 blink::WebEmbeddedWorkerStartData::DontWaitForDebugger
;
96 // TODO(michaeln): delete setting this deprecated member once blink
98 start_data
.startMode
=
99 params
.wait_for_debugger
?
100 blink::WebEmbeddedWorkerStartModePauseOnStart
:
101 blink::WebEmbeddedWorkerStartModeDontPauseOnStart
;
103 wrapper
->worker()->startWorkerContext(start_data
);
104 workers_
.AddWithID(wrapper
.release(), params
.embedded_worker_id
);
107 void EmbeddedWorkerDispatcher::OnStopWorker(int embedded_worker_id
) {
108 WorkerWrapper
* wrapper
= workers_
.Lookup(embedded_worker_id
);
110 LOG(WARNING
) << "Got OnStopWorker for nonexistent worker";
114 // This should eventually call WorkerContextDestroyed. (We may need to post
115 // a delayed task to forcibly abort the worker context if we find it
117 wrapper
->worker()->terminateWorkerContext();
120 void EmbeddedWorkerDispatcher::OnResumeAfterDownload(int embedded_worker_id
) {
121 WorkerWrapper
* wrapper
= workers_
.Lookup(embedded_worker_id
);
123 LOG(WARNING
) << "Got OnResumeAfterDownload for nonexistent worker";
126 wrapper
->worker()->resumeAfterDownload();
129 } // namespace content