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_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/single_thread_task_runner.h" | 10 #include "base/single_thread_task_runner.h" |
(...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 BackgroundSyncRegistration* registration = ref_registration->value(); | 285 BackgroundSyncRegistration* registration = ref_registration->value(); |
286 | 286 |
287 BackgroundSyncRegistrationOptions* options = registration->options(); | 287 BackgroundSyncRegistrationOptions* options = registration->options(); |
288 options->tag = registration_proto.tag(); | 288 options->tag = registration_proto.tag(); |
289 options->periodicity = registration_proto.periodicity(); | 289 options->periodicity = registration_proto.periodicity(); |
290 options->min_period = registration_proto.min_period(); | 290 options->min_period = registration_proto.min_period(); |
291 options->network_state = registration_proto.network_state(); | 291 options->network_state = registration_proto.network_state(); |
292 options->power_state = registration_proto.power_state(); | 292 options->power_state = registration_proto.power_state(); |
293 | 293 |
294 registration->set_id(registration_proto.id()); | 294 registration->set_id(registration_proto.id()); |
295 registration->set_sync_state(registration_proto.sync_state()); | |
296 | |
297 if (registration->sync_state() == SYNC_STATE_FIRING) { | |
298 // If the browser (or worker) closed while firing the event, consider | |
299 // it pending again> | |
300 registration->set_sync_state(SYNC_STATE_PENDING); | |
301 } | |
302 } | 295 } |
303 } | 296 } |
304 | 297 |
305 if (corruption_detected) | 298 if (corruption_detected) |
306 break; | 299 break; |
307 } | 300 } |
308 | 301 |
309 if (corruption_detected) { | 302 if (corruption_detected) { |
310 LOG(ERROR) << "Corruption detected in background sync backend"; | 303 LOG(ERROR) << "Corruption detected in background sync backend"; |
311 DisableAndClearManager(base::Bind(callback)); | 304 DisableAndClearManager(base::Bind(callback)); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
356 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); | 349 PostErrorResponse(BACKGROUND_SYNC_STATUS_NOT_ALLOWED, callback); |
357 return; | 350 return; |
358 } | 351 } |
359 | 352 |
360 RefCountedRegistration* existing_registration_ref = | 353 RefCountedRegistration* existing_registration_ref = |
361 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); | 354 LookupActiveRegistration(sw_registration_id, RegistrationKey(options)); |
362 if (existing_registration_ref && | 355 if (existing_registration_ref && |
363 existing_registration_ref->value()->options()->Equals(options)) { | 356 existing_registration_ref->value()->options()->Equals(options)) { |
364 BackgroundSyncRegistration* existing_registration = | 357 BackgroundSyncRegistration* existing_registration = |
365 existing_registration_ref->value(); | 358 existing_registration_ref->value(); |
366 if (existing_registration->sync_state() == SYNC_STATE_FAILED) { | 359 if (existing_registration->sync_state() == BACKGROUND_SYNC_STATE_FAILED) { |
367 existing_registration->set_sync_state(SYNC_STATE_PENDING); | 360 existing_registration->set_sync_state(BACKGROUND_SYNC_STATE_PENDING); |
368 StoreRegistrations( | 361 StoreRegistrations( |
369 sw_registration_id, | 362 sw_registration_id, |
370 base::Bind(&BackgroundSyncManager::RegisterDidStore, | 363 base::Bind(&BackgroundSyncManager::RegisterDidStore, |
371 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, | 364 weak_ptr_factory_.GetWeakPtr(), sw_registration_id, |
372 make_scoped_refptr(existing_registration_ref), callback)); | 365 make_scoped_refptr(existing_registration_ref), callback)); |
373 return; | 366 return; |
374 } | 367 } |
375 | 368 |
376 // Record the duplicated registration | 369 // Record the duplicated registration |
377 BackgroundSyncMetrics::CountRegister( | 370 BackgroundSyncMetrics::CountRegister( |
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
498 BackgroundSyncRegistrationsProto registrations_proto; | 491 BackgroundSyncRegistrationsProto registrations_proto; |
499 registrations_proto.set_next_registration_id(registrations.next_id); | 492 registrations_proto.set_next_registration_id(registrations.next_id); |
500 registrations_proto.set_origin(registrations.origin.spec()); | 493 registrations_proto.set_origin(registrations.origin.spec()); |
501 | 494 |
502 for (const auto& key_and_registration : registrations.registration_map) { | 495 for (const auto& key_and_registration : registrations.registration_map) { |
503 const BackgroundSyncRegistration& registration = | 496 const BackgroundSyncRegistration& registration = |
504 *key_and_registration.second->value(); | 497 *key_and_registration.second->value(); |
505 BackgroundSyncRegistrationProto* registration_proto = | 498 BackgroundSyncRegistrationProto* registration_proto = |
506 registrations_proto.add_registration(); | 499 registrations_proto.add_registration(); |
507 registration_proto->set_id(registration.id()); | 500 registration_proto->set_id(registration.id()); |
508 registration_proto->set_sync_state(registration.sync_state()); | |
509 registration_proto->set_tag(registration.options()->tag); | 501 registration_proto->set_tag(registration.options()->tag); |
510 registration_proto->set_periodicity(registration.options()->periodicity); | 502 registration_proto->set_periodicity(registration.options()->periodicity); |
511 registration_proto->set_min_period(registration.options()->min_period); | 503 registration_proto->set_min_period(registration.options()->min_period); |
512 registration_proto->set_network_state( | 504 registration_proto->set_network_state( |
513 registration.options()->network_state); | 505 registration.options()->network_state); |
514 registration_proto->set_power_state(registration.options()->power_state); | 506 registration_proto->set_power_state(registration.options()->power_state); |
515 } | 507 } |
516 std::string serialized; | 508 std::string serialized; |
517 bool success = registrations_proto.SerializeToString(&serialized); | 509 bool success = registrations_proto.SerializeToString(&serialized); |
518 DCHECK(success); | 510 DCHECK(success); |
(...skipping 310 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
829 } | 821 } |
830 | 822 |
831 bool BackgroundSyncManager::IsRegistrationReadyToFire( | 823 bool BackgroundSyncManager::IsRegistrationReadyToFire( |
832 const BackgroundSyncRegistration& registration) { | 824 const BackgroundSyncRegistration& registration) { |
833 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 825 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
834 | 826 |
835 // TODO(jkarlin): Add support for firing periodic registrations. | 827 // TODO(jkarlin): Add support for firing periodic registrations. |
836 if (registration.options()->periodicity == SYNC_PERIODIC) | 828 if (registration.options()->periodicity == SYNC_PERIODIC) |
837 return false; | 829 return false; |
838 | 830 |
839 if (registration.sync_state() != SYNC_STATE_PENDING) | 831 if (registration.sync_state() != BACKGROUND_SYNC_STATE_PENDING) |
840 return false; | 832 return false; |
841 | 833 |
842 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); | 834 DCHECK_EQ(SYNC_ONE_SHOT, registration.options()->periodicity); |
843 | 835 |
844 return AreOptionConditionsMet(*registration.options()); | 836 return AreOptionConditionsMet(*registration.options()); |
845 } | 837 } |
846 | 838 |
847 void BackgroundSyncManager::SchedulePendingRegistrations() { | 839 void BackgroundSyncManager::SchedulePendingRegistrations() { |
848 #if defined(OS_ANDROID) | 840 #if defined(OS_ANDROID) |
849 bool keep_browser_alive_for_one_shot = false; | 841 bool keep_browser_alive_for_one_shot = false; |
850 | 842 |
851 for (const auto& sw_id_and_registrations : active_registrations_) { | 843 for (const auto& sw_id_and_registrations : active_registrations_) { |
852 for (const auto& key_and_registration : | 844 for (const auto& key_and_registration : |
853 sw_id_and_registrations.second.registration_map) { | 845 sw_id_and_registrations.second.registration_map) { |
854 const BackgroundSyncRegistration& registration = | 846 const BackgroundSyncRegistration& registration = |
855 *key_and_registration.second->value(); | 847 *key_and_registration.second->value(); |
856 if (registration.sync_state() == SYNC_STATE_PENDING) { | 848 if (registration.sync_state() == BACKGROUND_SYNC_STATE_PENDING) { |
857 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 849 if (registration.options()->periodicity == SYNC_ONE_SHOT) { |
858 keep_browser_alive_for_one_shot = true; | 850 keep_browser_alive_for_one_shot = true; |
859 } else { | 851 } else { |
860 // TODO(jkarlin): Support keeping the browser alive for periodic | 852 // TODO(jkarlin): Support keeping the browser alive for periodic |
861 // syncs. | 853 // syncs. |
862 } | 854 } |
863 } | 855 } |
864 } | 856 } |
865 } | 857 } |
866 | 858 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
903 for (auto& key_and_registration : | 895 for (auto& key_and_registration : |
904 sw_id_and_registrations.second.registration_map) { | 896 sw_id_and_registrations.second.registration_map) { |
905 BackgroundSyncRegistration* registration = | 897 BackgroundSyncRegistration* registration = |
906 key_and_registration.second->value(); | 898 key_and_registration.second->value(); |
907 if (IsRegistrationReadyToFire(*registration)) { | 899 if (IsRegistrationReadyToFire(*registration)) { |
908 sw_id_and_keys_to_fire.push_back( | 900 sw_id_and_keys_to_fire.push_back( |
909 std::make_pair(service_worker_id, key_and_registration.first)); | 901 std::make_pair(service_worker_id, key_and_registration.first)); |
910 // The state change is not saved to persistent storage because | 902 // The state change is not saved to persistent storage because |
911 // if the sync event is killed mid-sync then it should return to | 903 // if the sync event is killed mid-sync then it should return to |
912 // SYNC_STATE_PENDING. | 904 // SYNC_STATE_PENDING. |
913 registration->set_sync_state(SYNC_STATE_FIRING); | 905 registration->set_sync_state(BACKGROUND_SYNC_STATE_FIRING); |
914 } | 906 } |
915 } | 907 } |
916 } | 908 } |
917 | 909 |
918 // If there are no registrations currently ready, then just run |callback|. | 910 // If there are no registrations currently ready, then just run |callback|. |
919 // Otherwise, fire them all, and record the result when done. | 911 // Otherwise, fire them all, and record the result when done. |
920 if (sw_id_and_keys_to_fire.size() == 0) { | 912 if (sw_id_and_keys_to_fire.size() == 0) { |
921 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 913 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
922 base::Bind(callback)); | 914 base::Bind(callback)); |
923 } else { | 915 } else { |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1028 | 1020 |
1029 // The event ran to completion, we should count it, no matter what happens | 1021 // The event ran to completion, we should count it, no matter what happens |
1030 // from here. | 1022 // from here. |
1031 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, | 1023 BackgroundSyncMetrics::RecordEventResult(registration->options()->periodicity, |
1032 status_code == SERVICE_WORKER_OK); | 1024 status_code == SERVICE_WORKER_OK); |
1033 | 1025 |
1034 if (registration->options()->periodicity == SYNC_ONE_SHOT) { | 1026 if (registration->options()->periodicity == SYNC_ONE_SHOT) { |
1035 if (status_code != SERVICE_WORKER_OK) { | 1027 if (status_code != SERVICE_WORKER_OK) { |
1036 // TODO(jkarlin) Fire the sync event on the next page load controlled by | 1028 // TODO(jkarlin) Fire the sync event on the next page load controlled by |
1037 // this registration. (crbug.com/479665) | 1029 // this registration. (crbug.com/479665) |
1038 registration->set_sync_state(SYNC_STATE_FAILED); | 1030 registration->set_sync_state(BACKGROUND_SYNC_STATE_FAILED); |
1039 } else { | 1031 } else { |
1040 RegistrationKey key(*registration); | 1032 RegistrationKey key(*registration); |
1041 // Remove the registration if it's still active. | 1033 // Remove the registration if it's still active. |
1042 RefCountedRegistration* active_registration = | 1034 RefCountedRegistration* active_registration = |
1043 LookupActiveRegistration(service_worker_id, key); | 1035 LookupActiveRegistration(service_worker_id, key); |
1044 if (active_registration && | 1036 if (active_registration && |
1045 active_registration->value()->id() == registration->id()) | 1037 active_registration->value()->id() == registration->id()) |
1046 RemoveActiveRegistration(service_worker_id, key); | 1038 RemoveActiveRegistration(service_worker_id, key); |
1047 } | 1039 } |
1048 } else { | 1040 } else { |
(...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1192 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1184 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1193 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1185 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1194 | 1186 |
1195 return base::Bind( | 1187 return base::Bind( |
1196 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1188 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1197 BackgroundSyncStatus>, | 1189 BackgroundSyncStatus>, |
1198 weak_ptr_factory_.GetWeakPtr(), callback); | 1190 weak_ptr_factory_.GetWeakPtr(), callback); |
1199 } | 1191 } |
1200 | 1192 |
1201 } // namespace content | 1193 } // namespace content |
OLD | NEW |