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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: content/renderer/service_worker/embedded_worker_dispatcher.cc
diff --git a/content/renderer/service_worker/embedded_worker_dispatcher.cc b/content/renderer/service_worker/embedded_worker_dispatcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..68685ef46606d93ddc70d7511e632032e2cb7f15
--- /dev/null
+++ b/content/renderer/service_worker/embedded_worker_dispatcher.cc
@@ -0,0 +1,110 @@
+// Copyright 2013 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "content/renderer/service_worker/embedded_worker_dispatcher.h"
+
+#include "base/memory/scoped_ptr.h"
+#include "base/strings/string16.h"
+#include "base/strings/utf_string_conversions.h"
+#include "content/child/child_process.h"
+#include "content/child/thread_safe_sender.h"
+#include "content/common/service_worker_messages.h"
+#include "content/renderer/render_thread_impl.h"
+#include "content/renderer/service_worker/service_worker_context_client.h"
+#include "third_party/WebKit/public/platform/WebString.h"
+#include "third_party/WebKit/public/platform/WebURL.h"
+#include "third_party/WebKit/public/web/WebEmbeddedWorker.h"
+#include "third_party/WebKit/public/web/WebEmbeddedWorkerStartData.h"
+#include "webkit/child/worker_task_runner.h"
+#include "webkit/common/user_agent/user_agent.h"
+
+namespace content {
+
+static const int kMaxTimeForRunawayWorkerSeconds = 3;
+
+// A thin wrapper of WebEmbeddedWorker which also adds and releases process
+// references automatically.
+class EmbeddedWorkerDispatcher::WorkerWrapper {
+ public:
+ explicit WorkerWrapper(blink::WebEmbeddedWorker* worker)
+ : worker_(worker) {
+ ChildProcess::current()->AddRefProcess();
+ }
+
+ ~WorkerWrapper() {
+ ChildProcess::current()->ReleaseProcess();
+ }
+
+ blink::WebEmbeddedWorker* worker() { return worker_.get(); }
+
+ private:
+ scoped_ptr<blink::WebEmbeddedWorker> worker_;
+};
+
+EmbeddedWorkerDispatcher::EmbeddedWorkerDispatcher() : weak_factory_(this) {}
+
+EmbeddedWorkerDispatcher::~EmbeddedWorkerDispatcher() {}
+
+bool EmbeddedWorkerDispatcher::OnMessageReceived(
+ const IPC::Message& message) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP(EmbeddedWorkerDispatcher, message)
+ IPC_MESSAGE_HANDLER(ServiceWorkerMsg_StartWorker, OnStartWorker)
+ IPC_MESSAGE_HANDLER(ServiceWorkerMsg_TerminateWorker, OnTerminateWorker)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP()
+ return handled;
+}
+
+void EmbeddedWorkerDispatcher::WorkerContextDestroyed(
+ int32 embedded_worker_id) {
+ // There's a chance that this gets called multiple times.
+ // (And Remove internally DCHECK if we attempt to delete nonexistent one)
+ if (workers_.Lookup(embedded_worker_id))
+ workers_.Remove(embedded_worker_id);
+}
+
+void EmbeddedWorkerDispatcher::OnStartWorker(
+ const ServiceWorkerMsg_StartWorker_Params& params) {
+ DCHECK(!workers_.Lookup(params.embedded_worker_id));
+ scoped_ptr<WorkerWrapper> wrapper(new WorkerWrapper(
+ blink::WebEmbeddedWorker::create(
+ new ServiceWorkerContextClient(
+ params.embedded_worker_id,
+ params.service_worker_version_id,
+ params.script_url))));
+
+ // TODO(kinuko): Make sure if we want to load the script source from
+ // script_url in the worker context or not. For now I assume we simply
+ // receive the entire script source from the browser.
+ blink::WebEmbeddedWorkerStartData start_data;
+ start_data.scriptURL = params.script_url;
+ start_data.scriptSource = params.script_source;
+ start_data.userAgent =
+ base::UTF8ToUTF16(webkit_glue::GetUserAgent(params.script_url));
+ start_data.contentSecurityPolicy = params.content_security_policy;
+ start_data.contentSecurityPolicyType = params.content_security_policy_type;
+ start_data.startMode = params.start_mode;
+
+ wrapper->worker()->startWorkerContext(start_data);
+ workers_.AddWithID(wrapper.release(), params.embedded_worker_id);
+}
+
+void EmbeddedWorkerDispatcher::OnTerminateWorker(int32 embedded_worker_id) {
+ WorkerWrapper* wrapper = workers_.Lookup(embedded_worker_id);
+ if (!wrapper) {
+ LOG(WARNING) << "Got OnTerminateWorker for nonexistent worker";
+ return;
+ }
+ wrapper->worker()->terminateWorkerContext();
+
+ // 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
+ base::MessageLoop::current()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&EmbeddedWorkerDispatcher::WorkerContextDestroyed,
+ weak_factory_.GetWeakPtr(), embedded_worker_id),
+ base::TimeDelta::FromSeconds(kMaxTimeForRunawayWorkerSeconds));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698