Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: components/wifi_sync/wifi_credential_syncable_service.cc

Issue 836303004: wifi_sync: implement ACTION_ADD in WifiCredentialSyncableService (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@submit-4.2-wifi-config-delegate
Patch Set: add more unit tests for ProcessSyncChanges Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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) {
stevenjb 2015/01/20 22:20:27 Is this clang formatted? It looks like these will
mukesh agrawal 2015/01/22 00:36:00 I'll reformat in PS6. (That should make the diff f
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()) {
117 DCHECK(network_config_delegate_);
19 } 118 }
20 119
21 WifiCredentialSyncableService::~WifiCredentialSyncableService() { 120 WifiCredentialSyncableService::~WifiCredentialSyncableService() {
22 } 121 }
23 122
24 // Implementation of syncer::SyncableService API. 123 // Implementation of syncer::SyncableService API.
25 syncer::SyncMergeResult WifiCredentialSyncableService::MergeDataAndStartSyncing( 124 syncer::SyncMergeResult WifiCredentialSyncableService::MergeDataAndStartSyncing(
26 syncer::ModelType type, 125 syncer::ModelType type,
27 const syncer::SyncDataList& initial_sync_data, 126 const syncer::SyncDataList& initial_sync_data,
28 scoped_ptr<syncer::SyncChangeProcessor> sync_processor, 127 scoped_ptr<syncer::SyncChangeProcessor> sync_processor,
29 scoped_ptr<syncer::SyncErrorFactory> /* error_handler */) { 128 scoped_ptr<syncer::SyncErrorFactory> /* error_handler */) {
30 DCHECK(!sync_processor_.get()); 129 DCHECK(!sync_processor_.get());
31 DCHECK(sync_processor.get()); 130 DCHECK(sync_processor.get());
32 DCHECK_EQ(kModelType, type); 131 DCHECK_EQ(kModelType, type);
33 132
34 sync_processor_ = sync_processor.Pass(); 133 sync_processor_ = sync_processor.Pass();
35 134
36 // TODO(quiche): Update local WiFi configuration. 135 // TODO(quiche): Update local WiFi configuration from |initial_sync_data|.
37 // TODO(quiche): Notify upper layers that sync is ready. 136 // TODO(quiche): Notify upper layers that sync is ready.
38 NOTIMPLEMENTED(); 137 NOTIMPLEMENTED();
39 138
40 return syncer::SyncMergeResult(type); 139 return syncer::SyncMergeResult(type);
41 } 140 }
42 141
43 void WifiCredentialSyncableService::StopSyncing(syncer::ModelType type) { 142 void WifiCredentialSyncableService::StopSyncing(syncer::ModelType type) {
44 DCHECK_EQ(kModelType, type); 143 DCHECK_EQ(kModelType, type);
45 sync_processor_.reset(); 144 sync_processor_.reset();
46 } 145 }
47 146
48 syncer::SyncDataList WifiCredentialSyncableService::GetAllSyncData( 147 syncer::SyncDataList WifiCredentialSyncableService::GetAllSyncData(
49 syncer::ModelType type) const { 148 syncer::ModelType type) const {
50 DCHECK_EQ(kModelType, type); 149 DCHECK_EQ(kModelType, type);
51 NOTIMPLEMENTED(); 150 NOTIMPLEMENTED();
52 return syncer::SyncDataList(); 151 return syncer::SyncDataList();
53 } 152 }
54 153
55 syncer::SyncError WifiCredentialSyncableService::ProcessSyncChanges( 154 syncer::SyncError WifiCredentialSyncableService::ProcessSyncChanges(
56 const tracked_objects::Location& /* caller_location */, 155 const tracked_objects::Location& /* caller_location */,
57 const syncer::SyncChangeList& change_list) { 156 const syncer::SyncChangeList& change_list) {
58 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 if (!sync_change.IsValid()) {
167 return syncer::SyncError(
168 FROM_HERE,
169 syncer::SyncError::DATATYPE_ERROR,
170 "ProcessSyncChanges was passed an invalid SyncChange",
171 kModelType);
172 }
173
174 std::vector<uint8_t> ssid_bytes;
175 WifiSecurityClass security_class;
176 std::string passphrase;
stevenjb 2015/01/20 22:20:27 nit: maybe use a struct for these to simplify the
mukesh agrawal 2015/01/22 00:35:59 Done.
177 if (!ParseSyncData(sync_change.sync_data(),
178 &ssid_bytes, &security_class, &passphrase)) {
179 LOG(WARNING) << "Failed to parse item; skipping "
180 << syncer::SyncChange::ChangeTypeToString(
181 sync_change.change_type());
182 continue;
183 }
184
185 scoped_ptr<WifiCredential> credential;
186 switch (sync_change.change_type()) {
187 case syncer::SyncChange::ACTION_ADD:
188 credential =
189 WifiCredential::Create(ssid_bytes, security_class, passphrase);
190 if (!credential)
191 LOG(WARNING) << "Failed to create credential; skipping";
192 else
193 network_config_delegate_->AddToLocalNetworks(*credential);
194 break;
195 case syncer::SyncChange::ACTION_UPDATE:
196 // TODO(quiche): Implement update, and add appropriate tests.
197 NOTIMPLEMENTED();
198 break;
199 case syncer::SyncChange::ACTION_DELETE:
200 // TODO(quiche): Implement delete, and add appropriate tests.
201 NOTIMPLEMENTED();
202 break;
203 default:
204 return syncer::SyncError(
205 FROM_HERE,
206 syncer::SyncError::DATATYPE_ERROR,
207 "ProcessSyncChanges given invalid SyncChangeType",
208 kModelType);
209 }
210 }
211
59 return syncer::SyncError(); 212 return syncer::SyncError();
60 } 213 }
61 214
215 bool WifiCredentialSyncableService::AddToSyncedNetworks(
216 const std::string& item_id, const WifiCredential& credential) {
217 if (!sync_processor_.get()) {
218 LOG(WARNING) << "WifiCredentials syncable service is not started.";
219 return false;
220 }
221
222 // TODO(quiche): Handle case where network already exists.
223 syncer::SyncChangeList change_list;
224 syncer::SyncError sync_error;
225 sync_pb::EntitySpecifics wifi_credential_specifics;
226 BuildSpecifics(credential.ssid(), credential.security_class(),
227 credential.passphrase(), &wifi_credential_specifics);
228 change_list.push_back(
229 syncer::SyncChange(
230 FROM_HERE,
231 syncer::SyncChange::ACTION_ADD,
232 syncer::SyncData::CreateLocalData(
233 item_id,
234 item_id,
235 wifi_credential_specifics)));
236 sync_error = sync_processor_->ProcessSyncChanges(FROM_HERE, change_list);
237 if (sync_error.IsSet()) {
238 LOG(ERROR) << sync_error.ToString();
239 return false;
240 }
241
242 return true;
243 }
244
62 } // namespace wifi_sync 245 } // namespace wifi_sync
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698