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

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: Updates based on recent changes to mojo definitions. 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) {
jkarlin 2015/05/01 16:13:51 DCHECK(service_registry_)?
iclelland 2015/05/05 14:52:01 Done.
34 }
35
36 BackgroundSyncProvider::~BackgroundSyncProvider() {
37 }
38
39 void BackgroundSyncProvider::registerBackgroundSync(
40 const blink::WebSyncRegistration* options,
41 blink::WebServiceWorkerRegistration* service_worker_registration,
42 blink::WebSyncRegistrationCallbacks* callbacks) {
43 DCHECK(options);
44 DCHECK(service_worker_registration);
45 DCHECK(callbacks);
46 RegisterBackgroundSyncForWorker(options, service_worker_registration,
47 callbacks, kNoWorkerThread);
48 }
49
50 void BackgroundSyncProvider::unregisterBackgroundSync(
51 blink::WebSyncRegistration::Periodicity periodicity,
52 int64_t id, const blink::WebString& tag,
53 blink::WebServiceWorkerRegistration* service_worker_registration,
54 blink::WebSyncUnregistrationCallbacks* callbacks) {
55 DCHECK(service_worker_registration);
56 DCHECK(callbacks);
57 UnregisterBackgroundSyncForWorker(periodicity, id, tag,
58 service_worker_registration, callbacks,
59 kNoWorkerThread);
60 }
61
62 void BackgroundSyncProvider::getRegistration(
63 blink::WebSyncRegistration::Periodicity periodicity,
64 const blink::WebString& tag,
65 blink::WebServiceWorkerRegistration* service_worker_registration,
66 blink::WebSyncRegistrationCallbacks* callbacks) {
67 DCHECK(service_worker_registration);
68 DCHECK(callbacks);
69 GetRegistrationForWorker(periodicity, tag, service_worker_registration,
70 callbacks, kNoWorkerThread);
71 }
72
73 void BackgroundSyncProvider::getRegistrations(
74 blink::WebSyncRegistration::Periodicity periodicity,
75 blink::WebServiceWorkerRegistration* service_worker_registration,
76 blink::WebSyncGetRegistrationsCallbacks* callbacks) {
77 DCHECK(service_worker_registration);
78 DCHECK(callbacks);
79 GetRegistrationsForWorker(periodicity, service_worker_registration, callbacks,
80 kNoWorkerThread);
81 }
82
83 void BackgroundSyncProvider::OnWorkerRunLoopStopped() {
84 delete this;
jkarlin 2015/05/01 16:13:51 This object is owned by BlinkPlatformImpl right? S
iclelland 2015/05/07 06:03:50 Right. I've removed this method, as it was part of
85 }
86
87 void BackgroundSyncProvider::RegisterBackgroundSyncForWorker(
88 const 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 delete options;
102 }
103
104 void BackgroundSyncProvider::UnregisterBackgroundSyncForWorker(
105 blink::WebSyncRegistration::Periodicity periodicity,
106 int64_t id,
107 const blink::WebString& tag,
108 blink::WebServiceWorkerRegistration* service_worker_registration,
109 blink::WebSyncUnregistrationCallbacks* callbacks,
110 int worker_thread_id) {
111 int64 service_worker_registration_id =
112 GetServiceWorkerRegistrationId(service_worker_registration);
113
114 GetBackgroundSyncServicePtr()->Unregister(
115 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity), id,
116 tag.utf8(), service_worker_registration_id,
117 base::Bind(&BackgroundSyncProvider::unregisterCallback,
118 base::Unretained(this),callbacks,
119 worker_thread_id));
120 }
121
122 void BackgroundSyncProvider::GetRegistrationForWorker(
123 blink::WebSyncRegistration::Periodicity periodicity,
124 const blink::WebString& tag,
125 blink::WebServiceWorkerRegistration* service_worker_registration,
126 blink::WebSyncRegistrationCallbacks* callbacks,
127 int worker_thread_id) {
128 int64 service_worker_registration_id =
129 GetServiceWorkerRegistrationId(service_worker_registration);
130
131 GetBackgroundSyncServicePtr()->GetRegistration(
132 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity),
133 tag.utf8(), service_worker_registration_id,
134 base::Bind(&BackgroundSyncProvider::registerCallback,
135 base::Unretained(this),callbacks,
136 worker_thread_id));
137 }
138
139 void BackgroundSyncProvider::GetRegistrationsForWorker(
140 blink::WebSyncRegistration::Periodicity periodicity,
141 blink::WebServiceWorkerRegistration* service_worker_registration,
142 blink::WebSyncGetRegistrationsCallbacks* callbacks,
143 int worker_thread_id) {
144 int64 service_worker_registration_id =
145 GetServiceWorkerRegistrationId(service_worker_registration);
146
147 GetBackgroundSyncServicePtr()->GetRegistrations(
148 mojo::ConvertTo<BackgroundSyncPeriodicity>(periodicity),
149 service_worker_registration_id,
150 base::Bind(&BackgroundSyncProvider::getRegistrationsCallback,
151 base::Unretained(this),
152 callbacks,
153 worker_thread_id));
154 }
155
156 void BackgroundSyncProvider::registerCallback(
157 blink::WebSyncRegistrationCallbacks* callbacks,
158 int worker_thread_id,
159 const SyncRegistrationPtr &options) {
160 scoped_ptr<blink::WebSyncRegistration> result;
161 if (!options.is_null())
162 result = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(options);
163
164 if (worker_thread_id == kNoWorkerThread) {
165 callbacks->onSuccess(result.release());
166 } else {
167 WorkerTaskRunner::Instance()->PostTask(
168 worker_thread_id,
169 base::Bind(&blink::WebSyncRegistrationCallbacks::onSuccess,
170 base::Unretained(callbacks),
171 result.release()));
172 }
173 }
174
175 void BackgroundSyncProvider::unregisterCallback(
176 blink::WebSyncUnregistrationCallbacks *callbacks, int worker_thread_id,
177 bool success) {
178 if (worker_thread_id == kNoWorkerThread) {
179 if (success) {
180 callbacks->onSuccess(new bool(success));
jkarlin 2015/05/01 16:13:51 Why does onSuccess take a bool* instead of just a
iclelland 2015/05/05 14:52:01 The template for WebCallbacks makes pointers out o
181 } else {
182 callbacks->onError(new blink::WebSyncError(
183 blink::WebSyncError::ErrorTypeUnknown,
184 "Sync registration does not exist"));
jkarlin 2015/05/01 16:13:51 Please create a bug (and a TODO) to handle the var
iclelland 2015/05/05 14:52:01 Done. crbug.com/484306
185 }
186 } else {
187 if (success) {
188 WorkerTaskRunner::Instance()->PostTask(
jkarlin 2015/05/01 16:13:51 Add a comment that it's safe to call PostTask() on
iclelland 2015/05/05 14:52:01 Done.
189 worker_thread_id,
190 base::Bind(&blink::WebSyncUnregistrationCallbacks::onSuccess,
191 base::Unretained(callbacks),
192 new bool(success)));
193 } else {
194 WorkerTaskRunner::Instance()->PostTask(
195 worker_thread_id,
196 base::Bind(&blink::WebSyncUnregistrationCallbacks::onError,
197 base::Unretained(callbacks),
198 new blink::WebSyncError(
199 blink::WebSyncError::ErrorTypeUnknown,
200 "Sync registration does not exist")));
201 }
202 }
203 }
204
205 void BackgroundSyncProvider::getRegistrationsCallback(
206 blink::WebSyncGetRegistrationsCallbacks* callbacks,
207 int worker_thread_id,
208 const mojo::Array<SyncRegistrationPtr> &registrations) {
209 blink::WebVector<blink::WebSyncRegistration*>* results =
210 new blink::WebVector<blink::WebSyncRegistration*>(registrations.size());
211 for (size_t i=0; i < registrations.size(); ++i) {
212 (*results)[i] = mojo::ConvertTo<scoped_ptr<blink::WebSyncRegistration>>(
213 registrations[i]).release();
214 }
215 if (worker_thread_id == kNoWorkerThread) {
216 callbacks->onSuccess(results);
217 } else {
218 WorkerTaskRunner::Instance()->PostTask(
219 worker_thread_id,
220 base::Bind(&blink::WebSyncGetRegistrationsCallbacks::onSuccess,
221 base::Unretained(callbacks),
222 results));
223 }
224 }
225
226 BackgroundSyncServicePtr&
227 BackgroundSyncProvider::GetBackgroundSyncServicePtr() {
228 if (!background_sync_service_.get())
229 service_registry_->ConnectToRemoteService(&background_sync_service_);
230 return background_sync_service_;
231 }
232
233 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698