| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h" | 5 #include "chrome/browser/captive_portal/captive_portal_tab_helper.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "chrome/browser/captive_portal/captive_portal_login_detector.h" | 8 #include "chrome/browser/captive_portal/captive_portal_login_detector.h" |
| 9 #include "chrome/browser/captive_portal/captive_portal_service_factory.h" | 9 #include "chrome/browser/captive_portal/captive_portal_service_factory.h" |
| 10 #include "chrome/browser/captive_portal/captive_portal_tab_reloader.h" | 10 #include "chrome/browser/captive_portal/captive_portal_tab_reloader.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 #include "content/public/browser/resource_request_details.h" | 24 #include "content/public/browser/resource_request_details.h" |
| 25 #include "content/public/browser/web_contents.h" | 25 #include "content/public/browser/web_contents.h" |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/ssl/ssl_info.h" | 27 #include "net/ssl/ssl_info.h" |
| 28 | 28 |
| 29 using captive_portal::CaptivePortalResult; | 29 using captive_portal::CaptivePortalResult; |
| 30 using content::ResourceType; | 30 using content::ResourceType; |
| 31 | 31 |
| 32 DEFINE_WEB_CONTENTS_USER_DATA_KEY(CaptivePortalTabHelper); | 32 DEFINE_WEB_CONTENTS_USER_DATA_KEY(CaptivePortalTabHelper); |
| 33 | 33 |
| 34 // The delay before displaying the SSL interstitial for cert errors. |
| 35 // - If a "captive portal detected" result arrives in this many seconds, |
| 36 // a captive portal interstitial is displayed. |
| 37 // - Otherwise, an SSL interstitial is displayed. |
| 38 const int kDefaultSSLInterstitialDisplayDelay = 2; |
| 39 |
| 34 CaptivePortalTabHelper::CaptivePortalTabHelper( | 40 CaptivePortalTabHelper::CaptivePortalTabHelper( |
| 35 content::WebContents* web_contents) | 41 content::WebContents* web_contents) |
| 36 : content::WebContentsObserver(web_contents), | 42 : content::WebContentsObserver(web_contents), |
| 37 // web_contents is NULL in unit tests. | 43 // web_contents is NULL in unit tests. |
| 38 profile_(web_contents ? Profile::FromBrowserContext( | 44 profile_(web_contents ? Profile::FromBrowserContext( |
| 39 web_contents->GetBrowserContext()) | 45 web_contents->GetBrowserContext()) |
| 40 : NULL), | 46 : NULL), |
| 41 tab_reloader_( | 47 tab_reloader_( |
| 42 new CaptivePortalTabReloader( | 48 new CaptivePortalTabReloader( |
| 43 profile_, | 49 profile_, |
| 44 web_contents, | 50 web_contents, |
| 45 base::Bind(&CaptivePortalTabHelper::OpenLoginTab, | 51 base::Bind(&CaptivePortalTabHelper::OpenLoginTabForWebContents, |
| 46 base::Unretained(this)))), | 52 web_contents, false))), |
| 47 login_detector_(new CaptivePortalLoginDetector(profile_)), | 53 login_detector_(new CaptivePortalLoginDetector(profile_)), |
| 48 web_contents_(web_contents), | 54 web_contents_(web_contents), |
| 49 pending_error_code_(net::OK), | 55 pending_error_code_(net::OK), |
| 56 ssl_error_delay_( |
| 57 base::TimeDelta::FromSeconds(kDefaultSSLInterstitialDisplayDelay)), |
| 50 provisional_render_view_host_(NULL) { | 58 provisional_render_view_host_(NULL) { |
| 51 registrar_.Add(this, | 59 registrar_.Add(this, |
| 52 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, | 60 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| 53 content::Source<Profile>(profile_)); | 61 content::Source<Profile>(profile_)); |
| 54 registrar_.Add(this, | 62 registrar_.Add(this, |
| 55 content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT, | 63 content::NOTIFICATION_RESOURCE_RECEIVED_REDIRECT, |
| 56 content::Source<content::WebContents>(web_contents)); | 64 content::Source<content::WebContents>(web_contents)); |
| 57 } | 65 } |
| 58 | 66 |
| 59 CaptivePortalTabHelper::~CaptivePortalTabHelper() { | 67 CaptivePortalTabHelper::~CaptivePortalTabHelper() { |
| (...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 186 } | 194 } |
| 187 | 195 |
| 188 void CaptivePortalTabHelper::OnSSLCertError(const net::SSLInfo& ssl_info) { | 196 void CaptivePortalTabHelper::OnSSLCertError(const net::SSLInfo& ssl_info) { |
| 189 tab_reloader_->OnSSLCertError(ssl_info); | 197 tab_reloader_->OnSSLCertError(ssl_info); |
| 190 } | 198 } |
| 191 | 199 |
| 192 bool CaptivePortalTabHelper::IsLoginTab() const { | 200 bool CaptivePortalTabHelper::IsLoginTab() const { |
| 193 return login_detector_->is_login_tab(); | 201 return login_detector_->is_login_tab(); |
| 194 } | 202 } |
| 195 | 203 |
| 204 base::TimeDelta CaptivePortalTabHelper::GetSSLErrorDelay() const { |
| 205 return ssl_error_delay_; |
| 206 } |
| 207 |
| 208 // static |
| 209 void CaptivePortalTabHelper::OpenLoginTabForWebContents( |
| 210 content::WebContents* web_contents, |
| 211 bool focus) { |
| 212 Browser* browser = chrome::FindBrowserWithWebContents(web_contents); |
| 213 |
| 214 // If the Profile doesn't have a tabbed browser window open, do nothing. |
| 215 if (!browser) |
| 216 return; |
| 217 |
| 218 // Check if the Profile's topmost browser window already has a login tab. |
| 219 // If so, do nothing. |
| 220 // TODO(mmenke): Consider focusing that tab, at least if this is the tab |
| 221 // helper for the currently active tab for the profile. |
| 222 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { |
| 223 content::WebContents* contents = |
| 224 browser->tab_strip_model()->GetWebContentsAt(i); |
| 225 CaptivePortalTabHelper* captive_portal_tab_helper = |
| 226 CaptivePortalTabHelper::FromWebContents(contents); |
| 227 if (captive_portal_tab_helper->IsLoginTab()) { |
| 228 if (focus) |
| 229 browser->tab_strip_model()->ActivateTabAt(i, false); |
| 230 return; |
| 231 } |
| 232 } |
| 233 |
| 234 // Otherwise, open a login tab. Only end up here when a captive portal result |
| 235 // was received, so it's safe to assume profile has a CaptivePortalService. |
| 236 content::WebContents* new_contents = chrome::AddSelectedTabWithURL( |
| 237 browser, |
| 238 CaptivePortalServiceFactory::GetForProfile( |
| 239 browser->profile())->test_url(), |
| 240 ui::PAGE_TRANSITION_TYPED); |
| 241 CaptivePortalTabHelper* captive_portal_tab_helper = |
| 242 CaptivePortalTabHelper::FromWebContents(new_contents); |
| 243 captive_portal_tab_helper->SetIsLoginTab(); |
| 244 } |
| 245 |
| 196 void CaptivePortalTabHelper::OnRedirect(int child_id, | 246 void CaptivePortalTabHelper::OnRedirect(int child_id, |
| 197 ResourceType resource_type, | 247 ResourceType resource_type, |
| 198 const GURL& new_url) { | 248 const GURL& new_url) { |
| 199 // Only main frame redirects for the provisional RenderViewHost matter. | 249 // Only main frame redirects for the provisional RenderViewHost matter. |
| 200 if (resource_type != content::RESOURCE_TYPE_MAIN_FRAME || | 250 if (resource_type != content::RESOURCE_TYPE_MAIN_FRAME || |
| 201 !provisional_render_view_host_ || | 251 !provisional_render_view_host_ || |
| 202 provisional_render_view_host_->GetProcess()->GetID() != child_id) { | 252 provisional_render_view_host_->GetProcess()->GetID() != child_id) { |
| 203 return; | 253 return; |
| 204 } | 254 } |
| 205 | 255 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 224 | 274 |
| 225 void CaptivePortalTabHelper::SetIsLoginTab() { | 275 void CaptivePortalTabHelper::SetIsLoginTab() { |
| 226 login_detector_->SetIsLoginTab(); | 276 login_detector_->SetIsLoginTab(); |
| 227 } | 277 } |
| 228 | 278 |
| 229 void CaptivePortalTabHelper::SetTabReloaderForTest( | 279 void CaptivePortalTabHelper::SetTabReloaderForTest( |
| 230 CaptivePortalTabReloader* tab_reloader) { | 280 CaptivePortalTabReloader* tab_reloader) { |
| 231 tab_reloader_.reset(tab_reloader); | 281 tab_reloader_.reset(tab_reloader); |
| 232 } | 282 } |
| 233 | 283 |
| 284 void CaptivePortalTabHelper::SetSSLErrorDelayForTest( |
| 285 base::TimeDelta ssl_error_delay) { |
| 286 ssl_error_delay_ = ssl_error_delay; |
| 287 } |
| 288 |
| 234 CaptivePortalTabReloader* CaptivePortalTabHelper::GetTabReloaderForTest() { | 289 CaptivePortalTabReloader* CaptivePortalTabHelper::GetTabReloaderForTest() { |
| 235 return tab_reloader_.get(); | 290 return tab_reloader_.get(); |
| 236 } | 291 } |
| 237 | |
| 238 void CaptivePortalTabHelper::OpenLoginTab() { | |
| 239 Browser* browser = chrome::FindBrowserWithWebContents(web_contents_); | |
| 240 | |
| 241 // If the Profile doesn't have a tabbed browser window open, do nothing. | |
| 242 if (!browser) | |
| 243 return; | |
| 244 | |
| 245 // Check if the Profile's topmost browser window already has a login tab. | |
| 246 // If so, do nothing. | |
| 247 // TODO(mmenke): Consider focusing that tab, at least if this is the tab | |
| 248 // helper for the currently active tab for the profile. | |
| 249 for (int i = 0; i < browser->tab_strip_model()->count(); ++i) { | |
| 250 content::WebContents* web_contents = | |
| 251 browser->tab_strip_model()->GetWebContentsAt(i); | |
| 252 CaptivePortalTabHelper* captive_portal_tab_helper = | |
| 253 CaptivePortalTabHelper::FromWebContents(web_contents); | |
| 254 if (captive_portal_tab_helper->IsLoginTab()) | |
| 255 return; | |
| 256 } | |
| 257 | |
| 258 // Otherwise, open a login tab. Only end up here when a captive portal result | |
| 259 // was received, so it's safe to assume |profile_| has a CaptivePortalService. | |
| 260 content::WebContents* web_contents = chrome::AddSelectedTabWithURL( | |
| 261 browser, | |
| 262 CaptivePortalServiceFactory::GetForProfile(profile_)->test_url(), | |
| 263 ui::PAGE_TRANSITION_TYPED); | |
| 264 CaptivePortalTabHelper* captive_portal_tab_helper = | |
| 265 CaptivePortalTabHelper::FromWebContents(web_contents); | |
| 266 captive_portal_tab_helper->SetIsLoginTab(); | |
| 267 } | |
| OLD | NEW |