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 888 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
975 if (registration.options()->periodicity == SYNC_ONE_SHOT) { | 985 if (registration.options()->periodicity == SYNC_ONE_SHOT) { |
976 keep_browser_alive_for_one_shot = true; | 986 keep_browser_alive_for_one_shot = true; |
977 } else { | 987 } else { |
978 // TODO(jkarlin): Support keeping the browser alive for periodic | 988 // TODO(jkarlin): Support keeping the browser alive for periodic |
979 // syncs. | 989 // syncs. |
980 } | 990 } |
981 } | 991 } |
982 } | 992 } |
983 } | 993 } |
984 | 994 |
985 // TODO(jkarlin): Use the context's path instead of the 'this' pointer as an | |
986 // identifier. See crbug.com/489705. | |
987 BrowserThread::PostTask( | 995 BrowserThread::PostTask( |
988 BrowserThread::UI, FROM_HERE, | 996 BrowserThread::UI, FROM_HERE, |
989 base::Bind(&BackgroundSyncLauncherAndroid::LaunchBrowserWhenNextOnline, | 997 base::Bind(&BackgroundSyncManager::SchedulePendingRegistrationsOnUIThread, |
990 this, keep_browser_alive_for_one_shot)); | 998 base::Unretained(this), keep_browser_alive_for_one_shot)); |
| 999 |
991 #else | 1000 #else |
992 // TODO(jkarlin): Toggle Chrome's background mode. | 1001 // TODO(jkarlin): Toggle Chrome's background mode. |
993 #endif | 1002 #endif |
994 } | 1003 } |
995 | 1004 |
| 1005 void BackgroundSyncManager::SchedulePendingRegistrationsOnUIThread( |
| 1006 bool keep_browser_alive_for_one_shot) { |
| 1007 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 1008 |
| 1009 BackgroundSyncController* background_sync_controller = |
| 1010 GetBackgroundSyncControllerOnUIThread(service_worker_context_); |
| 1011 if (background_sync_controller) { |
| 1012 // TODO(jkarlin): Use the context's path instead of the 'this' pointer as an |
| 1013 // identifier. See crbug.com/489705. |
| 1014 background_sync_controller->LaunchBrowserWhenNextOnline( |
| 1015 this, keep_browser_alive_for_one_shot); |
| 1016 } |
| 1017 } |
| 1018 |
996 void BackgroundSyncManager::FireReadyEvents() { | 1019 void BackgroundSyncManager::FireReadyEvents() { |
997 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1020 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
998 | 1021 |
999 if (disabled_) | 1022 if (disabled_) |
1000 return; | 1023 return; |
1001 | 1024 |
1002 op_scheduler_.ScheduleOperation( | 1025 op_scheduler_.ScheduleOperation( |
1003 base::Bind(&BackgroundSyncManager::FireReadyEventsImpl, | 1026 base::Bind(&BackgroundSyncManager::FireReadyEventsImpl, |
1004 weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion())); | 1027 weak_ptr_factory_.GetWeakPtr(), MakeEmptyCompletion())); |
1005 } | 1028 } |
(...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1319 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { | 1342 BackgroundSyncManager::MakeStatusCompletion(const StatusCallback& callback) { |
1320 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1343 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
1321 | 1344 |
1322 return base::Bind( | 1345 return base::Bind( |
1323 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, | 1346 &BackgroundSyncManager::CompleteOperationCallback<StatusCallback, |
1324 BackgroundSyncStatus>, | 1347 BackgroundSyncStatus>, |
1325 weak_ptr_factory_.GetWeakPtr(), callback); | 1348 weak_ptr_factory_.GetWeakPtr(), callback); |
1326 } | 1349 } |
1327 | 1350 |
1328 } // namespace content | 1351 } // namespace content |
OLD | NEW |