Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(101)

Side by Side Diff: content/renderer/service_worker/embedded_worker_dispatcher.cc

Issue 54573002: Initial child-process side plumbing for starting an embedded ServiceWorker context (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: WebKit -> blink etc Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "content/renderer/service_worker/embedded_worker_dispatcher.h"
6
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/thread_safe_sender.h"
12 #include "content/common/service_worker_messages.h"
13 #include "content/renderer/render_thread_impl.h"
14 #include "content/renderer/service_worker/service_worker_context_client.h"
15 #include "third_party/WebKit/public/platform/WebString.h"
16 #include "third_party/WebKit/public/platform/WebURL.h"
17 #include "third_party/WebKit/public/web/WebEmbeddedWorker.h"
18 #include "third_party/WebKit/public/web/WebEmbeddedWorkerStartData.h"
19 #include "webkit/child/worker_task_runner.h"
20 #include "webkit/common/user_agent/user_agent.h"
21
22 namespace content {
23
24 static const int kMaxTimeForRunawayWorkerSeconds = 3;
25
26 // A thin wrapper of WebEmbeddedWorker which also adds and releases process
27 // references automatically.
28 class EmbeddedWorkerDispatcher::WorkerWrapper {
29 public:
30 explicit WorkerWrapper(blink::WebEmbeddedWorker* worker)
31 : worker_(worker) {
32 ChildProcess::current()->AddRefProcess();
33 }
34
35 ~WorkerWrapper() {
36 ChildProcess::current()->ReleaseProcess();
37 }
38
39 blink::WebEmbeddedWorker* worker() { return worker_.get(); }
40
41 private:
42 scoped_ptr<blink::WebEmbeddedWorker> worker_;
43 };
44
45 EmbeddedWorkerDispatcher::EmbeddedWorkerDispatcher() : weak_factory_(this) {}
46
47 EmbeddedWorkerDispatcher::~EmbeddedWorkerDispatcher() {}
48
49 bool EmbeddedWorkerDispatcher::OnMessageReceived(
50 const IPC::Message& message) {
51 bool handled = true;
52 IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDispatcher, message)
53 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_StartWorker, OnStartWorker)
54 IPC_MESSAGE_HANDLER(ServiceWorkerMsg_TerminateWorker, OnTerminateWorker)
55 IPC_MESSAGE_UNHANDLED(handled = false)
56 IPC_END_MESSAGE_MAP()
57 return handled;
58 }
59
60 void EmbeddedWorkerDispatcher::WorkerContextDestroyed(
61 int32 embedded_worker_id) {
62 // There's a chance that this gets called multiple times.
63 // (And Remove internally DCHECK if we attempt to delete nonexistent one)
64 if (workers_.Lookup(embedded_worker_id))
65 workers_.Remove(embedded_worker_id);
66 }
67
68 void EmbeddedWorkerDispatcher::OnStartWorker(
69 const ServiceWorkerMsg_StartWorker_Params& params) {
70 DCHECK(!workers_.Lookup(params.embedded_worker_id));
71 scoped_ptr<WorkerWrapper> wrapper(new WorkerWrapper(
72 blink::WebEmbeddedWorker::create(
73 new ServiceWorkerContextClient(
74 params.embedded_worker_id,
75 params.service_worker_version_id,
76 params.script_url))));
77
78 // TODO(kinuko): Make sure if we want to load the script source from
79 // script_url in the worker context or not. For now I assume we simply
80 // receive the entire script source from the browser.
81 blink::WebEmbeddedWorkerStartData start_data;
82 start_data.scriptURL = params.script_url;
83 start_data.scriptSource = params.script_source;
84 start_data.userAgent =
85 base::UTF8ToUTF16(webkit_glue::GetUserAgent(params.script_url));
86 start_data.contentSecurityPolicy = params.content_security_policy;
87 start_data.contentSecurityPolicyType = params.content_security_policy_type;
88 start_data.startMode = params.start_mode;
89
90 wrapper->worker()->startWorkerContext(start_data);
91 workers_.AddWithID(wrapper.release(), params.embedded_worker_id);
92 }
93
94 void EmbeddedWorkerDispatcher::OnTerminateWorker(int32 embedded_worker_id) {
95 WorkerWrapper* wrapper = workers_.Lookup(embedded_worker_id);
96 if (!wrapper) {
97 LOG(WARNING) << "Got OnTerminateWorker for nonexistent worker";
98 return;
99 }
100 wrapper->worker()->terminateWorkerContext();
101
102 // Posts a delayed task to forcibly delete the worker instance to
michaeln 2013/11/13 00:31:37 Why do this? I guess the goal is to invoke ChildP
kinuko 2013/11/18 14:09:48 Yes, it's to make sure we terminate the worker con
103 base::MessageLoop::current()->PostDelayedTask(
104 FROM_HERE,
105 base::Bind(&EmbeddedWorkerDispatcher::WorkerContextDestroyed,
106 weak_factory_.GetWeakPtr(), embedded_worker_id),
107 base::TimeDelta::FromSeconds(kMaxTimeForRunawayWorkerSeconds));
108 }
109
110 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698