Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(94)

Side by Side Diff: chrome/browser/ui/android/infobars/auto_login_infobar_delegate.cc

Issue 208393015: Revert "Remove desktop auto-login since we don't plan on shipping it there. Refactor the base and … (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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/android/infobars/auto_login_infobar_delegate.h"
6
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_helper.h"
9 #include "base/android/jni_string.h"
10 #include "base/bind.h"
11 #include "base/logging.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/metrics/histogram.h"
14 #include "base/prefs/pref_service.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/google/google_util.h"
18 #include "chrome/browser/infobars/infobar.h"
19 #include "chrome/browser/infobars/infobar_service.h"
20 #include "chrome/browser/infobars/simple_alert_infobar_delegate.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
23 #include "chrome/browser/signin/signin_manager_factory.h"
24 #include "chrome/browser/ui/sync/sync_promo_ui.h"
25 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/pref_names.h"
27 #include "chrome/common/url_constants.h"
28 #include "components/signin/core/profile_oauth2_token_service.h"
29 #include "content/public/browser/navigation_controller.h"
30 #include "content/public/browser/page_navigator.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/browser/web_contents_observer.h"
33 #include "content/public/common/referrer.h"
34 #include "google_apis/gaia/gaia_constants.h"
35 #include "google_apis/gaia/gaia_urls.h"
36 #include "google_apis/gaia/ubertoken_fetcher.h"
37 #include "grit/chromium_strings.h"
38 #include "grit/generated_resources.h"
39 #include "grit/theme_resources.h"
40 #include "jni/AutoLoginDelegate_jni.h"
41 #include "net/base/escape.h"
42 #include "net/url_request/url_request.h"
43 #include "ui/base/l10n/l10n_util.h"
44
45 using base::android::ConvertUTF8ToJavaString;
46 using base::android::ScopedJavaLocalRef;
47
48
49 // AutoLoginRedirector --------------------------------------------------------
50
51 namespace {
52
53 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to
54 // auto-login. It holds context information needed while re-issuing service
55 // tokens using the OAuth2TokenService, gets the browser cookies with the
56 // TokenAuth API, and finally redirects the user to the correct page.
57 class AutoLoginRedirector : public UbertokenConsumer,
58 public content::WebContentsObserver {
59 public:
60 AutoLoginRedirector(content::WebContents* web_contents,
61 const std::string& args);
62 virtual ~AutoLoginRedirector();
63
64 private:
65 // Overriden from UbertokenConsumer:
66 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE;
67 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE;
68
69 // Implementation of content::WebContentsObserver
70 virtual void WebContentsDestroyed(
71 content::WebContents* web_contents) OVERRIDE;
72
73 // Redirect tab to MergeSession URL, logging the user in and navigating
74 // to the desired page.
75 void RedirectToMergeSession(const std::string& token);
76
77 const std::string args_;
78 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_;
79
80 DISALLOW_COPY_AND_ASSIGN(AutoLoginRedirector);
81 };
82
83 AutoLoginRedirector::AutoLoginRedirector(
84 content::WebContents* web_contents,
85 const std::string& args)
86 : content::WebContentsObserver(web_contents),
87 args_(args) {
88 Profile* profile =
89 Profile::FromBrowserContext(web_contents->GetBrowserContext());
90 ProfileOAuth2TokenService* token_service =
91 ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
92 SigninManagerBase* signin_manager =
93 SigninManagerFactory::GetInstance()->GetForProfile(profile);
94 ubertoken_fetcher_.reset(new UbertokenFetcher(token_service,
95 this,
96 profile->GetRequestContext()));
97 ubertoken_fetcher_->StartFetchingToken(
98 signin_manager->GetAuthenticatedAccountId());
99 }
100
101 AutoLoginRedirector::~AutoLoginRedirector() {
102 }
103
104 void AutoLoginRedirector::WebContentsDestroyed(
105 content::WebContents* web_contents) {
106 // The WebContents that started this has been destroyed. The request must be
107 // cancelled and this object must be deleted.
108 ubertoken_fetcher_.reset();
109 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
110 }
111
112 void AutoLoginRedirector::OnUbertokenSuccess(const std::string& token) {
113 RedirectToMergeSession(token);
114 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
115 }
116
117 void AutoLoginRedirector::OnUbertokenFailure(
118 const GoogleServiceAuthError& error) {
119 LOG(WARNING) << "AutoLoginRedirector: token request failed";
120 base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
121 }
122
123 void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) {
124 // TODO(rogerta): what is the correct page transition?
125 web_contents()->GetController().LoadURL(
126 GaiaUrls::GetInstance()->merge_session_url().Resolve(
127 "?source=chrome&uberauth=" + token + "&" + args_),
128 content::Referrer(), content::PAGE_TRANSITION_AUTO_BOOKMARK,
129 std::string());
130 }
131
132 } // namespace
133
134
135 // AutoLoginInfoBarDelegate ---------------------------------------------------
136
137 // static
138 bool AutoLoginInfoBarDelegate::Create(content::WebContents* web_contents,
139 const Params& params) {
140 // If |web_contents| is hosted in a WebDialog, there may be no infobar
141 // service.
142 InfoBarService* infobar_service =
143 InfoBarService::FromWebContents(web_contents);
144 if (!infobar_service)
145 return false;
146
147 Profile* profile =
148 Profile::FromBrowserContext(web_contents->GetBrowserContext());
149 return !!infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
150 scoped_ptr<ConfirmInfoBarDelegate>(
151 new AutoLoginInfoBarDelegate(params, profile))));
152 }
153
154 AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate(const Params& params,
155 Profile* profile)
156 : ConfirmInfoBarDelegate(),
157 params_(params),
158 profile_(profile),
159 button_pressed_(false) {
160 RecordHistogramAction(SHOWN);
161
162 // The AutoLogin infobar is shown in incognito mode on Android, so a
163 // SigninManager isn't guaranteed to exist for |profile_|.
164 SigninManagerBase* signin_manager =
165 SigninManagerFactory::GetInstance()->GetForProfile(profile_);
166 if (signin_manager)
167 signin_manager->AddObserver(this);
168 }
169
170 AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() {
171 // The AutoLogin infobar is shown in incognito mode on Android, so a
172 // SigninManager isn't guaranteed to exist for |profile_|.
173 SigninManagerBase* signin_manager =
174 SigninManagerFactory::GetInstance()->GetForProfile(profile_);
175 if (signin_manager)
176 signin_manager->RemoveObserver(this);
177
178 if (!button_pressed_)
179 RecordHistogramAction(IGNORED);
180 }
181
182 bool AutoLoginInfoBarDelegate::AttachAccount(
183 JavaObjectWeakGlobalRef weak_java_auto_login_delegate) {
184 weak_java_auto_login_delegate_ = weak_java_auto_login_delegate;
185 JNIEnv* env = base::android::AttachCurrentThread();
186 ScopedJavaLocalRef<jstring> jrealm = ConvertUTF8ToJavaString(env, realm());
187 ScopedJavaLocalRef<jstring> jaccount =
188 ConvertUTF8ToJavaString(env, account());
189 ScopedJavaLocalRef<jstring> jargs = ConvertUTF8ToJavaString(env, args());
190 DCHECK(!jrealm.is_null());
191 DCHECK(!jaccount.is_null());
192 DCHECK(!jargs.is_null());
193
194 ScopedJavaLocalRef<jobject> delegate =
195 weak_java_auto_login_delegate_.get(env);
196 DCHECK(delegate.obj());
197 user_ = base::android::ConvertJavaStringToUTF8(
198 Java_AutoLoginDelegate_initializeAccount(
199 env, delegate.obj(), reinterpret_cast<intptr_t>(this), jrealm.obj(),
200 jaccount.obj(), jargs.obj()));
201 return !user_.empty();
202 }
203
204 void AutoLoginInfoBarDelegate::LoginSuccess(JNIEnv* env,
205 jobject obj,
206 jstring result) {
207 if (!infobar()->owner())
208 return; // We're closing; don't call anything, it might access the owner.
209
210 // TODO(miguelg): Test whether the Stop() and RemoveInfoBar() calls here are
211 // necessary, or whether OpenURL() will do this for us.
212 content::WebContents* contents = web_contents();
213 contents->Stop();
214 infobar()->RemoveSelf();
215 // WARNING: |this| may be deleted at this point! Do not access any members!
216 contents->OpenURL(content::OpenURLParams(
217 GURL(base::android::ConvertJavaStringToUTF8(env, result)),
218 content::Referrer(), CURRENT_TAB, content::PAGE_TRANSITION_AUTO_BOOKMARK,
219 false));
220 }
221
222 void AutoLoginInfoBarDelegate::LoginFailed(JNIEnv* env, jobject obj) {
223 if (!infobar()->owner())
224 return; // We're closing; don't call anything, it might access the owner.
225
226 // TODO(miguelg): Using SimpleAlertInfoBarDelegate::Create() animates in a new
227 // infobar while we animate the current one closed. It would be better to use
228 // ReplaceInfoBar().
229 SimpleAlertInfoBarDelegate::Create(
230 infobar()->owner(), IDR_INFOBAR_WARNING,
231 l10n_util::GetStringUTF16(IDS_AUTO_LOGIN_FAILED), false);
232 infobar()->RemoveSelf();
233 }
234
235 void AutoLoginInfoBarDelegate::LoginDismiss(JNIEnv* env, jobject obj) {
236 infobar()->RemoveSelf();
237 }
238
239 bool AutoLoginInfoBarDelegate::Register(JNIEnv* env) {
240 return RegisterNativesImpl(env);
241 }
242
243 void AutoLoginInfoBarDelegate::InfoBarDismissed() {
244 RecordHistogramAction(DISMISSED);
245 button_pressed_ = true;
246 }
247
248 int AutoLoginInfoBarDelegate::GetIconID() const {
249 return IDR_INFOBAR_AUTOLOGIN;
250 }
251
252 InfoBarDelegate::Type AutoLoginInfoBarDelegate::GetInfoBarType() const {
253 return PAGE_ACTION_TYPE;
254 }
255
256 AutoLoginInfoBarDelegate*
257 AutoLoginInfoBarDelegate::AsAutoLoginInfoBarDelegate() {
258 return this;
259 }
260
261 base::string16 AutoLoginInfoBarDelegate::GetMessageText() const {
262 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE,
263 base::UTF8ToUTF16(user_));
264 }
265
266 base::string16 AutoLoginInfoBarDelegate::GetButtonLabel(
267 InfoBarButton button) const {
268 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
269 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON);
270 }
271
272 bool AutoLoginInfoBarDelegate::Accept() {
273 JNIEnv* env = base::android::AttachCurrentThread();
274 ScopedJavaLocalRef<jobject> delegate =
275 weak_java_auto_login_delegate_.get(env);
276 DCHECK(delegate.obj());
277 Java_AutoLoginDelegate_logIn(env, delegate.obj(),
278 reinterpret_cast<intptr_t>(this));
279 // Do not close the infobar on accept, it will be closed as part
280 // of the log in callback.
281 return false;
282 }
283
284 bool AutoLoginInfoBarDelegate::Cancel() {
285 JNIEnv* env = base::android::AttachCurrentThread();
286 ScopedJavaLocalRef<jobject> delegate =
287 weak_java_auto_login_delegate_.get(env);
288 DCHECK(delegate.obj());
289 Java_AutoLoginDelegate_cancelLogIn(env, delegate.obj(),
290 reinterpret_cast<intptr_t>(this));
291 return true;
292 }
293
294 void AutoLoginInfoBarDelegate::GoogleSignedOut(const std::string& username) {
295 infobar()->RemoveSelf();
296 }
297
298 void AutoLoginInfoBarDelegate::RecordHistogramAction(Actions action) {
299 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Regular", action,
300 HISTOGRAM_BOUNDING_VALUE);
301 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698