Add ICU message format support
[chromium-blink-merge.git] / ios / web / web_state / js / credential_util.mm
blobd0a9b4a1636d237f1651917ce8f9d85720332308
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"
12 #include "url/gurl.h"
14 namespace {
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";
22 }  // namespace
24 namespace web {
26 bool DictionaryValueToCredential(const base::DictionaryValue& value,
27                                  Credential* credential) {
28   DCHECK(credential);
30   base::string16 type;
31   if (!value.GetString("type", &type))
32     return false;
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;
38   else
39     return false;
41   base::string16 id;
42   if (!value.GetString("id", &id))
43     return false;
45   base::string16 name;
46   value.GetString("name", &name);
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   }
56   base::string16 password;
57   if (credential_type == CredentialType::CREDENTIAL_TYPE_PASSWORD &&
58       !value.GetString("password", &password)) {
59     return false;
60   }
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   }
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;
81 void CredentialToDictionaryValue(const Credential& credential,
82                                  base::DictionaryValue* value) {
83   DCHECK(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.
88       value->Clear();
89       return;
90     case CredentialType::CREDENTIAL_TYPE_PASSWORD:
91       value->SetString("type", kPasswordCredentialType);
92       value->SetString("password", credential.password);
93       break;
94     case CredentialType::CREDENTIAL_TYPE_FEDERATED:
95       value->SetString("type", kFederatedCredentialType);
96       value->SetString("federation", credential.federation_url.spec());
97       break;
98     default:
99       NOTREACHED();
100   }
101   value->SetString("id", credential.id);
102   value->SetString("name", credential.name);
103   value->SetString("avatarURL", credential.avatar_url.spec());
106 }  // web