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