Fix experimental app list search box disappearing on profile switch.
[chromium-blink-merge.git] / google_apis / gaia / gaia_auth_util.cc
blobc3cc764e8a988538b94fd3ac759b332aac91ad9c
1 // Copyright (c) 2012 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 "google_apis/gaia/gaia_auth_util.h"
7 #include "base/json/json_reader.h"
8 #include "base/logging.h"
9 #include "base/strings/string_split.h"
10 #include "base/strings/string_util.h"
11 #include "base/values.h"
12 #include "google_apis/gaia/gaia_urls.h"
13 #include "url/gurl.h"
15 namespace gaia {
17 namespace {
19 const char kGmailDomain[] = "gmail.com";
20 const char kGooglemailDomain[] = "googlemail.com";
22 std::string CanonicalizeEmailImpl(const std::string& email_address,
23 bool change_googlemail_to_gmail) {
24 std::vector<std::string> parts;
25 char at = '@';
26 base::SplitString(email_address, at, &parts);
27 if (parts.size() != 2U) {
28 NOTREACHED() << "expecting exactly one @, but got "
29 << (parts.empty() ? 0 : parts.size() - 1)
30 << " : " << email_address;
31 } else {
32 if (change_googlemail_to_gmail && parts[1] == kGooglemailDomain)
33 parts[1] = kGmailDomain;
35 if (parts[1] == kGmailDomain) // only strip '.' for gmail accounts.
36 base::RemoveChars(parts[0], ".", &parts[0]);
39 std::string new_email = base::StringToLowerASCII(JoinString(parts, at));
40 VLOG(1) << "Canonicalized " << email_address << " to " << new_email;
41 return new_email;
44 } // namespace
46 std::string CanonicalizeEmail(const std::string& email_address) {
47 // CanonicalizeEmail() is called to process email strings that are eventually
48 // shown to the user, and may also be used in persisting email strings. To
49 // avoid breaking this existing behavior, this function will not try to
50 // change googlemail to gmail.
51 return CanonicalizeEmailImpl(email_address, false);
54 std::string CanonicalizeDomain(const std::string& domain) {
55 // Canonicalization of domain names means lower-casing them. Make sure to
56 // update this function in sync with Canonicalize if this ever changes.
57 return base::StringToLowerASCII(domain);
60 std::string SanitizeEmail(const std::string& email_address) {
61 std::string sanitized(email_address);
63 // Apply a default domain if necessary.
64 if (sanitized.find('@') == std::string::npos) {
65 sanitized += '@';
66 sanitized += kGmailDomain;
69 return sanitized;
72 bool AreEmailsSame(const std::string& email1, const std::string& email2) {
73 return CanonicalizeEmailImpl(gaia::SanitizeEmail(email1), true) ==
74 CanonicalizeEmailImpl(gaia::SanitizeEmail(email2), true);
77 std::string ExtractDomainName(const std::string& email_address) {
78 // First canonicalize which will also verify we have proper domain part.
79 std::string email = CanonicalizeEmail(email_address);
80 size_t separator_pos = email.find('@');
81 if (separator_pos != email.npos && separator_pos < email.length() - 1)
82 return email.substr(separator_pos + 1);
83 else
84 NOTREACHED() << "Not a proper email address: " << email;
85 return std::string();
88 bool IsGaiaSignonRealm(const GURL& url) {
89 if (!url.SchemeIsSecure())
90 return false;
92 return url == GaiaUrls::GetInstance()->gaia_url();
96 bool ParseListAccountsData(
97 const std::string& data,
98 std::vector<std::pair<std::string, bool> >* accounts) {
99 accounts->clear();
101 // Parse returned data and make sure we have data.
102 scoped_ptr<base::Value> value(base::JSONReader::Read(data));
103 if (!value)
104 return false;
106 base::ListValue* list;
107 if (!value->GetAsList(&list) || list->GetSize() < 2)
108 return false;
110 // Get list of account info.
111 base::ListValue* account_list;
112 if (!list->GetList(1, &account_list) || accounts == NULL)
113 return false;
115 // Build a vector of accounts from the cookie. Order is important: the first
116 // account in the list is the primary account.
117 for (size_t i = 0; i < account_list->GetSize(); ++i) {
118 base::ListValue* account;
119 if (account_list->GetList(i, &account) && account != NULL) {
120 std::string email;
121 // Canonicalize the email since ListAccounts returns "display email".
122 if (account->GetString(3, &email) && !email.empty()) {
123 // New version if ListAccounts indicates whether the email's session
124 // is still valid or not. If this value is present and false, assume
125 // its invalid. Otherwise assume it's valid to remain compatible with
126 // old version.
127 int is_email_valid = 1;
128 if (!account->GetInteger(9, &is_email_valid))
129 is_email_valid = 1;
131 accounts->push_back(
132 std::make_pair(CanonicalizeEmail(email), is_email_valid != 0));
137 return true;
140 } // namespace gaia