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
|
// 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 "components/sync_driver/device_info_service.h"
#include <vector>
#include "base/bind.h"
#include "sync/api/model_type_change_processor.h"
#include "sync/api/sync_error.h"
#include "sync/protocol/sync.pb.h"
#include "sync/util/time.h"
namespace sync_driver_v2 {
using sync_driver::DeviceInfo;
using sync_pb::DeviceInfoSpecifics;
DeviceInfoService::DeviceInfoService(
sync_driver::LocalDeviceInfoProvider* local_device_info_provider)
: local_device_backup_time_(-1),
local_device_info_provider_(local_device_info_provider) {
DCHECK(local_device_info_provider);
if (local_device_info_provider->GetLocalDeviceInfo()) {
OnProviderInitialized();
} else {
subscription_ =
local_device_info_provider->RegisterOnInitializedCallback(base::Bind(
&DeviceInfoService::OnProviderInitialized, base::Unretained(this)));
}
}
DeviceInfoService::~DeviceInfoService() {}
syncer_v2::MetadataChanges* DeviceInfoService::CreateMetadataChanges() {
// TODO(skym): Implementation.
return nullptr;
}
syncer::SyncError DeviceInfoService::MergeSyncData(
syncer_v2::MetadataChanges* metadata_changes,
syncer_v2::EntityDataList entity_data_list) {
// TODO(skym): Implementation.
return syncer::SyncError();
}
syncer::SyncError DeviceInfoService::ApplySyncChanges(
syncer_v2::MetadataChanges* metadata_changes,
syncer_v2::EntityChangeList entity_changes) {
// TODO(skym): Implementation.
return syncer::SyncError();
}
void DeviceInfoService::LoadMetadata(MetadataCallback callback) {
// TODO(skym): Implementation.
}
void DeviceInfoService::GetData(ClientKeyList client_keys,
DataCallback callback) {
// TODO(skym): Implementation.
}
void DeviceInfoService::GetAllData(DataCallback callback) {
// TODO(skym): Implementation.
}
std::string DeviceInfoService::GetClientTag(
const syncer_v2::EntityData* entity_data) {
// TODO(skym): Implementation.
return "";
}
bool DeviceInfoService::IsSyncing() const {
return !all_data_.empty();
}
scoped_ptr<DeviceInfo> DeviceInfoService::GetDeviceInfo(
const std::string& client_id) const {
ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id);
if (iter == all_data_.end()) {
return scoped_ptr<DeviceInfo>();
}
return CreateDeviceInfo(*iter->second);
}
ScopedVector<DeviceInfo> DeviceInfoService::GetAllDeviceInfo() const {
ScopedVector<DeviceInfo> list;
for (ClientIdToSpecifics::const_iterator iter = all_data_.begin();
iter != all_data_.end(); ++iter) {
list.push_back(CreateDeviceInfo(*iter->second));
}
return list.Pass();
}
void DeviceInfoService::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}
void DeviceInfoService::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}
void DeviceInfoService::NotifyObservers() {
FOR_EACH_OBSERVER(Observer, observers_, OnDeviceInfoChange());
}
void DeviceInfoService::UpdateLocalDeviceBackupTime(base::Time backup_time) {
// TODO(skym): Replace with is initialized check, we've already started
// syncing, provider is ready, make sure we have processort, etc.
// Local device info must be available in advance.
DCHECK(local_device_info_provider_->GetLocalDeviceInfo());
// TODO(skym): Should this be a less than instead of not equal check?
if (GetLocalDeviceBackupTime() != backup_time) {
// TODO(skym): Storing this field doesn't really make sense, remove.
set_local_device_backup_time(syncer::TimeToProtoTime(backup_time));
scoped_ptr<DeviceInfoSpecifics> new_specifics = CreateLocalSpecifics();
// TODO(skym): Create correct update datastructure, such as EntityChange,
// EntityMetadata, or CommitRequestData.
// TODO(skym): Call ProcessChanges on SMTP.
// TODO(skym): Persist metadata and data.
StoreSpecifics(new_specifics.Pass());
}
// Don't call NotifyObservers() because backup time is not part of
// DeviceInfoTracker interface.
}
base::Time DeviceInfoService::GetLocalDeviceBackupTime() const {
return has_local_device_backup_time()
? syncer::ProtoTimeToTime(local_device_backup_time())
: base::Time();
}
// TODO(skym): It might not make sense for this to be a scoped_ptr.
scoped_ptr<DeviceInfoSpecifics> DeviceInfoService::CreateLocalSpecifics() {
const DeviceInfo* info = local_device_info_provider_->GetLocalDeviceInfo();
DCHECK(info);
scoped_ptr<DeviceInfoSpecifics> specifics = CreateSpecifics(*info);
if (has_local_device_backup_time()) {
specifics->set_backup_timestamp(local_device_backup_time());
}
// TODO(skym): Local tag and non unique name have no place to be set now.
return specifics;
}
// TODO(skym): It might not make sense for this to be a scoped_ptr.
// Static.
scoped_ptr<DeviceInfoSpecifics> DeviceInfoService::CreateSpecifics(
const DeviceInfo& info) {
scoped_ptr<DeviceInfoSpecifics> specifics =
make_scoped_ptr(new DeviceInfoSpecifics);
specifics->set_cache_guid(info.guid());
specifics->set_client_name(info.client_name());
specifics->set_chrome_version(info.chrome_version());
specifics->set_sync_user_agent(info.sync_user_agent());
specifics->set_device_type(info.device_type());
specifics->set_signin_scoped_device_id(info.signin_scoped_device_id());
return specifics;
}
// Static.
scoped_ptr<DeviceInfo> DeviceInfoService::CreateDeviceInfo(
const DeviceInfoSpecifics& specifics) {
return make_scoped_ptr(new DeviceInfo(
specifics.cache_guid(), specifics.client_name(),
specifics.chrome_version(), specifics.sync_user_agent(),
specifics.device_type(), specifics.signin_scoped_device_id()));
}
void DeviceInfoService::StoreSpecifics(
scoped_ptr<DeviceInfoSpecifics> specifics) {
DVLOG(1) << "Storing DEVICE_INFO for " << specifics->client_name()
<< " with ID " << specifics->cache_guid();
all_data_[specifics->cache_guid()] = std::move(specifics);
}
void DeviceInfoService::DeleteSpecifics(const std::string& client_id) {
ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id);
if (iter != all_data_.end()) {
DVLOG(1) << "Deleting DEVICE_INFO for " << iter->second->client_name()
<< " with ID " << client_id;
all_data_.erase(iter);
}
}
void DeviceInfoService::OnProviderInitialized() {
// TODO(skym): Do we need this?
}
} // namespace sync_driver_v2
|