Add ICU message format support
[chromium-blink-merge.git] / chrome / browser / ui / auto_login_infobar_delegate.cc
blob158b5def5e4440b8879045d396671a317f1dad13
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.
5 #include "chrome/browser/ui/auto_login_infobar_delegate.h"
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 "chrome/grit/generated_resources.h"
23 #include "components/google/core/browser/google_util.h"
24 #include "components/infobars/core/infobar.h"
25 #include "components/signin/core/browser/profile_oauth2_token_service.h"
26 #include "content/public/browser/navigation_controller.h"
27 #include "content/public/browser/page_navigator.h"
28 #include "content/public/browser/web_contents.h"
29 #include "content/public/browser/web_contents_observer.h"
30 #include "content/public/common/referrer.h"
31 #include "google_apis/gaia/gaia_constants.h"
32 #include "google_apis/gaia/gaia_urls.h"
33 #include "google_apis/gaia/ubertoken_fetcher.h"
34 #include "grit/theme_resources.h"
35 #include "net/base/escape.h"
36 #include "net/url_request/url_request.h"
37 #include "ui/base/l10n/l10n_util.h"
39 // AutoLoginRedirector --------------------------------------------------------
41 namespace {
43 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to
44 // auto-login. It holds context information needed while re-issuing service
45 // tokens using the OAuth2TokenService, gets the browser cookies with the
46 // TokenAuth API, and finally redirects the user to the correct page.
47 class AutoLoginRedirector : public UbertokenConsumer,
48 public content::WebContentsObserver {
49 public:
50 AutoLoginRedirector(content::WebContents* web_contents,
51 const std::string& args);
52 ~AutoLoginRedirector() override;
54 private:
55 // Overriden from UbertokenConsumer:
56 void OnUbertokenSuccess(const std::string& token) override;
57 void OnUbertokenFailure(const GoogleServiceAuthError& error) override;
59 // Implementation of content::WebContentsObserver
60 void WebContentsDestroyed() override;
62 // Redirect tab to MergeSession URL, logging the user in and navigating
63 // to the desired page.
64 void RedirectToMergeSession(const std::string& token);
66 const std::string args_;
67 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_;
69 DISALLOW_COPY_AND_ASSIGN(AutoLoginRedirector);
72 AutoLoginRedirector::AutoLoginRedirector(
73 content::WebContents* web_contents,
74 const std::string& args)
75 : content::WebContentsObserver(web_contents),
76 args_(args) {
77 Profile* profile =
78 Profile::FromBrowserContext(web_contents->GetBrowserContext());
79 ProfileOAuth2TokenService* token_service =
80 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
81 SigninManagerBase* signin_manager =
82 SigninManagerFactory::GetInstance()->GetForProfile(profile);
83 ubertoken_fetcher_.reset(new UbertokenFetcher(token_service,
84 this,
85 GaiaConstants::kChromeSource,
86 profile->GetRequestContext()));
87 ubertoken_fetcher_->StartFetchingToken(
88 signin_manager->GetAuthenticatedAccountId());
91 AutoLoginRedirector::~AutoLoginRedirector() {
94 void AutoLoginRedirector::WebContentsDestroyed() {
95 // The WebContents that started this has been destroyed. The request must be
96 // cancelled and this object must be deleted.
97 ubertoken_fetcher_.reset();
98 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
101 void AutoLoginRedirector::OnUbertokenSuccess(const std::string& token) {
102 RedirectToMergeSession(token);
103 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
106 void AutoLoginRedirector::OnUbertokenFailure(
107 const GoogleServiceAuthError& error) {
108 LOG(WARNING) << "AutoLoginRedirector: token request failed";
109 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
112 void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) {
113 // TODO(rogerta): what is the correct page transition?
114 web_contents()->GetController().LoadURL(
115 GaiaUrls::GetInstance()->merge_session_url().Resolve(
116 "?source=chrome&uberauth=" + token + "&" + args_),
117 content::Referrer(), ui::PAGE_TRANSITION_AUTO_BOOKMARK,
118 std::string());
121 } // namespace
124 // AutoLoginInfoBarDelegate ---------------------------------------------------
126 // static
127 bool AutoLoginInfoBarDelegate::Create(content::WebContents* web_contents,
128 const Params& params) {
129 // If |web_contents| is hosted in a WebDialog, there may be no infobar
130 // service.
131 InfoBarService* infobar_service =
132 InfoBarService::FromWebContents(web_contents);
133 if (!infobar_service)
134 return false;
136 Profile* profile =
137 Profile::FromBrowserContext(web_contents->GetBrowserContext());
138 typedef AutoLoginInfoBarDelegate Delegate;
139 return !!infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar(
140 scoped_ptr<ConfirmInfoBarDelegate>(new Delegate(params, profile))));
143 AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate(const Params& params,
144 Profile* profile)
145 : ConfirmInfoBarDelegate(),
146 params_(params),
147 profile_(profile),
148 button_pressed_(false) {
149 RecordHistogramAction(SHOWN);
151 // The AutoLogin infobar is shown in incognito mode on Android, so a
152 // SigninManager isn't guaranteed to exist for |profile_|.
153 SigninManagerBase* signin_manager =
154 SigninManagerFactory::GetInstance()->GetForProfile(profile_);
155 if (signin_manager)
156 signin_manager->AddObserver(this);
159 AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() {
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->RemoveObserver(this);
167 if (!button_pressed_)
168 RecordHistogramAction(IGNORED);
171 void AutoLoginInfoBarDelegate::RecordHistogramAction(Actions action) {
172 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Regular", action,
173 HISTOGRAM_BOUNDING_VALUE);
176 infobars::InfoBarDelegate::Type AutoLoginInfoBarDelegate::GetInfoBarType()
177 const {
178 return PAGE_ACTION_TYPE;
181 int AutoLoginInfoBarDelegate::GetIconID() const {
182 return IDR_INFOBAR_AUTOLOGIN;
185 void AutoLoginInfoBarDelegate::InfoBarDismissed() {
186 RecordHistogramAction(DISMISSED);
187 button_pressed_ = true;
190 AutoLoginInfoBarDelegate*
191 AutoLoginInfoBarDelegate::AsAutoLoginInfoBarDelegate() {
192 return this;
195 base::string16 AutoLoginInfoBarDelegate::GetMessageText() const {
196 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE,
197 base::UTF8ToUTF16(params_.username));
200 base::string16 AutoLoginInfoBarDelegate::GetButtonLabel(
201 InfoBarButton button) const {
202 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
203 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON);
206 bool AutoLoginInfoBarDelegate::Accept() {
207 // AutoLoginRedirector deletes itself.
208 content::WebContents* web_contents =
209 InfoBarService::WebContentsFromInfoBar(infobar());
210 new AutoLoginRedirector(web_contents, params_.header.args);
211 RecordHistogramAction(ACCEPTED);
212 button_pressed_ = true;
213 return true;
216 bool AutoLoginInfoBarDelegate::Cancel() {
217 content::WebContents* web_contents =
218 InfoBarService::WebContentsFromInfoBar(infobar());
219 PrefService* pref_service = Profile::FromBrowserContext(
220 web_contents->GetBrowserContext())->GetPrefs();
221 pref_service->SetBoolean(prefs::kAutologinEnabled, false);
222 RecordHistogramAction(REJECTED);
223 button_pressed_ = true;
224 return true;
227 void AutoLoginInfoBarDelegate::GoogleSignedOut(
228 const std::string& account_id,
229 const std::string& username) {
230 infobar()->RemoveSelf();