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.
5 #include "ios/web/web_state/js/credential_util.h"
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"
16 // "type" value for a DictionaryValue representation of PasswordCredential.
17 const char* kPasswordCredentialType = "PasswordCredential";
19 // "type" value for a DictionaryValue representation of FederatedCredential.
20 const char* kFederatedCredentialType = "FederatedCredential";
26 bool DictionaryValueToCredential(const base::DictionaryValue& value,
27 Credential* credential) {
31 if (!value.GetString("type", &type))
33 CredentialType credential_type;
34 if (type == base::ASCIIToUTF16(kPasswordCredentialType))
35 credential_type = CredentialType::CREDENTIAL_TYPE_PASSWORD;
36 else if (type == base::ASCIIToUTF16(kFederatedCredentialType))
37 credential_type = CredentialType::CREDENTIAL_TYPE_FEDERATED;
42 if (!value.GetString("id", &id))
46 value.GetString("name", &name);
48 base::string16 avatar;
50 if (value.GetString("avatarURL", &avatar)) {
51 avatar_url = GURL(avatar);
52 if (!avatar_url.is_valid())
56 base::string16 password;
57 if (credential_type == CredentialType::CREDENTIAL_TYPE_PASSWORD &&
58 !value.GetString("password", &password)) {
62 base::string16 federation;
64 if (credential_type == CredentialType::CREDENTIAL_TYPE_FEDERATED) {
65 if (!value.GetString("federation", &federation))
67 federation_url = GURL(federation);
68 if (!federation_url.is_valid())
72 credential->type = credential_type;
74 credential->name = name;
75 credential->avatar_url = avatar_url;
76 credential->password = password;
77 credential->federation_url = federation_url;
81 void CredentialToDictionaryValue(const Credential& credential,
82 base::DictionaryValue* value) {
84 switch (credential.type) {
85 case CredentialType::CREDENTIAL_TYPE_EMPTY:
86 // Return an empty dictionary. This will cause "null" to be returned to
87 // the JavaScript Promise resolver.
90 case CredentialType::CREDENTIAL_TYPE_PASSWORD:
91 value->SetString("type", kPasswordCredentialType);
92 value->SetString("password", credential.password);
94 case CredentialType::CREDENTIAL_TYPE_FEDERATED:
95 value->SetString("type", kFederatedCredentialType);
96 value->SetString("federation", credential.federation_url.spec());
101 value->SetString("id", credential.id);
102 value->SetString("name", credential.name);
103 value->SetString("avatarURL", credential.avatar_url.spec());