| 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/metrics/field_trial.h" | 10 #include "base/metrics/field_trial.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "content/browser/background_sync/background_sync_metrics.h" | 13 #include "content/browser/background_sync/background_sync_metrics.h" |
| 14 #include "content/browser/background_sync/background_sync_network_observer.h" | 14 #include "content/browser/background_sync/background_sync_network_observer.h" |
| 15 #include "content/browser/background_sync/background_sync_power_observer.h" | 15 #include "content/browser/background_sync/background_sync_power_observer.h" |
| 16 #include "content/browser/background_sync/background_sync_registration_handle.h" | 16 #include "content/browser/background_sync/background_sync_registration_handle.h" |
| 17 #include "content/browser/background_sync/background_sync_registration_options.h
" | 17 #include "content/browser/background_sync/background_sync_registration_options.h
" |
| 18 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 18 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
| 19 #include "content/browser/service_worker/service_worker_storage.h" | 19 #include "content/browser/service_worker/service_worker_storage.h" |
| 20 #include "content/browser/storage_partition_impl.h" | 20 #include "content/browser/storage_partition_impl.h" |
| 21 #include "content/public/browser/background_sync_controller.h" | 21 #include "content/public/browser/background_sync_controller.h" |
| 22 #include "content/public/browser/browser_context.h" | 22 #include "content/public/browser/browser_context.h" |
| 23 #include "content/public/browser/browser_thread.h" | 23 #include "content/public/browser/browser_thread.h" |
| 24 | 24 |
| 25 #if defined(OS_ANDROID) | 25 #if defined(OS_ANDROID) |
| 26 #include "content/browser/android/background_sync_launcher_android.h" | |
| 27 #include "content/browser/android/background_sync_network_observer_android.h" | 26 #include "content/browser/android/background_sync_network_observer_android.h" |
| 28 #endif | 27 #endif |
| 29 | 28 |
| 30 namespace content { | 29 namespace content { |
| 31 | 30 |
| 32 class BackgroundSyncManager::RefCountedRegistration | 31 class BackgroundSyncManager::RefCountedRegistration |
| 33 : public base::RefCounted<RefCountedRegistration> { | 32 : public base::RefCounted<RefCountedRegistration> { |
| 34 public: | 33 public: |
| 35 BackgroundSyncRegistration* value() { return ®istration_; } | 34 BackgroundSyncRegistration* value() { return ®istration_; } |
| 36 const BackgroundSyncRegistration* value() const { return ®istration_; } | 35 const BackgroundSyncRegistration* value() const { return ®istration_; } |
| (...skipping 18 matching lines...) Expand all Loading... |
| 55 callback, status, | 54 callback, status, |
| 56 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); | 55 base::Passed(scoped_ptr<BackgroundSyncRegistrationHandle>().Pass()))); |
| 57 } | 56 } |
| 58 | 57 |
| 59 bool ShouldDisableForFieldTrial() { | 58 bool ShouldDisableForFieldTrial() { |
| 60 std::string experiment = base::FieldTrialList::FindFullName("BackgroundSync"); | 59 std::string experiment = base::FieldTrialList::FindFullName("BackgroundSync"); |
| 61 return base::StartsWith(experiment, "ExperimentDisable", | 60 return base::StartsWith(experiment, "ExperimentDisable", |
| 62 base::CompareCase::INSENSITIVE_ASCII); | 61 base::CompareCase::INSENSITIVE_ASCII); |
| 63 } | 62 } |
| 64 | 63 |
| 64 // Returns nullptr if the controller cannot be accessed for any reason. |
| 65 BackgroundSyncController* GetBackgroundSyncControllerOnUIThread( |
| 66 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) { |
| 67 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 68 |
| 69 if (!service_worker_context) |
| 70 return nullptr; |
| 71 StoragePartitionImpl* storage_partition_impl = |
| 72 service_worker_context->storage_partition(); |
| 73 if (!storage_partition_impl) // may be null in tests |
| 74 return nullptr; |
| 75 |
| 76 return storage_partition_impl->browser_context() |
| 77 ->GetBackgroundSyncController(); |
| 78 } |
| 79 |
| 65 void NotifyBackgroundSyncRegisteredOnUIThread( | 80 void NotifyBackgroundSyncRegisteredOnUIThread( |
| 66 const scoped_refptr<ServiceWorkerContextWrapper>& sw_context_wrapper, | 81 const scoped_refptr<ServiceWorkerContextWrapper>& sw_context_wrapper, |
| 67 const GURL& origin) { | 82 const GURL& origin) { |
| 68 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 83 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 69 | 84 |
| 70 StoragePartitionImpl* storage_partition_impl = | |
| 71 sw_context_wrapper->storage_partition(); | |
| 72 if (!storage_partition_impl) // happens in tests | |
| 73 return; | |
| 74 | |
| 75 BackgroundSyncController* background_sync_controller = | 85 BackgroundSyncController* background_sync_controller = |
| 76 storage_partition_impl->browser_context()->GetBackgroundSyncController(); | 86 GetBackgroundSyncControllerOnUIThread(sw_context_wrapper); |
| 77 | 87 |
| 78 if (!background_sync_controller) | 88 if (!background_sync_controller) |
| 79 return; | 89 return; |
| 80 | 90 |
| 81 background_sync_controller->NotifyBackgroundSyncRegistered(origin); | 91 background_sync_controller->NotifyBackgroundSyncRegistered(origin); |
| 82 } | 92 } |
| 83 | 93 |
| 84 } // namespace | 94 } // namespace |
| 85 | 95 |
| 86 BackgroundSyncManager::BackgroundSyncRegistrations:: | 96 BackgroundSyncManager::BackgroundSyncRegistrations:: |
| (...skipping 887 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 974 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 984 if (registration.options()->periodicity == SYNC_ONE_SHOT) { |
| 975 keep_browser_alive_for_one_shot = true; | 985 keep_browser_alive_for_one_shot = true; |
| 976 } else { | 986 } else { |
| 977 // TODO(jkarlin): Support keeping the browser alive for periodic | 987 // TODO(jkarlin): Support keeping the browser alive for periodic |
| 978 // syncs. | 988 // syncs. |
| 979 } | 989 } |
| 980 } | 990 } |
| 981 } | 991 } |
| 982 } | 992 } |
| 983 | 993 |
| 984 // TODO(jkarlin): Use the context's path instead of the 'this' pointer as an | |
| 985 // identifier. See crbug.com/489705. | |
| 986 BrowserThread::PostTask( | 994 BrowserThread::PostTask( |
| 987 BrowserThread::UI, FROM_HERE, | 995 BrowserThread::UI, FROM_HERE, |
| 988 base::Bind(&BackgroundSyncLauncherAndroid::LaunchBrowserWhenNextOnline, | 996 base::Bind(&BackgroundSyncManager::SchedulePendingRegistrationsOnUIThread, |
| 989 this, keep_browser_alive_for_one_shot)); | 997 base::Unretained(this), keep_browser_alive_for_one_shot)); |
| 998 |
| 990 #else | 999 #else |
| 991 // TODO(jkarlin): Toggle Chrome's background mode. | 1000 // TODO(jkarlin): Toggle Chrome's background mode. |
| 992 #endif | 1001 #endif |
| 993 } | 1002 } |
| 994 | 1003 |
| 1004 void BackgroundSyncManager::SchedulePendingRegistrationsOnUIThread( |
| 1005 bool keep_browser_alive_for_one_shot) { |
| 1006 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1007 |
| 1008 BackgroundSyncController* background_sync_controller = |
| 1009 GetBackgroundSyncControllerOnUIThread(service_worker_context_); |
| 1010 if (background_sync_controller) { |
| 1011 // TODO(jkarlin): Use the context's path instead of the 'this' pointer as an |
| 1012 // identifier. See crbug.com/489705. |
| 1013 background_sync_controller->LaunchBrowserWhenNextOnline( |
| 1014 this, keep_browser_alive_for_one_shot); |
| 1015 } |
| 1016 } |
| 1017 |
| 995 void BackgroundSyncManager::FireReadyEvents() { | 1018 void BackgroundSyncManager::FireReadyEvents() { |
| 996 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1019 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 997 | 1020 |
| 998 if (disabled_) | 1021 if (disabled_) |
| 999 return; | 1022 return; |
| 1000 | 1023 |
| 1001 op_scheduler_.ScheduleOperation( | 1024 op_scheduler_.ScheduleOperation( |
| 1002 base::Bind(&BackgroundSyncManager::FireReadyEventsImpl, | 1025 base::Bind(&BackgroundSyncManager::FireReadyEventsImpl, |
| 1003 weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion())); | 1026 weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion())); |
| 1004 } | 1027 } |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1318 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1341 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
| 1319 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1342 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1320 | 1343 |
| 1321 return base::Bind( | 1344 return base::Bind( |
| 1322 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1345 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
| 1323 BackgroundSyncStatus>, | 1346 BackgroundSyncStatus>, |
| 1324 weak_ptr_factory_.GetWeakPtr(), callback); | 1347 weak_ptr_factory_.GetWeakPtr(), callback); |
| 1325 } | 1348 } |
| 1326 | 1349 |
| 1327 } // namespace content | 1350 } // namespace content |
| OLD | NEW |