| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "ios/web/web_state/js/credential_util.h" | 5 #include "ios/web/web_state/js/credential_util.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/strings/string16.h" | 8 #include "base/strings/string16.h" |
| 9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "ios/web/public/web_state/credential.h" | 11 #include "ios/web/public/web_state/credential.h" |
| 12 #include "url/gurl.h" | 12 #include "url/gurl.h" |
| 13 | 13 |
| 14 namespace { | 14 namespace { |
| 15 | 15 |
| 16 // "type" value for a DictionaryValue representation of LocalCredential. | 16 // "type" value for a DictionaryValue representation of PasswordCredential. |
| 17 const char* kLocalCredentialType = "LocalCredential"; | 17 const char* kPasswordCredentialType = "PasswordCredential"; |
| 18 | 18 |
| 19 // "type" value for a DictionaryValue representation of FederatedCredential. | 19 // "type" value for a DictionaryValue representation of FederatedCredential. |
| 20 const char* kFederatedCredentialType = "FederatedCredential"; | 20 const char* kFederatedCredentialType = "FederatedCredential"; |
| 21 | 21 |
| 22 } // namespace | 22 } // namespace |
| 23 | 23 |
| 24 namespace web { | 24 namespace web { |
| 25 | 25 |
| 26 bool DictionaryValueToCredential(const base::DictionaryValue& value, | 26 bool DictionaryValueToCredential(const base::DictionaryValue& value, |
| 27 Credential* credential) { | 27 Credential* credential) { |
| 28 DCHECK(credential); | 28 DCHECK(credential); |
| 29 | 29 |
| 30 base::string16 type; | 30 base::string16 type; |
| 31 if (!value.GetString("type", &type)) | 31 if (!value.GetString("type", &type)) |
| 32 return false; | 32 return false; |
| 33 CredentialType credential_type; | 33 CredentialType credential_type; |
| 34 if (type == base::ASCIIToUTF16(kLocalCredentialType)) | 34 if (type == base::ASCIIToUTF16(kPasswordCredentialType)) |
| 35 credential_type = CredentialType::CREDENTIAL_TYPE_LOCAL; | 35 credential_type = CredentialType::CREDENTIAL_TYPE_PASSWORD; |
| 36 else if (type == base::ASCIIToUTF16(kFederatedCredentialType)) | 36 else if (type == base::ASCIIToUTF16(kFederatedCredentialType)) |
| 37 credential_type = CredentialType::CREDENTIAL_TYPE_FEDERATED; | 37 credential_type = CredentialType::CREDENTIAL_TYPE_FEDERATED; |
| 38 else | 38 else |
| 39 return false; | 39 return false; |
| 40 | 40 |
| 41 base::string16 id; | 41 base::string16 id; |
| 42 if (!value.GetString("id", &id)) | 42 if (!value.GetString("id", &id)) |
| 43 return false; | 43 return false; |
| 44 | 44 |
| 45 base::string16 name; | 45 base::string16 name; |
| 46 value.GetString("name", &name); | 46 value.GetString("name", &name); |
| 47 | 47 |
| 48 base::string16 avatar; | 48 base::string16 avatar; |
| 49 GURL avatar_url; | 49 GURL avatar_url; |
| 50 if (value.GetString("avatarURL", &avatar)) { | 50 if (value.GetString("avatarURL", &avatar)) { |
| 51 avatar_url = GURL(avatar); | 51 avatar_url = GURL(avatar); |
| 52 if (!avatar_url.is_valid()) | 52 if (!avatar_url.is_valid()) |
| 53 return false; | 53 return false; |
| 54 } | 54 } |
| 55 | 55 |
| 56 base::string16 password; | 56 base::string16 password; |
| 57 if (credential_type == CredentialType::CREDENTIAL_TYPE_LOCAL && | 57 if (credential_type == CredentialType::CREDENTIAL_TYPE_PASSWORD && |
| 58 !value.GetString("password", &password)) { | 58 !value.GetString("password", &password)) { |
| 59 return false; | 59 return false; |
| 60 } | 60 } |
| 61 | 61 |
| 62 base::string16 federation; | 62 base::string16 federation; |
| 63 GURL federation_url; | 63 GURL federation_url; |
| 64 if (credential_type == CredentialType::CREDENTIAL_TYPE_FEDERATED) { | 64 if (credential_type == CredentialType::CREDENTIAL_TYPE_FEDERATED) { |
| 65 if (!value.GetString("federation", &federation)) | 65 if (!value.GetString("federation", &federation)) |
| 66 return false; | 66 return false; |
| 67 federation_url = GURL(federation); | 67 federation_url = GURL(federation); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 80 | 80 |
| 81 void CredentialToDictionaryValue(const Credential& credential, | 81 void CredentialToDictionaryValue(const Credential& credential, |
| 82 base::DictionaryValue* value) { | 82 base::DictionaryValue* value) { |
| 83 DCHECK(value); | 83 DCHECK(value); |
| 84 switch (credential.type) { | 84 switch (credential.type) { |
| 85 case CredentialType::CREDENTIAL_TYPE_EMPTY: | 85 case CredentialType::CREDENTIAL_TYPE_EMPTY: |
| 86 // Return an empty dictionary. This will cause "null" to be returned to | 86 // Return an empty dictionary. This will cause "null" to be returned to |
| 87 // the JavaScript Promise resolver. | 87 // the JavaScript Promise resolver. |
| 88 value->Clear(); | 88 value->Clear(); |
| 89 return; | 89 return; |
| 90 case CredentialType::CREDENTIAL_TYPE_LOCAL: | 90 case CredentialType::CREDENTIAL_TYPE_PASSWORD: |
| 91 value->SetString("type", kLocalCredentialType); | 91 value->SetString("type", kPasswordCredentialType); |
| 92 value->SetString("password", credential.password); | 92 value->SetString("password", credential.password); |
| 93 break; | 93 break; |
| 94 case CredentialType::CREDENTIAL_TYPE_FEDERATED: | 94 case CredentialType::CREDENTIAL_TYPE_FEDERATED: |
| 95 value->SetString("type", kFederatedCredentialType); | 95 value->SetString("type", kFederatedCredentialType); |
| 96 value->SetString("federation", credential.federation_url.spec()); | 96 value->SetString("federation", credential.federation_url.spec()); |
| 97 break; | 97 break; |
| 98 default: | 98 default: |
| 99 NOTREACHED(); | 99 NOTREACHED(); |
| 100 } | 100 } |
| 101 value->SetString("id", credential.id); | 101 value->SetString("id", credential.id); |
| 102 value->SetString("name", credential.name); | 102 value->SetString("name", credential.name); |
| 103 value->SetString("avatarURL", credential.avatar_url.spec()); | 103 value->SetString("avatarURL", credential.avatar_url.spec()); |
| 104 } | 104 } |
| 105 | 105 |
| 106 } // web | 106 } // web |
| OLD | NEW |