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..a5d37d898f8538a24dd127ff6d213cecd6f1c1d9 |
--- /dev/null |
+++ b/content/browser/service_worker/service_worker_mojo_event_dispatcher.cc |
@@ -0,0 +1,99 @@ |
+// 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 mojo { |
+ |
+// TODO(iclelland): Make these enums equivalent so that conversion can be a |
+// static cast. |
+content::ServiceWorkerStatusCode |
+TypeConverter<content::ServiceWorkerStatusCode, |
+ content::ServiceWorkerEventStatus>:: |
+ Convert(content::ServiceWorkerEventStatus status) { |
+ content::ServiceWorkerStatusCode status_code = content::SERVICE_WORKER_OK; |
+ if (status == content::SERVICE_WORKER_EVENT_STATUS_COMPLETED) { |
+ status_code = content::SERVICE_WORKER_OK; |
+ } else if (status == content::SERVICE_WORKER_EVENT_STATUS_REJECTED) { |
+ status_code = content::SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; |
+ } else if (status == content::SERVICE_WORKER_EVENT_STATUS_ABORTED) { |
+ status_code = content::SERVICE_WORKER_ERROR_ABORT; |
+ } else { |
+ NOTREACHED(); |
+ } |
+ return status_code; |
+} |
+ |
+} // namespace |
+ |
+namespace content { |
+ |
+namespace { |
+ |
+// Establishes a connection to a mojo service in the given render process. |
+// |
+// This function must be called on the UI thread, as it accesses the service |
+// registry member of the RenderProcessHost. However, the connection itself |
+// should be used on the thread that the InterfaceRequest was created on. |
+template <typename MojoServiceType> |
+void ConnectOnUIThread(mojo::InterfaceRequest<MojoServiceType> request, |
+ int render_process_id) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::UI); |
+ |
+ RenderProcessHost* host = RenderProcessHost::FromID(render_process_id); |
+ ServiceRegistry* registry = host->GetServiceRegistry(); |
+ registry->ConnectToRemoteService(request.Pass()); |
+} |
+ |
+} // namespace |
+ |
+ServiceWorkerMojoEventDispatcher::ServiceWorkerMojoEventDispatcher( |
+ int render_process_id) |
+ : render_process_id_(render_process_id), weak_factory_(this) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+} |
+ |
+ServiceWorkerMojoEventDispatcher::~ServiceWorkerMojoEventDispatcher() { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+} |
+ |
+void ServiceWorkerMojoEventDispatcher::DispatchSyncEvent( |
+ 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. This can happen asynchronously, and the Sync() call |
+ // will occur once the connection is established. |
+ BrowserThread::PostTask( |
+ BrowserThread::UI, FROM_HERE, |
+ base::Bind(&ConnectOnUIThread<BackgroundSyncServiceClient>, |
+ base::Passed(mojo::GetProxy(&background_sync_client_)), |
+ render_process_id_)); |
+ } |
+ |
+ // 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_factory_.GetWeakPtr(), callback)); |
+} |
+ |
+void ServiceWorkerMojoEventDispatcher::OnEventFinished( |
+ const StatusCallback& callback, |
+ ServiceWorkerEventStatus result) { |
+ DCHECK_CURRENTLY_ON(BrowserThread::IO); |
+ callback.Run(mojo::ConvertTo<ServiceWorkerStatusCode>(result)); |
+} |
+ |
+} // namespace content |