OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/background_sync/background_sync_service_impl.h" | 5 #include "content/browser/background_sync/background_sync_service_impl.h" |
6 | 6 |
7 #include "background_sync_registration_handle.h" | |
7 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/stl_util.h" | |
8 #include "content/browser/background_sync/background_sync_context_impl.h" | 10 #include "content/browser/background_sync/background_sync_context_impl.h" |
9 #include "content/browser/background_sync/background_sync_registration.h" | |
10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
11 | 12 |
12 namespace content { | 13 namespace content { |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 // TODO(iclelland): Move these converters to mojo::TypeConverter template | 17 // TODO(iclelland): Move these converters to mojo::TypeConverter template |
17 // specializations. | 18 // specializations. |
18 | 19 |
19 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( | 20 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( |
20 const SyncRegistrationPtr& in) { | 21 const SyncRegistrationPtr& in) { |
21 BackgroundSyncRegistrationOptions out; | 22 BackgroundSyncRegistrationOptions out; |
22 | 23 |
23 out.tag = in->tag; | 24 out.tag = in->tag; |
24 out.min_period = in->min_period_ms; | 25 out.min_period = in->min_period_ms; |
25 out.power_state = static_cast<SyncPowerState>(in->power_state); | 26 out.power_state = static_cast<SyncPowerState>(in->power_state); |
26 out.network_state = static_cast<SyncNetworkState>(in->network_state); | 27 out.network_state = static_cast<SyncNetworkState>(in->network_state); |
27 out.periodicity = static_cast<SyncPeriodicity>(in->periodicity); | 28 out.periodicity = static_cast<SyncPeriodicity>(in->periodicity); |
28 return out; | 29 return out; |
29 } | 30 } |
30 | 31 |
31 SyncRegistrationPtr ToMojoRegistration(const BackgroundSyncRegistration& in) { | 32 SyncRegistrationPtr ToMojoRegistration( |
33 const BackgroundSyncRegistrationHandle& in) { | |
32 SyncRegistrationPtr out(content::SyncRegistration::New()); | 34 SyncRegistrationPtr out(content::SyncRegistration::New()); |
33 out->id = in.id(); | 35 out->id = in.handle_id(); |
34 out->tag = in.options()->tag; | 36 out->tag = in.options()->tag; |
35 out->min_period_ms = in.options()->min_period; | 37 out->min_period_ms = in.options()->min_period; |
36 out->periodicity = static_cast<content::BackgroundSyncPeriodicity>( | 38 out->periodicity = static_cast<content::BackgroundSyncPeriodicity>( |
37 in.options()->periodicity); | 39 in.options()->periodicity); |
38 out->power_state = | 40 out->power_state = |
39 static_cast<content::BackgroundSyncPowerState>(in.options()->power_state); | 41 static_cast<content::BackgroundSyncPowerState>(in.options()->power_state); |
40 out->network_state = static_cast<content::BackgroundSyncNetworkState>( | 42 out->network_state = static_cast<content::BackgroundSyncNetworkState>( |
41 in.options()->network_state); | 43 in.options()->network_state); |
42 return out.Pass(); | 44 return out.Pass(); |
43 } | 45 } |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
81 | 83 |
82 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC, | 84 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC, |
83 SyncPeriodicity::SYNC_PERIODIC); | 85 SyncPeriodicity::SYNC_PERIODIC); |
84 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, | 86 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, |
85 SyncPeriodicity::SYNC_ONE_SHOT); | 87 SyncPeriodicity::SYNC_ONE_SHOT); |
86 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, | 88 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, |
87 SyncPeriodicity::SYNC_ONE_SHOT); | 89 SyncPeriodicity::SYNC_ONE_SHOT); |
88 | 90 |
89 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { | 91 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { |
90 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 92 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
93 DCHECK(background_sync_context_->background_sync_manager()); | |
91 } | 94 } |
92 | 95 |
93 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( | 96 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( |
94 BackgroundSyncContextImpl* background_sync_context, | 97 BackgroundSyncContextImpl* background_sync_context, |
95 mojo::InterfaceRequest<BackgroundSyncService> request) | 98 mojo::InterfaceRequest<BackgroundSyncService> request) |
96 : background_sync_context_(background_sync_context), | 99 : background_sync_context_(background_sync_context), |
97 binding_(this, request.Pass()), | 100 binding_(this, request.Pass()), |
98 weak_ptr_factory_(this) { | 101 weak_ptr_factory_(this) { |
99 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 102 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
100 DCHECK(background_sync_context); | 103 DCHECK(background_sync_context); |
(...skipping 21 matching lines...) Expand all Loading... | |
122 DCHECK(background_sync_manager); | 125 DCHECK(background_sync_manager); |
123 background_sync_manager->Register( | 126 background_sync_manager->Register( |
124 sw_registration_id, mgr_options, | 127 sw_registration_id, mgr_options, |
125 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, | 128 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, |
126 weak_ptr_factory_.GetWeakPtr(), callback)); | 129 weak_ptr_factory_.GetWeakPtr(), callback)); |
127 } | 130 } |
128 | 131 |
129 void BackgroundSyncServiceImpl::Unregister( | 132 void BackgroundSyncServiceImpl::Unregister( |
130 BackgroundSyncPeriodicity periodicity, | 133 BackgroundSyncPeriodicity periodicity, |
131 int64_t id, | 134 int64_t id, |
132 const mojo::String& tag, | |
133 int64_t sw_registration_id, | 135 int64_t sw_registration_id, |
134 const UnregisterCallback& callback) { | 136 const UnregisterCallback& callback) { |
135 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 137 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
136 BackgroundSyncManager* background_sync_manager = | 138 BackgroundSyncManager* background_sync_manager = |
137 background_sync_context_->background_sync_manager(); | 139 background_sync_context_->background_sync_manager(); |
138 DCHECK(background_sync_manager); | 140 DCHECK(background_sync_manager); |
139 background_sync_manager->Unregister( | 141 |
140 sw_registration_id, tag, content::SYNC_ONE_SHOT, id, | 142 BackgroundSyncRegistrationHandle* registration = active_handles_.Lookup(id); |
143 if (!registration) { | |
144 callback.Run(BACKGROUND_SYNC_ERROR_NOT_ALLOWED); | |
145 return; | |
146 } | |
147 | |
148 registration->Unregister( | |
149 sw_registration_id, | |
141 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, | 150 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, |
142 weak_ptr_factory_.GetWeakPtr(), callback)); | 151 weak_ptr_factory_.GetWeakPtr(), callback)); |
143 } | 152 } |
144 | 153 |
145 void BackgroundSyncServiceImpl::GetRegistration( | 154 void BackgroundSyncServiceImpl::GetRegistration( |
146 BackgroundSyncPeriodicity periodicity, | 155 BackgroundSyncPeriodicity periodicity, |
147 const mojo::String& tag, | 156 const mojo::String& tag, |
148 int64_t sw_registration_id, | 157 int64_t sw_registration_id, |
149 const GetRegistrationCallback& callback) { | 158 const GetRegistrationCallback& callback) { |
150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 159 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
(...skipping 25 matching lines...) Expand all Loading... | |
176 int64_t sw_registration_id, | 185 int64_t sw_registration_id, |
177 const GetPermissionStatusCallback& callback) { | 186 const GetPermissionStatusCallback& callback) { |
178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 187 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
179 | 188 |
180 // TODO(iclelland): Implement a real policy. This is a stub implementation. | 189 // TODO(iclelland): Implement a real policy. This is a stub implementation. |
181 // OneShot: crbug.com/482091 | 190 // OneShot: crbug.com/482091 |
182 // Periodic: crbug.com/482093 | 191 // Periodic: crbug.com/482093 |
183 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); | 192 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); |
184 } | 193 } |
185 | 194 |
195 void BackgroundSyncServiceImpl::DuplicateRegistrationHandle( | |
196 int64_t handle_id, | |
197 int64_t service_worker_registration_id, | |
michaeln
2015/08/28 02:53:11
if service_worker_registration_id needed? if not,
jkarlin
2015/09/02 23:51:40
Done.
| |
198 const DuplicateRegistrationHandleCallback& callback) { | |
199 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
200 BackgroundSyncManager* background_sync_manager = | |
201 background_sync_context_->background_sync_manager(); | |
202 DCHECK(background_sync_manager); | |
203 | |
204 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = | |
205 background_sync_manager->DuplicateRegistrationHandle( | |
206 service_worker_registration_id, handle_id); | |
207 | |
208 BackgroundSyncRegistrationHandle* handle_ptr = registration_handle.get(); | |
209 | |
210 if (!registration_handle) { | |
211 callback.Run(BACKGROUND_SYNC_ERROR_NOT_FOUND, | |
212 SyncRegistrationPtr(content::SyncRegistration::New())); | |
213 return; | |
214 } | |
215 | |
216 active_handles_.AddWithID(registration_handle.release(), | |
217 handle_ptr->handle_id()); | |
218 SyncRegistrationPtr mojoResult = ToMojoRegistration(*handle_ptr); | |
219 callback.Run(BACKGROUND_SYNC_ERROR_NONE, mojoResult.Pass()); | |
220 } | |
221 | |
222 void BackgroundSyncServiceImpl::ReleaseRegistration(int64_t handle_id) { | |
223 if (!active_handles_.Lookup(handle_id)) { | |
224 // TODO(jkarlin): Abort client. | |
225 LOG(WARNING) << "Client attempted to release non-existing registration"; | |
226 return; | |
227 } | |
228 | |
229 active_handles_.Remove(handle_id); | |
230 } | |
231 | |
186 void BackgroundSyncServiceImpl::OnRegisterResult( | 232 void BackgroundSyncServiceImpl::OnRegisterResult( |
187 const RegisterCallback& callback, | 233 const RegisterCallback& callback, |
188 BackgroundSyncStatus status, | 234 BackgroundSyncStatus status, |
189 const BackgroundSyncRegistration& result) { | 235 scoped_ptr<BackgroundSyncRegistrationHandle> result) { |
190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 236 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
191 SyncRegistrationPtr mojoResult = ToMojoRegistration(result); | 237 BackgroundSyncRegistrationHandle* result_ptr = result.get(); |
238 | |
239 if (status != BACKGROUND_SYNC_STATUS_OK) { | |
240 callback.Run(static_cast<content::BackgroundSyncError>(status), | |
241 SyncRegistrationPtr(content::SyncRegistration::New())); | |
242 return; | |
243 } | |
244 | |
245 active_handles_.AddWithID(result.release(), result_ptr->handle_id()); | |
246 SyncRegistrationPtr mojoResult = ToMojoRegistration(*result_ptr); | |
192 callback.Run(static_cast<content::BackgroundSyncError>(status), | 247 callback.Run(static_cast<content::BackgroundSyncError>(status), |
193 mojoResult.Pass()); | 248 mojoResult.Pass()); |
194 } | 249 } |
195 | 250 |
196 void BackgroundSyncServiceImpl::OnUnregisterResult( | 251 void BackgroundSyncServiceImpl::OnUnregisterResult( |
197 const UnregisterCallback& callback, | 252 const UnregisterCallback& callback, |
198 BackgroundSyncStatus status) { | 253 BackgroundSyncStatus status) { |
199 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 254 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
200 callback.Run(static_cast<content::BackgroundSyncError>(status)); | 255 callback.Run(static_cast<content::BackgroundSyncError>(status)); |
201 } | 256 } |
202 | 257 |
203 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( | 258 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( |
204 const GetRegistrationsCallback& callback, | 259 const GetRegistrationsCallback& callback, |
205 BackgroundSyncStatus status, | 260 BackgroundSyncStatus status, |
206 const std::vector<BackgroundSyncRegistration>& result_registrations) { | 261 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> |
262 result_registrations) { | |
207 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 263 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
208 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); | 264 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); |
209 for (const auto& registration : result_registrations) | 265 for (BackgroundSyncRegistrationHandle* registration : *result_registrations) { |
210 mojo_registrations.push_back(ToMojoRegistration(registration)); | 266 active_handles_.AddWithID(registration, registration->handle_id()); |
267 mojo_registrations.push_back(ToMojoRegistration(*registration)); | |
268 } | |
269 | |
270 result_registrations->weak_clear(); | |
271 | |
211 callback.Run(static_cast<content::BackgroundSyncError>(status), | 272 callback.Run(static_cast<content::BackgroundSyncError>(status), |
212 mojo_registrations.Pass()); | 273 mojo_registrations.Pass()); |
213 } | 274 } |
214 | 275 |
215 } // namespace content | 276 } // namespace content |
OLD | NEW |