Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / service_worker / embedded_worker_dispatcher.cc
blob8fcea3b6ffc1f00d0f615b0a236d2de05684c584
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/metrics/histogram_macros.h"
9 #include "base/strings/string16.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "content/child/child_process.h"
12 #include "content/child/scoped_child_process_reference.h"
13 #include "content/child/thread_safe_sender.h"
14 #include "content/child/worker_task_runner.h"
15 #include "content/common/devtools_messages.h"
16 #include "content/common/service_worker/embedded_worker_messages.h"
17 #include "content/public/common/content_client.h"
18 #include "content/renderer/render_thread_impl.h"
19 #include "content/renderer/service_worker/embedded_worker_devtools_agent.h"
20 #include "content/renderer/service_worker/service_worker_context_client.h"
21 #include "third_party/WebKit/public/platform/WebString.h"
22 #include "third_party/WebKit/public/platform/WebURL.h"
23 #include "third_party/WebKit/public/web/WebEmbeddedWorker.h"
24 #include "third_party/WebKit/public/web/WebEmbeddedWorkerStartData.h"
26 namespace content {
28 // A thin wrapper of WebEmbeddedWorker which also adds and releases process
29 // references automatically.
30 class EmbeddedWorkerDispatcher::WorkerWrapper {
31 public:
32 WorkerWrapper(blink::WebEmbeddedWorker* worker, int devtools_agent_route_id)
33 : worker_(worker),
34 dev_tools_agent_(
35 new EmbeddedWorkerDevToolsAgent(worker, devtools_agent_route_id)) {}
36 ~WorkerWrapper() {}
38 blink::WebEmbeddedWorker* worker() { return worker_.get(); }
40 private:
41 ScopedChildProcessReference process_ref_;
42 scoped_ptr<blink::WebEmbeddedWorker> worker_;
43 scoped_ptr<EmbeddedWorkerDevToolsAgent> dev_tools_agent_;
46 EmbeddedWorkerDispatcher::EmbeddedWorkerDispatcher() : weak_factory_(this) {}
48 EmbeddedWorkerDispatcher::~EmbeddedWorkerDispatcher() {}
50 bool EmbeddedWorkerDispatcher::OnMessageReceived(
51 const IPC::Message& message) {
52 bool handled = true;
53 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDispatcher, message)
54 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StartWorker, OnStartWorker)
55 IPC_MESSAGE_HANDLER(EmbeddedWorkerMsg_StopWorker, OnStopWorker)
56 IPC_MESSAGE_UNHANDLED(handled = false)
57 IPC_END_MESSAGE_MAP()
58 return handled;
61 void EmbeddedWorkerDispatcher::WorkerContextDestroyed(
62 int embedded_worker_id) {
63 if (ContainsKey(stop_worker_times_, embedded_worker_id)) {
64 base::TimeTicks stop_time = stop_worker_times_[embedded_worker_id];
65 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.TerminateThread.Time",
66 base::TimeTicks::Now() - stop_time);
67 stop_worker_times_.erase(embedded_worker_id);
70 RenderThreadImpl::current()->thread_safe_sender()->Send(
71 new EmbeddedWorkerHostMsg_WorkerStopped(embedded_worker_id));
72 workers_.Remove(embedded_worker_id);
75 void EmbeddedWorkerDispatcher::OnStartWorker(
76 const EmbeddedWorkerMsg_StartWorker_Params& params) {
77 DCHECK(!workers_.Lookup(params.embedded_worker_id));
78 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerDispatcher::OnStartWorker");
79 RenderThread::Get()->EnsureWebKitInitialized();
80 scoped_ptr<WorkerWrapper> wrapper(
81 new WorkerWrapper(blink::WebEmbeddedWorker::create(
82 new ServiceWorkerContextClient(
83 params.embedded_worker_id,
84 params.service_worker_version_id,
85 params.scope,
86 params.script_url,
87 params.worker_devtools_agent_route_id),
88 NULL),
89 params.worker_devtools_agent_route_id));
91 blink::WebEmbeddedWorkerStartData start_data;
92 start_data.scriptURL = params.script_url;
93 start_data.userAgent = base::UTF8ToUTF16(GetContentClient()->GetUserAgent());
94 start_data.waitForDebuggerMode =
95 params.wait_for_debugger ?
96 blink::WebEmbeddedWorkerStartData::WaitForDebugger :
97 blink::WebEmbeddedWorkerStartData::DontWaitForDebugger;
98 start_data.v8CacheOptions =
99 static_cast<blink::WebSettings::V8CacheOptions>(params.v8_cache_options);
101 wrapper->worker()->startWorkerContext(start_data);
102 workers_.AddWithID(wrapper.release(), params.embedded_worker_id);
105 void EmbeddedWorkerDispatcher::OnStopWorker(int embedded_worker_id) {
106 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerDispatcher::OnStopWorker");
107 WorkerWrapper* wrapper = workers_.Lookup(embedded_worker_id);
108 if (!wrapper) {
109 LOG(WARNING) << "Got OnStopWorker for nonexistent worker";
110 return;
113 // This should eventually call WorkerContextDestroyed. (We may need to post
114 // a delayed task to forcibly abort the worker context if we find it
115 // necessary)
116 stop_worker_times_[embedded_worker_id] = base::TimeTicks::Now();
117 wrapper->worker()->terminateWorkerContext();
120 } // namespace content