Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / content / browser / background_sync / background_sync_service_impl.cc
blob6c5c5af0f279014a383a725a640fcb9c5ab6ae25
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/browser/background_sync/background_sync_registration.h"
10 #include "content/public/browser/browser_thread.h"
12 namespace content {
14 namespace {
16 // TODO(iclelland): Move these converters to mojo::TypeConverter template
17 // specializations.
19 BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions(
20 const SyncRegistrationPtr& in) {
21 BackgroundSyncRegistrationOptions out;
23 out.tag = in->tag;
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(const BackgroundSyncRegistration& in) {
32 SyncRegistrationPtr out(content::SyncRegistration::New());
33 out->id = in.id();
34 out->tag = in.options()->tag;
35 out->min_period_ms = in.options()->min_period;
36 out->periodicity = static_cast<content::BackgroundSyncPeriodicity>(
37 in.options()->periodicity);
38 out->power_state =
39 static_cast<content::BackgroundSyncPowerState>(in.options()->power_state);
40 out->network_state = static_cast<content::BackgroundSyncNetworkState>(
41 in.options()->network_state);
42 return out.Pass();
45 } // namespace
47 #define COMPILE_ASSERT_MATCHING_ENUM(mojo_name, manager_name) \
48 COMPILE_ASSERT(static_cast<int>(content::mojo_name) == \
49 static_cast<int>(content::manager_name), \
50 mismatching_enums)
52 // TODO(iclelland): Move these tests somewhere else
53 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_NONE,
54 BackgroundSyncManager::ERROR_TYPE_OK);
55 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_STORAGE,
56 BackgroundSyncManager::ERROR_TYPE_STORAGE);
57 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_NOT_FOUND,
58 BackgroundSyncManager::ERROR_TYPE_NOT_FOUND);
59 COMPILE_ASSERT_MATCHING_ENUM(
60 BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER,
61 BackgroundSyncManager::ERROR_TYPE_NO_SERVICE_WORKER);
62 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_NOT_ALLOWED,
63 BackgroundSyncManager::ERROR_TYPE_NOT_ALLOWED);
64 COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_MAX,
65 BackgroundSyncManager::ERROR_TYPE_NOT_ALLOWED);
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 sw_registration_id,
124 const RegisterCallback& callback) {
125 DCHECK_CURRENTLY_ON(BrowserThread::IO);
127 BackgroundSyncRegistrationOptions mgr_options =
128 ToBackgroundSyncRegistrationOptions(options);
130 BackgroundSyncManager* background_sync_manager =
131 background_sync_context_->background_sync_manager();
132 DCHECK(background_sync_manager);
133 background_sync_manager->Register(
134 sw_registration_id, mgr_options,
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 sw_registration_id,
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 sw_registration_id, 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 sw_registration_id,
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 sw_registration_id, tag.get(), static_cast<SyncPeriodicity>(periodicity),
166 base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult,
167 weak_ptr_factory_.GetWeakPtr(), callback));
170 void BackgroundSyncServiceImpl::GetRegistrations(
171 BackgroundSyncPeriodicity periodicity,
172 int64_t sw_registration_id,
173 const GetRegistrationsCallback& callback) {
174 DCHECK_CURRENTLY_ON(BrowserThread::IO);
175 BackgroundSyncManager* background_sync_manager =
176 background_sync_context_->background_sync_manager();
177 DCHECK(background_sync_manager);
178 background_sync_manager->GetRegistrations(
179 sw_registration_id, static_cast<SyncPeriodicity>(periodicity),
180 base::Bind(&BackgroundSyncServiceImpl::OnGetRegistrationsResult,
181 weak_ptr_factory_.GetWeakPtr(), callback));
184 void BackgroundSyncServiceImpl::GetPermissionStatus(
185 BackgroundSyncPeriodicity periodicity,
186 int64_t sw_registration_id,
187 const GetPermissionStatusCallback& callback) {
188 DCHECK_CURRENTLY_ON(BrowserThread::IO);
190 // TODO(iclelland): Implement a real policy. This is a stub implementation.
191 // OneShot: crbug.com/482091
192 // Periodic: crbug.com/482093
193 callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED);
196 void BackgroundSyncServiceImpl::OnRegisterResult(
197 const RegisterCallback& callback,
198 BackgroundSyncManager::ErrorType error,
199 const BackgroundSyncRegistration& result) {
200 DCHECK_CURRENTLY_ON(BrowserThread::IO);
201 SyncRegistrationPtr mojoResult = ToMojoRegistration(result);
202 callback.Run(static_cast<content::BackgroundSyncError>(error),
203 mojoResult.Pass());
206 void BackgroundSyncServiceImpl::OnUnregisterResult(
207 const UnregisterCallback& callback,
208 BackgroundSyncManager::ErrorType error) {
209 DCHECK_CURRENTLY_ON(BrowserThread::IO);
210 callback.Run(static_cast<content::BackgroundSyncError>(error));
213 void BackgroundSyncServiceImpl::OnGetRegistrationsResult(
214 const GetRegistrationsCallback& callback,
215 BackgroundSyncManager::ErrorType error,
216 const std::vector<BackgroundSyncRegistration>& result_registrations) {
217 DCHECK_CURRENTLY_ON(BrowserThread::IO);
218 mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0);
219 for (const auto& registration : result_registrations)
220 mojo_registrations.push_back(ToMojoRegistration(registration));
221 callback.Run(static_cast<content::BackgroundSyncError>(error),
222 mojo_registrations.Pass());
225 } // namespace content