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.h" | 5 #include "components/wifi_sync/wifi_credential_syncable_service.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 #include <vector> |
| 9 |
7 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "components/wifi_sync/wifi_credential.h" |
| 12 #include "components/wifi_sync/wifi_security_class.h" |
8 #include "sync/api/sync_change.h" | 13 #include "sync/api/sync_change.h" |
| 14 #include "sync/api/sync_data.h" |
9 #include "sync/api/sync_error.h" | 15 #include "sync/api/sync_error.h" |
10 #include "sync/api/sync_error_factory.h" | 16 #include "sync/api/sync_error_factory.h" |
11 #include "sync/api/sync_merge_result.h" | 17 #include "sync/api/sync_merge_result.h" |
| 18 #include "sync/protocol/sync.pb.h" |
12 | 19 |
13 namespace wifi_sync { | 20 namespace wifi_sync { |
14 | 21 |
| 22 namespace { |
| 23 |
| 24 struct RawCredentialData { |
| 25 std::vector<uint8_t> ssid; |
| 26 WifiSecurityClass security_class; |
| 27 std::string passphrase; |
| 28 }; |
| 29 |
| 30 void BuildSpecifics(const WifiCredential& credential, |
| 31 sync_pb::EntitySpecifics* out_buffer) { |
| 32 DCHECK(out_buffer); |
| 33 sync_pb::WifiCredentialSpecifics* credential_specifics = |
| 34 out_buffer->mutable_wifi_credential(); |
| 35 DCHECK(credential_specifics); |
| 36 credential_specifics->set_ssid(credential.ssid().data(), |
| 37 credential.ssid().size()); |
| 38 credential_specifics->set_security_class( |
| 39 WifiSecurityClassToSyncSecurityClass(credential.security_class())); |
| 40 if (WifiSecurityClassSupportsPassphrases(credential.security_class())) { |
| 41 credential_specifics->set_passphrase(credential.passphrase().data(), |
| 42 credential.passphrase().size()); |
| 43 } |
| 44 } |
| 45 |
| 46 bool ParseSpecifics(const sync_pb::EntitySpecifics& specifics, |
| 47 RawCredentialData* raw_credential) { |
| 48 DCHECK(raw_credential); |
| 49 if (!specifics.has_wifi_credential()) { |
| 50 LOG(ERROR) << "Specifics with missing wifi_credential; skipping"; |
| 51 return false; |
| 52 } |
| 53 |
| 54 const sync_pb::WifiCredentialSpecifics& credential_specifics = |
| 55 specifics.wifi_credential(); |
| 56 if (!credential_specifics.has_ssid()) { |
| 57 LOG(ERROR) << "Specifics with missing SSID; skipping"; |
| 58 return false; |
| 59 } |
| 60 if (!credential_specifics.has_security_class()) { |
| 61 LOG(ERROR) << "Specifics with missing security class; skipping"; |
| 62 return false; |
| 63 } |
| 64 |
| 65 const WifiSecurityClass security_class = |
| 66 WifiSecurityClassFromSyncSecurityClass( |
| 67 credential_specifics.security_class()); |
| 68 if (WifiSecurityClassSupportsPassphrases(security_class) && |
| 69 !credential_specifics.has_passphrase()) { |
| 70 LOG(ERROR) << "Specifics for security class " |
| 71 << credential_specifics.security_class() |
| 72 << " is missing passphrase; skipping"; |
| 73 return false; |
| 74 } |
| 75 |
| 76 raw_credential->ssid.assign(credential_specifics.ssid().begin(), |
| 77 credential_specifics.ssid().end()); |
| 78 raw_credential->security_class = security_class; |
| 79 raw_credential->passphrase = credential_specifics.passphrase(); |
| 80 return true; |
| 81 } |
| 82 |
| 83 // TODO(quiche): Separate SyncData validation from parsing of |
| 84 // WifiCredentialSpecifics. |
| 85 bool ParseSyncData(const syncer::SyncData& sync_data, |
| 86 RawCredentialData* raw_credential) { |
| 87 DCHECK(raw_credential); |
| 88 if (!sync_data.IsValid()) { |
| 89 LOG(WARNING) << "Invalid SyncData; skipping item"; |
| 90 return false; |
| 91 } |
| 92 |
| 93 if (sync_data.GetDataType() != syncer::WIFI_CREDENTIALS) { |
| 94 LOG(WARNING) << "Unexpected SyncData of type " |
| 95 << syncer::ModelTypeToString(sync_data.GetDataType()) |
| 96 << "; skipping item"; |
| 97 return false; |
| 98 } |
| 99 |
| 100 return ParseSpecifics(sync_data.GetSpecifics(), raw_credential); |
| 101 } |
| 102 |
| 103 } // namespace |
| 104 |
15 const syncer::ModelType WifiCredentialSyncableService::kModelType = | 105 const syncer::ModelType WifiCredentialSyncableService::kModelType = |
16 syncer::WIFI_CREDENTIALS; | 106 syncer::WIFI_CREDENTIALS; |
17 | 107 |
18 WifiCredentialSyncableService::WifiCredentialSyncableService() { | 108 WifiCredentialSyncableService::WifiCredentialSyncableService( |
| 109 scoped_ptr<WifiConfigDelegate> network_config_delegate) |
| 110 : network_config_delegate_(network_config_delegate.Pass()) { |
| 111 DCHECK(network_config_delegate_); |
19 } | 112 } |
20 | 113 |
21 WifiCredentialSyncableService::~WifiCredentialSyncableService() { | 114 WifiCredentialSyncableService::~WifiCredentialSyncableService() { |
22 } | 115 } |
23 | 116 |
24 syncer::SyncMergeResult WifiCredentialSyncableService::MergeDataAndStartSyncing( | 117 syncer::SyncMergeResult WifiCredentialSyncableService::MergeDataAndStartSyncing( |
25 syncer::ModelType type, | 118 syncer::ModelType type, |
26 const syncer::SyncDataList& initial_sync_data, | 119 const syncer::SyncDataList& initial_sync_data, |
27 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, | 120 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, |
28 scoped_ptr<syncer::SyncErrorFactory> /* error_handler */) { | 121 scoped_ptr<syncer::SyncErrorFactory> /* error_handler */) { |
29 DCHECK(!sync_processor_.get()); | 122 DCHECK(!sync_processor_.get()); |
30 DCHECK(sync_processor.get()); | 123 DCHECK(sync_processor.get()); |
31 DCHECK_EQ(kModelType, type); | 124 DCHECK_EQ(kModelType, type); |
32 | 125 |
33 sync_processor_ = sync_processor.Pass(); | 126 sync_processor_ = sync_processor.Pass(); |
34 | 127 |
35 // TODO(quiche): Update local WiFi configuration. | 128 // TODO(quiche): Update local WiFi configuration from |initial_sync_data|. |
36 // TODO(quiche): Notify upper layers that sync is ready. | 129 // TODO(quiche): Notify upper layers that sync is ready. |
37 NOTIMPLEMENTED(); | 130 NOTIMPLEMENTED(); |
38 | 131 |
39 return syncer::SyncMergeResult(type); | 132 return syncer::SyncMergeResult(type); |
40 } | 133 } |
41 | 134 |
42 void WifiCredentialSyncableService::StopSyncing(syncer::ModelType type) { | 135 void WifiCredentialSyncableService::StopSyncing(syncer::ModelType type) { |
43 DCHECK_EQ(kModelType, type); | 136 DCHECK_EQ(kModelType, type); |
44 sync_processor_.reset(); | 137 sync_processor_.reset(); |
45 } | 138 } |
46 | 139 |
47 syncer::SyncDataList WifiCredentialSyncableService::GetAllSyncData( | 140 syncer::SyncDataList WifiCredentialSyncableService::GetAllSyncData( |
48 syncer::ModelType type) const { | 141 syncer::ModelType type) const { |
49 DCHECK_EQ(kModelType, type); | 142 DCHECK_EQ(kModelType, type); |
50 NOTIMPLEMENTED(); | 143 NOTIMPLEMENTED(); |
51 return syncer::SyncDataList(); | 144 return syncer::SyncDataList(); |
52 } | 145 } |
53 | 146 |
54 syncer::SyncError WifiCredentialSyncableService::ProcessSyncChanges( | 147 syncer::SyncError WifiCredentialSyncableService::ProcessSyncChanges( |
55 const tracked_objects::Location& /* caller_location */, | 148 const tracked_objects::Location& /* caller_location */, |
56 const syncer::SyncChangeList& change_list) { | 149 const syncer::SyncChangeList& change_list) { |
57 NOTIMPLEMENTED(); | 150 if (!sync_processor_.get()) { |
| 151 return syncer::SyncError( |
| 152 FROM_HERE, syncer::SyncError::UNREADY_ERROR, |
| 153 "ProcessSyncChanges called before MergeDataAndStartSyncing", |
| 154 kModelType); |
| 155 } |
| 156 |
| 157 for (const syncer::SyncChange& sync_change : change_list) { |
| 158 DCHECK(sync_change.IsValid()); |
| 159 RawCredentialData raw_credential; |
| 160 if (!ParseSyncData(sync_change.sync_data(), &raw_credential)) { |
| 161 LOG(WARNING) << "Failed to parse item; skipping " |
| 162 << syncer::SyncChange::ChangeTypeToString( |
| 163 sync_change.change_type()); |
| 164 continue; |
| 165 } |
| 166 |
| 167 scoped_ptr<WifiCredential> credential; |
| 168 switch (sync_change.change_type()) { |
| 169 case syncer::SyncChange::ACTION_ADD: |
| 170 credential = WifiCredential::Create(raw_credential.ssid, |
| 171 raw_credential.security_class, |
| 172 raw_credential.passphrase); |
| 173 if (!credential) |
| 174 LOG(WARNING) << "Failed to create credential; skipping"; |
| 175 else |
| 176 network_config_delegate_->AddToLocalNetworks(*credential); |
| 177 break; |
| 178 case syncer::SyncChange::ACTION_UPDATE: |
| 179 // TODO(quiche): Implement update, and add appropriate tests. |
| 180 NOTIMPLEMENTED(); |
| 181 break; |
| 182 case syncer::SyncChange::ACTION_DELETE: |
| 183 // TODO(quiche): Implement delete, and add appropriate tests. |
| 184 NOTIMPLEMENTED(); |
| 185 break; |
| 186 default: |
| 187 return syncer::SyncError( |
| 188 FROM_HERE, syncer::SyncError::DATATYPE_ERROR, |
| 189 "ProcessSyncChanges given invalid SyncChangeType", kModelType); |
| 190 } |
| 191 } |
| 192 |
58 return syncer::SyncError(); | 193 return syncer::SyncError(); |
59 } | 194 } |
60 | 195 |
| 196 bool WifiCredentialSyncableService::AddToSyncedNetworks( |
| 197 const std::string& item_id, |
| 198 const WifiCredential& credential) { |
| 199 if (!sync_processor_.get()) { |
| 200 // Callers must queue updates until MergeDataAndStartSyncing has |
| 201 // been called on this SyncableService. |
| 202 LOG(WARNING) << "WifiCredentials syncable service is not started."; |
| 203 return false; |
| 204 } |
| 205 |
| 206 // TODO(quiche): Handle case where network already exists. |
| 207 syncer::SyncChangeList change_list; |
| 208 syncer::SyncError sync_error; |
| 209 sync_pb::EntitySpecifics wifi_credential_specifics; |
| 210 BuildSpecifics(credential, &wifi_credential_specifics); |
| 211 change_list.push_back( |
| 212 syncer::SyncChange(FROM_HERE, syncer::SyncChange::ACTION_ADD, |
| 213 syncer::SyncData::CreateLocalData( |
| 214 item_id, item_id, wifi_credential_specifics))); |
| 215 sync_error = sync_processor_->ProcessSyncChanges(FROM_HERE, change_list); |
| 216 if (sync_error.IsSet()) { |
| 217 LOG(ERROR) << sync_error.ToString(); |
| 218 return false; |
| 219 } |
| 220 |
| 221 return true; |
| 222 } |
| 223 |
61 } // namespace wifi_sync | 224 } // namespace wifi_sync |
OLD | NEW |