15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/webui/signin/inline_login_handler_impl.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include <string>
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/bind.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/strings/string_util.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/strings/utf_string_conversions.h"
135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/values.h"
145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
15010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "chrome/browser/signin/about_signin_internals_factory.h"
16116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "chrome/browser/signin/chrome_signin_client_factory.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chrome/browser/signin/local_auth.h"
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
19cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/sync/profile_sync_service.h"
215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/sync/profile_sync_service_factory.h"
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/browser_finder.h"
23f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#include "chrome/browser/ui/browser_window.h"
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/sync/one_click_signin_helper.h"
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "chrome/browser/ui/sync/one_click_signin_histogram.h"
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/tabs/tab_strip_model.h"
276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "chrome/browser/ui/webui/signin/inline_login_ui.h"
2803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/browser/ui/webui/signin/login_ui_service.h"
2903b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)#include "chrome/browser/ui/webui/signin/login_ui_service_factory.h"
305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/common/url_constants.h"
31010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)#include "components/signin/core/browser/about_signin_internals.h"
32effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h"
33effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/signin_error_controller.h"
34c5cede9ae108bb15f6b7a8aea21c7e1fefa2834cBen Murdoch#include "components/signin/core/browser/signin_oauth_helper.h"
355f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/signin/core/common/profile_management_switches.h"
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/storage_partition.h"
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/web_ui.h"
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "google_apis/gaia/gaia_auth_fetcher.h"
39effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "google_apis/gaia/gaia_auth_util.h"
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "google_apis/gaia/gaia_constants.h"
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "google_apis/gaia/gaia_urls.h"
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "net/base/url_util.h"
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace {
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
465f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)class InlineSigninHelper : public SigninOAuthHelper::Consumer {
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  InlineSigninHelper(
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::WeakPtr<InlineLoginHandlerImpl> handler,
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::URLRequestContextGetter* getter,
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      Profile* profile,
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const GURL& current_url,
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& email,
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& password,
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& session_index,
56116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      const std::string& signin_scoped_device_id,
576e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      bool choose_what_to_sync,
586e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      bool confirm_untrusted_signin);
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) private:
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Overriden from SigninOAuthHelper::Consumer.
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnSigninOAuthInformationAvailable(
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& email,
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& display_email,
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& refresh_token) OVERRIDE;
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnSigninOAuthInformationFailure(
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const GoogleServiceAuthError& error) OVERRIDE;
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SigninOAuthHelper signin_oauth_helper_;
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::WeakPtr<InlineLoginHandlerImpl> handler_;
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Profile* profile_;
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL current_url_;
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string email_;
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string password_;
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string session_index_;
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool choose_what_to_sync_;
776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool confirm_untrusted_signin_;
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(InlineSigninHelper);
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)InlineSigninHelper::InlineSigninHelper(
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    base::WeakPtr<InlineLoginHandlerImpl> handler,
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    net::URLRequestContextGetter* getter,
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    Profile* profile,
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const GURL& current_url,
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& email,
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& password,
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& session_index,
90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch    const std::string& signin_scoped_device_id,
916e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool choose_what_to_sync,
926e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool confirm_untrusted_signin)
935f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    : signin_oauth_helper_(getter, session_index, signin_scoped_device_id,
945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                           this),
95a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      handler_(handler),
96a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      profile_(profile),
97a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      current_url_(current_url),
98a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      email_(email),
99a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      password_(password),
1006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      session_index_(session_index),
1016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      choose_what_to_sync_(choose_what_to_sync),
1026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      confirm_untrusted_signin_(confirm_untrusted_signin) {
103a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(profile_);
104a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DCHECK(!email_.empty());
105a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
106a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
107a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void InlineSigninHelper::OnSigninOAuthInformationAvailable(
108a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& email,
109a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& display_email,
110a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    const std::string& refresh_token) {
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  content::WebContents* contents = NULL;
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Browser* browser = NULL;
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (handler_) {
114a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    contents = handler_->web_ui()->GetWebContents();
115a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    browser = handler_->GetDesktopBrowser();
116a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
118010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  AboutSigninInternals* about_signin_internals =
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      AboutSigninInternalsFactory::GetForProfile(profile_);
120010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  about_signin_internals->OnRefreshTokenReceived("Successful");
121010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin::Source source = signin::GetSourceForPromoURL(current_url_);
1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::string primary_email =
1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      SigninManagerFactory::GetForProfile(profile_)->GetAuthenticatedUsername();
1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (gaia::AreEmailsSame(email, primary_email) &&
1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      source == signin::SOURCE_REAUTH &&
1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      switches::IsNewProfileManagement()) {
1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    chrome::SetLocalAuthCredentials(profile_, password_);
1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
132cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  if (source == signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT ||
133cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      source == signin::SOURCE_REAUTH) {
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        UpdateCredentials(email, refresh_token);
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (signin::IsAutoCloseEnabledInURL(current_url_)) {
138a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // Close the gaia sign in tab via a task to make sure we aren't in the
139a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // middle of any webui handler code.
140a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      base::MessageLoop::current()->PostTask(
141a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          FROM_HERE,
142a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::Bind(&InlineLoginHandlerImpl::CloseTab,
143f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          handler_,
144f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)          signin::ShouldShowAccountManagement(current_url_)));
145a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
146a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else {
147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    ProfileSyncService* sync_service =
148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        ProfileSyncServiceFactory::GetForProfile(profile_);
149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    SigninErrorController* error_controller =
150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)        ProfileOAuth2TokenServiceFactory::GetForProfile(profile_)->
151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            signin_error_controller();
1525f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    bool is_new_avatar_menu = switches::IsNewAvatarMenu();
1545f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1555f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    OneClickSigninSyncStarter::StartSyncMode start_mode;
1565f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    if (source == signin::SOURCE_SETTINGS || choose_what_to_sync_) {
1575f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      bool show_settings_without_configure =
1585f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          error_controller->HasError() &&
1595f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          sync_service &&
1605f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          sync_service->HasSyncSetupCompleted();
1615f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      start_mode = show_settings_without_configure ?
1625f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          OneClickSigninSyncStarter::SHOW_SETTINGS_WITHOUT_CONFIGURE :
1635f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST;
1645f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    } else {
1656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      start_mode = is_new_avatar_menu ?
1665f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          OneClickSigninSyncStarter::CONFIRM_SYNC_SETTINGS_FIRST :
1675f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)          OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS;
1685f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)    }
1695f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)
1706e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    OneClickSigninSyncStarter::ConfirmationRequired confirmation_required;
1716e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (confirm_untrusted_signin_) {
1726e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      confirmation_required =
1736e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          OneClickSigninSyncStarter::CONFIRM_UNTRUSTED_SIGNIN;
1746e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    } else if (is_new_avatar_menu) {
1756e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      confirmation_required = OneClickSigninSyncStarter::CONFIRM_AFTER_SIGNIN;
1766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    } else {
1776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      confirmation_required =
1786e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          source == signin::SOURCE_SETTINGS ||
1796e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)          choose_what_to_sync_ ?
1806e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)              OneClickSigninSyncStarter::NO_CONFIRMATION :
1816e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)              OneClickSigninSyncStarter::CONFIRM_AFTER_SIGNIN;
1826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    }
1836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
184cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    bool start_signin =
185cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        !OneClickSigninHelper::HandleCrossAccountError(
1865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)            profile_, "",
187a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            email, password_, refresh_token,
188a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT,
189a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            source, start_mode,
190a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)            base::Bind(&InlineLoginHandlerImpl::SyncStarterCallback,
191a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                       handler_));
192a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    if (start_signin) {
193a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // Call OneClickSigninSyncStarter to exchange oauth code for tokens.
194a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      // OneClickSigninSyncStarter will delete itself once the job is done.
195a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      new OneClickSigninSyncStarter(
196a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          profile_, browser,
197a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          email, password_, refresh_token,
198a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          start_mode,
199a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          contents,
200a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          confirmation_required,
201010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)          signin::GetNextPageURLForPromoURL(current_url_),
202a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          base::Bind(&InlineLoginHandlerImpl::SyncStarterCallback, handler_));
203a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    }
204a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
205a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
206a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
207a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
208a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
209a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)void InlineSigninHelper::OnSigninOAuthInformationFailure(
210a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const GoogleServiceAuthError& error) {
211a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (handler_)
212a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    handler_->HandleLoginError(error.ToString());
213a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
214010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  AboutSigninInternals* about_signin_internals =
215010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    AboutSigninInternalsFactory::GetForProfile(profile_);
216010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  about_signin_internals->OnRefreshTokenReceived("Failure");
217010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
218a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
219a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
220a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
221a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace
2225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)InlineLoginHandlerImpl::InlineLoginHandlerImpl()
2241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      : confirm_untrusted_signin_(false),
2251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        weak_factory_(this) {
2265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
2275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)InlineLoginHandlerImpl::~InlineLoginHandlerImpl() {}
2295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
230f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)bool InlineLoginHandlerImpl::HandleContextMenu(
231f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    const content::ContextMenuParams& params) {
232f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#ifndef NDEBUG
233f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return false;
234f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#else
235f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  return true;
236f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)#endif
237f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)}
238f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2396e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)void InlineLoginHandlerImpl::DidCommitProvisionalLoadForFrame(
2406e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    content::RenderFrameHost* render_frame_host,
2416e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    const GURL& url,
2421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    ui::PageTransition transition_type) {
2436e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (!web_contents())
2446e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
2456e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2466e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Returns early if this is not a gaia iframe navigation.
2476e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  const GURL kGaiaExtOrigin(
2486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      "chrome-extension://mfffpogegjflfpflabcdkioaeobkgjik/");
2496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  content::RenderFrameHost* gaia_iframe = InlineLoginUI::GetAuthIframe(
2506e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      web_contents(), kGaiaExtOrigin, "signin-frame");
2516e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  if (render_frame_host != gaia_iframe)
2526e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    return;
2536e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2546e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // Loading any untrusted (e.g., HTTP) URLs in the privileged sign-in process
2556e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  // will require confirmation before the sign in takes effect.
2561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (!url.is_empty()) {
2571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    GURL origin(url.GetOrigin());
2581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    if (url.spec() != url::kAboutBlankURL &&
2591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        origin != kGaiaExtOrigin &&
2601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        !gaia::IsGaiaSignonRealm(origin)) {
2611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      confirm_untrusted_signin_ = true;
2621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    }
2636e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  }
2646e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)}
2656e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
2665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void InlineLoginHandlerImpl::SetExtraInitParams(base::DictionaryValue& params) {
2675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  params.SetString("service", "chromiumsync");
2685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
269f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  content::WebContents* contents = web_ui()->GetWebContents();
270f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  const GURL& current_url = contents->GetURL();
271f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  std::string is_constrained;
272f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  net::GetValueForKeyInQuery(current_url, "constrained", &is_constrained);
273f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  if (is_constrained == "1")
274f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    contents->SetDelegate(this);
275f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
2766e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  content::WebContentsObserver::Observe(contents);
2776e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
278f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  signin::Source source = signin::GetSourceForPromoURL(current_url);
279a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  OneClickSigninHelper::LogHistogramValue(
280a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      source, one_click_signin::HISTOGRAM_SHOWN);
281a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
2825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void InlineLoginHandlerImpl::CompleteLogin(const base::ListValue* args) {
2845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::WebContents* contents = web_ui()->GetWebContents();
2855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const GURL& current_url = contents->GetURL();
2865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  const base::DictionaryValue* dict = NULL;
288a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  args->GetDictionary(0, &dict);
2895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
290a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool skip_for_now = false;
291a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  dict->GetBoolean("skipForNow", &skip_for_now);
292a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (skip_for_now) {
2935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    signin::SetUserSkippedPromo(Profile::FromWebUI(web_ui()));
2945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE);
2955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
2965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
2975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2986e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::string16 email_string16;
2996e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  dict->GetString("email", &email_string16);
3006e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DCHECK(!email_string16.empty());
3016e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::string email(base::UTF16ToASCII(email_string16));
3026e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
3036e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::string16 password_string16;
3046e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  dict->GetString("password", &password_string16);
3056e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::string password(base::UTF16ToASCII(password_string16));
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // When doing a SAML sign in, this email check may result in a false
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // positive.  This happens when the user types one email address in the
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // gaia sign in page, but signs in to a different account in the SAML sign in
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // page.
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string default_email;
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string validate_email;
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (net::GetValueForKeyInQuery(current_url, "email", &default_email) &&
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      net::GetValueForKeyInQuery(current_url, "validateEmail",
3155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                                 &validate_email) &&
3165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      validate_email == "1") {
3176e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)    if (!gaia::AreEmailsSame(email, default_email)) {
3185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE);
3195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      return;
3205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3236e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  base::string16 session_index_string16;
3246e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  dict->GetString("sessionIndex", &session_index_string16);
3256e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  std::string session_index = base::UTF16ToASCII(session_index_string16);
3266e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  DCHECK(!session_index.empty());
3276e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)
3286e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  bool choose_what_to_sync = false;
3296e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)  dict->GetBoolean("chooseWhatToSync", &choose_what_to_sync);
3305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  signin::Source source = signin::GetSourceForPromoURL(current_url);
332a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  OneClickSigninHelper::LogHistogramValue(
333a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      source, one_click_signin::HISTOGRAM_ACCEPTED);
334a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool switch_to_advanced =
3356e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      choose_what_to_sync && (source != signin::SOURCE_SETTINGS);
336a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  OneClickSigninHelper::LogHistogramValue(
337a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      source,
338a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      switch_to_advanced ? one_click_signin::HISTOGRAM_WITH_ADVANCED :
339a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                           one_click_signin::HISTOGRAM_WITH_DEFAULTS);
340a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
341a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  OneClickSigninHelper::CanOfferFor can_offer_for =
3425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      OneClickSigninHelper::CAN_OFFER_FOR_ALL;
343cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  switch (source) {
344cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT:
345cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      can_offer_for = OneClickSigninHelper::CAN_OFFER_FOR_SECONDARY_ACCOUNT;
346cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      break;
347cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    case signin::SOURCE_REAUTH: {
348cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      std::string primary_username =
349cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          SigninManagerFactory::GetForProfile(
350cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)              Profile::FromWebUI(web_ui()))->GetAuthenticatedUsername();
351cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      if (!gaia::AreEmailsSame(default_email, primary_username))
352cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)        can_offer_for = OneClickSigninHelper::CAN_OFFER_FOR_SECONDARY_ACCOUNT;
353cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      break;
354cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    }
355cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    default:
356cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      // No need to change |can_offer_for|.
357cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      break;
358cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  }
359cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
3605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  std::string error_msg;
361a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  bool can_offer = OneClickSigninHelper::CanOffer(
3626e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)      contents, can_offer_for, email, &error_msg);
363a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (!can_offer) {
3645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    HandleLoginError(error_msg);
3655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    return;
3665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
3675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
368010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  AboutSigninInternals* about_signin_internals =
369010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      AboutSigninInternalsFactory::GetForProfile(Profile::FromWebUI(web_ui()));
370010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)  about_signin_internals->OnAuthenticationResultReceived(
371010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)      "GAIA Auth Successful");
372010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)
3735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::StoragePartition* partition =
3745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      content::BrowserContext::GetStoragePartitionForSite(
3755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          contents->GetBrowserContext(),
3765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)          GURL(chrome::kChromeUIChromeSigninURL));
3775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
378116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  SigninClient* signin_client =
379116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      ChromeSigninClientFactory::GetForProfile(Profile::FromWebUI(web_ui()));
380116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  std::string signin_scoped_device_id =
381116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch      signin_client->GetSigninScopedDeviceId();
382a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // InlineSigninHelper will delete itself.
383a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  new InlineSigninHelper(GetWeakPtr(), partition->GetURLRequestContext(),
384a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                         Profile::FromWebUI(web_ui()), current_url,
3856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                         email, password, session_index,
3866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                         signin_scoped_device_id, choose_what_to_sync,
3876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)                         confirm_untrusted_signin_);
3885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  web_ui()->CallJavascriptFunction("inline.login.closeDialog");
3905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
3915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void InlineLoginHandlerImpl::HandleLoginError(const std::string& error_msg) {
3935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  SyncStarterCallback(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE);
3945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
395a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Browser* browser = GetDesktopBrowser();
396a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (browser && !error_msg.empty()) {
39703b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)    LoginUIServiceFactory::GetForProfile(Profile::FromWebUI(web_ui()))->
39803b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles)        DisplayLoginResult(browser, base::UTF8ToUTF16(error_msg));
399a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
400a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
401a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
402a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)Browser* InlineLoginHandlerImpl::GetDesktopBrowser() {
4035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(
4045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      web_ui()->GetWebContents());
4055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (!browser) {
4065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    browser = chrome::FindLastActiveWithProfile(
4075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        Profile::FromWebUI(web_ui()), chrome::GetActiveDesktop());
4085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
409a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  return browser;
4105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
4125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void InlineLoginHandlerImpl::SyncStarterCallback(
4135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    OneClickSigninSyncStarter::SyncSetupResult result) {
4145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::WebContents* contents = web_ui()->GetWebContents();
415a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
416a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (contents->GetController().GetPendingEntry()) {
417a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // Do nothing if a navigation is pending, since this call can be triggered
418a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // from DidStartLoading. This avoids deleting the pending entry while we are
419a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    // still navigating to it. See crbug/346632.
420a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return;
421a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
422a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
423a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  const GURL& current_url = contents->GetLastCommittedURL();
424a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  signin::Source source = signin::GetSourceForPromoURL(current_url);
4255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  bool auto_close = signin::IsAutoCloseEnabledInURL(current_url);
426a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
427a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  if (result == OneClickSigninSyncStarter::SYNC_SETUP_FAILURE) {
428a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    OneClickSigninHelper::RedirectToNtpOrAppsPage(contents, source);
429a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  } else if (auto_close) {
4305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    base::MessageLoop::current()->PostTask(
4315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        FROM_HERE,
4325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        base::Bind(&InlineLoginHandlerImpl::CloseTab,
433f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   weak_factory_.GetWeakPtr(),
434f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                   signin::ShouldShowAccountManagement(current_url)));
4355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  } else {
4365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)     OneClickSigninHelper::RedirectToNtpOrAppsPageIfNecessary(contents, source);
4375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
4395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
440f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)void InlineLoginHandlerImpl::CloseTab(bool show_account_management) {
4415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::WebContents* tab = web_ui()->GetWebContents();
4425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Browser* browser = chrome::FindBrowserWithWebContents(tab);
4435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  if (browser) {
4445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    TabStripModel* tab_strip_model = browser->tab_strip_model();
4455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    if (tab_strip_model) {
4465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int index = tab_strip_model->GetIndexOfWebContents(tab);
4475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      if (index != TabStripModel::kNoTab) {
4485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        tab_strip_model->ExecuteContextMenuCommand(
4495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)            index, TabStripModel::CommandCloseTab);
4505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      }
4515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    }
452f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
453f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    if (show_account_management) {
454f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      browser->window()->ShowAvatarBubbleFromAvatarButton(
455f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)            BrowserWindow::AVATAR_BUBBLE_MODE_ACCOUNT_MANAGEMENT,
4566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)            signin::ManageAccountsParams());
457f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)    }
4585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
4595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
460