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/common/service_worker/embedded_worker_messages.h"
14 #include "content/renderer/render_thread_impl.h"
15 #include "content/renderer/service_worker/embedded_worker_context_client.h"
16 #include "third_party/WebKit/public/platform/WebString.h"
17 #include "third_party/WebKit/public/platform/WebURL.h"
18 #include "third_party/WebKit/public/web/WebEmbeddedWorker.h"
19 #include "third_party/WebKit/public/web/WebEmbeddedWorkerStartData.h"
20 #include "webkit/child/worker_task_runner.h"
21 #include "webkit/common/user_agent/user_agent.h"
25 // A thin wrapper of WebEmbeddedWorker which also adds and releases process
26 // references automatically.
27 class EmbeddedWorkerDispatcher::WorkerWrapper
{
29 explicit WorkerWrapper(blink::WebEmbeddedWorker
* worker
) : worker_(worker
) {}
32 blink::WebEmbeddedWorker
* worker() { return worker_
.get(); }
35 ScopedChildProcessReference process_ref_
;
36 scoped_ptr
<blink::WebEmbeddedWorker
> worker_
;
39 EmbeddedWorkerDispatcher::EmbeddedWorkerDispatcher() : weak_factory_(this) {}
41 EmbeddedWorkerDispatcher::~EmbeddedWorkerDispatcher() {}
43 bool EmbeddedWorkerDispatcher::OnMessageReceived(
44 const IPC::Message
& message
) {
46 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDispatcher
, message
)
47 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker
, OnStartWorker
)
48 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker
, OnStopWorker
)
49 IPC_MESSAGE_UNHANDLED(handled
= false)
54 void EmbeddedWorkerDispatcher::WorkerContextDestroyed(
55 int embedded_worker_id
) {
56 RenderThreadImpl::current()->thread_safe_sender()->Send(
57 new EmbeddedWorkerHostMsg_WorkerStopped(embedded_worker_id
));
58 workers_
.Remove(embedded_worker_id
);
61 void EmbeddedWorkerDispatcher::OnStartWorker(
62 int embedded_worker_id
,
63 int64 service_worker_version_id
,
64 const GURL
& script_url
) {
65 DCHECK(!workers_
.Lookup(embedded_worker_id
));
66 scoped_ptr
<WorkerWrapper
> wrapper(new WorkerWrapper(
67 blink::WebEmbeddedWorker::create(
68 new EmbeddedWorkerContextClient(
70 service_worker_version_id
,
74 blink::WebEmbeddedWorkerStartData start_data
;
75 start_data
.scriptURL
= script_url
;
76 start_data
.userAgent
=
77 base::UTF8ToUTF16(webkit_glue::GetUserAgent(script_url
));
79 wrapper
->worker()->startWorkerContext(start_data
);
80 workers_
.AddWithID(wrapper
.release(), embedded_worker_id
);
83 void EmbeddedWorkerDispatcher::OnStopWorker(int embedded_worker_id
) {
84 WorkerWrapper
* wrapper
= workers_
.Lookup(embedded_worker_id
);
86 LOG(WARNING
) << "Got OnStopWorker for nonexistent worker";
90 // This should eventually call WorkerContextDestroyed. (We may need to post
91 // a delayed task to forcibly abort the worker context if we find it
93 wrapper
->worker()->terminateWorkerContext();
96 } // namespace content