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_service_impl.h" | 5 #include "content/browser/background_sync/background_sync_service_impl.h" |
6 | 6 |
7 #include "background_sync_registration_handle.h" | 7 #include "background_sync_registration_handle.h" |
8 #include "base/memory/weak_ptr.h" | 8 #include "base/memory/weak_ptr.h" |
9 #include "base/stl_util.h" | 9 #include "base/stl_util.h" |
10 #include "content/browser/background_sync/background_sync_context_impl.h" | |
11 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
12 | 11 |
13 namespace content { | 12 namespace content { |
14 | 13 |
15 namespace { | 14 namespace { |
16 | 15 |
17 // TODO(iclelland): Move these converters to mojo::TypeConverter template | 16 // TODO(iclelland): Move these converters to mojo::TypeConverter template |
18 // specializations. | 17 // specializations. |
19 | 18 |
20 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( | 19 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
83 | 82 |
84 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC, | 83 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC, |
85 SyncPeriodicity::SYNC_PERIODIC); | 84 SyncPeriodicity::SYNC_PERIODIC); |
86 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, | 85 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, |
87 SyncPeriodicity::SYNC_ONE_SHOT); | 86 SyncPeriodicity::SYNC_ONE_SHOT); |
88 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, | 87 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, |
89 SyncPeriodicity::SYNC_ONE_SHOT); | 88 SyncPeriodicity::SYNC_ONE_SHOT); |
90 | 89 |
91 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { | 90 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { |
92 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 91 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
93 DCHECK(background_sync_context_->background_sync_manager()); | |
94 } | 92 } |
95 | 93 |
96 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( | 94 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( |
97 BackgroundSyncContextImpl* background_sync_context, | 95 BackgroundSyncManager* background_sync_manager) |
98 mojo::InterfaceRequest<BackgroundSyncService> request) | 96 : background_sync_manager_(background_sync_manager), |
99 : background_sync_context_(background_sync_context), | |
100 binding_(this, request.Pass()), | |
101 weak_ptr_factory_(this) { | 97 weak_ptr_factory_(this) { |
102 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 98 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
103 DCHECK(background_sync_context); | 99 DCHECK(background_sync_manager); |
104 | |
105 binding_.set_connection_error_handler( | |
106 base::Bind(&BackgroundSyncServiceImpl::OnConnectionError, | |
107 base::Unretained(this) /* the channel is owned by this */)); | |
108 } | |
109 | |
110 void BackgroundSyncServiceImpl::OnConnectionError() { | |
111 background_sync_context_->ServiceHadConnectionError(this); | |
112 // |this| is now deleted. | |
113 } | 100 } |
114 | 101 |
115 void BackgroundSyncServiceImpl::Register(content::SyncRegistrationPtr options, | 102 void BackgroundSyncServiceImpl::Register(content::SyncRegistrationPtr options, |
116 int64_t sw_registration_id, | 103 int64_t sw_registration_id, |
117 bool requested_from_service_worker, | 104 bool requested_from_service_worker, |
118 const RegisterCallback& callback) { | 105 const RegisterCallback& callback) { |
119 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 106 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
120 | 107 |
121 BackgroundSyncRegistrationOptions mgr_options = | 108 BackgroundSyncRegistrationOptions mgr_options = |
122 ToBackgroundSyncRegistrationOptions(options); | 109 ToBackgroundSyncRegistrationOptions(options); |
123 | 110 |
124 BackgroundSyncManager* background_sync_manager = | 111 background_sync_manager_->Register( |
125 background_sync_context_->background_sync_manager(); | |
126 DCHECK(background_sync_manager); | |
127 background_sync_manager->Register( | |
128 sw_registration_id, mgr_options, requested_from_service_worker, | 112 sw_registration_id, mgr_options, requested_from_service_worker, |
129 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, | 113 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, |
130 weak_ptr_factory_.GetWeakPtr(), callback)); | 114 weak_ptr_factory_.GetWeakPtr(), callback)); |
131 } | 115 } |
132 | 116 |
133 void BackgroundSyncServiceImpl::Unregister( | 117 void BackgroundSyncServiceImpl::Unregister( |
134 BackgroundSyncRegistrationHandle::HandleId handle_id, | 118 BackgroundSyncRegistrationHandle::HandleId handle_id, |
135 int64_t sw_registration_id, | 119 int64_t sw_registration_id, |
136 const UnregisterCallback& callback) { | 120 const UnregisterCallback& callback) { |
137 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 121 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
(...skipping 10 matching lines...) Expand all Loading... |
148 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, | 132 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, |
149 weak_ptr_factory_.GetWeakPtr(), callback)); | 133 weak_ptr_factory_.GetWeakPtr(), callback)); |
150 } | 134 } |
151 | 135 |
152 void BackgroundSyncServiceImpl::GetRegistration( | 136 void BackgroundSyncServiceImpl::GetRegistration( |
153 BackgroundSyncPeriodicity periodicity, | 137 BackgroundSyncPeriodicity periodicity, |
154 const mojo::String& tag, | 138 const mojo::String& tag, |
155 int64_t sw_registration_id, | 139 int64_t sw_registration_id, |
156 const GetRegistrationCallback& callback) { | 140 const GetRegistrationCallback& callback) { |
157 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 141 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
158 BackgroundSyncManager* background_sync_manager = | 142 background_sync_manager_->GetRegistration( |
159 background_sync_context_->background_sync_manager(); | |
160 DCHECK(background_sync_manager); | |
161 background_sync_manager->GetRegistration( | |
162 sw_registration_id, tag.get(), static_cast<SyncPeriodicity>(periodicity), | 143 sw_registration_id, tag.get(), static_cast<SyncPeriodicity>(periodicity), |
163 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, | 144 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, |
164 weak_ptr_factory_.GetWeakPtr(), callback)); | 145 weak_ptr_factory_.GetWeakPtr(), callback)); |
165 } | 146 } |
166 | 147 |
167 void BackgroundSyncServiceImpl::GetRegistrations( | 148 void BackgroundSyncServiceImpl::GetRegistrations( |
168 BackgroundSyncPeriodicity periodicity, | 149 BackgroundSyncPeriodicity periodicity, |
169 int64_t sw_registration_id, | 150 int64_t sw_registration_id, |
170 const GetRegistrationsCallback& callback) { | 151 const GetRegistrationsCallback& callback) { |
171 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 152 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
172 BackgroundSyncManager* background_sync_manager = | 153 background_sync_manager_->GetRegistrations( |
173 background_sync_context_->background_sync_manager(); | |
174 DCHECK(background_sync_manager); | |
175 background_sync_manager->GetRegistrations( | |
176 sw_registration_id, static_cast<SyncPeriodicity>(periodicity), | 154 sw_registration_id, static_cast<SyncPeriodicity>(periodicity), |
177 base::Bind(&BackgroundSyncServiceImpl::OnGetRegistrationsResult, | 155 base::Bind(&BackgroundSyncServiceImpl::OnGetRegistrationsResult, |
178 weak_ptr_factory_.GetWeakPtr(), callback)); | 156 weak_ptr_factory_.GetWeakPtr(), callback)); |
179 } | 157 } |
180 | 158 |
181 void BackgroundSyncServiceImpl::GetPermissionStatus( | 159 void BackgroundSyncServiceImpl::GetPermissionStatus( |
182 BackgroundSyncPeriodicity periodicity, | 160 BackgroundSyncPeriodicity periodicity, |
183 int64_t sw_registration_id, | 161 int64_t sw_registration_id, |
184 const GetPermissionStatusCallback& callback) { | 162 const GetPermissionStatusCallback& callback) { |
185 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
186 | 164 |
187 // TODO(iclelland): Implement a real policy. This is a stub implementation. | 165 // TODO(iclelland): Implement a real policy. This is a stub implementation. |
188 // OneShot: crbug.com/482091 | 166 // OneShot: crbug.com/482091 |
189 // Periodic: crbug.com/482093 | 167 // Periodic: crbug.com/482093 |
190 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); | 168 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); |
191 } | 169 } |
192 | 170 |
193 void BackgroundSyncServiceImpl::DuplicateRegistrationHandle( | 171 void BackgroundSyncServiceImpl::DuplicateRegistrationHandle( |
194 BackgroundSyncRegistrationHandle::HandleId handle_id, | 172 BackgroundSyncRegistrationHandle::HandleId handle_id, |
195 const DuplicateRegistrationHandleCallback& callback) { | 173 const DuplicateRegistrationHandleCallback& callback) { |
196 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 174 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
197 BackgroundSyncManager* background_sync_manager = | |
198 background_sync_context_->background_sync_manager(); | |
199 DCHECK(background_sync_manager); | |
200 | 175 |
201 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = | 176 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = |
202 background_sync_manager->DuplicateRegistrationHandle(handle_id); | 177 background_sync_manager_->DuplicateRegistrationHandle(handle_id); |
203 | 178 |
204 BackgroundSyncRegistrationHandle* handle_ptr = registration_handle.get(); | 179 BackgroundSyncRegistrationHandle* handle_ptr = registration_handle.get(); |
205 | 180 |
206 if (!registration_handle) { | 181 if (!registration_handle) { |
207 callback.Run(BACKGROUND_SYNC_ERROR_NOT_FOUND, | 182 callback.Run(BACKGROUND_SYNC_ERROR_NOT_FOUND, |
208 SyncRegistrationPtr(content::SyncRegistration::New())); | 183 SyncRegistrationPtr(content::SyncRegistration::New())); |
209 return; | 184 return; |
210 } | 185 } |
211 | 186 |
212 active_handles_.AddWithID(registration_handle.release(), | 187 active_handles_.AddWithID(registration_handle.release(), |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
289 | 264 |
290 void BackgroundSyncServiceImpl::OnNotifyWhenFinishedResult( | 265 void BackgroundSyncServiceImpl::OnNotifyWhenFinishedResult( |
291 const NotifyWhenFinishedCallback& callback, | 266 const NotifyWhenFinishedCallback& callback, |
292 BackgroundSyncStatus status, | 267 BackgroundSyncStatus status, |
293 BackgroundSyncState sync_state) { | 268 BackgroundSyncState sync_state) { |
294 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 269 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
295 callback.Run(static_cast<content::BackgroundSyncError>(status), sync_state); | 270 callback.Run(static_cast<content::BackgroundSyncError>(status), sync_state); |
296 } | 271 } |
297 | 272 |
298 } // namespace content | 273 } // namespace content |
OLD | NEW |