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