1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/background_sync/background_sync_service_impl.h"
#include "base/memory/weak_ptr.h"
#include "content/browser/background_sync/background_sync_context_impl.h"
#include "content/browser/background_sync/background_sync_registration.h"
#include "content/public/browser/browser_thread.h"
namespace content {
namespace {
// TODO(iclelland): Move these converters to mojo::TypeConverter template
// specializations.
BackgroundSyncRegistrationOptions ToBackgroundSyncRegistrationOptions(
const SyncRegistrationPtr& in) {
BackgroundSyncRegistrationOptions out;
out.tag = in->tag;
out.min_period = in->min_period_ms;
out.power_state = static_cast<SyncPowerState>(in->power_state);
out.network_state = static_cast<SyncNetworkState>(in->network_state);
out.periodicity = static_cast<SyncPeriodicity>(in->periodicity);
return out;
}
SyncRegistrationPtr ToMojoRegistration(const BackgroundSyncRegistration& in) {
SyncRegistrationPtr out(content::SyncRegistration::New());
out->id = in.id();
out->tag = in.options()->tag;
out->min_period_ms = in.options()->min_period;
out->periodicity = static_cast<content::BackgroundSyncPeriodicity>(
in.options()->periodicity);
out->power_state =
static_cast<content::BackgroundSyncPowerState>(in.options()->power_state);
out->network_state = static_cast<content::BackgroundSyncNetworkState>(
in.options()->network_state);
return out.Pass();
}
} // namespace
#define COMPILE_ASSERT_MATCHING_ENUM(mojo_name, manager_name) \
COMPILE_ASSERT(static_cast<int>(content::mojo_name) == \
static_cast<int>(content::manager_name), \
mismatching_enums)
// TODO(iclelland): Move these tests somewhere else
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_NONE,
BACKGROUND_SYNC_STATUS_OK);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_STORAGE,
BACKGROUND_SYNC_STATUS_STORAGE_ERROR);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_NOT_FOUND,
BACKGROUND_SYNC_STATUS_NOT_FOUND);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_NO_SERVICE_WORKER,
BACKGROUND_SYNC_STATUS_NO_SERVICE_WORKER);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_NOT_ALLOWED,
BACKGROUND_SYNC_STATUS_NOT_ALLOWED);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_ERROR_MAX,
BACKGROUND_SYNC_STATUS_NOT_ALLOWED);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_NETWORK_STATE_ANY,
SyncNetworkState::NETWORK_STATE_ANY);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_NETWORK_STATE_AVOID_CELLULAR,
SyncNetworkState::NETWORK_STATE_AVOID_CELLULAR);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_NETWORK_STATE_ONLINE,
SyncNetworkState::NETWORK_STATE_ONLINE);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_NETWORK_STATE_MAX,
SyncNetworkState::NETWORK_STATE_ONLINE);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_POWER_STATE_AUTO,
SyncPowerState::POWER_STATE_AUTO);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_POWER_STATE_AVOID_DRAINING,
SyncPowerState::POWER_STATE_AVOID_DRAINING);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_POWER_STATE_MAX,
SyncPowerState::POWER_STATE_AVOID_DRAINING);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_PERIODIC,
SyncPeriodicity::SYNC_PERIODIC);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_ONE_SHOT,
SyncPeriodicity::SYNC_ONE_SHOT);
COMPILE_ASSERT_MATCHING_ENUM(BACKGROUND_SYNC_PERIODICITY_MAX,
SyncPeriodicity::SYNC_ONE_SHOT);
// static
void BackgroundSyncServiceImpl::Create(
const scoped_refptr<BackgroundSyncContextImpl>& background_sync_context,
mojo::InterfaceRequest<BackgroundSyncService> request) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
BrowserThread::PostTask(
BrowserThread::IO, FROM_HERE,
base::Bind(&BackgroundSyncServiceImpl::CreateOnIOThread,
background_sync_context, base::Passed(&request)));
}
BackgroundSyncServiceImpl::~BackgroundSyncServiceImpl() {
// Should always be destroyed on the IO thread, but can't check that with
// DCHECK_CURRENTLY_ON because this class could be destroyed during thread
// shutdown, at which point that check doesn't work.
}
// static
void BackgroundSyncServiceImpl::CreateOnIOThread(
const scoped_refptr<BackgroundSyncContextImpl>& background_sync_context,
mojo::InterfaceRequest<BackgroundSyncService> request) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
new BackgroundSyncServiceImpl(background_sync_context, request.Pass());
}
BackgroundSyncServiceImpl::BackgroundSyncServiceImpl(
const scoped_refptr<BackgroundSyncContextImpl>& background_sync_context,
mojo::InterfaceRequest<BackgroundSyncService> request)
: background_sync_context_(background_sync_context),
binding_(this, request.Pass()),
weak_ptr_factory_(this) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
}
void BackgroundSyncServiceImpl::Register(content::SyncRegistrationPtr options,
int64_t sw_registration_id,
const RegisterCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
BackgroundSyncRegistrationOptions mgr_options =
ToBackgroundSyncRegistrationOptions(options);
BackgroundSyncManager* background_sync_manager =
background_sync_context_->background_sync_manager();
DCHECK(background_sync_manager);
background_sync_manager->Register(
sw_registration_id, mgr_options,
base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void BackgroundSyncServiceImpl::Unregister(
BackgroundSyncPeriodicity periodicity,
int64_t id,
const mojo::String& tag,
int64_t sw_registration_id,
const UnregisterCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
BackgroundSyncManager* background_sync_manager =
background_sync_context_->background_sync_manager();
DCHECK(background_sync_manager);
background_sync_manager->Unregister(
sw_registration_id, tag, content::SYNC_ONE_SHOT, id,
base::Bind(&BackgroundSyncServiceImpl::OnUnregisterResult,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void BackgroundSyncServiceImpl::GetRegistration(
BackgroundSyncPeriodicity periodicity,
const mojo::String& tag,
int64_t sw_registration_id,
const GetRegistrationCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
BackgroundSyncManager* background_sync_manager =
background_sync_context_->background_sync_manager();
DCHECK(background_sync_manager);
background_sync_manager->GetRegistration(
sw_registration_id, tag.get(), static_cast<SyncPeriodicity>(periodicity),
base::Bind(&BackgroundSyncServiceImpl::OnRegisterResult,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void BackgroundSyncServiceImpl::GetRegistrations(
BackgroundSyncPeriodicity periodicity,
int64_t sw_registration_id,
const GetRegistrationsCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
BackgroundSyncManager* background_sync_manager =
background_sync_context_->background_sync_manager();
DCHECK(background_sync_manager);
background_sync_manager->GetRegistrations(
sw_registration_id, static_cast<SyncPeriodicity>(periodicity),
base::Bind(&BackgroundSyncServiceImpl::OnGetRegistrationsResult,
weak_ptr_factory_.GetWeakPtr(), callback));
}
void BackgroundSyncServiceImpl::GetPermissionStatus(
BackgroundSyncPeriodicity periodicity,
int64_t sw_registration_id,
const GetPermissionStatusCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// TODO(iclelland): Implement a real policy. This is a stub implementation.
// OneShot: crbug.com/482091
// Periodic: crbug.com/482093
callback.Run(BACKGROUND_SYNC_ERROR_NONE, PERMISSION_STATUS_GRANTED);
}
void BackgroundSyncServiceImpl::OnRegisterResult(
const RegisterCallback& callback,
BackgroundSyncStatus status,
const BackgroundSyncRegistration& result) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
SyncRegistrationPtr mojoResult = ToMojoRegistration(result);
callback.Run(static_cast<content::BackgroundSyncError>(status),
mojoResult.Pass());
}
void BackgroundSyncServiceImpl::OnUnregisterResult(
const UnregisterCallback& callback,
BackgroundSyncStatus status) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
callback.Run(static_cast<content::BackgroundSyncError>(status));
}
void BackgroundSyncServiceImpl::OnGetRegistrationsResult(
const GetRegistrationsCallback& callback,
BackgroundSyncStatus status,
const std::vector<BackgroundSyncRegistration>& result_registrations) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
mojo::Array<content::SyncRegistrationPtr> mojo_registrations(0);
for (const auto& registration : result_registrations)
mojo_registrations.push_back(ToMojoRegistration(registration));
callback.Run(static_cast<content::BackgroundSyncError>(status),
mojo_registrations.Pass());
}
} // namespace content
|