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

Unified Diff: content/browser/service_worker/service_worker_mojo_event_dispatcher.cc

Issue 1171173002: [Background Sync] Use Mojo IPC to fire background sync events (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressing review comments on event dispatcher Created 5 years, 6 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/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..fb172105040feedae26d00ea573b0aa00e94a5d4
--- /dev/null
+++ b/content/browser/service_worker/service_worker_mojo_event_dispatcher.cc
@@ -0,0 +1,137 @@
+// 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(
+ 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_ABORT) {
+ status_code = SERVICE_WORKER_ERROR_ABORT;
+ } else {
+ NOTREACHED();
+ }
+ return status_code;
+}
+
+// Establishes a connection to a mojo service, and then transfers the local
+// endpoint of the message pipe to the IO thread so that it can be called from
+// there in the future without switching threads.
+//
+// On success, the callback is called with a detached InterfacePtrInfo, which
+// can be used to build a new InterfacePtr on the calling thread. Failure is
+// indicated by calling the callback with 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>
+void ConnectOnUIThread(
+ int render_process_id,
+ const base::Callback<void(mojo::InterfacePtrInfo<MojoServiceType>)>&
+ callback) {
+ 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));
+ }
+ }
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(callback, base::Passed(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 ServiceWorkerVersion::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::PostTask(
jkarlin 2015/06/17 15:17:29 This is much better, thanks! One more suggestion.
iclelland 2015/06/18 16:01:14 That's a good API -- thanks for that pointer!
+ 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 ServiceWorkerVersion::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_ABORT);
+ return;
+ }
+ background_sync_client_.Bind(mojo_interface_ptr.Pass());
+ }
+ DispatchSyncEventInternal(thread_id, callback);
+}
+
+void ServiceWorkerMojoEventDispatcher::DispatchSyncEventInternal(
+ int thread_id,
+ const ServiceWorkerVersion::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,
jkarlin 2015/06/17 15:17:29 Why not just pass the callback here? What purpose
iclelland 2015/06/18 16:01:14 The only thing it does currently is convert the ca
+ weak_ptr_factory_.GetWeakPtr(), callback));
+}
+
+void ServiceWorkerMojoEventDispatcher::OnEventFinished(
+ const ServiceWorkerVersion::StatusCallback& callback,
+ ServiceWorkerEventStatus result) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ callback.Run(statusCodeFromMojoStatus(result));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698