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