OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/wifi_sync/wifi_credential_syncable_service_impl.h" | 5 #include "components/wifi_sync/wifi_credential_syncable_service_impl.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/stringprintf.h" |
11 #include "components/wifi_sync/wifi_credential.h" | 13 #include "components/wifi_sync/wifi_credential.h" |
12 #include "components/wifi_sync/wifi_security_class.h" | 14 #include "components/wifi_sync/wifi_security_class.h" |
13 #include "sync/api/sync_change.h" | 15 #include "sync/api/sync_change.h" |
14 #include "sync/api/sync_data.h" | 16 #include "sync/api/sync_data.h" |
15 #include "sync/api/sync_error.h" | 17 #include "sync/api/sync_error.h" |
16 #include "sync/api/sync_error_factory.h" | 18 #include "sync/api/sync_error_factory.h" |
17 #include "sync/api/sync_merge_result.h" | 19 #include "sync/api/sync_merge_result.h" |
18 #include "sync/protocol/sync.pb.h" | 20 #include "sync/protocol/sync.pb.h" |
19 | 21 |
20 namespace wifi_sync { | 22 namespace wifi_sync { |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 if (sync_data.GetDataType() != syncer::WIFI_CREDENTIALS) { | 95 if (sync_data.GetDataType() != syncer::WIFI_CREDENTIALS) { |
94 LOG(WARNING) << "Unexpected SyncData of type " | 96 LOG(WARNING) << "Unexpected SyncData of type " |
95 << syncer::ModelTypeToString(sync_data.GetDataType()) | 97 << syncer::ModelTypeToString(sync_data.GetDataType()) |
96 << "; skipping item"; | 98 << "; skipping item"; |
97 return false; | 99 return false; |
98 } | 100 } |
99 | 101 |
100 return ParseSpecifics(sync_data.GetSpecifics(), raw_credential); | 102 return ParseSpecifics(sync_data.GetSpecifics(), raw_credential); |
101 } | 103 } |
102 | 104 |
| 105 std::string ComputeItemId(const WifiCredential credential) { |
| 106 return base::StringPrintf( |
| 107 "%s:%d", |
| 108 base::HexEncode(credential.ssid().data(), |
| 109 credential.ssid().size()).c_str(), |
| 110 credential.security_class()); |
| 111 } |
| 112 |
103 } // namespace | 113 } // namespace |
104 | 114 |
105 const syncer::ModelType WifiCredentialSyncableServiceImpl::kModelType = | 115 const syncer::ModelType WifiCredentialSyncableServiceImpl::kModelType = |
106 syncer::WIFI_CREDENTIALS; | 116 syncer::WIFI_CREDENTIALS; |
107 | 117 |
108 WifiCredentialSyncableServiceImpl::WifiCredentialSyncableServiceImpl( | 118 WifiCredentialSyncableServiceImpl::WifiCredentialSyncableServiceImpl( |
109 scoped_ptr<WifiConfigDelegate> network_config_delegate) | 119 scoped_ptr<WifiConfigDelegate> network_config_delegate, |
110 : network_config_delegate_(network_config_delegate.Pass()) { | 120 scoped_ptr<WifiConfigObserver> network_config_observer) |
| 121 : network_config_delegate_(network_config_delegate.Pass()), |
| 122 network_config_observer_(network_config_observer.Pass()) { |
111 DCHECK(network_config_delegate_); | 123 DCHECK(network_config_delegate_); |
| 124 DCHECK(network_config_observer_); |
112 } | 125 } |
113 | 126 |
114 WifiCredentialSyncableServiceImpl::~WifiCredentialSyncableServiceImpl() { | 127 WifiCredentialSyncableServiceImpl::~WifiCredentialSyncableServiceImpl() { |
115 } | 128 } |
116 | 129 |
117 syncer::SyncMergeResult WifiCredentialSyncableServiceImpl::MergeDataAndStartSync
ing( | 130 syncer::SyncMergeResult WifiCredentialSyncableServiceImpl::MergeDataAndStartSync
ing( |
118 syncer::ModelType type, | 131 syncer::ModelType type, |
119 const syncer::SyncDataList& initial_sync_data, | 132 const syncer::SyncDataList& initial_sync_data, |
120 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 133 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
121 scoped_ptr<syncer::SyncErrorFactory> /* error_handler */) { | 134 scoped_ptr<syncer::SyncErrorFactory> /* error_handler */) { |
122 DCHECK(!sync_processor_.get()); | 135 DCHECK(!sync_processor_.get()); |
123 DCHECK(sync_processor.get()); | 136 DCHECK(sync_processor.get()); |
124 DCHECK_EQ(kModelType, type); | 137 DCHECK_EQ(kModelType, type); |
125 | 138 |
126 sync_processor_ = sync_processor.Pass(); | 139 sync_processor_ = sync_processor.Pass(); |
127 | 140 |
128 // TODO(quiche): Update local WiFi configuration from |initial_sync_data|. | 141 // TODO(quiche): Update local WiFi configuration from |initial_sync_data|. |
129 // TODO(quiche): Notify upper layers that sync is ready. | |
130 NOTIMPLEMENTED(); | 142 NOTIMPLEMENTED(); |
131 | 143 |
| 144 network_config_observer_->StartSyncing(base::AsWeakPtr(this)); |
132 return syncer::SyncMergeResult(type); | 145 return syncer::SyncMergeResult(type); |
133 } | 146 } |
134 | 147 |
135 void WifiCredentialSyncableServiceImpl::StopSyncing(syncer::ModelType type) { | 148 void WifiCredentialSyncableServiceImpl::StopSyncing(syncer::ModelType type) { |
136 DCHECK_EQ(kModelType, type); | 149 DCHECK_EQ(kModelType, type); |
| 150 network_config_observer_->StopSyncing(); |
137 sync_processor_.reset(); | 151 sync_processor_.reset(); |
138 } | 152 } |
139 | 153 |
140 syncer::SyncDataList WifiCredentialSyncableServiceImpl::GetAllSyncData( | 154 syncer::SyncDataList WifiCredentialSyncableServiceImpl::GetAllSyncData( |
141 syncer::ModelType type) const { | 155 syncer::ModelType type) const { |
142 DCHECK_EQ(kModelType, type); | 156 DCHECK_EQ(kModelType, type); |
143 NOTIMPLEMENTED(); | 157 NOTIMPLEMENTED(); |
144 return syncer::SyncDataList(); | 158 return syncer::SyncDataList(); |
145 } | 159 } |
146 | 160 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
187 return syncer::SyncError( | 201 return syncer::SyncError( |
188 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, | 202 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
189 "ProcessSyncChanges given invalid SyncChangeType", kModelType); | 203 "ProcessSyncChanges given invalid SyncChangeType", kModelType); |
190 } | 204 } |
191 } | 205 } |
192 | 206 |
193 return syncer::SyncError(); | 207 return syncer::SyncError(); |
194 } | 208 } |
195 | 209 |
196 bool WifiCredentialSyncableServiceImpl::AddToSyncedNetworks( | 210 bool WifiCredentialSyncableServiceImpl::AddToSyncedNetworks( |
197 const std::string& item_id, | |
198 const WifiCredential& credential) { | 211 const WifiCredential& credential) { |
199 if (!sync_processor_.get()) { | 212 if (!sync_processor_.get()) { |
200 // Callers must queue updates until MergeDataAndStartSyncing has | 213 // Callers must queue updates until MergeDataAndStartSyncing has |
201 // been called on this SyncableService. | 214 // been called on this SyncableService. |
202 LOG(WARNING) << "WifiCredentials syncable service is not started."; | 215 LOG(WARNING) << "WifiCredentials syncable service is not started."; |
203 return false; | 216 return false; |
204 } | 217 } |
205 | 218 |
206 const SsidAndSecurityClass network_id( | 219 const SsidAndSecurityClass network_id( |
207 credential.ssid(), credential.security_class()); | 220 credential.ssid(), credential.security_class()); |
208 if (synced_networks_and_passphrases_.find(network_id) != | 221 if (synced_networks_and_passphrases_.find(network_id) != |
209 synced_networks_and_passphrases_.end()) { | 222 synced_networks_and_passphrases_.end()) { |
210 // TODO(quiche): If passphrase has changed, submit this to sync as | 223 // TODO(quiche): If passphrase has changed, submit this to sync as |
211 // an ACTION_UPDATE. crbug.com/431436 | 224 // an ACTION_UPDATE. crbug.com/431436 |
212 return false; | 225 return false; |
213 } | 226 } |
214 | 227 |
215 syncer::SyncChangeList change_list; | 228 syncer::SyncChangeList change_list; |
216 syncer::SyncError sync_error; | 229 syncer::SyncError sync_error; |
217 sync_pb::EntitySpecifics wifi_credential_specifics; | 230 sync_pb::EntitySpecifics wifi_credential_specifics; |
| 231 std::string item_id(ComputeItemId(credential)); |
218 BuildSpecifics(credential, &wifi_credential_specifics); | 232 BuildSpecifics(credential, &wifi_credential_specifics); |
219 change_list.push_back( | 233 change_list.push_back( |
220 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_ADD, | 234 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_ADD, |
221 syncer::SyncData::CreateLocalData( | 235 syncer::SyncData::CreateLocalData( |
222 item_id, item_id, wifi_credential_specifics))); | 236 item_id, item_id, wifi_credential_specifics))); |
223 sync_error = sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); | 237 sync_error = sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); |
224 if (sync_error.IsSet()) { | 238 if (sync_error.IsSet()) { |
225 LOG(ERROR) << sync_error.ToString(); | 239 LOG(ERROR) << sync_error.ToString(); |
226 return false; | 240 return false; |
227 } | 241 } |
228 | 242 |
229 synced_networks_and_passphrases_[network_id] = credential.passphrase(); | 243 synced_networks_and_passphrases_[network_id] = credential.passphrase(); |
230 return true; | 244 return true; |
231 } | 245 } |
232 | 246 |
233 } // namespace wifi_sync | 247 } // namespace wifi_sync |
OLD | NEW |