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

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

Issue 1471763003: [BackgroundSync] Only allow SyncManager.register to occur from main frame (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@split
Patch Set: Address comments from PS5 Created 5 years 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_manager.h" 5 #include "content/browser/background_sync/background_sync_manager.h"
6 6
7 #include "base/barrier_closure.h" 7 #include "base/barrier_closure.h"
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/metrics/field_trial.h" 10 #include "base/metrics/field_trial.h"
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
174 const StatusAndRegistrationCallback& callback) { 174 const StatusAndRegistrationCallback& callback) {
175 DCHECK_CURRENTLY_ON(BrowserThread::IO); 175 DCHECK_CURRENTLY_ON(BrowserThread::IO);
176 176
177 if (disabled_) { 177 if (disabled_) {
178 BackgroundSyncMetrics::CountRegisterFailure( 178 BackgroundSyncMetrics::CountRegisterFailure(
179 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 179 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
180 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); 180 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
181 return; 181 return;
182 } 182 }
183 183
184 op_scheduler_.ScheduleOperation(base::Bind( 184 if (requested_from_service_worker) {
185 &BackgroundSyncManager::RegisterImpl, weak_ptr_factory_.GetWeakPtr(), 185 op_scheduler_.ScheduleOperation(
186 sw_registration_id, options, requested_from_service_worker, 186 base::Bind(&BackgroundSyncManager::RegisterCheckIfMainFrame,
187 MakeStatusAndRegistrationCompletion(callback))); 187 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options,
188 MakeStatusAndRegistrationCompletion(callback)));
189 return;
190 }
191
192 op_scheduler_.ScheduleOperation(
193 base::Bind(&BackgroundSyncManager::RegisterImpl,
194 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options,
195 MakeStatusAndRegistrationCompletion(callback)));
188 } 196 }
189 197
190 void BackgroundSyncManager::GetRegistration( 198 void BackgroundSyncManager::GetRegistration(
191 int64 sw_registration_id, 199 int64 sw_registration_id,
192 const std::string& sync_registration_tag, 200 const std::string& sync_registration_tag,
193 SyncPeriodicity periodicity, 201 SyncPeriodicity periodicity,
194 const StatusAndRegistrationCallback& callback) { 202 const StatusAndRegistrationCallback& callback) {
195 DCHECK_CURRENTLY_ON(BrowserThread::IO); 203 DCHECK_CURRENTLY_ON(BrowserThread::IO);
196 204
197 if (disabled_) { 205 if (disabled_) {
(...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after
385 DisableAndClearManager(base::Bind(callback)); 393 DisableAndClearManager(base::Bind(callback));
386 return; 394 return;
387 } 395 }
388 396
389 FireReadyEvents(); 397 FireReadyEvents();
390 398
391 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, 399 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE,
392 base::Bind(callback)); 400 base::Bind(callback));
393 } 401 }
394 402
403 void BackgroundSyncManager::RegisterCheckIfMainFrame(
iclelland 2015/11/25 14:58:16 I know it makes it a bit longer, but I'd prefer so
jkarlin 2015/11/25 16:47:25 Done.
404 int64 sw_registration_id,
405 const BackgroundSyncRegistrationOptions& options,
406 const StatusAndRegistrationCallback& callback) {
407 DCHECK_CURRENTLY_ON(BrowserThread::IO);
408
409 ServiceWorkerRegistration* sw_registration =
410 service_worker_context_->GetLiveRegistration(sw_registration_id);
411 if (!sw_registration || !sw_registration->active_version()) {
412 BackgroundSyncMetrics::CountRegisterFailure(
413 options.periodicity, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER);
414 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback);
415 return;
416 }
417
418 HasMainFrameProviderHost(
419 sw_registration->pattern().GetOrigin(),
420 base::Bind(&BackgroundSyncManager::RegisterDidCheckIfMainFrame,
421 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, options,
422 callback));
423 }
424
425 void BackgroundSyncManager::RegisterDidCheckIfMainFrame(
426 int64 sw_registration_id,
427 const BackgroundSyncRegistrationOptions& options,
428 const StatusAndRegistrationCallback& callback,
429 bool has_main_frame_client) {
430 DCHECK_CURRENTLY_ON(BrowserThread::IO);
431
432 if (!has_main_frame_client) {
433 BackgroundSyncMetrics::CountRegisterFailure(
434 options.periodicity, BACKGROUND_SYNC_STATUS_NOT_ALLOWED);
435 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback);
436 return;
437 }
438 RegisterImpl(sw_registration_id, options, callback);
439 }
440
395 void BackgroundSyncManager::RegisterImpl( 441 void BackgroundSyncManager::RegisterImpl(
396 int64 sw_registration_id, 442 int64 sw_registration_id,
397 const BackgroundSyncRegistrationOptions& options, 443 const BackgroundSyncRegistrationOptions& options,
398 bool requested_from_service_worker,
399 const StatusAndRegistrationCallback& callback) { 444 const StatusAndRegistrationCallback& callback) {
400 DCHECK_CURRENTLY_ON(BrowserThread::IO); 445 DCHECK_CURRENTLY_ON(BrowserThread::IO);
401 446
402 if (disabled_) { 447 if (disabled_) {
403 BackgroundSyncMetrics::CountRegisterFailure( 448 BackgroundSyncMetrics::CountRegisterFailure(
404 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR); 449 options.periodicity, BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
405 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback); 450 PostErrorResponse(BACKGROUND_SYNC_STATUS_STORAGE_ERROR, callback);
406 return; 451 return;
407 } 452 }
408 453
(...skipping 13 matching lines...) Expand all
422 467
423 ServiceWorkerRegistration* sw_registration = 468 ServiceWorkerRegistration* sw_registration =
424 service_worker_context_->GetLiveRegistration(sw_registration_id); 469 service_worker_context_->GetLiveRegistration(sw_registration_id);
425 if (!sw_registration || !sw_registration->active_version()) { 470 if (!sw_registration || !sw_registration->active_version()) {
426 BackgroundSyncMetrics::CountRegisterFailure( 471 BackgroundSyncMetrics::CountRegisterFailure(
427 options.periodicity, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER); 472 options.periodicity, BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER);
428 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback); 473 PostErrorResponse(BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER, callback);
429 return; 474 return;
430 } 475 }
431 476
432 if (requested_from_service_worker &&
433 !service_worker_context_->HasWindowProviderHost(
434 sw_registration->pattern().GetOrigin())) {
435 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback);
436 return;
437 }
438
439 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, 477 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
440 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread, 478 base::Bind(&NotifyBackgroundSyncRegisteredOnUIThread,
441 service_worker_context_, 479 service_worker_context_,
442 sw_registration->pattern().GetOrigin())); 480 sw_registration->pattern().GetOrigin()));
443 481
444 RefCountedRegistration* existing_registration_ref = 482 RefCountedRegistration* existing_registration_ref =
445 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); 483 LookupActiveRegistration(sw_registration_id, RegistrationKey(options));
446 if (existing_registration_ref) { 484 if (existing_registration_ref) {
447 if (existing_registration_ref->value()->options()->Equals(options)) { 485 if (existing_registration_ref->value()->options()->Equals(options)) {
448 BackgroundSyncRegistration* existing_registration = 486 BackgroundSyncRegistration* existing_registration =
(...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after
731 // (with a new unique handle id). 769 // (with a new unique handle id).
732 active_version->DispatchSyncEvent(handle_id, last_chance, callback); 770 active_version->DispatchSyncEvent(handle_id, last_chance, callback);
733 } 771 }
734 772
735 void BackgroundSyncManager::ScheduleDelayedTask(const base::Closure& callback, 773 void BackgroundSyncManager::ScheduleDelayedTask(const base::Closure& callback,
736 base::TimeDelta delay) { 774 base::TimeDelta delay) {
737 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, callback, 775 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE, callback,
738 delay); 776 delay);
739 } 777 }
740 778
779 void BackgroundSyncManager::HasMainFrameProviderHost(
780 const GURL& origin,
781 const BoolCallback& callback) {
782 service_worker_context_->HasMainFrameProviderHost(origin, callback);
783 }
784
741 scoped_ptr<BackgroundSyncRegistrationHandle> 785 scoped_ptr<BackgroundSyncRegistrationHandle>
742 BackgroundSyncManager::CreateRegistrationHandle( 786 BackgroundSyncManager::CreateRegistrationHandle(
743 const scoped_refptr<RefCountedRegistration>& registration) { 787 const scoped_refptr<RefCountedRegistration>& registration) {
744 scoped_refptr<RefCountedRegistration>* ptr = 788 scoped_refptr<RefCountedRegistration>* ptr =
745 new scoped_refptr<RefCountedRegistration>(registration); 789 new scoped_refptr<RefCountedRegistration>(registration);
746 790
747 // Registration handles have unique handle ids. The handle id maps to an 791 // Registration handles have unique handle ids. The handle id maps to an
748 // internal RefCountedRegistration (which has the persistent registration id) 792 // internal RefCountedRegistration (which has the persistent registration id)
749 // via 793 // via
750 // registration_reference_ids_. 794 // registration_reference_ids_.
(...skipping 473 matching lines...) Expand 10 before | Expand all | Expand 10 after
1224 1268
1225 registration->set_num_attempts(registration->num_attempts() + 1); 1269 registration->set_num_attempts(registration->num_attempts() + 1);
1226 1270
1227 num_firing_registrations_ -= 1; 1271 num_firing_registrations_ -= 1;
1228 1272
1229 // The event ran to completion, we should count it, no matter what happens 1273 // The event ran to completion, we should count it, no matter what happens
1230 // from here. 1274 // from here.
1231 ServiceWorkerRegistration* sw_registration = 1275 ServiceWorkerRegistration* sw_registration =
1232 service_worker_context_->GetLiveRegistration(service_worker_id); 1276 service_worker_context_->GetLiveRegistration(service_worker_id);
1233 if (sw_registration) { 1277 if (sw_registration) {
1234 bool foreground = service_worker_context_->HasWindowProviderHost( 1278 HasMainFrameProviderHost(
1235 sw_registration->pattern().GetOrigin()); 1279 sw_registration->pattern().GetOrigin(),
1236 BackgroundSyncMetrics::RecordEventResult( 1280 base::Bind(&BackgroundSyncMetrics::RecordEventResult,
1237 registration->options()->periodicity, status_code == SERVICE_WORKER_OK, 1281 registration->options()->periodicity,
1238 foreground); 1282 status_code == SERVICE_WORKER_OK));
1239 } 1283 }
1240 1284
1241 if (registration->options()->periodicity == SYNC_ONE_SHOT) { 1285 if (registration->options()->periodicity == SYNC_ONE_SHOT) {
1242 if (registration->sync_state() == 1286 if (registration->sync_state() ==
1243 BACKGROUND_SYNC_STATE_REREGISTERED_WHILE_FIRING) { 1287 BACKGROUND_SYNC_STATE_REREGISTERED_WHILE_FIRING) {
1244 registration->set_sync_state(BACKGROUND_SYNC_STATE_PENDING); 1288 registration->set_sync_state(BACKGROUND_SYNC_STATE_PENDING);
1245 registration->set_num_attempts(0); 1289 registration->set_num_attempts(0);
1246 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed 1290 } else if (status_code != SERVICE_WORKER_OK) { // Sync failed
1247 bool can_retry = registration->num_attempts() < max_sync_attempts_; 1291 bool can_retry = registration->num_attempts() < max_sync_attempts_;
1248 if (registration->sync_state() == 1292 if (registration->sync_state() ==
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after
1431 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { 1475 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) {
1432 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1476 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1433 1477
1434 return base::Bind( 1478 return base::Bind(
1435 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, 1479 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback,
1436 BackgroundSyncStatus>, 1480 BackgroundSyncStatus>,
1437 weak_ptr_factory_.GetWeakPtr(), callback); 1481 weak_ptr_factory_.GetWeakPtr(), callback);
1438 } 1482 }
1439 1483
1440 } // namespace content 1484 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698