OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ios/web/web_state/js/credential_util.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "ios/web/public/web_state/credential.h" |
| 12 #include "url/gurl.h" |
| 13 |
| 14 namespace { |
| 15 |
| 16 // "type" value for a DictionaryValue representation of LocalCredential. |
| 17 const char* kLocalCredentialType = "LocalCredential"; |
| 18 |
| 19 // "type" value for a DictionaryValue representation of FederatedCredential. |
| 20 const char* kFederatedCredentialType = "FederatedCredential"; |
| 21 |
| 22 } // namespace |
| 23 |
| 24 namespace web { |
| 25 |
| 26 bool DictionaryValueToCredential(const base::DictionaryValue& value, |
| 27 Credential* credential) { |
| 28 DCHECK(credential); |
| 29 |
| 30 base::string16 type; |
| 31 if (!value.GetString("type", &type)) |
| 32 return false; |
| 33 CredentialType credential_type; |
| 34 if (type == base::ASCIIToUTF16(kLocalCredentialType)) |
| 35 credential_type = CredentialType::CREDENTIAL_TYPE_LOCAL; |
| 36 else if (type == base::ASCIIToUTF16(kFederatedCredentialType)) |
| 37 credential_type = CredentialType::CREDENTIAL_TYPE_FEDERATED; |
| 38 else |
| 39 return false; |
| 40 |
| 41 base::string16 id; |
| 42 if (!value.GetString("id", &id)) |
| 43 return false; |
| 44 |
| 45 base::string16 name; |
| 46 value.GetString("name", &name); |
| 47 |
| 48 base::string16 avatar; |
| 49 GURL avatar_url; |
| 50 if (value.GetString("avatarURL", &avatar)) { |
| 51 avatar_url = GURL(avatar); |
| 52 if (!avatar_url.is_valid()) |
| 53 return false; |
| 54 } |
| 55 |
| 56 base::string16 password; |
| 57 if (credential_type == CredentialType::CREDENTIAL_TYPE_LOCAL && |
| 58 !value.GetString("password", &password)) { |
| 59 return false; |
| 60 } |
| 61 |
| 62 base::string16 federation; |
| 63 GURL federation_url; |
| 64 if (credential_type == CredentialType::CREDENTIAL_TYPE_FEDERATED) { |
| 65 if (!value.GetString("federation", &federation)) |
| 66 return false; |
| 67 federation_url = GURL(federation); |
| 68 if (!federation_url.is_valid()) |
| 69 return false; |
| 70 } |
| 71 |
| 72 credential->type = credential_type; |
| 73 credential->id = id; |
| 74 credential->name = name; |
| 75 credential->avatar_url = avatar_url; |
| 76 credential->password = password; |
| 77 credential->federation_url = federation_url; |
| 78 return true; |
| 79 } |
| 80 |
| 81 void CredentialToDictionaryValue(const Credential& credential, |
| 82 base::DictionaryValue* value) { |
| 83 switch (credential.type) { |
| 84 case CredentialType::CREDENTIAL_TYPE_LOCAL: |
| 85 value->SetString("type", kLocalCredentialType); |
| 86 value->SetString("password", credential.password); |
| 87 break; |
| 88 case CredentialType::CREDENTIAL_TYPE_FEDERATED: |
| 89 value->SetString("type", kFederatedCredentialType); |
| 90 value->SetString("federation", credential.federation_url.spec()); |
| 91 break; |
| 92 default: |
| 93 NOTREACHED(); |
| 94 } |
| 95 value->SetString("id", credential.id); |
| 96 value->SetString("name", credential.name); |
| 97 value->SetString("avatarURL", credential.avatar_url.spec()); |
| 98 } |
| 99 |
| 100 } // web |
OLD | NEW |