Index: content/browser/service_worker/service_worker_mojo_event_dispatcher.cc |
diff --git a/content/browser/service_worker/service_worker_mojo_event_dispatcher.cc b/content/browser/service_worker/service_worker_mojo_event_dispatcher.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1a4cc80727e717c31c8cc0a55d2f9743778f9065 |
--- /dev/null |
+++ b/content/browser/service_worker/service_worker_mojo_event_dispatcher.cc |
@@ -0,0 +1,132 @@ |
+// Copyright 2015 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/browser/service_worker/service_worker_mojo_event_dispatcher.h" |
+ |
+#include "content/public/browser/browser_thread.h" |
+#include "content/public/browser/render_process_host.h" |
+#include "content/public/common/service_registry.h" |
+#include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr_info.h" |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+// Converts mojo enum content::ServiceWorkerEventStatus values to Chromium |
+// content::ServiceWorkerStatusCode values. |
+// TODO(iclelland): Make these enums equivalent so that conversion can be a |
+// static cast. |
+ServiceWorkerStatusCode statusCodeFromMojoStatus( |
jkarlin
2015/06/18 18:13:56
caps, StatusCodeFromMojoStatus
iclelland
2015/06/19 13:44:32
oldHabitsDieHard :)
Done.
|
+ ServiceWorkerEventStatus status) { |
+ ServiceWorkerStatusCode status_code = SERVICE_WORKER_OK; |
+ if (status == SERVICE_WORKER_EVENT_STATUS_COMPLETED) { |
+ status_code = SERVICE_WORKER_OK; |
+ } else if (status == SERVICE_WORKER_EVENT_STATUS_REJECTED) { |
+ status_code = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; |
+ } else if (status == SERVICE_WORKER_EVENT_STATUS_ABORTED) { |
+ status_code = SERVICE_WORKER_ERROR_ABORT; |
+ } else { |
+ NOTREACHED(); |
+ } |
+ return status_code; |
+} |
+ |
+// Establishes a connection to a mojo service, and then detaches the local |
jkarlin
2015/06/18 18:13:56
"detaches the local endpoint of the message pipe f
iclelland
2015/06/19 13:44:32
It's actually what InterfacePtr<>::PassInterface i
jkarlin
2015/06/19 13:51:42
Got it, thanks.
michaeln
2015/06/24 01:34:03
ah... now i see where that occurs :)
|
+// endpoint of the message pipe from the UI thread, and returns an |
+// InterfacePtrInfo struct that can be used to build a new InterfacePtr on a |
+// different thread. |
+// |
+// Failure is indicated by returning an invalid InterfacePtrInfo. |
+// |
+// This function must be called on the UI thread, as it accesses the service |
+// registry member of the RenderProcessHost. |
+template <typename MojoServiceType> |
+mojo::InterfacePtrInfo<MojoServiceType> ConnectOnUIThread( |
+ int render_process_id) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ mojo::InterfacePtr<MojoServiceType> mojo_interface_ptr; |
+ |
+ RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); |
+ if (host) { |
+ content::ServiceRegistry* registry = host->GetServiceRegistry(); |
+ if (registry) { |
+ registry->ConnectToRemoteService(mojo::GetProxy(&mojo_interface_ptr)); |
+ } |
+ } |
+ return mojo_interface_ptr.PassInterface(); |
+} |
+ |
+} // namespace |
+ |
+ServiceWorkerMojoEventDispatcher::ServiceWorkerMojoEventDispatcher() |
+ : weak_ptr_factory_(this) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+} |
+ |
+ServiceWorkerMojoEventDispatcher::~ServiceWorkerMojoEventDispatcher() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+} |
+ |
+void ServiceWorkerMojoEventDispatcher::DispatchSyncEvent( |
+ int render_process_id, |
+ int thread_id, |
+ const StatusCallback& callback) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ |
+ if (!background_sync_client_.get()) { |
+ // If we have not connected to the mojo service before, switch to the UI |
+ // thread to connect, and then dispatch the event once the connection is |
+ // established. |
+ BrowserThread::PostTaskAndReplyWithResult( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&ConnectOnUIThread<BackgroundSyncServiceClient>, |
+ render_process_id), |
+ base::Bind( |
+ &ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent, |
+ weak_ptr_factory_.GetWeakPtr(), thread_id, callback)); |
+ return; |
+ } |
+ DispatchSyncEventInternal(thread_id, callback); |
+} |
+ |
+void ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent( |
+ int thread_id, |
+ const StatusCallback& callback, |
+ mojo::InterfacePtrInfo<BackgroundSyncServiceClient> mojo_interface_ptr) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ if (!background_sync_client_.get()) { |
+ // If the connect call failed, mojo_interface_ptr will be an invalid |
+ // pointer, so abort now. |
+ if (!mojo_interface_ptr.is_valid()) { |
+ OnEventFinished(callback, SERVICE_WORKER_EVENT_STATUS_ABORTED); |
+ return; |
+ } |
+ background_sync_client_.Bind(mojo_interface_ptr.Pass()); |
+ } |
+ DispatchSyncEventInternal(thread_id, callback); |
+} |
+ |
+void ServiceWorkerMojoEventDispatcher::DispatchSyncEventInternal( |
+ int thread_id, |
+ const StatusCallback& callback) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ |
+ // TODO(iclelland): Replace this with the real event registration details |
+ // crbug.com/482066 |
+ content::SyncRegistrationPtr null_event(content::SyncRegistration::New()); |
+ |
+ background_sync_client_->Sync( |
+ null_event.Pass(), thread_id, |
+ base::Bind(&ServiceWorkerMojoEventDispatcher::OnEventFinished, |
+ weak_ptr_factory_.GetWeakPtr(), callback)); |
+} |
+ |
+void ServiceWorkerMojoEventDispatcher::OnEventFinished( |
+ const StatusCallback& callback, |
+ ServiceWorkerEventStatus result) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ callback.Run(statusCodeFromMojoStatus(result)); |
+} |
+ |
+} // namespace content |