Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(556)

Side by Side Diff: components/sync_driver/device_info_service.cc

Issue 1408123003: [Sync] Initial USS service implementation for DeviceInfo. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added some stub imaginary methods for the ModelTypeService interface. Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include "components/sync_driver/device_info_service.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/metrics/histogram_macros.h"
10 #include "base/stl_util.h"
11 #include "components/sync_driver/local_device_info_provider.h"
12 #include "sync/api/model_type_change_processor.h"
13 #include "sync/api/sync_error.h"
14 #include "sync/protocol/sync.pb.h"
15 #include "sync/util/time.h"
16
17 namespace sync_driver {
18
19 using sync_pb::DeviceInfoSpecifics;
20
21 DeviceInfoService::DeviceInfoService(
22 LocalDeviceInfoProvider* local_device_info_provider)
23 : local_device_backup_time_(-1),
24 local_device_info_provider_(local_device_info_provider) {
25 DCHECK(local_device_info_provider);
26
27 if (local_device_info_provider->GetLocalDeviceInfo()) {
28 OnProviderInitialized();
29 } else {
30 subscription_ =
31 local_device_info_provider->RegisterOnInitializedCallback(base::Bind(
32 &DeviceInfoService::OnProviderInitialized, base::Unretained(this)));
33 }
34 }
35
36 DeviceInfoService::~DeviceInfoService() {}
37
38 syncer::SyncError DeviceInfoService::ApplySyncChanges() {
39 // TODO(skym): For every model change, if not local, apply to memory + disk.
40 // TODO(skym): For ever entity metadata change, apply to disk.
41 // TODO(skym): Apply type metadata to disk.
42 return syncer::SyncError();
43 }
44
45 syncer::SyncError DeviceInfoService::LoadMetadata() {
46 // TODO(skym): Read out metadata from disk.
47 return syncer::SyncError();
48 }
49
50 syncer::SyncError DeviceInfoService::UpdateMetadata() {
51 // TODO(skym): Persist metadata to disk.
52 return syncer::SyncError();
53 }
54
55 syncer::SyncError DeviceInfoService::GetData() {
56 // TODO(skym): This is tricky. We're currently indexing data in memory by
57 // cache_guid, not client tags. And we cannot change this, because they're not
58 // equal on old clients. So maybe O(n*m) iterating through our map for every
59 // peice of data we were asked for? Alternative we could make a set, and do
60 // O(n + m). Another approach would to have two maps, which one ruling
61 // ownership. Or we could just read out of disk for this, instead of memory.
62 return syncer::SyncError();
63 }
64
65 syncer::SyncError DeviceInfoService::GetAllData() {
66 // TODO(skym): Return all data from memory, unless we're not initialized.
67 return syncer::SyncError();
68 }
69
70 syncer::SyncError DeviceInfoService::ClearMetadata() {
71 // We act a little differently than most sync services here. All functionality
72 // is to be shutdown and stopped, and all data is to be deleted.
73
74 bool was_syncing = IsSyncing();
75
76 all_data_.clear();
77 clear_local_device_backup_time();
78 clear_change_processor();
79
80 if (was_syncing) {
81 NotifyObservers();
82 }
83
84 // TODO(skym): Remove all data that's been persisted to storage.
85 // TODO(skym): Somehow remember we're no longer intialize.
86
87 return syncer::SyncError();
88 }
89
90 syncer_v2::ModelTypeChangeProcessor* DeviceInfoService::get_change_processor() {
91 return change_processor_.get();
92 }
93
94 void DeviceInfoService::set_change_processor(
95 scoped_ptr<syncer_v2::ModelTypeChangeProcessor> change_processor) {
96 change_processor_.swap(change_processor);
97 }
98
99 void DeviceInfoService::clear_change_processor() {
100 change_processor_.reset();
101 }
102
103 bool DeviceInfoService::IsSyncing() const {
104 return !all_data_.empty();
105 }
106
107 scoped_ptr<DeviceInfo> DeviceInfoService::GetDeviceInfo(
108 const std::string& client_id) const {
109 ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id);
110 if (iter == all_data_.end()) {
111 return scoped_ptr<DeviceInfo>();
112 }
113
114 return CreateDeviceInfo(*iter->second);
115 }
116
117 ScopedVector<DeviceInfo> DeviceInfoService::GetAllDeviceInfo() const {
118 ScopedVector<DeviceInfo> list;
119
120 for (ClientIdToSpecifics::const_iterator iter = all_data_.begin();
121 iter != all_data_.end(); ++iter) {
122 list.push_back(CreateDeviceInfo(*iter->second));
123 }
124
125 return list.Pass();
126 }
127
128 void DeviceInfoService::AddObserver(Observer* observer) {
129 observers_.AddObserver(observer);
130 }
131
132 void DeviceInfoService::RemoveObserver(Observer* observer) {
133 observers_.RemoveObserver(observer);
134 }
135
136 void DeviceInfoService::NotifyObservers() {
137 FOR_EACH_OBSERVER(Observer, observers_, OnDeviceInfoChange());
138 }
139
140 void DeviceInfoService::UpdateLocalDeviceBackupTime(base::Time backup_time) {
141 // TODO(skym): Replace with is initialized check, we've already started
142 // syncing, provider is ready, make sure we have processort, etc.
143
144 // Local device info must be available in advance.
145 DCHECK(local_device_info_provider_->GetLocalDeviceInfo());
146
147 // TODO(skym): Should this be a less than instead of not equal check?
148 if (GetLocalDeviceBackupTime() != backup_time) {
149 // TODO(skym): Storing this field doesn't really make sense, remove.
150 set_local_device_backup_time(syncer::TimeToProtoTime(backup_time));
151 scoped_ptr<DeviceInfoSpecifics> new_specifics = CreateLocalSpecifics();
152
153 // TODO(skym): Create correct update datastructure, such as EntityChange,
154 // EntityMetadata, or CommitRequestData.
155 // TODO(skym): Call ProcessChanges on SMTP.
156 // TODO(skym): Persist metadata and data.
157 StoreSpecifics(new_specifics.Pass());
158 }
159
160 // Don't call NotifyObservers() because backup time is not part of
161 // DeviceInfoTracker interface.
162 }
163
164 base::Time DeviceInfoService::GetLocalDeviceBackupTime() const {
165 return has_local_device_backup_time()
166 ? syncer::ProtoTimeToTime(local_device_backup_time())
167 : base::Time();
168 }
169
170 // TODO(skym): It might not make sense for this to be a scoped_ptr.
171 scoped_ptr<DeviceInfoSpecifics> DeviceInfoService::CreateLocalSpecifics() {
172 const DeviceInfo* info = local_device_info_provider_->GetLocalDeviceInfo();
173 DCHECK(info);
174 scoped_ptr<DeviceInfoSpecifics> specifics = CreateSpecifics(*info);
175 if (has_local_device_backup_time()) {
176 specifics->set_backup_timestamp(local_device_backup_time());
177 }
178 // TODO(skym): Local tag and non unique name have no place to be set now.
179 return specifics;
180 }
181
182 // TODO(skym): It might not make sense for this to be a scoped_ptr.
183 // Static.
184 scoped_ptr<DeviceInfoSpecifics> DeviceInfoService::CreateSpecifics(
185 const DeviceInfo& info) {
186 scoped_ptr<DeviceInfoSpecifics> specifics =
187 make_scoped_ptr(new DeviceInfoSpecifics);
188 specifics->set_cache_guid(info.guid());
189 specifics->set_client_name(info.client_name());
190 specifics->set_chrome_version(info.chrome_version());
191 specifics->set_sync_user_agent(info.sync_user_agent());
192 specifics->set_device_type(info.device_type());
193 specifics->set_signin_scoped_device_id(info.signin_scoped_device_id());
194 return specifics;
195 }
196
197 // Static.
198 scoped_ptr<DeviceInfo> DeviceInfoService::CreateDeviceInfo(
199 const DeviceInfoSpecifics& specifics) {
200 return make_scoped_ptr(new DeviceInfo(
201 specifics.cache_guid(), specifics.client_name(),
202 specifics.chrome_version(), specifics.sync_user_agent(),
203 specifics.device_type(), specifics.signin_scoped_device_id()));
204 }
205
206 void DeviceInfoService::StoreSpecifics(
207 scoped_ptr<DeviceInfoSpecifics> specifics) {
208 DVLOG(1) << "Storing DEVICE_INFO for " << specifics->client_name()
209 << " with ID " << specifics->cache_guid();
210 const std::string& key = specifics->cache_guid();
211 all_data_.set(key, specifics.Pass());
212 }
213
214 void DeviceInfoService::DeleteSpecifics(const std::string& client_id) {
215 ClientIdToSpecifics::const_iterator iter = all_data_.find(client_id);
216 if (iter != all_data_.end()) {
217 DVLOG(1) << "Deleting DEVICE_INFO for " << iter->second->client_name()
218 << " with ID " << client_id;
219 all_data_.erase(iter);
220 }
221 }
222
223 void DeviceInfoService::OnProviderInitialized() {
224 // TODO(skym): Do we need this?
225 }
226
227 } // namespace sync_driver
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698