Chromium Code Reviews| 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 "base/memory/weak_ptr.h" | 7 #include "base/memory/weak_ptr.h" |
| 8 #include "base/stl_util.h" | |
| 8 #include "content/browser/background_sync/background_sync_context_impl.h" | 9 #include "content/browser/background_sync/background_sync_context_impl.h" |
| 9 #include "content/browser/background_sync/background_sync_registration.h" | 10 #include "content/browser/background_sync/client_background_sync_registration.h" |
| 10 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
| 11 | 12 |
| 12 namespace content { | 13 namespace content { |
| 13 | 14 |
| 14 namespace { | 15 namespace { |
| 15 | 16 |
| 16 // TODO(iclelland): Move these converters to mojo::TypeConverter template | 17 // TODO(iclelland): Move these converters to mojo::TypeConverter template |
| 17 // specializations. | 18 // specializations. |
| 18 | 19 |
| 19 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( | 20 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 81 | 82 |
| 82 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC, | 83 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC, |
| 83 SyncPeriodicity::SYNC_PERIODIC); | 84 SyncPeriodicity::SYNC_PERIODIC); |
| 84 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, | 85 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT, |
| 85 SyncPeriodicity::SYNC_ONE_SHOT); | 86 SyncPeriodicity::SYNC_ONE_SHOT); |
| 86 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, | 87 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX, |
| 87 SyncPeriodicity::SYNC_ONE_SHOT); | 88 SyncPeriodicity::SYNC_ONE_SHOT); |
| 88 | 89 |
| 89 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { | 90 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() { |
| 90 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 91 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 92 BackgroundSyncManager* background_sync_manager = | |
| 93 background_sync_context_->background_sync_manager(); | |
| 94 DCHECK(background_sync_manager); | |
| 91 } | 95 } |
| 92 | 96 |
| 93 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( | 97 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl( |
| 94 BackgroundSyncContextImpl* background_sync_context, | 98 BackgroundSyncContextImpl* background_sync_context, |
| 95 mojo::InterfaceRequest<BackgroundSyncService> request) | 99 mojo::InterfaceRequest<BackgroundSyncService> request) |
| 96 : background_sync_context_(background_sync_context), | 100 : background_sync_context_(background_sync_context), |
| 97 binding_(this, request.Pass()), | 101 binding_(this, request.Pass()), |
| 98 weak_ptr_factory_(this) { | 102 weak_ptr_factory_(this) { |
| 99 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 103 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 100 DCHECK(background_sync_context); | 104 DCHECK(background_sync_context); |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 122 DCHECK(background_sync_manager); | 126 DCHECK(background_sync_manager); |
| 123 background_sync_manager->Register( | 127 background_sync_manager->Register( |
| 124 sw_registration_id, mgr_options, | 128 sw_registration_id, mgr_options, |
| 125 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, | 129 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult, |
| 126 weak_ptr_factory_.GetWeakPtr(), callback)); | 130 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 127 } | 131 } |
| 128 | 132 |
| 129 void BackgroundSyncServiceImpl::Unregister( | 133 void BackgroundSyncServiceImpl::Unregister( |
| 130 BackgroundSyncPeriodicity periodicity, | 134 BackgroundSyncPeriodicity periodicity, |
| 131 int64_t id, | 135 int64_t id, |
| 132 const mojo::String& tag, | |
| 133 int64_t sw_registration_id, | 136 int64_t sw_registration_id, |
| 134 const UnregisterCallback& callback) { | 137 const UnregisterCallback& callback) { |
| 135 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 138 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 136 BackgroundSyncManager* background_sync_manager = | 139 BackgroundSyncManager* background_sync_manager = |
| 137 background_sync_context_->background_sync_manager(); | 140 background_sync_context_->background_sync_manager(); |
| 138 DCHECK(background_sync_manager); | 141 DCHECK(background_sync_manager); |
| 139 background_sync_manager->Unregister( | 142 |
| 140 sw_registration_id, tag, content::SYNC_ONE_SHOT, id, | 143 ClientBackgroundSyncRegistration* registration = |
| 144 hosted_registrations_.Lookup(id); | |
| 145 if (!registration) { | |
| 146 callback.Run(BACKGROUND_SYNC_ERROR_NOT_ALLOWED); | |
| 147 return; | |
| 148 } | |
| 149 | |
| 150 registration->Unregister( | |
| 151 sw_registration_id, | |
| 141 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, | 152 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult, |
| 142 weak_ptr_factory_.GetWeakPtr(), callback)); | 153 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 143 } | 154 } |
| 144 | 155 |
| 145 void BackgroundSyncServiceImpl::GetRegistration( | 156 void BackgroundSyncServiceImpl::GetRegistration( |
| 146 BackgroundSyncPeriodicity periodicity, | 157 BackgroundSyncPeriodicity periodicity, |
| 147 const mojo::String& tag, | 158 const mojo::String& tag, |
| 148 int64_t sw_registration_id, | 159 int64_t sw_registration_id, |
| 149 const GetRegistrationCallback& callback) { | 160 const GetRegistrationCallback& callback) { |
| 150 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 161 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 176 int64_t sw_registration_id, | 187 int64_t sw_registration_id, |
| 177 const GetPermissionStatusCallback& callback) { | 188 const GetPermissionStatusCallback& callback) { |
| 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 189 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 179 | 190 |
| 180 // TODO(iclelland): Implement a real policy. This is a stub implementation. | 191 // TODO(iclelland): Implement a real policy. This is a stub implementation. |
| 181 // OneShot: crbug.com/482091 | 192 // OneShot: crbug.com/482091 |
| 182 // Periodic: crbug.com/482093 | 193 // Periodic: crbug.com/482093 |
| 183 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); | 194 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED); |
| 184 } | 195 } |
| 185 | 196 |
| 197 void BackgroundSyncServiceImpl::TrackRegistration( | |
| 198 SyncRegistrationPtr sync_registration) { | |
| 199 // Registrations that the client acquires without the | |
| 200 // BackgroundSyncServiceImpl's knowing are registered here. | |
| 201 BackgroundSyncManager* background_sync_manager = | |
| 202 background_sync_context_->background_sync_manager(); | |
| 203 DCHECK(background_sync_manager); | |
| 204 | |
| 205 if (hosted_registrations_.Lookup(sync_registration->id)) { | |
| 206 // TODO(jkarlin): Abort client. | |
| 207 LOG(WARNING) << "Client attempted to track an already tracked registration"; | |
| 208 return; | |
| 209 } | |
| 210 | |
| 211 BackgroundSyncRegistrationOptions options = | |
| 212 ToBackgroundSyncRegistrationOptions(sync_registration); | |
| 213 | |
| 214 scoped_ptr<ClientBackgroundSyncRegistration> registration( | |
| 215 new ClientBackgroundSyncRegistration(background_sync_manager)); | |
| 216 *registration->options() = options; | |
| 217 registration->set_id(sync_registration->id); | |
| 218 | |
| 219 if (!registration->IsValid()) { | |
| 220 // TODO(jkarlin): Abort client. | |
| 221 LOG(WARNING) << "Client attempted to track a non-existant registration"; | |
| 222 return; | |
| 223 } | |
| 224 | |
| 225 hosted_registrations_.AddWithID(registration.release(), | |
| 226 sync_registration->id); | |
| 227 } | |
| 228 | |
| 229 void BackgroundSyncServiceImpl::ReleaseRegistration(int64_t sync_id) { | |
| 230 if (!hosted_registrations_.Lookup(sync_id)) { | |
| 231 // TODO(jkarlin): Abort client. | |
| 232 LOG(WARNING) << "Client attempted to release non-existing registration"; | |
| 233 return; | |
| 234 } | |
| 235 | |
| 236 hosted_registrations_.Remove(sync_id); | |
| 237 } | |
| 238 | |
| 186 void BackgroundSyncServiceImpl::OnRegisterResult( | 239 void BackgroundSyncServiceImpl::OnRegisterResult( |
| 187 const RegisterCallback& callback, | 240 const RegisterCallback& callback, |
| 188 BackgroundSyncStatus status, | 241 BackgroundSyncStatus status, |
| 189 const BackgroundSyncRegistration& result) { | 242 scoped_ptr<ClientBackgroundSyncRegistration> result) { |
| 190 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 243 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 191 SyncRegistrationPtr mojoResult = ToMojoRegistration(result); | 244 ClientBackgroundSyncRegistration* result_ptr = result.get(); |
| 245 | |
| 246 if (status != BACKGROUND_SYNC_STATUS_OK) { | |
| 247 callback.Run(static_cast<content::BackgroundSyncError>(status), | |
| 248 SyncRegistrationPtr(content::SyncRegistration::New())); | |
| 249 return; | |
| 250 } | |
| 251 | |
| 252 hosted_registrations_.AddWithID(result.release(), result_ptr->id()); | |
| 253 SyncRegistrationPtr mojoResult = ToMojoRegistration(*result_ptr); | |
| 192 callback.Run(static_cast<content::BackgroundSyncError>(status), | 254 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 193 mojoResult.Pass()); | 255 mojoResult.Pass()); |
| 194 } | 256 } |
| 195 | 257 |
| 196 void BackgroundSyncServiceImpl::OnUnregisterResult( | 258 void BackgroundSyncServiceImpl::OnUnregisterResult( |
| 197 const UnregisterCallback& callback, | 259 const UnregisterCallback& callback, |
| 198 BackgroundSyncStatus status) { | 260 BackgroundSyncStatus status) { |
| 199 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 261 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 200 callback.Run(static_cast<content::BackgroundSyncError>(status)); | 262 callback.Run(static_cast<content::BackgroundSyncError>(status)); |
| 201 } | 263 } |
| 202 | 264 |
| 203 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( | 265 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( |
| 204 const GetRegistrationsCallback& callback, | 266 const GetRegistrationsCallback& callback, |
| 205 BackgroundSyncStatus status, | 267 BackgroundSyncStatus status, |
| 206 const std::vector<BackgroundSyncRegistration>& result_registrations) { | 268 scoped_ptr<ScopedVector<ClientBackgroundSyncRegistration>> |
| 269 result_registrations) { | |
| 207 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 270 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 208 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); | 271 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0); |
| 209 for (const auto& registration : result_registrations) | 272 for (ClientBackgroundSyncRegistration* registration : *result_registrations) { |
| 210 mojo_registrations.push_back(ToMojoRegistration(registration)); | 273 hosted_registrations_.AddWithID(registration, registration->id()); |
|
michaeln
2015/08/19 01:56:01
Does this leak (for the lifetime of the renderer p
jkarlin
2015/08/19 12:47:06
Yes. See https://codereview.chromium.org/129990300
| |
| 274 mojo_registrations.push_back(ToMojoRegistration(*registration)); | |
| 275 } | |
| 276 | |
| 277 result_registrations->weak_clear(); | |
| 278 | |
| 211 callback.Run(static_cast<content::BackgroundSyncError>(status), | 279 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 212 mojo_registrations.Pass()); | 280 mojo_registrations.Pass()); |
| 213 } | 281 } |
| 214 | 282 |
| 215 } // namespace content | 283 } // namespace content |
| OLD | NEW |