This source file includes following definitions.
- AskForConfirmation
- web_contents_
- GetTitle
- GetDialogMessage
- GetAcceptButtonTitle
- GetCancelButtonTitle
- GetLinkText
- OnAccepted
- OnCanceled
- OnClosed
- OnLinkClicked
- AddEmailToOneClickRejectedList
- LogOneClickHistogramValue
- RedirectToNtpOrAppsPageWithIds
- StartSync
- StartExplicitSync
- ClearPendingEmailOnIOThread
- GetSigninSource
- IsValidGaiaSigninRedirectOrResponseURL
- AreWeShowingSignin
- DidCommitProvisionalLoadForFrame
- WebContentsDestroyed
- CloseTab
- source
- callback
- weak_pointer_factory_
- OnSigninOAuthInformationAvailable
- OnSigninOAuthInformationFailure
- OnBrowserRemoved
- VerifyGaiaCookiesBeforeSignIn
- OnGaiaCookiesFetched
- DisplayErrorBubble
- StartSigninOAuthHelper
- StartOneClickSigninSyncStarter
- weak_pointer_factory_
- LogHistogramValue
- CreateForWebContentsWithPasswordManager
- CanOffer
- CanOfferOnIOThread
- CanOfferOnIOThreadImpl
- ShowInfoBarIfPossible
- LogConfirmHistogramValue
- ShowInfoBarUIThread
- RemoveSigninRedirectURLHistoryItem
- ShowSigninErrorBubble
- HandleCrossAccountError
- RedirectToNtpOrAppsPage
- RedirectToNtpOrAppsPageIfNecessary
- RedirectToSignin
- CleanTransientState
- PasswordSubmitted
- SetDoNotClearPendingEmailForTesting
- set_do_not_start_sync_for_testing
- DidStartNavigationToPendingEntry
- DidNavigateMainFrame
- DidStopLoading
- WebContentsDestroyed
- OnStateChanged
- CreateSyncStarterCallback
- SyncSetupCompletedCallback
#include "chrome/browser/ui/sync/one_click_signin_helper.h"
#include <algorithm>
#include <functional>
#include <utility>
#include <vector>
#include "base/bind.h"
#include "base/callback_forward.h"
#include "base/callback_helpers.h"
#include "base/compiler_specific.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/prefs/pref_service.h"
#include "base/prefs/scoped_user_pref_update.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/supports_user_data.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/defaults.h"
#include "chrome/browser/google/google_util.h"
#include "chrome/browser/history/history_service.h"
#include "chrome/browser/history/history_service_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_info_cache.h"
#include "chrome/browser/profiles/profile_io_data.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/search/search.h"
#include "chrome/browser/signin/chrome_signin_client.h"
#include "chrome/browser/signin/chrome_signin_client_factory.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/signin/signin_manager_factory.h"
#include "chrome/browser/signin/signin_names_io_thread.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_tabstrip.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/chrome_pages.h"
#include "chrome/browser/ui/sync/one_click_signin_histogram.h"
#include "chrome/browser/ui/sync/one_click_signin_sync_starter.h"
#include "chrome/browser/ui/sync/signin_histogram.h"
#include "chrome/browser/ui/tab_modal_confirm_dialog.h"
#include "chrome/browser/ui/tab_modal_confirm_dialog_delegate.h"
#include "chrome/browser/ui/tabs/tab_strip_model.h"
#include "chrome/common/chrome_version_info.h"
#include "chrome/common/net/url_util.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/profile_management_switches.h"
#include "chrome/common/url_constants.h"
#include "components/autofill/core/common/password_form.h"
#include "components/password_manager/core/browser/password_manager.h"
#include "components/signin/core/browser/profile_oauth2_token_service.h"
#include "components/signin/core/browser/signin_client.h"
#include "components/signin/core/browser/signin_error_controller.h"
#include "components/signin/core/browser/signin_manager.h"
#include "components/signin/core/browser/signin_manager_cookie_helper.h"
#include "components/sync_driver/sync_prefs.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/page_navigator.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/browser/web_contents_view.h"
#include "content/public/common/frame_navigate_params.h"
#include "content/public/common/page_transition_types.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "google_apis/gaia/gaia_urls.h"
#include "grit/chromium_strings.h"
#include "grit/generated_resources.h"
#include "grit/theme_resources.h"
#include "ipc/ipc_message_macros.h"
#include "net/base/url_util.h"
#include "net/cookies/cookie_monster.h"
#include "net/url_request/url_request.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "url/gurl.h"
namespace {
class ConfirmEmailDialogDelegate : public TabModalConfirmDialogDelegate {
public:
enum Action {
CREATE_NEW_USER,
START_SYNC,
CLOSE
};
typedef base::Callback<void(Action)> Callback;
static void AskForConfirmation(content::WebContents* contents,
const std::string& last_email,
const std::string& email,
Callback callback);
private:
ConfirmEmailDialogDelegate(content::WebContents* contents,
const std::string& last_email,
const std::string& email,
Callback callback);
virtual ~ConfirmEmailDialogDelegate();
virtual base::string16 GetTitle() OVERRIDE;
virtual base::string16 GetDialogMessage() OVERRIDE;
virtual base::string16 GetAcceptButtonTitle() OVERRIDE;
virtual base::string16 GetCancelButtonTitle() OVERRIDE;
virtual base::string16 GetLinkText() const OVERRIDE;
virtual void OnAccepted() OVERRIDE;
virtual void OnCanceled() OVERRIDE;
virtual void OnClosed() OVERRIDE;
virtual void OnLinkClicked(WindowOpenDisposition disposition) OVERRIDE;
std::string last_email_;
std::string email_;
Callback callback_;
content::WebContents* web_contents_;
DISALLOW_COPY_AND_ASSIGN(ConfirmEmailDialogDelegate);
};
void ConfirmEmailDialogDelegate::AskForConfirmation(
content::WebContents* contents,
const std::string& last_email,
const std::string& email,
Callback callback) {
TabModalConfirmDialog::Create(
new ConfirmEmailDialogDelegate(contents, last_email, email,
callback), contents);
}
ConfirmEmailDialogDelegate::ConfirmEmailDialogDelegate(
content::WebContents* contents,
const std::string& last_email,
const std::string& email,
Callback callback)
: TabModalConfirmDialogDelegate(contents),
last_email_(last_email),
email_(email),
callback_(callback),
web_contents_(contents) {
}
ConfirmEmailDialogDelegate::~ConfirmEmailDialogDelegate() {
}
base::string16 ConfirmEmailDialogDelegate::GetTitle() {
return l10n_util::GetStringUTF16(
IDS_ONE_CLICK_SIGNIN_CONFIRM_EMAIL_DIALOG_TITLE);
}
base::string16 ConfirmEmailDialogDelegate::GetDialogMessage() {
return l10n_util::GetStringFUTF16(
IDS_ONE_CLICK_SIGNIN_CONFIRM_EMAIL_DIALOG_MESSAGE,
base::UTF8ToUTF16(last_email_), base::UTF8ToUTF16(email_));
}
base::string16 ConfirmEmailDialogDelegate::GetAcceptButtonTitle() {
return l10n_util::GetStringUTF16(
IDS_ONE_CLICK_SIGNIN_CONFIRM_EMAIL_DIALOG_OK_BUTTON);
}
base::string16 ConfirmEmailDialogDelegate::GetCancelButtonTitle() {
return l10n_util::GetStringUTF16(
IDS_ONE_CLICK_SIGNIN_CONFIRM_EMAIL_DIALOG_CANCEL_BUTTON);
}
base::string16 ConfirmEmailDialogDelegate::GetLinkText() const {
return l10n_util::GetStringUTF16(IDS_LEARN_MORE);
}
void ConfirmEmailDialogDelegate::OnAccepted() {
base::ResetAndReturn(&callback_).Run(CREATE_NEW_USER);
}
void ConfirmEmailDialogDelegate::OnCanceled() {
base::ResetAndReturn(&callback_).Run(START_SYNC);
}
void ConfirmEmailDialogDelegate::OnClosed() {
base::ResetAndReturn(&callback_).Run(CLOSE);
}
void ConfirmEmailDialogDelegate::OnLinkClicked(
WindowOpenDisposition disposition) {
content::OpenURLParams params(
GURL(chrome::kChromeSyncMergeTroubleshootingURL),
content::Referrer(),
NEW_POPUP,
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
false);
web_contents_->OpenURL(params);
}
void AddEmailToOneClickRejectedList(Profile* profile,
const std::string& email) {
ListPrefUpdate updater(profile->GetPrefs(),
prefs::kReverseAutologinRejectedEmailList);
updater->AppendIfNotPresent(new base::StringValue(email));
}
void LogOneClickHistogramValue(int action) {
UMA_HISTOGRAM_ENUMERATION("Signin.OneClickActions", action,
one_click_signin::HISTOGRAM_MAX);
UMA_HISTOGRAM_ENUMERATION("Signin.AllAccessPointActions", action,
one_click_signin::HISTOGRAM_MAX);
}
void RedirectToNtpOrAppsPageWithIds(int child_id,
int route_id,
signin::Source source) {
content::WebContents* web_contents = tab_util::GetWebContentsByID(child_id,
route_id);
if (!web_contents)
return;
OneClickSigninHelper::RedirectToNtpOrAppsPage(web_contents, source);
}
void StartSync(const OneClickSigninHelper::StartSyncArgs& args,
OneClickSigninSyncStarter::StartSyncMode start_mode) {
if (start_mode == OneClickSigninSyncStarter::UNDO_SYNC) {
LogOneClickHistogramValue(one_click_signin::HISTOGRAM_UNDO);
return;
}
OneClickSigninHelper::SyncStarterWrapper* wrapper =
new OneClickSigninHelper::SyncStarterWrapper(args, start_mode);
wrapper->Start();
int action = one_click_signin::HISTOGRAM_MAX;
switch (args.auto_accept) {
case OneClickSigninHelper::AUTO_ACCEPT_EXPLICIT:
break;
case OneClickSigninHelper::AUTO_ACCEPT_ACCEPTED:
action =
start_mode == OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS ?
one_click_signin::HISTOGRAM_AUTO_WITH_DEFAULTS :
one_click_signin::HISTOGRAM_AUTO_WITH_ADVANCED;
break;
case OneClickSigninHelper::AUTO_ACCEPT_CONFIGURE:
DCHECK(start_mode == OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST);
action = one_click_signin::HISTOGRAM_AUTO_WITH_ADVANCED;
break;
default:
NOTREACHED() << "Invalid auto_accept: " << args.auto_accept;
break;
}
if (action != one_click_signin::HISTOGRAM_MAX)
LogOneClickHistogramValue(action);
}
void StartExplicitSync(const OneClickSigninHelper::StartSyncArgs& args,
content::WebContents* contents,
OneClickSigninSyncStarter::StartSyncMode start_mode,
ConfirmEmailDialogDelegate::Action action) {
bool enable_inline = !switches::IsEnableWebBasedSignin();
if (action == ConfirmEmailDialogDelegate::START_SYNC) {
StartSync(args, start_mode);
if (!enable_inline) {
OneClickSigninHelper::RedirectToNtpOrAppsPageIfNecessary(
contents, args.source);
}
} else {
if (enable_inline) {
args.callback.Run(OneClickSigninSyncStarter::SYNC_SETUP_FAILURE);
} else {
if (signin::IsContinueUrlForWebBasedSigninFlow(
contents->GetVisibleURL())) {
base::MessageLoopProxy::current()->PostNonNestableTask(
FROM_HERE,
base::Bind(RedirectToNtpOrAppsPageWithIds,
contents->GetRenderProcessHost()->GetID(),
contents->GetRoutingID(),
args.source));
}
}
if (action == ConfirmEmailDialogDelegate::CREATE_NEW_USER) {
chrome::ShowSettingsSubPage(args.browser,
std::string(chrome::kCreateProfileSubPage));
}
}
}
void ClearPendingEmailOnIOThread(content::ResourceContext* context) {
ProfileIOData* io_data = ProfileIOData::FromResourceContext(context);
DCHECK(io_data);
io_data->set_reverse_autologin_pending_email(std::string());
}
signin::Source GetSigninSource(const GURL& url, GURL* continue_url) {
DCHECK(url.is_valid());
std::string value;
net::GetValueForKeyInQuery(url, "service", &value);
bool possibly_an_explicit_signin = value == "chromiumsync";
GURL local_continue_url = signin::GetNextPageURLForPromoURL(url);
while (gaia::IsGaiaSignonRealm(local_continue_url.GetOrigin())) {
GURL next_continue_url =
signin::GetNextPageURLForPromoURL(local_continue_url);
if (!next_continue_url.is_valid())
break;
local_continue_url = next_continue_url;
}
if (continue_url && local_continue_url.is_valid()) {
DCHECK(!continue_url->is_valid() || *continue_url == local_continue_url);
*continue_url = local_continue_url;
}
return possibly_an_explicit_signin ?
signin::GetSourceForPromoURL(local_continue_url) :
signin::SOURCE_UNKNOWN;
}
bool IsValidGaiaSigninRedirectOrResponseURL(const GURL& url) {
std::string hostname = url.host();
if (google_util::IsGoogleHostname(hostname, google_util::ALLOW_SUBDOMAIN)) {
return gaia::IsGaiaSignonRealm(url.GetOrigin()) ||
StartsWithASCII(hostname, "accounts.", false);
}
GURL origin = url.GetOrigin();
if (origin == GURL("https://accounts.youtube.com") ||
origin == GURL("https://accounts.blogger.com"))
return true;
return false;
}
bool AreWeShowingSignin(GURL url, signin::Source source, std::string email) {
GURL::Replacements replacements;
replacements.ClearQuery();
GURL clean_login_url =
GaiaUrls::GetInstance()->service_login_url().ReplaceComponents(
replacements);
return (url.ReplaceComponents(replacements) == clean_login_url &&
source != signin::SOURCE_UNKNOWN) ||
(IsValidGaiaSigninRedirectOrResponseURL(url) &&
url.spec().find("ChromeLoginPrompt") != std::string::npos &&
!email.empty());
}
class CurrentHistoryCleaner : public content::WebContentsObserver {
public:
explicit CurrentHistoryCleaner(content::WebContents* contents);
virtual ~CurrentHistoryCleaner();
virtual void WebContentsDestroyed(content::WebContents* contents) OVERRIDE;
virtual void DidCommitProvisionalLoadForFrame(
int64 frame_id,
const base::string16& frame_unique_name,
bool is_main_frame,
const GURL& url,
content::PageTransition transition_type,
content::RenderViewHost* render_view_host) OVERRIDE;
private:
scoped_ptr<content::WebContents> contents_;
int history_index_to_remove_;
DISALLOW_COPY_AND_ASSIGN(CurrentHistoryCleaner);
};
CurrentHistoryCleaner::CurrentHistoryCleaner(content::WebContents* contents)
: WebContentsObserver(contents) {
history_index_to_remove_ =
web_contents()->GetController().GetLastCommittedEntryIndex();
}
CurrentHistoryCleaner::~CurrentHistoryCleaner() {
}
void CurrentHistoryCleaner::DidCommitProvisionalLoadForFrame(
int64 frame_id,
const base::string16& frame_unique_name,
bool is_main_frame,
const GURL& url,
content::PageTransition transition_type,
content::RenderViewHost* render_view_host) {
if (!is_main_frame)
return;
content::NavigationController* nc = &web_contents()->GetController();
HistoryService* hs = HistoryServiceFactory::GetForProfile(
Profile::FromBrowserContext(web_contents()->GetBrowserContext()),
Profile::IMPLICIT_ACCESS);
if (history_index_to_remove_ < nc->GetLastCommittedEntryIndex()) {
content::NavigationEntry* entry =
nc->GetEntryAtIndex(history_index_to_remove_);
if (signin::IsContinueUrlForWebBasedSigninFlow(entry->GetURL())) {
hs->DeleteURL(entry->GetURL());
nc->RemoveEntryAtIndex(history_index_to_remove_);
delete this;
}
}
}
void CurrentHistoryCleaner::WebContentsDestroyed(
content::WebContents* contents) {
delete this;
}
void CloseTab(content::WebContents* tab) {
content::WebContentsDelegate* tab_delegate = tab->GetDelegate();
if (tab_delegate)
tab_delegate->CloseContents(tab);
}
}
OneClickSigninHelper::StartSyncArgs::StartSyncArgs()
: profile(NULL),
browser(NULL),
auto_accept(AUTO_ACCEPT_NONE),
web_contents(NULL),
confirmation_required(OneClickSigninSyncStarter::NO_CONFIRMATION),
source(signin::SOURCE_UNKNOWN) {}
OneClickSigninHelper::StartSyncArgs::StartSyncArgs(
Profile* profile,
Browser* browser,
OneClickSigninHelper::AutoAccept auto_accept,
const std::string& session_index,
const std::string& email,
const std::string& password,
const std::string& refresh_token,
content::WebContents* web_contents,
bool untrusted_confirmation_required,
signin::Source source,
OneClickSigninSyncStarter::Callback callback)
: profile(profile),
browser(browser),
auto_accept(auto_accept),
session_index(session_index),
email(email),
password(password),
refresh_token(refresh_token),
web_contents(web_contents),
source(source),
callback(callback) {
DCHECK(session_index.empty() != refresh_token.empty());
if (untrusted_confirmation_required) {
confirmation_required = OneClickSigninSyncStarter::CONFIRM_UNTRUSTED_SIGNIN;
} else if (source == signin::SOURCE_SETTINGS ||
source == signin::SOURCE_WEBSTORE_INSTALL) {
confirmation_required = OneClickSigninSyncStarter::NO_CONFIRMATION;
} else {
confirmation_required = OneClickSigninSyncStarter::CONFIRM_AFTER_SIGNIN;
}
}
OneClickSigninHelper::StartSyncArgs::~StartSyncArgs() {}
OneClickSigninHelper::SyncStarterWrapper::SyncStarterWrapper(
const OneClickSigninHelper::StartSyncArgs& args,
OneClickSigninSyncStarter::StartSyncMode start_mode)
: args_(args), start_mode_(start_mode), weak_pointer_factory_(this) {
BrowserList::AddObserver(this);
desktop_type_ = args_.browser ? args_.browser->host_desktop_type()
: chrome::GetActiveDesktop();
}
OneClickSigninHelper::SyncStarterWrapper::~SyncStarterWrapper() {
BrowserList::RemoveObserver(this);
}
void OneClickSigninHelper::SyncStarterWrapper::Start() {
if (args_.refresh_token.empty()) {
if (args_.password.empty()) {
VerifyGaiaCookiesBeforeSignIn();
} else {
StartSigninOAuthHelper();
}
} else {
OnSigninOAuthInformationAvailable(args_.email, args_.email,
args_.refresh_token);
}
}
void
OneClickSigninHelper::SyncStarterWrapper::OnSigninOAuthInformationAvailable(
const std::string& email,
const std::string& display_email,
const std::string& refresh_token) {
if (!gaia::AreEmailsSame(display_email, args_.email)) {
DisplayErrorBubble(
GoogleServiceAuthError(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS).ToString());
} else {
StartOneClickSigninSyncStarter(email, refresh_token);
}
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
void OneClickSigninHelper::SyncStarterWrapper::OnSigninOAuthInformationFailure(
const GoogleServiceAuthError& error) {
DisplayErrorBubble(error.ToString());
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
void OneClickSigninHelper::SyncStarterWrapper::OnBrowserRemoved(
Browser* browser) {
if (args_.browser == browser)
args_.browser = NULL;
}
void OneClickSigninHelper::SyncStarterWrapper::VerifyGaiaCookiesBeforeSignIn() {
scoped_refptr<SigninManagerCookieHelper> cookie_helper(
new SigninManagerCookieHelper(
args_.profile->GetRequestContext(),
content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::UI),
content::BrowserThread::GetMessageLoopProxyForThread(
content::BrowserThread::IO)));
cookie_helper->StartFetchingGaiaCookiesOnUIThread(
base::Bind(&SyncStarterWrapper::OnGaiaCookiesFetched,
weak_pointer_factory_.GetWeakPtr(),
args_.session_index));
}
void OneClickSigninHelper::SyncStarterWrapper::OnGaiaCookiesFetched(
const std::string session_index, const net::CookieList& cookie_list) {
net::CookieList::const_iterator it;
bool success = false;
for (it = cookie_list.begin(); it != cookie_list.end(); ++it) {
if (it->Name() == "LSID") {
if (it->IsHostCookie() && it->IsHttpOnly() && it->IsSecure()) {
success = true;
} else {
success = false;
break;
}
}
}
if (success) {
StartSigninOAuthHelper();
} else {
DisplayErrorBubble(
GoogleServiceAuthError(
GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS).ToString());
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
}
void OneClickSigninHelper::SyncStarterWrapper::DisplayErrorBubble(
const std::string& error_message) {
args_.browser = OneClickSigninSyncStarter::EnsureBrowser(
args_.browser, args_.profile, desktop_type_);
args_.browser->window()->ShowOneClickSigninBubble(
BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE,
base::string16(),
base::UTF8ToUTF16(error_message),
BrowserWindow::StartSyncCallback());
}
void OneClickSigninHelper::SyncStarterWrapper::StartSigninOAuthHelper() {
signin_oauth_helper_.reset(
new SigninOAuthHelper(args_.profile->GetRequestContext(),
args_.session_index, this));
}
void
OneClickSigninHelper::SyncStarterWrapper::StartOneClickSigninSyncStarter(
const std::string& email,
const std::string& refresh_token) {
new OneClickSigninSyncStarter(args_.profile, args_.browser,
email, args_.password,
refresh_token, start_mode_,
args_.web_contents,
args_.confirmation_required,
args_.callback);
}
DEFINE_WEB_CONTENTS_USER_DATA_KEY(OneClickSigninHelper);
const int OneClickSigninHelper::kMaxNavigationsSince = 10;
OneClickSigninHelper::OneClickSigninHelper(content::WebContents* web_contents,
PasswordManager* password_manager)
: content::WebContentsObserver(web_contents),
showing_signin_(false),
auto_accept_(AUTO_ACCEPT_NONE),
source_(signin::SOURCE_UNKNOWN),
switched_to_advanced_(false),
untrusted_navigations_since_signin_visit_(0),
untrusted_confirmation_required_(false),
do_not_clear_pending_email_(false),
do_not_start_sync_for_testing_(false),
weak_pointer_factory_(this) {
if (password_manager) {
password_manager->AddSubmissionCallback(
base::Bind(&OneClickSigninHelper::PasswordSubmitted,
weak_pointer_factory_.GetWeakPtr()));
}
}
OneClickSigninHelper::~OneClickSigninHelper() {
DCHECK(!web_contents());
}
void OneClickSigninHelper::LogHistogramValue(
signin::Source source, int action) {
switch (source) {
case signin::SOURCE_START_PAGE:
UMA_HISTOGRAM_ENUMERATION("Signin.StartPageActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_NTP_LINK:
UMA_HISTOGRAM_ENUMERATION("Signin.NTPLinkActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_MENU:
UMA_HISTOGRAM_ENUMERATION("Signin.MenuActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_SETTINGS:
UMA_HISTOGRAM_ENUMERATION("Signin.SettingsActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_EXTENSION_INSTALL_BUBBLE:
UMA_HISTOGRAM_ENUMERATION("Signin.ExtensionInstallBubbleActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_WEBSTORE_INSTALL:
UMA_HISTOGRAM_ENUMERATION("Signin.WebstoreInstallActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_APP_LAUNCHER:
UMA_HISTOGRAM_ENUMERATION("Signin.AppLauncherActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_APPS_PAGE_LINK:
UMA_HISTOGRAM_ENUMERATION("Signin.AppsPageLinkActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_BOOKMARK_BUBBLE:
UMA_HISTOGRAM_ENUMERATION("Signin.BookmarkBubbleActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_AVATAR_BUBBLE_SIGN_IN:
UMA_HISTOGRAM_ENUMERATION("Signin.AvatarBubbleActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT:
UMA_HISTOGRAM_ENUMERATION("Signin.AvatarBubbleActions", action,
one_click_signin::HISTOGRAM_MAX);
break;
case signin::SOURCE_DEVICES_PAGE:
UMA_HISTOGRAM_ENUMERATION("Signin.DevicesPageActions", action,
one_click_signin::HISTOGRAM_MAX);
default:
COMPILE_ASSERT(signin::SOURCE_UNKNOWN == 12,
kSourceEnumHasChangedButNotThisSwitchStatement);
NOTREACHED();
return;
}
UMA_HISTOGRAM_ENUMERATION("Signin.AllAccessPointActions", action,
one_click_signin::HISTOGRAM_MAX);
}
void OneClickSigninHelper::CreateForWebContentsWithPasswordManager(
content::WebContents* contents,
PasswordManager* password_manager) {
if (!FromWebContents(contents)) {
contents->SetUserData(UserDataKey(),
new OneClickSigninHelper(contents, password_manager));
}
}
bool OneClickSigninHelper::CanOffer(content::WebContents* web_contents,
CanOfferFor can_offer_for,
const std::string& email,
std::string* error_message) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
VLOG(1) << "OneClickSigninHelper::CanOffer";
if (error_message)
error_message->clear();
if (!web_contents)
return false;
if (web_contents->GetBrowserContext()->IsOffTheRecord())
return false;
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
if (!profile)
return false;
SigninManager* manager =
SigninManagerFactory::GetForProfile(profile);
if (manager && !manager->IsSigninAllowed())
return false;
if (can_offer_for == CAN_OFFER_FOR_INTERSTITAL_ONLY &&
!profile->GetPrefs()->GetBoolean(prefs::kReverseAutologinEnabled))
return false;
if (!ChromeSigninClient::ProfileAllowsSigninCookies(profile))
return false;
if (!email.empty()) {
if (!manager)
return false;
if (!manager->IsAllowedUsername(email)) {
if (error_message) {
error_message->assign(
l10n_util::GetStringUTF8(IDS_SYNC_LOGIN_NAME_PROHIBITED));
}
return false;
}
if (can_offer_for != CAN_OFFER_FOR_SECONDARY_ACCOUNT) {
std::string current_email = manager->GetAuthenticatedUsername();
const bool same_email = gaia::AreEmailsSame(current_email, email);
if (!current_email.empty() && !same_email) {
UMA_HISTOGRAM_ENUMERATION("Signin.Reauth",
signin::HISTOGRAM_ACCOUNT_MISSMATCH,
signin::HISTOGRAM_MAX);
if (error_message) {
error_message->assign(
l10n_util::GetStringFUTF8(IDS_SYNC_WRONG_EMAIL,
base::UTF8ToUTF16(current_email)));
}
return false;
}
if (g_browser_process && !same_email) {
ProfileManager* manager = g_browser_process->profile_manager();
if (manager) {
ProfileInfoCache& cache = manager->GetProfileInfoCache();
for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) {
std::string current_email =
base::UTF16ToUTF8(cache.GetUserNameOfProfileAtIndex(i));
if (gaia::AreEmailsSame(email, current_email)) {
if (error_message) {
error_message->assign(
l10n_util::GetStringUTF8(IDS_SYNC_USER_NAME_IN_USE_ERROR));
}
return false;
}
}
}
}
}
if (can_offer_for == CAN_OFFER_FOR_INTERSTITAL_ONLY) {
const base::ListValue* rejected_emails = profile->GetPrefs()->GetList(
prefs::kReverseAutologinRejectedEmailList);
if (!rejected_emails->empty()) {
base::ListValue::const_iterator iter = rejected_emails->Find(
base::StringValue(email));
if (iter != rejected_emails->end())
return false;
}
}
}
VLOG(1) << "OneClickSigninHelper::CanOffer: yes we can";
return true;
}
OneClickSigninHelper::Offer OneClickSigninHelper::CanOfferOnIOThread(
net::URLRequest* request,
ProfileIOData* io_data) {
return CanOfferOnIOThreadImpl(request->url(), request, io_data);
}
OneClickSigninHelper::Offer OneClickSigninHelper::CanOfferOnIOThreadImpl(
const GURL& url,
base::SupportsUserData* request,
ProfileIOData* io_data) {
if (!gaia::IsGaiaSignonRealm(url.GetOrigin()))
return IGNORE_REQUEST;
if (!io_data)
return DONT_OFFER;
if (io_data->IsOffTheRecord())
return DONT_OFFER;
if (!io_data->signin_allowed()->GetValue())
return DONT_OFFER;
if (!io_data->reverse_autologin_enabled()->GetValue())
return DONT_OFFER;
if (!io_data->google_services_username()->GetValue().empty())
return DONT_OFFER;
if (!ChromeSigninClient::SettingsAllowSigninCookies(
io_data->GetCookieSettings()))
return DONT_OFFER;
const std::string& pending_email = io_data->reverse_autologin_pending_email();
if (!pending_email.empty()) {
if (!SigninManager::IsUsernameAllowedByPolicy(pending_email,
io_data->google_services_username_pattern()->GetValue())) {
return DONT_OFFER;
}
std::vector<std::string> rejected_emails =
io_data->one_click_signin_rejected_email_list()->GetValue();
if (std::count_if(rejected_emails.begin(), rejected_emails.end(),
std::bind2nd(std::equal_to<std::string>(),
pending_email)) > 0) {
return DONT_OFFER;
}
if (io_data->signin_names()->GetEmails().count(
base::UTF8ToUTF16(pending_email)) > 0) {
return DONT_OFFER;
}
}
return CAN_OFFER;
}
void OneClickSigninHelper::ShowInfoBarIfPossible(net::URLRequest* request,
ProfileIOData* io_data,
int child_id,
int route_id) {
std::string google_chrome_signin_value;
std::string google_accounts_signin_value;
request->GetResponseHeaderByName("Google-Chrome-SignIn",
&google_chrome_signin_value);
request->GetResponseHeaderByName("Google-Accounts-SignIn",
&google_accounts_signin_value);
if (!google_accounts_signin_value.empty() ||
!google_chrome_signin_value.empty()) {
VLOG(1) << "OneClickSigninHelper::ShowInfoBarIfPossible:"
<< " g-a-s='" << google_accounts_signin_value << "'"
<< " g-c-s='" << google_chrome_signin_value << "'";
}
if (!gaia::IsGaiaSignonRealm(request->url().GetOrigin()))
return;
std::vector<std::pair<std::string, std::string> > pairs;
base::SplitStringIntoKeyValuePairs(google_accounts_signin_value, '=', ',',
&pairs);
std::string session_index;
std::string email;
for (size_t i = 0; i < pairs.size(); ++i) {
const std::pair<std::string, std::string>& pair = pairs[i];
const std::string& key = pair.first;
const std::string& value = pair.second;
if (key == "email") {
base::TrimString(value, "\"", &email);
} else if (key == "sessionindex") {
session_index = value;
}
}
if (!email.empty())
io_data->set_reverse_autologin_pending_email(email);
if (!email.empty() || !session_index.empty()) {
VLOG(1) << "OneClickSigninHelper::ShowInfoBarIfPossible:"
<< " email=" << email
<< " sessionindex=" << session_index;
}
AutoAccept auto_accept = AUTO_ACCEPT_NONE;
signin::Source source = signin::SOURCE_UNKNOWN;
GURL continue_url;
std::vector<std::string> tokens;
base::SplitString(google_chrome_signin_value, ',', &tokens);
for (size_t i = 0; i < tokens.size(); ++i) {
const std::string& token = tokens[i];
if (token == "accepted") {
auto_accept = AUTO_ACCEPT_ACCEPTED;
} else if (token == "configure") {
auto_accept = AUTO_ACCEPT_CONFIGURE;
} else if (token == "rejected-for-profile") {
auto_accept = AUTO_ACCEPT_REJECTED_FOR_PROFILE;
}
}
source = GetSigninSource(request->url(), &continue_url);
if (source != signin::SOURCE_UNKNOWN)
auto_accept = AUTO_ACCEPT_EXPLICIT;
if (auto_accept != AUTO_ACCEPT_NONE) {
VLOG(1) << "OneClickSigninHelper::ShowInfoBarIfPossible:"
<< " auto_accept=" << auto_accept;
}
if (session_index.empty() && email.empty() &&
auto_accept == AUTO_ACCEPT_NONE && !continue_url.is_valid()) {
return;
}
content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE,
base::Bind(&OneClickSigninHelper::ShowInfoBarUIThread, session_index,
email, auto_accept, source, continue_url, child_id, route_id));
}
void OneClickSigninHelper::LogConfirmHistogramValue(int action) {
UMA_HISTOGRAM_ENUMERATION("Signin.OneClickConfirmation", action,
one_click_signin::HISTOGRAM_CONFIRM_MAX);
}
void OneClickSigninHelper::ShowInfoBarUIThread(
const std::string& session_index,
const std::string& email,
AutoAccept auto_accept,
signin::Source source,
const GURL& continue_url,
int child_id,
int route_id) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
content::WebContents* web_contents = tab_util::GetWebContentsByID(child_id,
route_id);
if (!web_contents)
return;
OneClickSigninHelper* helper =
OneClickSigninHelper::FromWebContents(web_contents);
if (!helper)
return;
if (auto_accept != AUTO_ACCEPT_NONE)
helper->auto_accept_ = auto_accept;
if (source != signin::SOURCE_UNKNOWN &&
helper->source_ == signin::SOURCE_UNKNOWN) {
helper->source_ = source;
}
if (!session_index.empty())
helper->session_index_ = session_index;
if (!email.empty())
helper->email_ = email;
CanOfferFor can_offer_for =
(auto_accept != AUTO_ACCEPT_EXPLICIT &&
helper->auto_accept_ != AUTO_ACCEPT_EXPLICIT) ?
CAN_OFFER_FOR_INTERSTITAL_ONLY : CAN_OFFER_FOR_ALL;
std::string error_message;
if (!web_contents || !CanOffer(web_contents, can_offer_for, email,
&error_message)) {
VLOG(1) << "OneClickSigninHelper::ShowInfoBarUIThread: not offering";
if (helper && helper->error_message_.empty() && !error_message.empty())
helper->error_message_ = error_message;
return;
}
Profile* profile =
Profile::FromBrowserContext(web_contents->GetBrowserContext());
ChromeSigninClient* signin_client =
profile ? ChromeSigninClientFactory::GetForProfile(profile) : NULL;
helper->untrusted_confirmation_required_ |=
(signin_client && !signin_client->IsSigninProcess(child_id));
if (continue_url.is_valid()) {
if (helper->original_continue_url_.is_empty())
helper->original_continue_url_ = continue_url;
helper->continue_url_ = continue_url;
}
}
void OneClickSigninHelper::RemoveSigninRedirectURLHistoryItem(
content::WebContents* web_contents) {
if (signin::IsContinueUrlForWebBasedSigninFlow(
web_contents->GetLastCommittedURL())) {
new CurrentHistoryCleaner(web_contents);
}
}
void OneClickSigninHelper::ShowSigninErrorBubble(Browser* browser,
const std::string& error) {
DCHECK(!error.empty());
browser->window()->ShowOneClickSigninBubble(
BrowserWindow::ONE_CLICK_SIGNIN_BUBBLE_TYPE_BUBBLE,
base::string16(),
base::UTF8ToUTF16(error),
BrowserWindow::StartSyncCallback());
}
bool OneClickSigninHelper::HandleCrossAccountError(
content::WebContents* contents,
const std::string& session_index,
const std::string& email,
const std::string& password,
const std::string& refresh_token,
OneClickSigninHelper::AutoAccept auto_accept,
signin::Source source,
OneClickSigninSyncStarter::StartSyncMode start_mode,
OneClickSigninSyncStarter::Callback sync_callback) {
Profile* profile =
Profile::FromBrowserContext(contents->GetBrowserContext());
std::string last_email =
profile->GetPrefs()->GetString(prefs::kGoogleServicesLastUsername);
if (!last_email.empty() && !gaia::AreEmailsSame(last_email, email)) {
Browser* browser = chrome::FindBrowserWithWebContents(contents);
ConfirmEmailDialogDelegate::AskForConfirmation(
contents,
last_email,
email,
base::Bind(
&StartExplicitSync,
StartSyncArgs(profile, browser, auto_accept,
session_index, email, password, refresh_token,
contents, false , source,
sync_callback),
contents,
start_mode));
return true;
}
return false;
}
void OneClickSigninHelper::RedirectToNtpOrAppsPage(
content::WebContents* contents, signin::Source source) {
if (contents->GetController().GetPendingEntry())
return;
VLOG(1) << "RedirectToNtpOrAppsPage";
GURL url(source == signin::SOURCE_APPS_PAGE_LINK ?
chrome::kChromeUIAppsURL : chrome::kChromeUINewTabURL);
content::OpenURLParams params(url,
content::Referrer(),
CURRENT_TAB,
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
false);
contents->OpenURL(params);
}
void OneClickSigninHelper::RedirectToNtpOrAppsPageIfNecessary(
content::WebContents* contents, signin::Source source) {
if (source != signin::SOURCE_SETTINGS &&
source != signin::SOURCE_WEBSTORE_INSTALL) {
RedirectToNtpOrAppsPage(contents, source);
}
}
void OneClickSigninHelper::RedirectToSignin() {
VLOG(1) << "OneClickSigninHelper::RedirectToSignin";
signin::Source source = signin::GetSourceForPromoURL(continue_url_);
if (source == signin::SOURCE_UNKNOWN)
source = signin::SOURCE_MENU;
GURL page = signin::GetPromoURL(source, false);
content::WebContents* contents = web_contents();
contents->GetController().LoadURL(page,
content::Referrer(),
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
}
void OneClickSigninHelper::CleanTransientState() {
VLOG(1) << "OneClickSigninHelper::CleanTransientState";
showing_signin_ = false;
email_.clear();
password_.clear();
auto_accept_ = AUTO_ACCEPT_NONE;
source_ = signin::SOURCE_UNKNOWN;
switched_to_advanced_ = false;
continue_url_ = GURL();
untrusted_navigations_since_signin_visit_ = 0;
untrusted_confirmation_required_ = false;
error_message_.clear();
if (!do_not_clear_pending_email_) {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE,
base::Bind(&ClearPendingEmailOnIOThread,
base::Unretained(profile->GetResourceContext())));
}
}
void OneClickSigninHelper::PasswordSubmitted(
const autofill::PasswordForm& form) {
if (gaia::IsGaiaSignonRealm(GURL(form.signon_realm))) {
VLOG(1) << "OneClickSigninHelper::DidNavigateAnyFrame: got password";
password_ = base::UTF16ToUTF8(form.password_value);
}
}
void OneClickSigninHelper::SetDoNotClearPendingEmailForTesting() {
do_not_clear_pending_email_ = true;
}
void OneClickSigninHelper::set_do_not_start_sync_for_testing() {
do_not_start_sync_for_testing_ = true;
}
void OneClickSigninHelper::DidStartNavigationToPendingEntry(
const GURL& url,
content::NavigationController::ReloadType reload_type) {
VLOG(1) << "OneClickSigninHelper::DidStartNavigationToPendingEntry: url=" <<
url.spec();
GURL::Replacements replacements;
replacements.ClearQuery();
if (!IsValidGaiaSigninRedirectOrResponseURL(url) &&
continue_url_.is_valid() &&
url.ReplaceComponents(replacements) !=
continue_url_.ReplaceComponents(replacements)) {
if (++untrusted_navigations_since_signin_visit_ > kMaxNavigationsSince)
CleanTransientState();
}
}
void OneClickSigninHelper::DidNavigateMainFrame(
const content::LoadCommittedDetails& details,
const content::FrameNavigateParams& params) {
if (!SigninManager::IsWebBasedSigninFlowURL(params.url)) {
Profile* profile =
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
ChromeSigninClient* signin_client =
profile ? ChromeSigninClientFactory::GetForProfile(profile) : NULL;
int process_id = web_contents()->GetRenderProcessHost()->GetID();
if (signin_client && signin_client->IsSigninProcess(process_id))
signin_client->ClearSigninProcess();
if (!content::PageTransitionIsWebTriggerable(params.transition) &&
auto_accept_ != AUTO_ACCEPT_NONE) {
CleanTransientState();
}
}
}
void OneClickSigninHelper::DidStopLoading(
content::RenderViewHost* render_view_host) {
content::WebContents* contents = web_contents();
const GURL url = contents->GetLastCommittedURL();
Profile* profile =
Profile::FromBrowserContext(contents->GetBrowserContext());
VLOG(1) << "OneClickSigninHelper::DidStopLoading: url=" << url.spec();
if (url.scheme() == content::kChromeUIScheme) {
VLOG(1) << "OneClickSigninHelper::DidStopLoading: suppressed for url="
<< url.spec();
CleanTransientState();
return;
}
if (!error_message_.empty() && auto_accept_ == AUTO_ACCEPT_EXPLICIT) {
VLOG(1) << "OneClickSigninHelper::DidStopLoading: error=" << error_message_;
RemoveSigninRedirectURLHistoryItem(contents);
Browser* browser = chrome::FindBrowserWithWebContents(contents);
RedirectToNtpOrAppsPage(web_contents(), source_);
ShowSigninErrorBubble(browser, error_message_);
CleanTransientState();
return;
}
if (AreWeShowingSignin(url, source_, email_)) {
if (!showing_signin_) {
if (source_ == signin::SOURCE_UNKNOWN)
LogOneClickHistogramValue(one_click_signin::HISTOGRAM_SHOWN);
else
LogHistogramValue(source_, one_click_signin::HISTOGRAM_SHOWN);
}
showing_signin_ = true;
}
GURL::Replacements replacements;
replacements.ClearQuery();
bool google_domain_url = google_util::IsGoogleDomainUrl(
url,
google_util::ALLOW_SUBDOMAIN,
google_util::DISALLOW_NON_STANDARD_PORTS);
const bool continue_url_match =
google_domain_url &&
url.ReplaceComponents(replacements).path() ==
continue_url_.ReplaceComponents(replacements).path();
const bool original_continue_url_match =
google_domain_url &&
url.ReplaceComponents(replacements).path() ==
original_continue_url_.ReplaceComponents(replacements).path();
if (continue_url_match)
RemoveSigninRedirectURLHistoryItem(contents);
if (email_.empty()) {
VLOG(1) << "OneClickSigninHelper::DidStopLoading: nothing to do";
if (original_continue_url_match) {
if (auto_accept_ == AUTO_ACCEPT_EXPLICIT)
RedirectToSignin();
std::string unused_value;
if (net::GetValueForKeyInQuery(url, "ntp", &unused_value)) {
signin::SetUserSkippedPromo(profile);
RedirectToNtpOrAppsPage(web_contents(), source_);
}
} else {
if (!IsValidGaiaSigninRedirectOrResponseURL(url) &&
++untrusted_navigations_since_signin_visit_ > kMaxNavigationsSince) {
CleanTransientState();
}
}
return;
}
if (!continue_url_match && IsValidGaiaSigninRedirectOrResponseURL(url))
return;
if (auto_accept_ == AUTO_ACCEPT_EXPLICIT) {
DCHECK(source_ != signin::SOURCE_UNKNOWN);
if (!continue_url_match) {
VLOG(1) << "OneClickSigninHelper::DidStopLoading: invalid url='"
<< url.spec()
<< "' expected continue url=" << continue_url_;
CleanTransientState();
return;
}
signin::Source source = signin::GetSourceForPromoURL(url);
if (source != source_) {
source_ = source;
switched_to_advanced_ = source == signin::SOURCE_SETTINGS;
}
}
Browser* browser = chrome::FindBrowserWithWebContents(contents);
VLOG(1) << "OneClickSigninHelper::DidStopLoading: signin is go."
<< " auto_accept=" << auto_accept_
<< " source=" << source_;
switch (auto_accept_) {
case AUTO_ACCEPT_NONE:
if (showing_signin_)
LogOneClickHistogramValue(one_click_signin::HISTOGRAM_DISMISSED);
break;
case AUTO_ACCEPT_ACCEPTED:
LogOneClickHistogramValue(one_click_signin::HISTOGRAM_ACCEPTED);
LogOneClickHistogramValue(one_click_signin::HISTOGRAM_WITH_DEFAULTS);
SigninManager::DisableOneClickSignIn(profile->GetPrefs());
if (!do_not_start_sync_for_testing_) {
StartSync(
StartSyncArgs(profile, browser, auto_accept_,
session_index_, email_, password_, "",
NULL ,
true , source_,
CreateSyncStarterCallback()),
OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS);
}
break;
case AUTO_ACCEPT_CONFIGURE:
LogOneClickHistogramValue(one_click_signin::HISTOGRAM_ACCEPTED);
LogOneClickHistogramValue(one_click_signin::HISTOGRAM_WITH_ADVANCED);
SigninManager::DisableOneClickSignIn(profile->GetPrefs());
if (!do_not_start_sync_for_testing_) {
StartSync(
StartSyncArgs(profile, browser, auto_accept_,
session_index_, email_, password_, "",
NULL ,
true , source_,
CreateSyncStarterCallback()),
OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST);
}
break;
case AUTO_ACCEPT_EXPLICIT: {
signin::Source original_source =
signin::GetSourceForPromoURL(original_continue_url_);
if (switched_to_advanced_) {
LogHistogramValue(original_source,
one_click_signin::HISTOGRAM_WITH_ADVANCED);
LogHistogramValue(original_source,
one_click_signin::HISTOGRAM_ACCEPTED);
} else {
LogHistogramValue(source_, one_click_signin::HISTOGRAM_ACCEPTED);
LogHistogramValue(source_, one_click_signin::HISTOGRAM_WITH_DEFAULTS);
}
ProfileSyncService* sync_service =
ProfileSyncServiceFactory::GetForProfile(profile);
SigninErrorController* error_controller =
ProfileOAuth2TokenServiceFactory::GetForProfile(profile)->
signin_error_controller();
OneClickSigninSyncStarter::StartSyncMode start_mode =
source_ == signin::SOURCE_SETTINGS ?
(error_controller->HasError() &&
sync_service && sync_service->HasSyncSetupCompleted()) ?
OneClickSigninSyncStarter::SHOW_SETTINGS_WITHOUT_CONFIGURE :
OneClickSigninSyncStarter::CONFIGURE_SYNC_FIRST :
OneClickSigninSyncStarter::SYNC_WITH_DEFAULT_SETTINGS;
if (!HandleCrossAccountError(contents, session_index_, email_, password_,
"", auto_accept_, source_, start_mode,
CreateSyncStarterCallback())) {
if (!do_not_start_sync_for_testing_) {
StartSync(
StartSyncArgs(profile, browser, auto_accept_,
session_index_, email_, password_, "",
contents,
untrusted_confirmation_required_, source_,
CreateSyncStarterCallback()),
start_mode);
}
RedirectToNtpOrAppsPageIfNecessary(web_contents(), source_);
}
if (original_source == signin::SOURCE_SETTINGS ||
(original_source == signin::SOURCE_WEBSTORE_INSTALL &&
source_ == signin::SOURCE_SETTINGS)) {
ProfileSyncService* sync_service =
ProfileSyncServiceFactory::GetForProfile(profile);
if (sync_service)
sync_service->AddObserver(this);
}
break;
}
case AUTO_ACCEPT_REJECTED_FOR_PROFILE:
AddEmailToOneClickRejectedList(profile, email_);
LogOneClickHistogramValue(one_click_signin::HISTOGRAM_REJECTED);
break;
default:
NOTREACHED() << "Invalid auto_accept=" << auto_accept_;
break;
}
CleanTransientState();
}
void OneClickSigninHelper::WebContentsDestroyed(
content::WebContents* contents) {
Profile* profile =
Profile::FromBrowserContext(contents->GetBrowserContext());
ProfileSyncService* sync_service =
ProfileSyncServiceFactory::GetForProfile(profile);
if (sync_service)
sync_service->RemoveObserver(this);
}
void OneClickSigninHelper::OnStateChanged() {
DCHECK(!original_continue_url_.is_empty());
content::WebContents* contents = web_contents();
Profile* profile =
Profile::FromBrowserContext(contents->GetBrowserContext());
ProfileSyncService* sync_service =
ProfileSyncServiceFactory::GetForProfile(profile);
if (signin::IsAutoCloseEnabledInURL(original_continue_url_)) {
base::MessageLoop::current()->PostTask(
FROM_HERE,
base::Bind(&CloseTab, base::Unretained(contents)));
} else {
if (sync_service->FirstSetupInProgress())
return;
if (sync_service->sync_initialized() &&
signin::GetSourceForPromoURL(original_continue_url_)
!= signin::SOURCE_SETTINGS) {
contents->GetController().LoadURL(original_continue_url_,
content::Referrer(),
content::PAGE_TRANSITION_AUTO_TOPLEVEL,
std::string());
}
}
original_continue_url_ = GURL();
sync_service->RemoveObserver(this);
}
OneClickSigninSyncStarter::Callback
OneClickSigninHelper::CreateSyncStarterCallback() {
return base::Bind(&OneClickSigninHelper::SyncSetupCompletedCallback,
weak_pointer_factory_.GetWeakPtr());
}
void OneClickSigninHelper::SyncSetupCompletedCallback(
OneClickSigninSyncStarter::SyncSetupResult result) {
if (result == OneClickSigninSyncStarter::SYNC_SETUP_FAILURE &&
web_contents()) {
GURL current_url = web_contents()->GetVisibleURL();
if (signin::IsContinueUrlForWebBasedSigninFlow(current_url) &&
!signin::IsAutoCloseEnabledInURL(original_continue_url_)) {
RedirectToNtpOrAppsPage(
web_contents(),
signin::GetSourceForPromoURL(original_continue_url_));
}
}
}