1 // Copyright 2014 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.
4
5 #include "chrome/browser/ui/auto_login_infobar_delegate.h"
6
7 #include "base/bind.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/infobars/infobar_service.h"
15 #include "chrome/browser/profiles/profile.h"
16 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
17 #include "chrome/browser/signin/signin_manager_factory.h"
18 #include "chrome/browser/ui/sync/sync_promo_ui.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "chrome/common/pref_names.h"
21 #include "chrome/common/url_constants.h"
22 #include "components/google/core/browser/google_util.h"
23 #include "components/infobars/core/infobar.h"
24 #include "components/signin/core/browser/profile_oauth2_token_service.h"
25 #include "content/public/browser/navigation_controller.h"
26 #include "content/public/browser/page_navigator.h"
27 #include "content/public/browser/web_contents.h"
28 #include "content/public/browser/web_contents_observer.h"
29 #include "content/public/common/referrer.h"
30 #include "google_apis/gaia/gaia_constants.h"
31 #include "google_apis/gaia/gaia_urls.h"
32 #include "google_apis/gaia/ubertoken_fetcher.h"
33 #include "grit/chromium_strings.h"
34 #include "grit/generated_resources.h"
35 #include "grit/theme_resources.h"
36 #include "net/base/escape.h"
37 #include "net/url_request/url_request.h"
38 #include "ui/base/l10n/l10n_util.h"
39
40 #if defined(OS_ANDROID)
41 #include "chrome/browser/ui/android/infobars/auto_login_infobar_delegate_android.h"
42 #endif
43
44
45 // AutoLoginRedirector --------------------------------------------------------
46
47 namespace {
48
49 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to
50 // auto-login. It holds context information needed while re-issuing service
51 // tokens using the OAuth2TokenService, gets the browser cookies with the
52 // TokenAuth API, and finally redirects the user to the correct page.
53 class AutoLoginRedirector : public UbertokenConsumer,
54 public content::WebContentsObserver {
55 public:
56 AutoLoginRedirector(content::WebContents* web_contents,
57 const std::string& args);
58 virtual ~AutoLoginRedirector();
59
60 private:
61 // Overriden from UbertokenConsumer:
62 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
63 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
64
65 // Implementation of content::WebContentsObserver
66 virtual void WebContentsDestroyed() OVERRIDE;
67
68 // Redirect tab to MergeSession URL, logging the user in and navigating
69 // to the desired page.
70 void RedirectToMergeSession(const std::string& token);
71
72 const std::string args_;
73 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_;
74
75 DISALLOW_COPY_AND_ASSIGN(AutoLoginRedirector);
76 };
77
AutoLoginRedirector(content::WebContents * web_contents,const std::string & args)78 AutoLoginRedirector::AutoLoginRedirector(
79 content::WebContents* web_contents,
80 const std::string& args)
81 : content::WebContentsObserver(web_contents),
82 args_(args) {
83 Profile* profile =
84 Profile::FromBrowserContext(web_contents->GetBrowserContext());
85 ProfileOAuth2TokenService* token_service =
86 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
87 SigninManagerBase* signin_manager =
88 SigninManagerFactory::GetInstance()->GetForProfile(profile);
89 ubertoken_fetcher_.reset(new UbertokenFetcher(token_service,
90 this,
91 profile->GetRequestContext()));
92 ubertoken_fetcher_->StartFetchingToken(
93 signin_manager->GetAuthenticatedAccountId());
94 }
95
~AutoLoginRedirector()96 AutoLoginRedirector::~AutoLoginRedirector() {
97 }
98
WebContentsDestroyed()99 void AutoLoginRedirector::WebContentsDestroyed() {
100 // The WebContents that started this has been destroyed. The request must be
101 // cancelled and this object must be deleted.
102 ubertoken_fetcher_.reset();
103 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
104 }
105
OnUbertokenSuccess(const std::string & token)106 void AutoLoginRedirector::OnUbertokenSuccess(const std::string& token) {
107 RedirectToMergeSession(token);
108 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
109 }
110
OnUbertokenFailure(const GoogleServiceAuthError & error)111 void AutoLoginRedirector::OnUbertokenFailure(
112 const GoogleServiceAuthError& error) {
113 LOG(WARNING) << "AutoLoginRedirector: token request failed";
114 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
115 }
116
RedirectToMergeSession(const std::string & token)117 void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) {
118 // TODO(rogerta): what is the correct page transition?
119 web_contents()->GetController().LoadURL(
120 GaiaUrls::GetInstance()->merge_session_url().Resolve(
121 "?source=chrome&uberauth=" + token + "&" + args_),
122 content::Referrer(), content::PAGE_TRANSITION_AUTO_BOOKMARK,
123 std::string());
124 }
125
126 } // namespace
127
128
129 // AutoLoginInfoBarDelegate ---------------------------------------------------
130
131 // static
Create(content::WebContents * web_contents,const Params & params)132 bool AutoLoginInfoBarDelegate::Create(content::WebContents* web_contents,
133 const Params& params) {
134 // If |web_contents| is hosted in a WebDialog, there may be no infobar
135 // service.
136 InfoBarService* infobar_service =
137 InfoBarService::FromWebContents(web_contents);
138 if (!infobar_service)
139 return false;
140
141 Profile* profile =
142 Profile::FromBrowserContext(web_contents->GetBrowserContext());
143 #if defined(OS_ANDROID)
144 typedef AutoLoginInfoBarDelegateAndroid Delegate;
145 #else
146 typedef AutoLoginInfoBarDelegate Delegate;
147 #endif
148 return !!infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
149 scoped_ptr<ConfirmInfoBarDelegate>(new Delegate(params, profile))));
150 }
151
AutoLoginInfoBarDelegate(const Params & params,Profile * profile)152 AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate(const Params& params,
153 Profile* profile)
154 : ConfirmInfoBarDelegate(),
155 params_(params),
156 profile_(profile),
157 button_pressed_(false) {
158 RecordHistogramAction(SHOWN);
159
160 // The AutoLogin infobar is shown in incognito mode on Android, so a
161 // SigninManager isn't guaranteed to exist for |profile_|.
162 SigninManagerBase* signin_manager =
163 SigninManagerFactory::GetInstance()->GetForProfile(profile_);
164 if (signin_manager)
165 signin_manager->AddObserver(this);
166 }
167
~AutoLoginInfoBarDelegate()168 AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() {
169 // The AutoLogin infobar is shown in incognito mode on Android, so a
170 // SigninManager isn't guaranteed to exist for |profile_|.
171 SigninManagerBase* signin_manager =
172 SigninManagerFactory::GetInstance()->GetForProfile(profile_);
173 if (signin_manager)
174 signin_manager->RemoveObserver(this);
175
176 if (!button_pressed_)
177 RecordHistogramAction(IGNORED);
178 }
179
RecordHistogramAction(Actions action)180 void AutoLoginInfoBarDelegate::RecordHistogramAction(Actions action) {
181 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Regular", action,
182 HISTOGRAM_BOUNDING_VALUE);
183 }
184
InfoBarDismissed()185 void AutoLoginInfoBarDelegate::InfoBarDismissed() {
186 RecordHistogramAction(DISMISSED);
187 button_pressed_ = true;
188 }
189
GetIconID() const190 int AutoLoginInfoBarDelegate::GetIconID() const {
191 return IDR_INFOBAR_AUTOLOGIN;
192 }
193
GetInfoBarType() const194 infobars::InfoBarDelegate::Type AutoLoginInfoBarDelegate::GetInfoBarType()
195 const {
196 return PAGE_ACTION_TYPE;
197 }
198
199 AutoLoginInfoBarDelegate*
AsAutoLoginInfoBarDelegate()200 AutoLoginInfoBarDelegate::AsAutoLoginInfoBarDelegate() {
201 return this;
202 }
203
GetMessageText() const204 base::string16 AutoLoginInfoBarDelegate::GetMessageText() const {
205 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE,
206 base::UTF8ToUTF16(params_.username));
207 }
208
GetButtonLabel(InfoBarButton button) const209 base::string16 AutoLoginInfoBarDelegate::GetButtonLabel(
210 InfoBarButton button) const {
211 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
212 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON);
213 }
214
Accept()215 bool AutoLoginInfoBarDelegate::Accept() {
216 // AutoLoginRedirector deletes itself.
217 content::WebContents* web_contents =
218 InfoBarService::WebContentsFromInfoBar(infobar());
219 new AutoLoginRedirector(web_contents, params_.header.args);
220 RecordHistogramAction(ACCEPTED);
221 button_pressed_ = true;
222 return true;
223 }
224
Cancel()225 bool AutoLoginInfoBarDelegate::Cancel() {
226 content::WebContents* web_contents =
227 InfoBarService::WebContentsFromInfoBar(infobar());
228 PrefService* pref_service = Profile::FromBrowserContext(
229 web_contents->GetBrowserContext())->GetPrefs();
230 pref_service->SetBoolean(prefs::kAutologinEnabled, false);
231 RecordHistogramAction(REJECTED);
232 button_pressed_ = true;
233 return true;
234 }
235
GoogleSignedOut(const std::string & username)236 void AutoLoginInfoBarDelegate::GoogleSignedOut(const std::string& username) {
237 infobar()->RemoveSelf();
238 }
239