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

Side by Side Diff: content/child/background_sync/background_sync_provider.cc

Issue 1104283002: [Background Sync] Add renderer code to interface between JS API and back-end service (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bgsync-mojo
Patch Set: Add comment re: BackgroundSyncProvider lifetime Created 5 years, 7 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/child/background_sync/background_sync_provider.h"
6
7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "content/child/background_sync/background_sync_type_converters.h"
10 #include "content/child/service_worker/web_service_worker_registration_impl.h"
11 #include "content/child/worker_task_runner.h"
12 #include "content/public/common/service_registry.h"
13 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncErro r.h"
14 #include "third_party/WebKit/public/platform/modules/background_sync/WebSyncRegi stration.h"
15
16 namespace content {
17 namespace {
18
19 // Returns the id of the given |service_worker_registration|, which
20 // is only available on the implementation of the interface.
21 int64 GetServiceWorkerRegistrationId(
22 blink::WebServiceWorkerRegistration* service_worker_registration) {
23 return static_cast<WebServiceWorkerRegistrationImpl*>(
24 service_worker_registration)->registration_id();
25 }
26
27 const int kNoWorkerThread = 0;
28
29 } // namespace
30
31 BackgroundSyncProvider::BackgroundSyncProvider(
32 ServiceRegistry* service_registry)
33 : service_registry_(service_registry) {
34 DCHECK(service_registry);
35 }
36
37 BackgroundSyncProvider::~BackgroundSyncProvider() {
38 }
39
40 void BackgroundSyncProvider::registerBackgroundSync(
41 const blink::WebSyncRegistration* options,
42 blink::WebServiceWorkerRegistration* service_worker_registration,
43 blink::WebSyncRegistrationCallbacks* callbacks) {
44 DCHECK(options);
45 DCHECK(service_worker_registration);
46 DCHECK(callbacks);
47 scoped_ptr<blink::WebSyncRegistration> options_ptr(
48 const_cast<blink::WebSyncRegistration*>(options));
jkarlin 2015/05/07 11:51:53 You could keep the constness of the pointer by hav
iclelland 2015/05/09 06:41:16 Done. But then I removed all of the *ForWorker fun
49 RegisterBackgroundSyncForWorker(options_ptr.Pass(),
50 service_worker_registration, callbacks,
51 kNoWorkerThread);
52 }
53
54 void BackgroundSyncProvider::unregisterBackgroundSync(
55 blink::WebSyncRegistration::Periodicity periodicity,
56 int64_t id, const blink::WebString& tag,
57 blink::WebServiceWorkerRegistration* service_worker_registration,
58 blink::WebSyncUnregistrationCallbacks* callbacks) {
59 DCHECK(service_worker_registration);
60 DCHECK(callbacks);
61 UnregisterBackgroundSyncForWorker(periodicity, id, tag,
62 service_worker_registration, callbacks,
63 kNoWorkerThread);
64 }
65
66 void BackgroundSyncProvider::getRegistration(
67 blink::WebSyncRegistration::Periodicity periodicity,
68 const blink::WebString& tag,
69 blink::WebServiceWorkerRegistration* service_worker_registration,
70 blink::WebSyncRegistrationCallbacks* callbacks) {
71 DCHECK(service_worker_registration);
72 DCHECK(callbacks);
73 GetRegistrationForWorker(periodicity, tag, service_worker_registration,
74 callbacks, kNoWorkerThread);
75 }
76
77 void BackgroundSyncProvider::getRegistrations(
78 blink::WebSyncRegistration::Periodicity periodicity,
79 blink::WebServiceWorkerRegistration* service_worker_registration,
80 blink::WebSyncGetRegistrationsCallbacks* callbacks) {
81 DCHECK(service_worker_registration);
82 DCHECK(callbacks);
83 GetRegistrationsForWorker(periodicity, service_worker_registration, callbacks,
84 kNoWorkerThread);
85 }
86
87 void BackgroundSyncProvider::RegisterBackgroundSyncForWorker(
jkarlin 2015/05/07 11:51:53 Given the comments for this class, it seems like B
iclelland 2015/05/09 06:41:16 Done. I really like this approach, rather than swi
88 scoped_ptr<blink::WebSyncRegistration> options,
89 blink::WebServiceWorkerRegistration* service_worker_registration,
90 blink::WebSyncRegistrationCallbacks* callbacks,
91 int worker_thread_id) {
92 int64 service_worker_registration_id =
93 GetServiceWorkerRegistrationId(service_worker_registration);
94
95 GetBackgroundSyncServicePtr()->Register(
96 mojo::ConvertTo<SyncRegistrationPtr>(*options),
97 service_worker_registration_id,
98 base::Bind(&BackgroundSyncProvider::RegisterCallback,
99 base::Unretained(this),callbacks,
100 worker_thread_id));
101 }
102
103 void BackgroundSyncProvider::UnregisterBackgroundSyncForWorker(
104 blink::WebSyncRegistration::Periodicity periodicity,
105 int64_t id,
106 const blink::WebString& tag,
107 blink::WebServiceWorkerRegistration* service_worker_registration,
108 blink::WebSyncUnregistrationCallbacks* callbacks,
109 int worker_thread_id) {
110 int64 service_worker_registration_id =
111 GetServiceWorkerRegistrationId(service_worker_registration);
112
113 GetBackgroundSyncServicePtr()->Unregister(
114 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), id,
115 tag.utf8(), service_worker_registration_id,
116 base::Bind(&BackgroundSyncProvider::UnregisterCallback,
117 base::Unretained(this),callbacks,
118 worker_thread_id));
119 }
120
121 void BackgroundSyncProvider::GetRegistrationForWorker(
122 blink::WebSyncRegistration::Periodicity periodicity,
123 const blink::WebString& tag,
124 blink::WebServiceWorkerRegistration* service_worker_registration,
125 blink::WebSyncRegistrationCallbacks* callbacks,
126 int worker_thread_id) {
127 int64 service_worker_registration_id =
128 GetServiceWorkerRegistrationId(service_worker_registration);
129
130 GetBackgroundSyncServicePtr()->GetRegistration(
131 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity),
132 tag.utf8(), service_worker_registration_id,
133 base::Bind(&BackgroundSyncProvider::RegisterCallback,
134 base::Unretained(this),callbacks,
135 worker_thread_id));
136 }
137
138 void BackgroundSyncProvider::GetRegistrationsForWorker(
139 blink::WebSyncRegistration::Periodicity periodicity,
140 blink::WebServiceWorkerRegistration* service_worker_registration,
141 blink::WebSyncGetRegistrationsCallbacks* callbacks,
142 int worker_thread_id) {
143 int64 service_worker_registration_id =
144 GetServiceWorkerRegistrationId(service_worker_registration);
145
146 GetBackgroundSyncServicePtr()->GetRegistrations(
147 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity),
148 service_worker_registration_id,
149 base::Bind(&BackgroundSyncProvider::GetRegistrationsCallback,
150 base::Unretained(this),
151 callbacks,
152 worker_thread_id));
153 }
154
155 void BackgroundSyncProvider::RegisterCallback(
156 blink::WebSyncRegistrationCallbacks* callbacks,
157 int worker_thread_id,
158 const SyncRegistrationPtr &options) {
159 // TODO(iclelland): Pass through the various errors from the manager to here
160 // and handle them appropriately.
161 scoped_ptr<blink::WebSyncRegistration> result;
162 if (!options.is_null())
163 result = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
164
165 if (worker_thread_id == kNoWorkerThread) {
166 callbacks->onSuccess(result.release());
167 } else {
168 // If the worker thread has been destroyed, then this task will be
169 // silently discarded.
170 WorkerTaskRunner::Instance()->PostTask(
171 worker_thread_id,
172 base::Bind(&blink::WebSyncRegistrationCallbacks::onSuccess,
173 base::Unretained(callbacks),
174 result.release()));
175 }
176 }
177
178 void BackgroundSyncProvider::UnregisterCallback(
179 blink::WebSyncUnregistrationCallbacks *callbacks, int worker_thread_id,
180 bool success) {
181 // TODO(iclelland): Pass through the various errors from the manager to here
182 // and handle them appropriately.
183 if (worker_thread_id == kNoWorkerThread) {
184 if (success) {
185 callbacks->onSuccess(new bool(success));
186 } else {
187 callbacks->onError(new blink::WebSyncError(
188 blink::WebSyncError::ErrorTypeUnknown,
189 "Sync registration does not exist"));
190 }
191 } else {
192 if (success) {
193 // If the worker thread has been destroyed, then this task will be
194 // silently discarded.
195 WorkerTaskRunner::Instance()->PostTask(
196 worker_thread_id,
197 base::Bind(&blink::WebSyncUnregistrationCallbacks::onSuccess,
198 base::Unretained(callbacks),
199 new bool(success)));
200 } else {
201 // If the worker thread has been destroyed, then this task will be
202 // silently discarded.
203 WorkerTaskRunner::Instance()->PostTask(
204 worker_thread_id,
205 base::Bind(&blink::WebSyncUnregistrationCallbacks::onError,
206 base::Unretained(callbacks),
207 new blink::WebSyncError(
208 blink::WebSyncError::ErrorTypeUnknown,
209 "Sync registration does not exist")));
210 }
211 }
212 }
213
214 void BackgroundSyncProvider::GetRegistrationsCallback(
215 blink::WebSyncGetRegistrationsCallbacks* callbacks,
216 int worker_thread_id,
217 const mojo::Array<SyncRegistrationPtr> &registrations) {
218 // TODO(iclelland): Pass through the various errors from the manager to here
219 // and handle them appropriately.
220 blink::WebVector<blink::WebSyncRegistration*>* results =
221 new blink::WebVector<blink::WebSyncRegistration*>(registrations.size());
222 for (size_t i=0; i < registrations.size(); ++i) {
223 (*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(
jkarlin 2015/05/07 11:51:53 make_scoped_ptr(registrations[i])
iclelland 2015/05/09 06:41:16 registrations[i] is a content::SyncRegistrationPtr
224 registrations[i]).release();
225 }
226 if (worker_thread_id == kNoWorkerThread) {
227 callbacks->onSuccess(results);
228 } else {
229 WorkerTaskRunner::Instance()->PostTask(
230 worker_thread_id,
231 base::Bind(&blink::WebSyncGetRegistrationsCallbacks::onSuccess,
232 base::Unretained(callbacks),
233 results));
234 }
235 }
236
237 BackgroundSyncServicePtr&
238 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
239 if (!background_sync_service_.get())
240 service_registry_->ConnectToRemoteService(&background_sync_service_);
241 return background_sync_service_;
242 }
243
244 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698