Chromium Code Reviews| Index: chrome/browser/ui/auto_login_prompter.cc |
| diff --git a/chrome/browser/ui/auto_login_prompter.cc b/chrome/browser/ui/auto_login_prompter.cc |
| index 267154b3e4a8ed9a1bfd5917cad3aa620894dd52..16f44815ffc3d12c0bd8cb1849d527ed2c07facc 100644 |
| --- a/chrome/browser/ui/auto_login_prompter.cc |
| +++ b/chrome/browser/ui/auto_login_prompter.cc |
| @@ -39,13 +39,38 @@ using content::BrowserThread; |
| using content::NavigationController; |
| using content::WebContents; |
| +namespace { |
| + |
| +bool FetchUsername(Profile* profile, std::string* output) { |
| + // In an incognito window, there may not be a profile sync service and/or |
| + // signin manager. |
| + if (!ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( |
| + profile)) { |
| + return false; |
| + } |
| + |
| + if (!TokenServiceFactory::GetForProfile(profile)->AreCredentialsValid()) |
| + return false; |
| + |
| + SigninManager* signin_manager = |
| + SigninManagerFactory::GetInstance()->GetForProfile(profile); |
| + if (!signin_manager) |
| + return false; |
| + |
| + *output = signin_manager->GetAuthenticatedUsername(); |
| + return true; |
| +} |
| + |
| +} // namespace |
| + |
| +AutoLoginPrompter::Params::Params() {} |
| +AutoLoginPrompter::Params::~Params() {} |
| + |
| AutoLoginPrompter::AutoLoginPrompter( |
| WebContents* web_contents, |
| - const std::string& username, |
| - const std::string& args) |
| + const Params& params) |
| : web_contents_(web_contents), |
| - username_(username), |
| - args_(args) { |
| + params_(params) { |
| registrar_.Add(this, content::NOTIFICATION_LOAD_STOP, |
| content::Source<NavigationController>( |
| &web_contents_->GetController())); |
| @@ -68,48 +93,22 @@ void AutoLoginPrompter::ShowInfoBarIfPossible(net::URLRequest* request, |
| // suggest auto-login, if available. |
| std::string value; |
| request->GetResponseHeaderByName("X-Auto-Login", &value); |
| - if (value.empty()) |
| - return; |
| - |
| - std::vector<std::pair<std::string, std::string> > pairs; |
| - if (!base::SplitStringIntoKeyValuePairs(value, '=', '&', &pairs)) |
| - return; |
| - |
| - // Parse the information from the value string. |
| - std::string realm; |
| - std::string account; |
| - std::string args; |
| - for (size_t i = 0; i < pairs.size(); ++i) { |
| - const std::pair<std::string, std::string>& pair = pairs[i]; |
| - if (pair.first == "realm") { |
| - realm = net::UnescapeURLComponent(pair.second, |
| - net::UnescapeRule::URL_SPECIAL_CHARS); |
| - } else if (pair.first == "account") { |
| - account = net::UnescapeURLComponent(pair.second, |
| - net::UnescapeRule::URL_SPECIAL_CHARS); |
| - } else if (pair.first == "args") { |
| - args = pair.second; |
| - } |
| - } |
| - |
| - // Currently we only accept GAIA credentials. |
| - if (realm != "com.google") |
| + Params params; |
| + if (!ParseAutoLoginHeader(value, ¶ms)) |
| return; |
| BrowserThread::PostTask( |
| BrowserThread::UI, FROM_HERE, |
| - base::Bind(&AutoLoginPrompter::ShowInfoBarUIThread, account, args, |
| - request->url(), child_id, route_id)); |
| + base::Bind(&ShowInfoBarUIThread, |
| + params, request->url(), child_id, route_id)); |
| } |
| // static |
| -void AutoLoginPrompter::ShowInfoBarUIThread(const std::string& account, |
| - const std::string& args, |
| +void AutoLoginPrompter::ShowInfoBarUIThread(const Params& params, |
| const GURL& url, |
| int child_id, |
| int route_id) { |
| DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| - |
| WebContents* web_contents = tab_util::GetWebContentsByID(child_id, route_id); |
| if (!web_contents) |
| return; |
| @@ -120,33 +119,25 @@ void AutoLoginPrompter::ShowInfoBarUIThread(const std::string& account, |
| if (!profile->GetPrefs()->GetBoolean(prefs::kAutologinEnabled)) |
| return; |
| - // In an incognito window, there may not be a profile sync service and/or |
| - // signin manager. |
| - if (!ProfileSyncServiceFactory::GetInstance()->HasProfileSyncService( |
| - profile)) { |
| + Params params_copy(params); |
| +#if !defined(OS_ANDROID) |
| + // On Android, the username is fetched later from the AccountManager on the |
| + // Java side. |
| + if (!FetchUsername(profile, ¶ms_copy.username)) |
| return; |
| - } |
| - |
| - if (!TokenServiceFactory::GetForProfile(profile)->AreCredentialsValid()) |
| - return; |
| - |
| - SigninManager* signin_manager = |
| - SigninManagerFactory::GetInstance()->GetForProfile(profile); |
| - if (!signin_manager) |
| - return; |
| - |
| - const std::string& username = signin_manager->GetAuthenticatedUsername(); |
| +#endif |
| // Make sure that |account|, if specified, matches the logged in user. |
| // However, |account| is usually empty. |
| - if (!account.empty() && (username != account)) |
| + if (!params.username.empty() && !params.account.empty() && |
| + params_copy.username != params.account) { |
| return; |
| - |
| + } |
| // We can't add the infobar just yet, since we need to wait for the tab to |
| // finish loading. If we don't, the info bar appears and then disappears |
| // immediately. Create an AutoLoginPrompter instance to listen for the |
| // relevant notifications; it will delete itself. |
| - new AutoLoginPrompter(web_contents, username, args); |
| + new AutoLoginPrompter(web_contents, params_copy); |
| } |
| void AutoLoginPrompter::Observe(int type, |
| @@ -157,9 +148,9 @@ void AutoLoginPrompter::Observe(int type, |
| // |tab_contents| is NULL for WebContents hosted in WebDialog. |
| if (tab_contents) { |
| InfoBarTabHelper* infobar_helper = tab_contents->infobar_tab_helper(); |
| - infobar_helper->AddInfoBar(new AutoLoginInfoBarDelegate(infobar_helper, |
| - username_, |
| - args_)); |
| + infobar_helper->AddInfoBar(new AutoLoginInfoBarDelegate( |
| + infobar_helper, |
| + params_.realm, params_.account, params_.args, params_.username)); |
| } |
| } |
| // Either we couldn't add the infobar, we added the infobar, or the tab |
| @@ -167,3 +158,33 @@ void AutoLoginPrompter::Observe(int type, |
| // there's no reason to live further. |
| delete this; |
| } |
| + |
| +// static |
| +bool AutoLoginPrompter::ParseAutoLoginHeader(const std::string& input, |
| + Params* output) { |
| + if (input.empty()) |
| + return false; |
| + |
| + std::vector<std::pair<std::string, std::string> > pairs; |
| + if (!base::SplitStringIntoKeyValuePairs(input, '=', '&', &pairs)) |
| + return false; |
| + |
| + // Parse the information from the |input| string. |
| + for (size_t i = 0; i < pairs.size(); ++i) { |
| + const std::pair<std::string, std::string>& pair = pairs[i]; |
| + if (pair.first == "realm") { |
| + output->realm = net::UnescapeURLComponent( |
|
Nico
2012/06/27 15:14:33
nit: functions with a bool result generally should
Philippe
2012/06/27 16:52:02
Good point.
|
| + pair.second, net::UnescapeRule::URL_SPECIAL_CHARS); |
| + // Currently we only accept GAIA credentials. |
| + if (output->realm != "com.google") |
| + return false; |
| + } else if (pair.first == "account") { |
| + output->account = net::UnescapeURLComponent( |
| + pair.second, net::UnescapeRule::URL_SPECIAL_CHARS); |
| + } else if (pair.first == "args") { |
| + output->args = net::UnescapeURLComponent( |
| + pair.second, net::UnescapeRule::URL_SPECIAL_CHARS); |
| + } |
| + } |
| + return !output->realm.empty() && !output->args.empty(); |
| +} |