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..96e4f4977edb28a647c1f98dd68b87c9a1de8dde |
--- /dev/null |
+++ b/content/child/service_worker/service_worker_provider_context.cc |
@@ -0,0 +1,129 @@ |
+// 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/scoped_service_worker_reference.h" |
+#include "content/child/service_worker/service_worker_dispatcher.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 { |
+ |
+namespace { |
+ |
+void ForwardMessage(scoped_ptr<IPC::Message> message) { |
+ if (ServiceWorkerDispatcher* dispatcher = |
+ ServiceWorkerDispatcher::GetThreadSpecificInstance()) { |
+ dispatcher->OnMessageReceived(*message); |
+ } |
+} |
+ |
+} // namespace |
+ |
+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::AddProviderThreadAndGetInitializationInfo( |
+ int thread_id, |
+ scoped_ptr<ScopedServiceWorkerReference>* current) { |
+ base::AutoLock lock(lock_); |
+ DCHECK(!ContainsKey(provider_thread_ids_, thread_id)); |
+ provider_thread_ids_.insert(thread_id); |
+ if (current_) { |
+ *current = ScopedServiceWorkerReference::Create( |
+ current_->info(), thread_safe_sender_); |
+ } |
+} |
+ |
+void ServiceWorkerProviderContext::RemoveProviderThread(int thread_id) { |
+ base::AutoLock lock(lock_); |
+ provider_thread_ids_.erase(thread_id); |
+} |
+ |
+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); |
+ std::set<int> thread_ids; |
+ { |
+ base::AutoLock lock(lock_); |
+ current_->set_state(state); |
+ thread_ids = provider_thread_ids_; |
+ } |
+ ForwardMessageToProviderThreads( |
+ ServiceWorkerMsg_ServiceWorkerStateChanged( |
+ thread_id, handle_id, state), thread_ids); |
+} |
+ |
+void ServiceWorkerProviderContext::OnSetCurrentServiceWorker( |
+ int thread_id, |
+ int provider_id, |
+ const ServiceWorkerObjectInfo& info) { |
+ DCHECK_EQ(provider_id_, provider_id); |
+ std::set<int> thread_ids; |
+ { |
+ base::AutoLock lock(lock_); |
+ current_ = ScopedServiceWorkerReference::Create( |
+ info, thread_safe_sender_); |
+ thread_ids = provider_thread_ids_; |
+ } |
+ ForwardMessageToProviderThreads( |
+ ServiceWorkerMsg_SetCurrentServiceWorker( |
+ thread_id, provider_id, info), thread_ids); |
falken
2014/05/01 12:19:29
I'm trying to understand how this works. At first
kinuko
2014/05/02 10:00:56
This may not be very obvious, but:
1. ProviderCont
|
+} |
+ |
+const ServiceWorkerObjectInfo& ServiceWorkerProviderContext::current() const { |
+ base::AutoLock lock(lock_); |
+ return current_->info(); |
michaeln
2014/05/01 01:25:18
don't think we can return this ptr type to the str
kinuko
2014/05/02 10:00:56
Changed to return it as a value type.
|
+} |
+ |
+void ServiceWorkerProviderContext::DestructOnMainThread() const { |
+ if (!main_thread_loop_proxy_->RunsTasksOnCurrentThread() && |
+ main_thread_loop_proxy_->DeleteSoon(FROM_HERE, this)) { |
michaeln
2014/05/01 01:25:18
not sure if its possible for a background thread t
kinuko
2014/05/02 10:00:56
Hmm.. it might be better. Done.
|
+ return; |
+ } |
+ delete this; |
+} |
+ |
+void ServiceWorkerProviderContext::ForwardMessageToProviderThreads( |
michaeln
2014/05/01 01:25:18
I see how messages are forwarded, but I don't see
kinuko
2014/05/02 10:00:56
WebSWProviderImpls are created on the main thread
|
+ const IPC::Message& message, |
+ const std::set<int>& thread_ids) { |
+ for (std::set<int>::iterator it = thread_ids.begin(); |
+ it != thread_ids.end(); |
+ ++it) { |
+ int thread_id = *it; |
+ if (thread_id != WorkerTaskRunner::Instance()->CurrentWorkerId()) { |
+ WorkerTaskRunner::Instance()->PostTask( |
+ thread_id, |
+ base::Bind(&ForwardMessage, |
+ base::Passed(make_scoped_ptr(new IPC::Message(message))))); |
+ } |
+ } |
+} |
+ |
+} // namespace content |