| 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 "chrome/browser/local_discovery/privetv3_session.h" | 5 #include "chrome/browser/local_discovery/privetv3_session.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/json/json_writer.h" | 8 #include "base/json/json_writer.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 51 | 51 |
| 52 const int kUrlFetcherTimeoutSec = 30; | 52 const int kUrlFetcherTimeoutSec = 30; |
| 53 | 53 |
| 54 GURL CreatePrivetURL(const std::string& path) { | 54 GURL CreatePrivetURL(const std::string& path) { |
| 55 GURL url(kUrlPlaceHolder); | 55 GURL url(kUrlPlaceHolder); |
| 56 GURL::Replacements replacements; | 56 GURL::Replacements replacements; |
| 57 replacements.SetPathStr(path); | 57 replacements.SetPathStr(path); |
| 58 return url.ReplaceComponents(replacements); | 58 return url.ReplaceComponents(replacements); |
| 59 } | 59 } |
| 60 | 60 |
| 61 template <typename T> | |
| 62 class EnumToStringMap { | |
| 63 public: | |
| 64 static std::string FindNameById(T id) { | |
| 65 for (const Element& m : kMap) { | |
| 66 if (m.id == id) { | |
| 67 DCHECK(m.name); | |
| 68 return m.name; | |
| 69 } | |
| 70 } | |
| 71 NOTREACHED(); | |
| 72 return std::string(); | |
| 73 } | |
| 74 | |
| 75 static bool FindIdByName(const std::string& name, T* id) { | |
| 76 for (const Element& m : kMap) { | |
| 77 if (m.name && m.name == name) { | |
| 78 *id = m.id; | |
| 79 return true; | |
| 80 } | |
| 81 } | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 private: | |
| 86 struct Element { | |
| 87 const T id; | |
| 88 const char* const name; | |
| 89 }; | |
| 90 static const Element kMap[]; | |
| 91 }; | |
| 92 | |
| 93 using PairingType = PrivetV3Session::PairingType; | 61 using PairingType = PrivetV3Session::PairingType; |
| 94 | 62 |
| 95 template <> | |
| 96 const EnumToStringMap<PrivetV3Session::PairingType>::Element | |
| 97 EnumToStringMap<PrivetV3Session::PairingType>::kMap[] = { | |
| 98 {PairingType::PAIRING_TYPE_PINCODE, "pinCode"}, | |
| 99 {PairingType::PAIRING_TYPE_EMBEDDEDCODE, "embeddedCode"}, | |
| 100 {PairingType::PAIRING_TYPE_ULTRASOUND32, "ultrasound32"}, | |
| 101 {PairingType::PAIRING_TYPE_AUDIBLE32, "audible32"}, | |
| 102 }; | |
| 103 | |
| 104 template <typename T> | |
| 105 std::string EnumToString(T id) { | |
| 106 return EnumToStringMap<T>::FindNameById(id); | |
| 107 } | |
| 108 | |
| 109 template <typename T> | |
| 110 bool StringToEnum(const std::string& name, T* id) { | |
| 111 return EnumToStringMap<T>::FindIdByName(name, id); | |
| 112 } | |
| 113 | |
| 114 bool GetDecodedString(const base::DictionaryValue& response, | 63 bool GetDecodedString(const base::DictionaryValue& response, |
| 115 const std::string& key, | 64 const std::string& key, |
| 116 std::string* value) { | 65 std::string* value) { |
| 117 std::string base64; | 66 std::string base64; |
| 118 return response.GetString(key, &base64) && base::Base64Decode(base64, value); | 67 return response.GetString(key, &base64) && base::Base64Decode(base64, value); |
| 119 } | 68 } |
| 120 | 69 |
| 121 bool ContainsString(const base::DictionaryValue& dictionary, | 70 bool ContainsString(const base::DictionaryValue& dictionary, |
| 122 const std::string& key, | 71 const std::string& key, |
| 123 const std::string& expected_value) { | 72 const std::string& expected_value) { |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 270 privet_auth_token_ = kPrivetV3AuthAnonymous; | 219 privet_auth_token_ = kPrivetV3AuthAnonymous; |
| 271 | 220 |
| 272 StartGetRequest(kPrivetInfoPath, | 221 StartGetRequest(kPrivetInfoPath, |
| 273 base::Bind(&PrivetV3Session::OnInfoDone, | 222 base::Bind(&PrivetV3Session::OnInfoDone, |
| 274 weak_ptr_factory_.GetWeakPtr(), callback)); | 223 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 275 } | 224 } |
| 276 | 225 |
| 277 void PrivetV3Session::OnInfoDone(const InitCallback& callback, | 226 void PrivetV3Session::OnInfoDone(const InitCallback& callback, |
| 278 Result result, | 227 Result result, |
| 279 const base::DictionaryValue& response) { | 228 const base::DictionaryValue& response) { |
| 280 std::vector<PairingType> pairing_types; | |
| 281 if (result != Result::STATUS_SUCCESS) | 229 if (result != Result::STATUS_SUCCESS) |
| 282 return callback.Run(result, pairing_types); | 230 return callback.Run(result, response); |
| 283 | 231 |
| 284 std::string version; | 232 std::string version; |
| 285 if (!response.GetString(kPrivetV3InfoKeyVersion, &version) || | 233 if (!response.GetString(kPrivetV3InfoKeyVersion, &version) || |
| 286 version != kPrivetV3InfoVersion) { | 234 version != kPrivetV3InfoVersion) { |
| 287 LOG(ERROR) << "Response: " << response; | 235 LOG(ERROR) << "Response: " << response; |
| 288 return callback.Run(Result::STATUS_SESSIONERROR, pairing_types); | 236 return callback.Run(Result::STATUS_SESSIONERROR, response); |
| 289 } | 237 } |
| 290 | 238 |
| 291 const base::DictionaryValue* authentication = nullptr; | 239 const base::DictionaryValue* authentication = nullptr; |
| 292 const base::ListValue* pairing = nullptr; | 240 const base::ListValue* pairing = nullptr; |
| 293 if (!response.GetDictionary(kPrivetV3InfoKeyAuth, &authentication) || | 241 if (!response.GetDictionary(kPrivetV3InfoKeyAuth, &authentication) || |
| 294 !authentication->GetList(kPrivetV3KeyPairing, &pairing)) { | 242 !authentication->GetList(kPrivetV3KeyPairing, &pairing)) { |
| 295 LOG(ERROR) << "Response: " << response; | 243 LOG(ERROR) << "Response: " << response; |
| 296 return callback.Run(Result::STATUS_SESSIONERROR, pairing_types); | 244 return callback.Run(Result::STATUS_SESSIONERROR, response); |
| 297 } | 245 } |
| 298 | 246 |
| 299 // The only supported crypto. | 247 // The only supported crypto. |
| 300 if (!ContainsString(*authentication, kPrivetV3KeyCrypto, | 248 if (!ContainsString(*authentication, kPrivetV3KeyCrypto, |
| 301 kPrivetV3CryptoP224Spake2) || | 249 kPrivetV3CryptoP224Spake2) || |
| 302 !ContainsString(*authentication, kPrivetV3KeyMode, kPrivetV3KeyPairing)) { | 250 !ContainsString(*authentication, kPrivetV3KeyMode, kPrivetV3KeyPairing)) { |
| 303 LOG(ERROR) << "Response: " << response; | 251 LOG(ERROR) << "Response: " << response; |
| 304 return callback.Run(Result::STATUS_SESSIONERROR, pairing_types); | 252 return callback.Run(Result::STATUS_SESSIONERROR, response); |
| 305 } | 253 } |
| 306 | 254 |
| 307 for (const base::Value* value : *pairing) { | 255 callback.Run(Result::STATUS_SUCCESS, response); |
| 308 std::string pairing_string; | |
| 309 PairingType pairing_type; | |
| 310 if (!value->GetAsString(&pairing_string) || | |
| 311 !StringToEnum(pairing_string, &pairing_type)) { | |
| 312 continue; // Skip unknown pairing. | |
| 313 } | |
| 314 pairing_types.push_back(pairing_type); | |
| 315 } | |
| 316 | |
| 317 callback.Run(Result::STATUS_SUCCESS, pairing_types); | |
| 318 } | 256 } |
| 319 | 257 |
| 320 void PrivetV3Session::StartPairing(PairingType pairing_type, | 258 void PrivetV3Session::StartPairing(PairingType pairing_type, |
| 321 const ResultCallback& callback) { | 259 const ResultCallback& callback) { |
| 322 base::DictionaryValue input; | 260 base::DictionaryValue input; |
| 323 input.SetString(kPrivetV3KeyPairing, EnumToString(pairing_type)); | 261 input.SetString(kPrivetV3KeyPairing, |
| 262 extensions::api::gcd_private::ToString(pairing_type)); |
| 324 input.SetString(kPrivetV3KeyCrypto, kPrivetV3CryptoP224Spake2); | 263 input.SetString(kPrivetV3KeyCrypto, kPrivetV3CryptoP224Spake2); |
| 325 | 264 |
| 326 StartPostRequest(kPrivetV3PairingStartPath, input, | 265 StartPostRequest(kPrivetV3PairingStartPath, input, |
| 327 base::Bind(&PrivetV3Session::OnPairingStartDone, | 266 base::Bind(&PrivetV3Session::OnPairingStartDone, |
| 328 weak_ptr_factory_.GetWeakPtr(), callback)); | 267 weak_ptr_factory_.GetWeakPtr(), callback)); |
| 329 } | 268 } |
| 330 | 269 |
| 331 void PrivetV3Session::OnPairingStartDone( | 270 void PrivetV3Session::OnPairingStartDone( |
| 332 const ResultCallback& callback, | 271 const ResultCallback& callback, |
| 333 Result result, | 272 Result result, |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 499 void PrivetV3Session::Cancel() { | 438 void PrivetV3Session::Cancel() { |
| 500 // Cancel started unconfirmed sessions. | 439 // Cancel started unconfirmed sessions. |
| 501 if (session_id_.empty() || !fingerprint_.empty()) | 440 if (session_id_.empty() || !fingerprint_.empty()) |
| 502 return; | 441 return; |
| 503 base::DictionaryValue input; | 442 base::DictionaryValue input; |
| 504 input.SetString(kPrivetV3KeySessionId, session_id_); | 443 input.SetString(kPrivetV3KeySessionId, session_id_); |
| 505 StartPostRequest(kPrivetV3PairingCancelPath, input, MessageCallback()); | 444 StartPostRequest(kPrivetV3PairingCancelPath, input, MessageCallback()); |
| 506 } | 445 } |
| 507 | 446 |
| 508 } // namespace local_discovery | 447 } // namespace local_discovery |
| OLD | NEW |