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

Side by Side 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: Reduce thread hopping with a better ServiceWorkerMojoEventDispatcher 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/service_worker/service_worker_mojo_event_dispatcher.h"
6
7 #include "content/public/browser/render_process_host.h"
8 #include "content/public/common/service_registry.h"
9 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr_info.h"
10
11 namespace content {
12
13 namespace {
14
15 // Converts mojo enum content::ServiceWorkerEventStatus values to Chromium
16 // content::ServiceWorkerStatusCode values.
17 // TODO(iclelland): Make these enums equivalent so that conversion can be a
18 // static cast.
19 ServiceWorkerStatusCode statusCodeFromMojoStatus(
20 ServiceWorkerEventStatus status) {
21 ServiceWorkerStatusCode status_code = SERVICE_WORKER_OK;
22 if (status == SERVICE_WORKER_EVENT_STATUS_COMPLETED) {
23 status_code = SERVICE_WORKER_OK;
24 } else if (status == SERVICE_WORKER_EVENT_STATUS_REJECTED) {
25 status_code = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED;
26 } else if (status == SERVICE_WORKER_EVENT_STATUS_ABORT) {
27 status_code = SERVICE_WORKER_ERROR_ABORT;
28 } else {
29 NOTREACHED();
30 }
31 return status_code;
32 }
33
34 } // namespace
35
36 ServiceWorkerMojoEventDispatcher::ServiceWorkerMojoEventDispatcher() {
jkarlin 2015/06/16 18:59:59 DCHECK_CURRENTLY_ON(BrowserThread::IO);
iclelland 2015/06/17 12:39:24 Done.
37 }
38
39 void ServiceWorkerMojoEventDispatcher::DispatchSyncEvent(
40 int render_process_id,
41 int thread_id,
42 const ServiceWorkerVersion::StatusCallback& callback) {
43 DCHECK_CURRENTLY_ON(BrowserThread::IO);
44
45 if (!background_sync_client_.get()) {
46 // We have not connected to the mojo service before. Switch to the UI thread
47 // to connect, and then dispatch the event once the connection is
48 // established.
49 BrowserThread::PostTask(
50 BrowserThread::UI, FROM_HERE,
51 base::Bind(&ServiceWorkerMojoEventDispatcher::Connect<
52 BackgroundSyncServiceClient>,
53 this, render_process_id,
54 base::Bind(&ServiceWorkerMojoEventDispatcher::
55 BindChannelAndDispatchSyncEvent,
56 this, thread_id, callback)));
jkarlin 2015/06/16 19:00:00 nit: prefer you return here and remove the else {}
iclelland 2015/06/17 12:39:24 Done.
57 } else {
58 DispatchSyncEventInternal(thread_id, callback);
59 }
60 }
61
62 ServiceWorkerMojoEventDispatcher::~ServiceWorkerMojoEventDispatcher() {
63 }
64
65 // Establishes a connection to a mojo service, and then transfers the local
66 // endpoint of the message pipe to the IO thread so that it can be called from
67 // there in the future without switching threads.
68 // This method must be called on the UI thread, as it accesses the service
69 // registry member of the RenderProcessHost.
70 template <typename MojoServiceType>
71 void ServiceWorkerMojoEventDispatcher::Connect(
jkarlin 2015/06/16 19:00:00 This should be called ConnectOnUIThread and should
iclelland 2015/06/17 12:39:24 Done.
72 int render_process_id,
73 const base::Callback<void(mojo::InterfacePtrInfo<MojoServiceType>)>&
74 callback) {
75 DCHECK_CURRENTLY_ON(BrowserThread::UI);
76 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
jkarlin 2015/06/16 19:00:00 There is no guarantee that the render process stil
iclelland 2015/06/17 12:39:24 Done.
77 content::ServiceRegistry* registry = host->GetServiceRegistry();
78 if (!registry)
79 return;
80
81 mojo::InterfacePtr<MojoServiceType> background_sync_client;
82 registry->ConnectToRemoteService(mojo::GetProxy(&background_sync_client));
jkarlin 2015/06/16 19:00:00 ConnectToRemoveService isn't documented, but appea
iclelland 2015/06/17 12:39:24 Yes; see cleanup patch. My understanding (from con
83 BrowserThread::PostTask(
84 BrowserThread::IO, FROM_HERE,
85 base::Bind(callback,
86 base::Passed(background_sync_client.PassInterface())));
87 }
88
89 void ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent(
90 int thread_id,
91 const ServiceWorkerVersion::StatusCallback& callback,
92 mojo::InterfacePtrInfo<BackgroundSyncServiceClient> ptri) {
93 DCHECK_CURRENTLY_ON(BrowserThread::IO);
94 if (!background_sync_client_.get()) {
jkarlin 2015/06/16 18:59:59 DCHECK(background_sync_client_.get())?
iclelland 2015/06/17 12:39:24 No -- this is checking whether the interface has b
95 background_sync_client_.Bind(ptri.Pass());
96 }
97 DispatchSyncEventInternal(thread_id, callback);
98 }
99
100 void ServiceWorkerMojoEventDispatcher::DispatchSyncEventInternal(
101 int thread_id,
102 const ServiceWorkerVersion::StatusCallback& callback) {
103 DCHECK_CURRENTLY_ON(BrowserThread::IO);
104
105 // TODO(iclelland): Replace this with the real event registration details
106 // crbug.com/482066
107 content::SyncRegistrationPtr TEMP_null_event(
jkarlin 2015/06/16 19:00:00 tmp_null_event, lowercase
iclelland 2015/06/17 12:39:24 I didn't expect this identifier to last too long,
108 content::SyncRegistration::New());
109
110 background_sync_client_->Sync(
111 TEMP_null_event.Pass(), thread_id,
112 base::Bind(&ServiceWorkerMojoEventDispatcher::OnEventFinished, this,
113 callback));
114 }
115
116 void ServiceWorkerMojoEventDispatcher::OnEventFinished(
117 const ServiceWorkerVersion::StatusCallback& callback,
118 ServiceWorkerEventStatus result) {
119 DCHECK_CURRENTLY_ON(BrowserThread::IO);
120 callback.Run(statusCodeFromMojoStatus(result));
121 }
122
123 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698