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

Unified Diff: content/child/service_worker/service_worker_provider_context.cc

Issue 261533003: Populate .current when navigator.serviceWorker is accessed (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: dropped dedicated worker support Created 6 years, 8 months 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/child/service_worker/service_worker_provider_context.cc
diff --git a/content/child/service_worker/service_worker_provider_context.cc b/content/child/service_worker/service_worker_provider_context.cc
new file mode 100644
index 0000000000000000000000000000000000000000..97ddd1f9c0a347ab364ea3a8ca51a54e771ba6d3
--- /dev/null
+++ b/content/child/service_worker/service_worker_provider_context.cc
@@ -0,0 +1,79 @@
+// Copyright 2014 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/child/service_worker/service_worker_provider_context.h"
+
+#include "base/bind.h"
+#include "base/message_loop/message_loop_proxy.h"
+#include "base/stl_util.h"
+#include "content/child/child_thread.h"
+#include "content/child/service_worker/service_worker_dispatcher.h"
+#include "content/child/service_worker/service_worker_handle_reference.h"
+#include "content/child/thread_safe_sender.h"
+#include "content/child/worker_task_runner.h"
+#include "content/common/service_worker/service_worker_messages.h"
+
+namespace content {
+
+ServiceWorkerProviderContext::ServiceWorkerProviderContext(int provider_id)
+ : provider_id_(provider_id),
+ main_thread_loop_proxy_(base::MessageLoopProxy::current()),
+ thread_safe_sender_(ChildThread::current()->thread_safe_sender()) {
+ ServiceWorkerDispatcher* dispatcher =
+ ServiceWorkerDispatcher::GetOrCreateThreadSpecificInstance(
+ thread_safe_sender_);
+ DCHECK(dispatcher);
+ dispatcher->AddProviderContext(this);
+}
+
+ServiceWorkerProviderContext::~ServiceWorkerProviderContext() {
+ DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread());
+ if (ServiceWorkerDispatcher* dispatcher =
+ ServiceWorkerDispatcher::GetThreadSpecificInstance()) {
+ dispatcher->RemoveProviderContext(this);
+ }
+}
+
+void ServiceWorkerProviderContext::GetInitializationInfo(
+ scoped_ptr<ServiceWorkerHandleReference>* current) {
+ DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread());
+ if (current_) {
+ *current = ServiceWorkerHandleReference::Create(
+ current_->info(), thread_safe_sender_);
+ }
+}
+
+void ServiceWorkerProviderContext::OnServiceWorkerStateChanged(
+ int thread_id,
+ int handle_id,
+ blink::WebServiceWorkerState state) {
+ // Currently .current is the only ServiceWorker associated to this provider.
+ DCHECK_EQ(current_handle_id(), handle_id);
+ current_->set_state(state);
+
+ // TODO(kinuko): Forward the message to other threads when we support
+ // navigator.serviceWorker in dedicated workers.
+}
+
+void ServiceWorkerProviderContext::OnSetCurrentServiceWorker(
+ int thread_id,
+ int provider_id,
+ const ServiceWorkerObjectInfo& info) {
+ DCHECK_EQ(provider_id_, provider_id);
+
+ // This context is is the primary owner of this handle, keeps the
+ // initial reference until it goes away.
+ current_ = ServiceWorkerHandleReference::CreateForDeleter(
+ info, thread_safe_sender_);
+
+ // TODO(kinuko): Forward the message to other threads when we support
+ // navigator.serviceWorker in dedicated workers.
+}
+
+int ServiceWorkerProviderContext::current_handle_id() const {
+ DCHECK(main_thread_loop_proxy_->RunsTasksOnCurrentThread());
+ return current_ ? current_->info().handle_id : kInvalidServiceWorkerHandleId;
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698