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

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: Move ServiceWorkerMojoEventDispatcher to ServiceWorkerDispatcherHost 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/browser_thread.h"
8 #include "content/public/browser/render_process_host.h"
9 #include "content/public/common/service_registry.h"
10 #include "third_party/mojo/src/mojo/public/cpp/bindings/interface_ptr_info.h"
11
12 namespace content {
13
14 namespace {
15
16 // Converts mojo enum content::ServiceWorkerEventStatus values to Chromium
17 // content::ServiceWorkerStatusCode values.
18 // TODO(iclelland): Make these enums equivalent so that conversion can be a
19 // static cast.
20 ServiceWorkerStatusCode StatusCodeFromMojoStatus(
Marijn Kruisselbrink 2015/06/25 20:03:15 this should probably be a mojo::TypeConverter and
21 ServiceWorkerEventStatus status) {
22 ServiceWorkerStatusCode status_code = SERVICE_WORKER_OK;
23 if (status == SERVICE_WORKER_EVENT_STATUS_COMPLETED) {
24 status_code = SERVICE_WORKER_OK;
25 } else if (status == SERVICE_WORKER_EVENT_STATUS_REJECTED) {
26 status_code = SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED;
27 } else if (status == SERVICE_WORKER_EVENT_STATUS_ABORTED) {
28 status_code = SERVICE_WORKER_ERROR_ABORT;
29 } else {
30 NOTREACHED();
31 }
32 return status_code;
33 }
34
35 // Establishes a connection to a mojo service, and then unbinds the local side
36 // of the connection from the UI thread, and returns an InterfacePtrInfo struct
37 // that can be used to build a new InterfacePtr on a different thread.
38 //
39 // Failure is indicated by returning an invalid InterfacePtrInfo.
40 //
41 // This function must be called on the UI thread, as it accesses the service
42 // registry member of the RenderProcessHost.
43 template <typename MojoServiceType>
44 mojo::InterfacePtrInfo<MojoServiceType> ConnectOnUIThread(
45 int render_process_id) {
46 DCHECK_CURRENTLY_ON(BrowserThread::UI);
47
48 RenderProcessHost* host = RenderProcessHost::FromID(render_process_id);
49 if (!host)
50 return mojo::InterfacePtr<MojoServiceType>().PassInterface();
51
52 ServiceRegistry* registry = host->GetServiceRegistry();
53 if (!registry)
54 return mojo::InterfacePtr<MojoServiceType>().PassInterface();
55
56 mojo::InterfacePtr<MojoServiceType> mojo_interface_ptr;
57 registry->ConnectToRemoteService(mojo::GetProxy(&mojo_interface_ptr));
Marijn Kruisselbrink 2015/06/25 19:04:03 My understanding of the "canonical" way to connect
58 return mojo_interface_ptr.PassInterface();
59 }
60
61 } // namespace
62
63 ServiceWorkerMojoEventDispatcher::ServiceWorkerMojoEventDispatcher(
64 int render_process_id)
65 : render_process_id_(render_process_id), weak_factory_(this) {
66 DCHECK_CURRENTLY_ON(BrowserThread::IO);
67 }
68
69 ServiceWorkerMojoEventDispatcher::~ServiceWorkerMojoEventDispatcher() {
70 DCHECK_CURRENTLY_ON(BrowserThread::IO);
71 }
72
73 void ServiceWorkerMojoEventDispatcher::DispatchSyncEvent(
74 int thread_id,
75 const StatusCallback& callback) {
76 DCHECK_CURRENTLY_ON(BrowserThread::IO);
77
78 if (!background_sync_client_.get()) {
79 // If we have not connected to the mojo service before, switch to the UI
80 // thread to connect, and then dispatch the event once the connection is
81 // established.
82 BrowserThread::PostTaskAndReplyWithResult(
83 BrowserThread::UI, FROM_HERE,
84 base::Bind(&ConnectOnUIThread<BackgroundSyncServiceClient>,
85 render_process_id_),
86 base::Bind(
87 &ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent,
88 weak_factory_.GetWeakPtr(), thread_id, callback));
89 return;
90 }
91 DispatchSyncEventInternal(thread_id, callback);
92 }
93
94 void ServiceWorkerMojoEventDispatcher::BindChannelAndDispatchSyncEvent(
95 int thread_id,
96 const StatusCallback& callback,
97 mojo::InterfacePtrInfo<BackgroundSyncServiceClient> mojo_interface_ptr) {
98 DCHECK_CURRENTLY_ON(BrowserThread::IO);
99 if (!background_sync_client_.get()) {
100 // If the connect call failed, mojo_interface_ptr will be an invalid
101 // pointer, so abort now.
102 if (!mojo_interface_ptr.is_valid()) {
103 OnEventFinished(callback, SERVICE_WORKER_EVENT_STATUS_ABORTED);
104 return;
105 }
106 background_sync_client_.Bind(mojo_interface_ptr.Pass());
107 }
108 DispatchSyncEventInternal(thread_id, callback);
109 }
110
111 void ServiceWorkerMojoEventDispatcher::DispatchSyncEventInternal(
112 int thread_id,
113 const StatusCallback& callback) {
114 DCHECK_CURRENTLY_ON(BrowserThread::IO);
115
116 // TODO(iclelland): Replace this with the real event registration details
117 // crbug.com/482066
118 content::SyncRegistrationPtr null_event(content::SyncRegistration::New());
119
120 background_sync_client_->Sync(
121 null_event.Pass(), thread_id,
122 base::Bind(&ServiceWorkerMojoEventDispatcher::OnEventFinished,
123 weak_factory_.GetWeakPtr(), callback));
124 }
125
126 void ServiceWorkerMojoEventDispatcher::OnEventFinished(
127 const StatusCallback& callback,
128 ServiceWorkerEventStatus result) {
129 DCHECK_CURRENTLY_ON(BrowserThread::IO);
130 callback.Run(StatusCodeFromMojoStatus(result));
131 }
132
133 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698