chrome/browser/extensions: Remove use of MessageLoopProxy and deprecated MessageLoop...
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_service_impl.cc
blob7ba71dae9e31136191bdc24f0bff196b4b413dc2
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
3 // found in the LICENSE file.
5 #include "content/browser/background_sync/background_sync_service_impl.h"
7 #include "base/memory/weak_ptr.h"
8 #include "content/browser/background_sync/background_sync_context_impl.h"
9 #include "content/public/browser/browser_thread.h"
11 namespace content {
13 namespace {
15 // TODO(iclelland): Move these converters to mojo::TypeConverter template
16 // specializations.
18 scoped_ptr<BackgroundSyncManager::BackgroundSyncRegistration>
19 ToBackgroundSyncRegistration(const SyncRegistrationPtr& in) {
20 scoped_ptr<BackgroundSyncManager::BackgroundSyncRegistration> out(
21 new BackgroundSyncManager::BackgroundSyncRegistration());
22 out->tag = in->tag;
23 out->id = in->id;
24 out->min_period = in->min_period_ms;
25 out->power_state = static_cast<SyncPowerState>(in->power_state);
26 out->network_state = static_cast<SyncNetworkState>(in->network_state);
27 out->periodicity = static_cast<SyncPeriodicity>(in->periodicity);
28 return out;
31 SyncRegistrationPtr ToMojoRegistration(
32 const BackgroundSyncManager::BackgroundSyncRegistration& in) {
33 SyncRegistrationPtr out(content::SyncRegistration::New());
34 out->id = in.id;
35 out->tag = in.tag;
36 out->min_period_ms = in.min_period;
37 out->periodicity =
38 static_cast<content::BackgroundSyncPeriodicity>(in.periodicity);
39 out->power_state =
40 static_cast<content::BackgroundSyncPowerState>(in.power_state);
41 out->network_state =
42 static_cast<content::BackgroundSyncNetworkState>(in.network_state);
43 return out.Pass();
46 } // namespace
48 #define COMPILE_ASSERT_MATCHING_ENUM(mojo_name, manager_name) \
49 COMPILE_ASSERT(static_cast<int>(content::mojo_name) == \
50 static_cast<int>(content::manager_name), \
51 mismatching_enums)
53 // TODO(iclelland): Move these tests somewhere else
54 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_NONE,
55 BackgroundSyncManager::ERROR_TYPE_OK);
56 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_STORAGE,
57 BackgroundSyncManager::ERROR_TYPE_STORAGE);
58 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_NOT_FOUND,
59 BackgroundSyncManager::ERROR_TYPE_NOT_FOUND);
60 COMPILE_ASSERT_MATCHING_ENUM(
61 BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER,
62 BackgroundSyncManager::ERROR_TYPE_NO_SERVICE_WORKER);
63 COMPILE_ASSERT_MATCHING_ENUM(
64 BACKGROUND_SYNC_ERROR_MAX,
65 BackgroundSyncManager::ERROR_TYPE_NO_SERVICE_WORKER);
67 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_NETWORK_STATE_ANY,
68 SyncNetworkState::NETWORK_STATE_ANY);
69 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_NETWORK_STATE_AVOID_CELLULAR,
70 SyncNetworkState::NETWORK_STATE_AVOID_CELLULAR);
71 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_NETWORK_STATE_ONLINE,
72 SyncNetworkState::NETWORK_STATE_ONLINE);
73 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_NETWORK_STATE_MAX,
74 SyncNetworkState::NETWORK_STATE_ONLINE);
76 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_POWER_STATE_AUTO,
77 SyncPowerState::POWER_STATE_AUTO);
78 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_POWER_STATE_AVOID_DRAINING,
79 SyncPowerState::POWER_STATE_AVOID_DRAINING);
80 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_POWER_STATE_MAX,
81 SyncPowerState::POWER_STATE_AVOID_DRAINING);
83 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC,
84 SyncPeriodicity::SYNC_PERIODIC);
85 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT,
86 SyncPeriodicity::SYNC_ONE_SHOT);
87 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX,
88 SyncPeriodicity::SYNC_ONE_SHOT);
90 // static
91 void BackgroundSyncServiceImpl::Create(
92 const scoped_refptr<BackgroundSyncContextImpl>& background_sync_context,
93 mojo::InterfaceRequest<BackgroundSyncService> request) {
94 DCHECK_CURRENTLY_ON(BrowserThread::UI);
95 BrowserThread::PostTask(
96 BrowserThread::IO, FROM_HERE,
97 base::Bind(&BackgroundSyncServiceImpl::CreateOnIOThread,
98 background_sync_context, base::Passed(&request)));
101 BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() {
102 DCHECK_CURRENTLY_ON(BrowserThread::IO);
105 // static
106 void BackgroundSyncServiceImpl::CreateOnIOThread(
107 const scoped_refptr<BackgroundSyncContextImpl>& background_sync_context,
108 mojo::InterfaceRequest<BackgroundSyncService> request) {
109 DCHECK_CURRENTLY_ON(BrowserThread::IO);
110 new BackgroundSyncServiceImpl(background_sync_context, request.Pass());
113 BackgroundSyncServiceImpl::BackgroundSyncServiceImpl(
114 const scoped_refptr<BackgroundSyncContextImpl>& background_sync_context,
115 mojo::InterfaceRequest<BackgroundSyncService> request)
116 : background_sync_context_(background_sync_context),
117 binding_(this, request.Pass()),
118 weak_ptr_factory_(this) {
119 DCHECK_CURRENTLY_ON(BrowserThread::IO);
122 void BackgroundSyncServiceImpl::Register(content::SyncRegistrationPtr options,
123 int64_t serviceWorkerRegistrationId,
124 const RegisterCallback& callback) {
125 DCHECK_CURRENTLY_ON(BrowserThread::IO);
127 scoped_ptr<BackgroundSyncManager::BackgroundSyncRegistration> reg =
128 ToBackgroundSyncRegistration(options);
130 BackgroundSyncManager* background_sync_manager =
131 background_sync_context_->background_sync_manager();
132 DCHECK(background_sync_manager);
133 background_sync_manager->Register(
134 serviceWorkerRegistrationId, *(reg.get()),
135 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult,
136 weak_ptr_factory_.GetWeakPtr(), callback));
139 void BackgroundSyncServiceImpl::Unregister(
140 BackgroundSyncPeriodicity periodicity,
141 int64_t id,
142 const mojo::String& tag,
143 int64_t serviceWorkerRegistrationId,
144 const UnregisterCallback& callback) {
145 DCHECK_CURRENTLY_ON(BrowserThread::IO);
146 BackgroundSyncManager* background_sync_manager =
147 background_sync_context_->background_sync_manager();
148 DCHECK(background_sync_manager);
149 background_sync_manager->Unregister(
150 serviceWorkerRegistrationId, tag, content::SYNC_ONE_SHOT, id,
151 base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult,
152 weak_ptr_factory_.GetWeakPtr(), callback));
155 void BackgroundSyncServiceImpl::GetRegistration(
156 BackgroundSyncPeriodicity periodicity,
157 const mojo::String& tag,
158 int64_t serviceWorkerRegistrationId,
159 const GetRegistrationCallback& callback) {
160 DCHECK_CURRENTLY_ON(BrowserThread::IO);
161 BackgroundSyncManager* background_sync_manager =
162 background_sync_context_->background_sync_manager();
163 DCHECK(background_sync_manager);
164 background_sync_manager->GetRegistration(
165 serviceWorkerRegistrationId, tag.get(),
166 static_cast<SyncPeriodicity>(periodicity),
167 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult,
168 weak_ptr_factory_.GetWeakPtr(), callback));
171 void BackgroundSyncServiceImpl::GetRegistrations(
172 BackgroundSyncPeriodicity periodicity,
173 int64_t serviceWorkerRegistrationId,
174 const GetRegistrationsCallback& callback) {
175 DCHECK_CURRENTLY_ON(BrowserThread::IO);
176 BackgroundSyncManager* background_sync_manager =
177 background_sync_context_->background_sync_manager();
178 DCHECK(background_sync_manager);
179 background_sync_manager->GetRegistrations(
180 serviceWorkerRegistrationId, static_cast<SyncPeriodicity>(periodicity),
181 base::Bind(&BackgroundSyncServiceImpl::OnGetRegistrationsResult,
182 weak_ptr_factory_.GetWeakPtr(), callback));
185 void BackgroundSyncServiceImpl::GetPermissionStatus(
186 BackgroundSyncPeriodicity periodicity,
187 int64_t serviceWorkerRegistrationId,
188 const GetPermissionStatusCallback& callback) {
189 DCHECK_CURRENTLY_ON(BrowserThread::IO);
191 // TODO(iclelland): Implement a real policy. This is a stub implementation.
192 // OneShot: crbug.com/482091
193 // Periodic: crbug.com/482093
194 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED);
197 void BackgroundSyncServiceImpl::OnRegisterResult(
198 const RegisterCallback& callback,
199 BackgroundSyncManager::ErrorType error,
200 const BackgroundSyncManager::BackgroundSyncRegistration& result) {
201 DCHECK_CURRENTLY_ON(BrowserThread::IO);
202 SyncRegistrationPtr mojoResult = ToMojoRegistration(result);
203 callback.Run(static_cast<content::BackgroundSyncError>(error),
204 mojoResult.Pass());
207 void BackgroundSyncServiceImpl::OnUnregisterResult(
208 const UnregisterCallback& callback,
209 BackgroundSyncManager::ErrorType error) {
210 DCHECK_CURRENTLY_ON(BrowserThread::IO);
211 callback.Run(static_cast<content::BackgroundSyncError>(error));
214 void BackgroundSyncServiceImpl::OnGetRegistrationsResult(
215 const GetRegistrationsCallback& callback,
216 BackgroundSyncManager::ErrorType error,
217 const std::vector<BackgroundSyncManager::BackgroundSyncRegistration>&
218 result_registrations) {
219 DCHECK_CURRENTLY_ON(BrowserThread::IO);
220 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0);
221 for (const auto& registration : result_registrations)
222 mojo_registrations.push_back(ToMojoRegistration(registration));
223 callback.Run(static_cast<content::BackgroundSyncError>(error),
224 mojo_registrations.Pass());
227 } // namespace content