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/captive_portal/captive_portal_tab_helper.h" |
| 6 |
| 7 #include "chrome/browser/captive_portal/captive_portal_login_detector.h" |
| 8 #include "chrome/browser/captive_portal/captive_portal_service.h" |
| 9 #include "chrome/browser/captive_portal/captive_portal_service_factory.h" |
| 10 #include "chrome/common/chrome_notification_types.h" |
| 11 #include "content/public/browser/notification_details.h" |
| 12 #include "content/public/browser/notification_source.h" |
| 13 #include "net/base/net_errors.h" |
| 14 |
| 15 namespace captive_portal { |
| 16 |
| 17 CaptivePortalTabHelper::CaptivePortalTabHelper( |
| 18 Profile* profile, |
| 19 content::WebContents* web_contents) |
| 20 : content::WebContentsObserver(web_contents), |
| 21 tab_reloader_(new CaptivePortalTabReloader(profile, web_contents)), |
| 22 login_detector_(new CaptivePortalLoginDetector(profile)), |
| 23 profile_(profile), |
| 24 pending_error_code_(net::OK) { |
| 25 registrar_.Add(this, |
| 26 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| 27 content::Source<Profile>(profile_)); |
| 28 } |
| 29 |
| 30 CaptivePortalTabHelper::~CaptivePortalTabHelper() { |
| 31 } |
| 32 |
| 33 void CaptivePortalTabHelper::DidStartProvisionalLoadForFrame( |
| 34 int64 frame_id, |
| 35 bool is_main_frame, |
| 36 const GURL& validated_url, |
| 37 bool is_error_page, |
| 38 content::RenderViewHost* render_view_host) { |
| 39 DCHECK(CalledOnValidThread()); |
| 40 |
| 41 // Ignore subframes. |
| 42 if (!is_main_frame) |
| 43 return; |
| 44 |
| 45 // If loading an error page for a previous failure, treat this as part of |
| 46 // the previous load. The second check is needed because Link Doctor pages |
| 47 // result in two error page provisional loads in a row. Currently, the |
| 48 // second load is treated as a normal load, rather than reusing old error |
| 49 // codes. |
| 50 if (is_error_page && pending_error_code_ != net::OK) |
| 51 return; |
| 52 |
| 53 // Makes the second load for Link Doctor pages act as a normal load. |
| 54 // TODO(mmenke): Figure out if this affects any other cases. |
| 55 pending_error_code_ = net::OK; |
| 56 |
| 57 tab_reloader_->OnLoadStart(validated_url.SchemeIsSecure()); |
| 58 } |
| 59 |
| 60 void CaptivePortalTabHelper::DidCommitProvisionalLoadForFrame( |
| 61 int64 frame_id, |
| 62 bool is_main_frame, |
| 63 const GURL& url, |
| 64 content::PageTransition transition_type, |
| 65 content::RenderViewHost* render_view_host) { |
| 66 DCHECK(CalledOnValidThread()); |
| 67 |
| 68 // Ignore subframes. |
| 69 if (!is_main_frame) |
| 70 return; |
| 71 |
| 72 tab_reloader_->OnLoadCommitted(pending_error_code_); |
| 73 pending_error_code_ = net::OK; |
| 74 } |
| 75 |
| 76 void CaptivePortalTabHelper::DidFailProvisionalLoad( |
| 77 int64 frame_id, |
| 78 bool is_main_frame, |
| 79 const GURL& validated_url, |
| 80 int error_code, |
| 81 const string16& error_description, |
| 82 content::RenderViewHost* render_view_host) { |
| 83 DCHECK(CalledOnValidThread()); |
| 84 |
| 85 // Ignore subframes. |
| 86 if (!is_main_frame) |
| 87 return; |
| 88 |
| 89 // Aborts generally aren't followed by loading an error page, so go ahead and |
| 90 // reset the state now, to prevent any captive portal checks from triggering. |
| 91 if (error_code == net::ERR_ABORTED) { |
| 92 // May have been aborting the load of an error page. |
| 93 pending_error_code_ = net::OK; |
| 94 |
| 95 tab_reloader_->OnAbort(); |
| 96 return; |
| 97 } |
| 98 |
| 99 pending_error_code_ = error_code; |
| 100 } |
| 101 |
| 102 void CaptivePortalTabHelper::DidStopLoading() { |
| 103 DCHECK(CalledOnValidThread()); |
| 104 |
| 105 login_detector_->OnStoppedLoading(); |
| 106 } |
| 107 |
| 108 void CaptivePortalTabHelper::Observe( |
| 109 int type, |
| 110 const content::NotificationSource& source, |
| 111 const content::NotificationDetails& details) { |
| 112 DCHECK(CalledOnValidThread()); |
| 113 DCHECK_EQ(chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, type); |
| 114 DCHECK_EQ(profile_, content::Source<Profile>(source).ptr()); |
| 115 |
| 116 CaptivePortalService::Results* results = |
| 117 content::Details<CaptivePortalService::Results>(details).ptr(); |
| 118 |
| 119 tab_reloader_->OnCaptivePortalResults(results->previous_result, |
| 120 results->result); |
| 121 login_detector_->OnCaptivePortalResults(results->previous_result, |
| 122 results->result); |
| 123 } |
| 124 |
| 125 bool CaptivePortalTabHelper::IsLoginTab() const { |
| 126 return login_detector_->is_login_tab(); |
| 127 } |
| 128 |
| 129 void CaptivePortalTabHelper::SetIsLoginTab() { |
| 130 login_detector_->set_is_login_tab(); |
| 131 } |
| 132 |
| 133 void CaptivePortalTabHelper::SetTabReloaderForTest( |
| 134 CaptivePortalTabReloader* tab_reloader) { |
| 135 tab_reloader_.reset(tab_reloader); |
| 136 } |
| 137 |
| 138 CaptivePortalTabReloader* CaptivePortalTabHelper::GetTabReloaderForTest() { |
| 139 return tab_reloader_.get(); |
| 140 } |
| 141 |
| 142 } // namespace captive_portal |
OLD | NEW |