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

Side by Side Diff: content/browser/background_sync/background_sync_service_impl.cc

Issue 1282013004: BackgroundSyncManager tracks client registrations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Android fix Created 5 years, 4 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
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 "base/memory/weak_ptr.h" 7 #include "base/memory/weak_ptr.h"
8 #include "base/stl_util.h"
8 #include "content/browser/background_sync/background_sync_context_impl.h" 9 #include "content/browser/background_sync/background_sync_context_impl.h"
9 #include "content/browser/background_sync/background_sync_registration.h" 10 #include "content/browser/background_sync/client_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(
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
81 82
82 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC, 83 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC,
83 SyncPeriodicity::SYNC_PERIODIC); 84 SyncPeriodicity::SYNC_PERIODIC);
84 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, 85 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT,
85 SyncPeriodicity::SYNC_ONE_SHOT); 86 SyncPeriodicity::SYNC_ONE_SHOT);
86 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, 87 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX,
87 SyncPeriodicity::SYNC_ONE_SHOT); 88 SyncPeriodicity::SYNC_ONE_SHOT);
88 89
89 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { 90 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() {
90 DCHECK_CURRENTLY_ON(BrowserThread::IO); 91 DCHECK_CURRENTLY_ON(BrowserThread::IO);
92 BackgroundSyncManager* background_sync_manager =
93 background_sync_context_->background_sync_manager();
94 DCHECK(background_sync_manager);
iclelland 2015/08/14 14:44:19 What's the purpose of this check on destruction?
jkarlin 2015/08/17 17:14:49 Just to verify that services delete before the man
91 } 95 }
92 96
93 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( 97 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl(
94 BackgroundSyncContextImpl* background_sync_context, 98 BackgroundSyncContextImpl* background_sync_context,
95 mojo::InterfaceRequest<BackgroundSyncService> request) 99 mojo::InterfaceRequest<BackgroundSyncService> request)
96 : background_sync_context_(background_sync_context), 100 : background_sync_context_(background_sync_context),
97 binding_(this, request.Pass()), 101 binding_(this, request.Pass()),
98 weak_ptr_factory_(this) { 102 weak_ptr_factory_(this) {
99 DCHECK_CURRENTLY_ON(BrowserThread::IO); 103 DCHECK_CURRENTLY_ON(BrowserThread::IO);
100 DCHECK(background_sync_context); 104 DCHECK(background_sync_context);
(...skipping 21 matching lines...) Expand all
122 DCHECK(background_sync_manager); 126 DCHECK(background_sync_manager);
123 background_sync_manager->Register( 127 background_sync_manager->Register(
124 sw_registration_id, mgr_options, 128 sw_registration_id, mgr_options,
125 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, 129 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult,
126 weak_ptr_factory_.GetWeakPtr(), callback)); 130 weak_ptr_factory_.GetWeakPtr(), callback));
127 } 131 }
128 132
129 void BackgroundSyncServiceImpl::Unregister( 133 void BackgroundSyncServiceImpl::Unregister(
130 BackgroundSyncPeriodicity periodicity, 134 BackgroundSyncPeriodicity periodicity,
131 int64_t id, 135 int64_t id,
132 const mojo::String& tag,
133 int64_t sw_registration_id, 136 int64_t sw_registration_id,
134 const UnregisterCallback& callback) { 137 const UnregisterCallback& callback) {
135 DCHECK_CURRENTLY_ON(BrowserThread::IO); 138 DCHECK_CURRENTLY_ON(BrowserThread::IO);
136 BackgroundSyncManager* background_sync_manager = 139 BackgroundSyncManager* background_sync_manager =
137 background_sync_context_->background_sync_manager(); 140 background_sync_context_->background_sync_manager();
138 DCHECK(background_sync_manager); 141 DCHECK(background_sync_manager);
139 background_sync_manager->Unregister( 142
140 sw_registration_id, tag, content::SYNC_ONE_SHOT, id, 143 ClientBackgroundSyncRegistration* registration =
144 hosted_registrations_.Lookup(id);
145 if (!registration) {
146 callback.Run(BACKGROUND_SYNC_ERROR_NOT_ALLOWED);
147 return;
148 }
149
150 registration->Unregister(
151 sw_registration_id,
141 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, 152 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult,
142 weak_ptr_factory_.GetWeakPtr(), callback)); 153 weak_ptr_factory_.GetWeakPtr(), callback));
143 } 154 }
144 155
145 void BackgroundSyncServiceImpl::GetRegistration( 156 void BackgroundSyncServiceImpl::GetRegistration(
146 BackgroundSyncPeriodicity periodicity, 157 BackgroundSyncPeriodicity periodicity,
147 const mojo::String& tag, 158 const mojo::String& tag,
148 int64_t sw_registration_id, 159 int64_t sw_registration_id,
149 const GetRegistrationCallback& callback) { 160 const GetRegistrationCallback& callback) {
150 DCHECK_CURRENTLY_ON(BrowserThread::IO); 161 DCHECK_CURRENTLY_ON(BrowserThread::IO);
(...skipping 25 matching lines...) Expand all
176 int64_t sw_registration_id, 187 int64_t sw_registration_id,
177 const GetPermissionStatusCallback& callback) { 188 const GetPermissionStatusCallback& callback) {
178 DCHECK_CURRENTLY_ON(BrowserThread::IO); 189 DCHECK_CURRENTLY_ON(BrowserThread::IO);
179 190
180 // TODO(iclelland): Implement a real policy. This is a stub implementation. 191 // TODO(iclelland): Implement a real policy. This is a stub implementation.
181 // OneShot: crbug.com/482091 192 // OneShot: crbug.com/482091
182 // Periodic: crbug.com/482093 193 // Periodic: crbug.com/482093
183 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); 194 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED);
184 } 195 }
185 196
197 void BackgroundSyncServiceImpl::TrackRegistration(
198 SyncRegistrationPtr sync_registration) {
199 // Registrations that the client acquires without the
200 // BackgroundSyncServiceImpl's knowing are registered here.
201 BackgroundSyncManager* background_sync_manager =
202 background_sync_context_->background_sync_manager();
203 DCHECK(background_sync_manager);
204
205 BackgroundSyncRegistrationOptions options =
206 ToBackgroundSyncRegistrationOptions(sync_registration);
207
208 scoped_ptr<ClientBackgroundSyncRegistration> registration(
209 new ClientBackgroundSyncRegistration(background_sync_manager));
210 *registration->options() = options;
211 registration->set_id(sync_registration->id);
212
213 if (hosted_registrations_.Lookup(sync_registration->id)) {
214 // TODO(jkarlin): Abort client.
215 LOG(WARNING) << "Client attempted to track existing registration";
216 return;
217 }
218
219 hosted_registrations_.AddWithID(registration.release(),
220 sync_registration->id);
221 }
222
223 void BackgroundSyncServiceImpl::ReleaseRegistration(int64_t sync_id) {
224 if (!hosted_registrations_.Lookup(sync_id)) {
225 // TODO(jkarlin): Abort client.
iclelland 2015/08/14 14:44:18 As in BackgroundSyncManager -- what happens if we
jkarlin 2015/08/17 17:14:49 We should abort long term :) In the meanwhile, a c
iclelland 2015/08/17 19:22:02 As long as it's not a browser-destablising situati
226 LOG(WARNING) << "Client attempted to release non-existing registration";
227 return;
228 }
229
230 hosted_registrations_.Remove(sync_id);
231 }
232
186 void BackgroundSyncServiceImpl::OnRegisterResult( 233 void BackgroundSyncServiceImpl::OnRegisterResult(
187 const RegisterCallback& callback, 234 const RegisterCallback& callback,
188 BackgroundSyncStatus status, 235 BackgroundSyncStatus status,
189 const BackgroundSyncRegistration& result) { 236 scoped_ptr<ClientBackgroundSyncRegistration> result) {
190 DCHECK_CURRENTLY_ON(BrowserThread::IO); 237 DCHECK_CURRENTLY_ON(BrowserThread::IO);
191 SyncRegistrationPtr mojoResult = ToMojoRegistration(result); 238 ClientBackgroundSyncRegistration* result_ptr = result.get();
239
240 if (status != BACKGROUND_SYNC_STATUS_OK) {
241 callback.Run(static_cast<content::BackgroundSyncError>(status),
242 SyncRegistrationPtr(content::SyncRegistration::New()));
243 return;
244 }
245
246 hosted_registrations_.AddWithID(result.release(), result_ptr->id());
247 SyncRegistrationPtr mojoResult = ToMojoRegistration(*result_ptr);
192 callback.Run(static_cast<content::BackgroundSyncError>(status), 248 callback.Run(static_cast<content::BackgroundSyncError>(status),
193 mojoResult.Pass()); 249 mojoResult.Pass());
194 } 250 }
195 251
196 void BackgroundSyncServiceImpl::OnUnregisterResult( 252 void BackgroundSyncServiceImpl::OnUnregisterResult(
197 const UnregisterCallback& callback, 253 const UnregisterCallback& callback,
198 BackgroundSyncStatus status) { 254 BackgroundSyncStatus status) {
199 DCHECK_CURRENTLY_ON(BrowserThread::IO); 255 DCHECK_CURRENTLY_ON(BrowserThread::IO);
200 callback.Run(static_cast<content::BackgroundSyncError>(status)); 256 callback.Run(static_cast<content::BackgroundSyncError>(status));
201 } 257 }
202 258
203 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( 259 void BackgroundSyncServiceImpl::OnGetRegistrationsResult(
204 const GetRegistrationsCallback& callback, 260 const GetRegistrationsCallback& callback,
205 BackgroundSyncStatus status, 261 BackgroundSyncStatus status,
206 const std::vector<BackgroundSyncRegistration>& result_registrations) { 262 scoped_ptr<ScopedVector<ClientBackgroundSyncRegistration>>
263 result_registrations) {
207 DCHECK_CURRENTLY_ON(BrowserThread::IO); 264 DCHECK_CURRENTLY_ON(BrowserThread::IO);
208 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); 265 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0);
209 for (const auto& registration : result_registrations) 266 for (ClientBackgroundSyncRegistration* registration : *result_registrations) {
210 mojo_registrations.push_back(ToMojoRegistration(registration)); 267 hosted_registrations_.AddWithID(registration, registration->id());
268 mojo_registrations.push_back(ToMojoRegistration(*registration));
269 }
270
271 result_registrations->weak_clear();
272
211 callback.Run(static_cast<content::BackgroundSyncError>(status), 273 callback.Run(static_cast<content::BackgroundSyncError>(status),
212 mojo_registrations.Pass()); 274 mojo_registrations.Pass());
213 } 275 }
214 276
215 } // namespace content 277 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698