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