[tracing] Add browser-side discardable memory reporting
[chromium-blink-merge.git] / content / renderer / service_worker / embedded_worker_dispatcher.cc
blob0a5c3fd875e0e72b03cc405e9b5324cd791e4c06
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"
25 namespace content {
27 // A thin wrapper of WebEmbeddedWorker which also adds and releases process
28 // references automatically.
29 class EmbeddedWorkerDispatcher::WorkerWrapper {
30 public:
31 WorkerWrapper(blink::WebEmbeddedWorker* worker, int devtools_agent_route_id)
32 : worker_(worker),
33 dev_tools_agent_(
34 new EmbeddedWorkerDevToolsAgent(worker, devtools_agent_route_id)) {}
35 ~WorkerWrapper() {}
37 blink::WebEmbeddedWorker* worker() { return worker_.get(); }
39 private:
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) {
51 bool handled = true;
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)
58 IPC_END_MESSAGE_MAP()
59 return handled;
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 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerDispatcher::OnStartWorker");
73 RenderThread::Get()->EnsureWebKitInitialized();
74 scoped_ptr<WorkerWrapper> wrapper(
75 new WorkerWrapper(blink::WebEmbeddedWorker::create(
76 new ServiceWorkerContextClient(
77 params.embedded_worker_id,
78 params.service_worker_version_id,
79 params.scope,
80 params.script_url,
81 params.worker_devtools_agent_route_id),
82 NULL),
83 params.worker_devtools_agent_route_id));
85 blink::WebEmbeddedWorkerStartData start_data;
86 start_data.scriptURL = params.script_url;
87 start_data.userAgent = base::UTF8ToUTF16(GetContentClient()->GetUserAgent());
88 start_data.pauseAfterDownloadMode =
89 params.pause_after_download ?
90 blink::WebEmbeddedWorkerStartData::PauseAfterDownload :
91 blink::WebEmbeddedWorkerStartData::DontPauseAfterDownload;
92 start_data.waitForDebuggerMode =
93 params.wait_for_debugger ?
94 blink::WebEmbeddedWorkerStartData::WaitForDebugger :
95 blink::WebEmbeddedWorkerStartData::DontWaitForDebugger;
96 start_data.v8CacheOptions =
97 static_cast<blink::WebSettings::V8CacheOptions>(params.v8_cache_options);
99 wrapper->worker()->startWorkerContext(start_data);
100 workers_.AddWithID(wrapper.release(), params.embedded_worker_id);
103 void EmbeddedWorkerDispatcher::OnStopWorker(int embedded_worker_id) {
104 TRACE_EVENT0("ServiceWorker", "EmbeddedWorkerDispatcher::OnStopWorker");
105 WorkerWrapper* wrapper = workers_.Lookup(embedded_worker_id);
106 if (!wrapper) {
107 LOG(WARNING) << "Got OnStopWorker for nonexistent worker";
108 return;
111 // This should eventually call WorkerContextDestroyed. (We may need to post
112 // a delayed task to forcibly abort the worker context if we find it
113 // necessary)
114 wrapper->worker()->terminateWorkerContext();
117 void EmbeddedWorkerDispatcher::OnResumeAfterDownload(int embedded_worker_id) {
118 TRACE_EVENT0("ServiceWorker",
119 "EmbeddedWorkerDispatcher::OnResumeAfterDownload");
120 WorkerWrapper* wrapper = workers_.Lookup(embedded_worker_id);
121 if (!wrapper) {
122 LOG(WARNING) << "Got OnResumeAfterDownload for nonexistent worker";
123 return;
125 wrapper->worker()->resumeAfterDownload();
128 } // namespace content