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 <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "background_sync_registration_handle.h" | |
| 10 #include "base/memory/weak_ptr.h" | 9 #include "base/memory/weak_ptr.h" |
| 11 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
| 12 #include "content/browser/background_sync/background_sync_context_impl.h" | 11 #include "content/browser/background_sync/background_sync_context_impl.h" |
| 13 #include "content/public/browser/browser_thread.h" | 12 #include "content/public/browser/browser_thread.h" |
| 14 | 13 |
| 15 namespace content { | 14 namespace content { |
| 16 | 15 |
| 17 namespace { | 16 namespace { |
| 18 | 17 |
| 19 // TODO(iclelland): Move these converters to mojo::TypeConverter template | 18 // TODO(iclelland): Move these converters to mojo::TypeConverter template |
| 20 // specializations. | 19 // specializations. |
| 21 | 20 |
| 22 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( | 21 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions( |
| 23 const SyncRegistrationPtr& in) { | 22 const SyncRegistrationPtr& in) { |
| 24 BackgroundSyncRegistrationOptions out; | 23 BackgroundSyncRegistrationOptions out; |
| 25 | 24 |
| 26 out.tag = in->tag; | 25 out.tag = in->tag; |
| 27 out.network_state = static_cast<SyncNetworkState>(in->network_state); | 26 out.network_state = static_cast<SyncNetworkState>(in->network_state); |
| 28 return out; | 27 return out; |
| 29 } | 28 } |
| 30 | 29 |
| 31 SyncRegistrationPtr ToMojoRegistration( | 30 SyncRegistrationPtr ToMojoRegistration(const BackgroundSyncRegistration& in) { |
| 32 const BackgroundSyncRegistrationHandle& in) { | |
| 33 SyncRegistrationPtr out(content::SyncRegistration::New()); | 31 SyncRegistrationPtr out(content::SyncRegistration::New()); |
| 34 out->handle_id = in.handle_id(); | 32 out->id = in.id(); |
| 35 out->tag = in.options()->tag; | 33 out->tag = in.options()->tag; |
| 36 out->network_state = static_cast<content::BackgroundSyncNetworkState>( | 34 out->network_state = static_cast<content::BackgroundSyncNetworkState>( |
| 37 in.options()->network_state); | 35 in.options()->network_state); |
| 38 return out; | 36 return out; |
| 39 } | 37 } |
| 40 | 38 |
| 41 } // namespace | 39 } // namespace |
| 42 | 40 |
| 43 #define COMPILE_ASSERT_MATCHING_ENUM(mojo_name, manager_name) \ | 41 #define COMPILE_ASSERT_MATCHING_ENUM(mojo_name, manager_name) \ |
| 44 static_assert(static_cast<int>(content::mojo_name) == \ | 42 static_assert(static_cast<int>(content::mojo_name) == \ |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 116 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 117 BackgroundSyncManager* background_sync_manager = | 115 BackgroundSyncManager* background_sync_manager = |
| 118 background_sync_context_->background_sync_manager(); | 116 background_sync_context_->background_sync_manager(); |
| 119 DCHECK(background_sync_manager); | 117 DCHECK(background_sync_manager); |
| 120 background_sync_manager->GetRegistrations( | 118 background_sync_manager->GetRegistrations( |
| 121 sw_registration_id, | 119 sw_registration_id, |
| 122 base::Bind(&BackgroundSyncServiceImpl::OnGetRegistrationsResult, | 120 base::Bind(&BackgroundSyncServiceImpl::OnGetRegistrationsResult, |
| 123 weak_ptr_factory_.GetWeakPtr(), callback)); | 121 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 124 } | 122 } |
| 125 | 123 |
| 126 void BackgroundSyncServiceImpl::DuplicateRegistrationHandle( | |
| 127 BackgroundSyncRegistrationHandle::HandleId handle_id, | |
| 128 const DuplicateRegistrationHandleCallback& callback) { | |
| 129 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 130 BackgroundSyncManager* background_sync_manager = | |
| 131 background_sync_context_->background_sync_manager(); | |
| 132 DCHECK(background_sync_manager); | |
| 133 | |
| 134 scoped_ptr<BackgroundSyncRegistrationHandle> registration_handle = | |
| 135 background_sync_manager->DuplicateRegistrationHandle(handle_id); | |
| 136 | |
| 137 BackgroundSyncRegistrationHandle* handle_ptr = registration_handle.get(); | |
| 138 | |
| 139 if (!registration_handle) { | |
| 140 callback.Run(BackgroundSyncError::NOT_FOUND, | |
| 141 SyncRegistrationPtr(content::SyncRegistration::New())); | |
| 142 return; | |
| 143 } | |
| 144 | |
| 145 active_handles_.AddWithID(registration_handle.release(), | |
| 146 handle_ptr->handle_id()); | |
| 147 SyncRegistrationPtr mojoResult = ToMojoRegistration(*handle_ptr); | |
| 148 callback.Run(BackgroundSyncError::NONE, std::move(mojoResult)); | |
| 149 } | |
| 150 | |
| 151 void BackgroundSyncServiceImpl::ReleaseRegistration( | |
| 152 BackgroundSyncRegistrationHandle::HandleId handle_id) { | |
| 153 if (!active_handles_.Lookup(handle_id)) { | |
| 154 // TODO(jkarlin): Abort client. | |
| 155 LOG(WARNING) << "Client attempted to release non-existing registration"; | |
| 156 return; | |
| 157 } | |
| 158 | |
| 159 active_handles_.Remove(handle_id); | |
| 160 } | |
| 161 | |
| 162 void BackgroundSyncServiceImpl::OnRegisterResult( | 124 void BackgroundSyncServiceImpl::OnRegisterResult( |
| 163 const RegisterCallback& callback, | 125 const RegisterCallback& callback, |
| 164 BackgroundSyncStatus status, | 126 BackgroundSyncStatus status, |
| 165 scoped_ptr<BackgroundSyncRegistrationHandle> result) { | 127 scoped_ptr<BackgroundSyncRegistration> result) { |
| 166 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 128 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 167 BackgroundSyncRegistrationHandle* result_ptr = result.get(); | |
| 168 | 129 |
| 169 if (status != BACKGROUND_SYNC_STATUS_OK) { | 130 if (status != BACKGROUND_SYNC_STATUS_OK) { |
| 170 callback.Run(static_cast<content::BackgroundSyncError>(status), | 131 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 171 SyncRegistrationPtr(content::SyncRegistration::New())); | 132 SyncRegistrationPtr(content::SyncRegistration::New())); |
| 172 return; | 133 return; |
| 173 } | 134 } |
| 174 | 135 |
| 175 DCHECK(result); | 136 DCHECK(result); |
| 176 active_handles_.AddWithID(result.release(), result_ptr->handle_id()); | 137 SyncRegistrationPtr mojoResult = ToMojoRegistration(*result); |
| 177 SyncRegistrationPtr mojoResult = ToMojoRegistration(*result_ptr); | |
| 178 callback.Run(static_cast<content::BackgroundSyncError>(status), | 138 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 179 std::move(mojoResult)); | 139 std::move(mojoResult)); |
| 180 } | 140 } |
| 181 | 141 |
| 182 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( | 142 void BackgroundSyncServiceImpl::OnGetRegistrationsResult( |
| 183 const GetRegistrationsCallback& callback, | 143 const GetRegistrationsCallback& callback, |
| 184 BackgroundSyncStatus status, | 144 BackgroundSyncStatus status, |
| 185 scoped_ptr<ScopedVector<BackgroundSyncRegistrationHandle>> | 145 scoped_ptr<ScopedVector<BackgroundSyncRegistration>> result_registrations) { |
| 186 result_registrations) { | |
| 187 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 146 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 188 DCHECK(result_registrations); | 147 DCHECK(result_registrations); |
| 189 | 148 |
| 190 mojo::Array<content::SyncRegistrationPtr> mojo_registrations; | 149 mojo::Array<content::SyncRegistrationPtr> mojo_registrations; |
| 191 for (BackgroundSyncRegistrationHandle* registration : *result_registrations) { | 150 for (const BackgroundSyncRegistration* registration : *result_registrations) |
| 192 active_handles_.AddWithID(registration, registration->handle_id()); | |
| 193 mojo_registrations.push_back(ToMojoRegistration(*registration)); | 151 mojo_registrations.push_back(ToMojoRegistration(*registration)); |
| 194 } | |
| 195 | 152 |
| 196 result_registrations->weak_clear(); | 153 result_registrations->weak_clear(); |
|
iclelland
2016/03/08 16:48:14
I think this leaks the registrations now (they wer
jkarlin
2016/03/08 19:37:36
Removed this line. Thanks for the catch!
| |
| 197 | 154 |
| 198 callback.Run(static_cast<content::BackgroundSyncError>(status), | 155 callback.Run(static_cast<content::BackgroundSyncError>(status), |
| 199 std::move(mojo_registrations)); | 156 std::move(mojo_registrations)); |
| 200 } | 157 } |
| 201 | 158 |
| 202 } // namespace content | 159 } // namespace content |
| OLD | NEW |