1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/auto_login_infobar_delegate.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/metrics/histogram.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/chrome_notification_types.h"
15 #include "chrome/browser/google/google_util.h"
16 #include "chrome/browser/infobars/infobar.h"
17 #include "chrome/browser/infobars/infobar_service.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/signin/profile_oauth2_token_service.h"
20 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
21 #include "chrome/browser/ui/sync/sync_promo_ui.h"
22 #include "chrome/common/chrome_switches.h"
23 #include "chrome/common/pref_names.h"
24 #include "chrome/common/url_constants.h"
25 #include "content/public/browser/navigation_controller.h"
26 #include "content/public/browser/notification_details.h"
27 #include "content/public/browser/notification_observer.h"
28 #include "content/public/browser/notification_registrar.h"
29 #include "content/public/browser/notification_source.h"
30 #include "content/public/browser/notification_types.h"
31 #include "content/public/browser/page_navigator.h"
32 #include "content/public/browser/web_contents.h"
33 #include "content/public/browser/web_contents_observer.h"
34 #include "content/public/common/referrer.h"
35 #include "google_apis/gaia/gaia_constants.h"
36 #include "google_apis/gaia/gaia_urls.h"
37 #include "google_apis/gaia/ubertoken_fetcher.h"
38 #include "grit/chromium_strings.h"
39 #include "grit/generated_resources.h"
40 #include "grit/theme_resources.h"
41 #include "net/base/escape.h"
42 #include "net/url_request/url_request.h"
43 #include "ui/base/l10n/l10n_util.h"
45 #if defined(OS_ANDROID)
46 #include "chrome/browser/ui/android/infobars/auto_login_infobar_delegate_android.h"
50 // AutoLoginRedirector --------------------------------------------------------
54 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to
55 // auto-login. It holds context information needed while re-issuing service
56 // tokens using the OAuth2TokenService, gets the browser cookies with the
57 // TokenAuth API, and finally redirects the user to the correct page.
58 class AutoLoginRedirector
: public UbertokenConsumer
,
59 public content::WebContentsObserver
{
61 AutoLoginRedirector(content::WebContents
* web_contents
,
62 const std::string
& args
);
63 virtual ~AutoLoginRedirector();
66 // Overriden from UbertokenConsumer:
67 virtual void OnUbertokenSuccess(const std::string
& token
) OVERRIDE
;
68 virtual void OnUbertokenFailure(const GoogleServiceAuthError
& error
) OVERRIDE
;
70 // Implementation of content::WebContentsObserver
71 virtual void WebContentsDestroyed(
72 content::WebContents
* web_contents
) OVERRIDE
;
74 // Redirect tab to MergeSession URL, logging the user in and navigating
75 // to the desired page.
76 void RedirectToMergeSession(const std::string
& token
);
78 const std::string args_
;
79 scoped_ptr
<UbertokenFetcher
> ubertoken_fetcher_
;
81 DISALLOW_COPY_AND_ASSIGN(AutoLoginRedirector
);
84 AutoLoginRedirector::AutoLoginRedirector(
85 content::WebContents
* web_contents
,
86 const std::string
& args
)
87 : content::WebContentsObserver(web_contents
),
90 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
91 ProfileOAuth2TokenService
* token_service
=
92 ProfileOAuth2TokenServiceFactory::GetForProfile(profile
);
93 ubertoken_fetcher_
.reset(new UbertokenFetcher(token_service
,
95 profile
->GetRequestContext()));
96 ubertoken_fetcher_
->StartFetchingToken(token_service
->GetPrimaryAccountId());
99 AutoLoginRedirector::~AutoLoginRedirector() {
102 void AutoLoginRedirector::WebContentsDestroyed(
103 content::WebContents
* web_contents
) {
104 // The WebContents that started this has been destroyed. The request must be
105 // cancelled and this object must be deleted.
106 ubertoken_fetcher_
.reset();
107 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
110 void AutoLoginRedirector::OnUbertokenSuccess(const std::string
& token
) {
111 RedirectToMergeSession(token
);
112 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
115 void AutoLoginRedirector::OnUbertokenFailure(
116 const GoogleServiceAuthError
& error
) {
117 LOG(WARNING
) << "AutoLoginRedirector: token request failed";
118 base::MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
121 void AutoLoginRedirector::RedirectToMergeSession(const std::string
& token
) {
122 // TODO(rogerta): what is the correct page transition?
123 web_contents()->GetController().LoadURL(
124 GaiaUrls::GetInstance()->merge_session_url().Resolve(
125 "?source=chrome&uberauth=" + token
+ "&" + args_
),
126 content::Referrer(), content::PAGE_TRANSITION_AUTO_BOOKMARK
,
133 // AutoLoginInfoBarDelegate ---------------------------------------------------
136 bool AutoLoginInfoBarDelegate::Create(content::WebContents
* web_contents
,
137 const Params
& params
) {
138 // If |web_contents| is hosted in a WebDialog, there may be no infobar
140 InfoBarService
* infobar_service
=
141 InfoBarService::FromWebContents(web_contents
);
142 if (!infobar_service
)
146 Profile::FromBrowserContext(web_contents
->GetBrowserContext());
147 #if defined(OS_ANDROID)
148 typedef AutoLoginInfoBarDelegateAndroid Delegate
;
150 typedef AutoLoginInfoBarDelegate Delegate
;
152 return !!infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
153 scoped_ptr
<ConfirmInfoBarDelegate
>(new Delegate(params
, profile
))));
156 AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate(const Params
& params
,
158 : ConfirmInfoBarDelegate(),
160 button_pressed_(false) {
161 RecordHistogramAction(SHOWN
);
162 registrar_
.Add(this, chrome::NOTIFICATION_GOOGLE_SIGNED_OUT
,
163 content::Source
<Profile
>(profile
));
166 AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() {
167 if (!button_pressed_
)
168 RecordHistogramAction(IGNORED
);
171 void AutoLoginInfoBarDelegate::InfoBarDismissed() {
172 RecordHistogramAction(DISMISSED
);
173 button_pressed_
= true;
176 int AutoLoginInfoBarDelegate::GetIconID() const {
177 return IDR_INFOBAR_AUTOLOGIN
;
180 InfoBarDelegate::Type
AutoLoginInfoBarDelegate::GetInfoBarType() const {
181 return PAGE_ACTION_TYPE
;
184 AutoLoginInfoBarDelegate
*
185 AutoLoginInfoBarDelegate::AsAutoLoginInfoBarDelegate() {
189 base::string16
AutoLoginInfoBarDelegate::GetMessageText() const {
190 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE
,
191 base::UTF8ToUTF16(params_
.username
));
194 base::string16
AutoLoginInfoBarDelegate::GetButtonLabel(
195 InfoBarButton button
) const {
196 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
197 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON
: IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON
);
200 bool AutoLoginInfoBarDelegate::Accept() {
201 // AutoLoginRedirector deletes itself.
202 new AutoLoginRedirector(web_contents(), params_
.header
.args
);
203 RecordHistogramAction(ACCEPTED
);
204 button_pressed_
= true;
208 bool AutoLoginInfoBarDelegate::Cancel() {
209 PrefService
* pref_service
= Profile::FromBrowserContext(
210 web_contents()->GetBrowserContext())->GetPrefs();
211 pref_service
->SetBoolean(prefs::kAutologinEnabled
, false);
212 RecordHistogramAction(REJECTED
);
213 button_pressed_
= true;
217 void AutoLoginInfoBarDelegate::Observe(
219 const content::NotificationSource
& source
,
220 const content::NotificationDetails
& details
) {
221 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT
, type
);
222 infobar()->RemoveSelf();
225 void AutoLoginInfoBarDelegate::RecordHistogramAction(Actions action
) {
226 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Regular", action
,
227 HISTOGRAM_BOUNDING_VALUE
);