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_info_bar_delegate.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/logging.h" | |
9 #include "base/message_loop.h" | |
10 #include "base/metrics/histogram.h" | |
11 #include "base/prefs/pref_service.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/browser/google/google_util.h" | |
15 #include "chrome/browser/infobars/confirm_infobar_delegate.h" | |
16 #include "chrome/browser/infobars/infobar_service.h" | |
17 #include "chrome/browser/profiles/profile.h" | |
18 #include "chrome/browser/signin/ubertoken_fetcher.h" | |
19 #include "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" | |
20 #include "chrome/common/chrome_notification_types.h" | |
21 #include "chrome/common/chrome_switches.h" | |
22 #include "chrome/common/pref_names.h" | |
23 #include "chrome/common/url_constants.h" | |
24 #include "content/public/browser/navigation_controller.h" | |
25 #include "content/public/browser/notification_details.h" | |
26 #include "content/public/browser/notification_observer.h" | |
27 #include "content/public/browser/notification_registrar.h" | |
28 #include "content/public/browser/notification_source.h" | |
29 #include "content/public/browser/notification_types.h" | |
30 #include "content/public/browser/page_navigator.h" | |
31 #include "content/public/browser/web_contents.h" | |
32 #include "content/public/common/referrer.h" | |
33 #include "google_apis/gaia/gaia_constants.h" | |
34 #include "google_apis/gaia/gaia_urls.h" | |
35 #include "grit/chromium_strings.h" | |
36 #include "grit/generated_resources.h" | |
37 #include "grit/theme_resources.h" | |
38 #include "net/base/escape.h" | |
39 #include "net/url_request/url_request.h" | |
40 #include "ui/base/l10n/l10n_util.h" | |
41 #include "ui/base/resource/resource_bundle.h" | |
42 | |
43 using content::NavigationController; | |
44 using content::NotificationSource; | |
45 using content::NotificationDetails; | |
46 | |
47 namespace { | |
48 | |
49 // Enum values used for UMA histograms. | |
50 enum { | |
51 // The infobar was shown to the user. | |
52 HISTOGRAM_SHOWN, | |
53 | |
54 // The user pressed the accept button to perform the suggested action. | |
55 HISTOGRAM_ACCEPTED, | |
56 | |
57 // The user pressed the reject to turn off the feature. | |
58 HISTOGRAM_REJECTED, | |
59 | |
60 // The user pressed the X button to dismiss the infobar this time. | |
61 HISTOGRAM_DISMISSED, | |
62 | |
63 // The user completely ignored the infoar. Either they navigated away, or | |
64 // they used the page as is. | |
65 HISTOGRAM_IGNORED, | |
66 | |
67 // The user clicked on the learn more link in the infobar. | |
68 HISTOGRAM_LEARN_MORE, | |
69 | |
70 HISTOGRAM_MAX | |
71 }; | |
72 | |
73 // AutoLoginRedirector -------------------------------------------------------- | |
74 | |
75 // This class is created by the AutoLoginInfoBarDelegate when the user wishes to | |
76 // auto-login. It holds context information needed while re-issuing service | |
77 // tokens using the TokenService, gets the browser cookies with the TokenAuth | |
78 // API, and finally redirects the user to the correct page. | |
79 class AutoLoginRedirector : public UbertokenConsumer, | |
80 public content::NotificationObserver { | |
81 public: | |
82 AutoLoginRedirector(NavigationController* navigation_controller, | |
83 const std::string& args); | |
84 virtual ~AutoLoginRedirector(); | |
85 | |
86 private: | |
87 // Overriden from UbertokenConsumer: | |
88 virtual void OnUbertokenSuccess(const std::string& token) OVERRIDE; | |
89 virtual void OnUbertokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | |
90 | |
91 // Implementation of content::NotificationObserver | |
92 virtual void Observe(int type, | |
93 const content::NotificationSource& source, | |
94 const content::NotificationDetails& details) OVERRIDE; | |
95 | |
96 // Redirect tab to MergeSession URL, logging the user in and navigating | |
97 // to the desired page. | |
98 void RedirectToMergeSession(const std::string& token); | |
99 | |
100 NavigationController* navigation_controller_; | |
101 const std::string args_; | |
102 scoped_ptr<UbertokenFetcher> ubertoken_fetcher_; | |
103 | |
104 // For listening to NavigationController destruction. | |
105 content::NotificationRegistrar registrar_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(AutoLoginRedirector); | |
108 }; | |
109 | |
110 AutoLoginRedirector::AutoLoginRedirector( | |
111 NavigationController* navigation_controller, | |
112 const std::string& args) | |
113 : navigation_controller_(navigation_controller), | |
114 args_(args) { | |
115 ubertoken_fetcher_.reset(new UbertokenFetcher( | |
116 Profile::FromBrowserContext(navigation_controller_->GetBrowserContext()), | |
117 this)); | |
118 registrar_.Add(this, | |
119 content::NOTIFICATION_WEB_CONTENTS_DESTROYED, | |
120 content::Source<content::WebContents>( | |
121 navigation_controller_->GetWebContents())); | |
122 ubertoken_fetcher_->StartFetchingToken(); | |
123 } | |
124 | |
125 AutoLoginRedirector::~AutoLoginRedirector() { | |
126 } | |
127 | |
128 void AutoLoginRedirector::Observe(int type, | |
129 const NotificationSource& source, | |
130 const NotificationDetails& details) { | |
131 DCHECK(type == content::NOTIFICATION_WEB_CONTENTS_DESTROYED); | |
132 // The WebContents that started this has been destroyed. The request must be | |
133 // cancelled and this object must be deleted. | |
134 ubertoken_fetcher_.reset(); | |
135 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
136 } | |
137 | |
138 void AutoLoginRedirector::OnUbertokenSuccess(const std::string& token) { | |
139 RedirectToMergeSession(token); | |
140 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
141 } | |
142 | |
143 void AutoLoginRedirector::OnUbertokenFailure( | |
144 const GoogleServiceAuthError& error) { | |
145 LOG(WARNING) << "AutoLoginRedirector: token request failed"; | |
146 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | |
147 } | |
148 | |
149 void AutoLoginRedirector::RedirectToMergeSession(const std::string& token) { | |
150 // TODO(rogerta): what is the correct page transition? | |
151 navigation_controller_->LoadURL( | |
152 GURL(GaiaUrls::GetInstance()->merge_session_url() + | |
153 "?source=chrome&uberauth=" + token + "&" + args_), | |
154 content::Referrer(), content::PAGE_TRANSITION_AUTO_BOOKMARK, | |
155 std::string()); | |
156 } | |
157 | |
158 } // namepsace | |
159 | |
160 | |
161 // AutoLoginInfoBarDelegate --------------------------------------------------- | |
162 | |
163 // AutoLoginInfoBarDelegate::Params ------------------------------------------- | |
164 | |
165 AutoLoginInfoBarDelegate::Params::Params() {} | |
166 AutoLoginInfoBarDelegate::Params::~Params() {} | |
167 | |
168 // static | |
169 void AutoLoginInfoBarDelegate::Create(InfoBarService* infobar_service, | |
170 const Params& params) { | |
171 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( | |
172 new AutoLoginInfoBarDelegate(infobar_service, params))); | |
173 } | |
174 | |
175 AutoLoginInfoBarDelegate* | |
176 AutoLoginInfoBarDelegate::AsAutoLoginInfoBarDelegate() { | |
177 return this; | |
178 } | |
179 | |
180 void AutoLoginInfoBarDelegate::InfoBarDismissed() { | |
181 RecordHistogramAction(HISTOGRAM_DISMISSED); | |
182 button_pressed_ = true; | |
183 } | |
184 | |
185 gfx::Image* AutoLoginInfoBarDelegate::GetIcon() const { | |
186 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( | |
187 IDR_INFOBAR_AUTOLOGIN); | |
188 } | |
189 | |
190 InfoBarDelegate::Type AutoLoginInfoBarDelegate::GetInfoBarType() const { | |
191 return PAGE_ACTION_TYPE; | |
192 } | |
193 | |
194 string16 AutoLoginInfoBarDelegate::GetMessageText() const { | |
195 return GetMessageText(params_.username); | |
196 } | |
197 | |
198 string16 AutoLoginInfoBarDelegate::GetButtonLabel( | |
199 InfoBarButton button) const { | |
200 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? | |
201 IDS_AUTOLOGIN_INFOBAR_OK_BUTTON : IDS_AUTOLOGIN_INFOBAR_CANCEL_BUTTON); | |
202 } | |
203 | |
204 bool AutoLoginInfoBarDelegate::Accept() { | |
205 // AutoLoginRedirector deletes itself. | |
206 new AutoLoginRedirector(&owner()->GetWebContents()->GetController(), | |
207 params_.header.args); | |
208 RecordHistogramAction(HISTOGRAM_ACCEPTED); | |
209 button_pressed_ = true; | |
210 return true; | |
211 } | |
212 | |
213 bool AutoLoginInfoBarDelegate::Cancel() { | |
214 Profile* profile = Profile::FromBrowserContext( | |
215 owner()->GetWebContents()->GetBrowserContext()); | |
216 PrefService* pref_service = profile->GetPrefs(); | |
217 pref_service->SetBoolean(prefs::kAutologinEnabled, false); | |
218 RecordHistogramAction(HISTOGRAM_REJECTED); | |
219 button_pressed_ = true; | |
220 return true; | |
221 } | |
222 | |
223 string16 AutoLoginInfoBarDelegate::GetMessageText( | |
224 const std::string& username) const { | |
225 return l10n_util::GetStringFUTF16(IDS_AUTOLOGIN_INFOBAR_MESSAGE, | |
226 UTF8ToUTF16(username)); | |
227 } | |
228 | |
229 AutoLoginInfoBarDelegate::AutoLoginInfoBarDelegate( | |
230 InfoBarService* owner, | |
231 const Params& params) | |
232 : ConfirmInfoBarDelegate(owner), | |
233 params_(params), | |
234 button_pressed_(false) { | |
235 RecordHistogramAction(HISTOGRAM_SHOWN); | |
236 registrar_.Add(this, | |
237 chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, | |
238 content::Source<Profile>(Profile::FromBrowserContext( | |
239 owner->GetWebContents()->GetBrowserContext()))); | |
240 } | |
241 | |
242 AutoLoginInfoBarDelegate::~AutoLoginInfoBarDelegate() { | |
243 if (!button_pressed_) | |
244 RecordHistogramAction(HISTOGRAM_IGNORED); | |
245 } | |
246 | |
247 void AutoLoginInfoBarDelegate::RecordHistogramAction(int action) { | |
248 UMA_HISTOGRAM_ENUMERATION("AutoLogin.Regular", action, HISTOGRAM_MAX); | |
249 } | |
250 | |
251 void AutoLoginInfoBarDelegate::Observe(int type, | |
252 const NotificationSource& source, | |
253 const NotificationDetails& details) { | |
254 DCHECK_EQ(chrome::NOTIFICATION_GOOGLE_SIGNED_OUT, type); | |
255 // owner() can be NULL when InfoBarService removes us. See | |
256 // |InfoBarDelegate::clear_owner|. | |
257 if (owner()) | |
258 owner()->RemoveInfoBar(this); | |
259 } | |
OLD | NEW |