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 "base/bind.h" |
| 8 #include "chrome/browser/captive_portal/captive_portal_login_detector.h" |
| 9 #include "chrome/browser/captive_portal/captive_portal_service.h" |
| 10 #include "chrome/browser/captive_portal/captive_portal_service_factory.h" |
| 11 #include "chrome/browser/ui/browser.h" |
| 12 #include "chrome/browser/ui/browser_finder.h" |
| 13 #include "chrome/browser/ui/tab_contents/tab_contents.h" |
| 14 #include "chrome/common/chrome_notification_types.h" |
| 15 #include "content/public/browser/notification_details.h" |
| 16 #include "content/public/browser/notification_source.h" |
| 17 #include "net/base/net_errors.h" |
| 18 |
| 19 namespace captive_portal { |
| 20 |
| 21 CaptivePortalTabHelper::CaptivePortalTabHelper( |
| 22 Profile* profile, |
| 23 content::WebContents* web_contents) |
| 24 : content::WebContentsObserver(web_contents), |
| 25 tab_reloader_( |
| 26 new CaptivePortalTabReloader( |
| 27 profile, |
| 28 web_contents, |
| 29 base::Bind(&CaptivePortalTabHelper::OpenLoginTab, |
| 30 base::Unretained(this)))), |
| 31 login_detector_(new CaptivePortalLoginDetector(profile)), |
| 32 profile_(profile), |
| 33 pending_error_code_(net::OK) { |
| 34 registrar_.Add(this, |
| 35 chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, |
| 36 content::Source<Profile>(profile_)); |
| 37 } |
| 38 |
| 39 CaptivePortalTabHelper::~CaptivePortalTabHelper() { |
| 40 } |
| 41 |
| 42 void CaptivePortalTabHelper::DidStartProvisionalLoadForFrame( |
| 43 int64 frame_id, |
| 44 bool is_main_frame, |
| 45 const GURL& validated_url, |
| 46 bool is_error_page, |
| 47 content::RenderViewHost* render_view_host) { |
| 48 DCHECK(CalledOnValidThread()); |
| 49 |
| 50 // Ignore subframes. |
| 51 if (!is_main_frame) |
| 52 return; |
| 53 |
| 54 // If loading an error page for a previous failure, treat this as part of |
| 55 // the previous load. The second check is needed because Link Doctor pages |
| 56 // result in two error page provisional loads in a row. Currently, the |
| 57 // second load is treated as a normal load, rather than reusing old error |
| 58 // codes. |
| 59 if (is_error_page && pending_error_code_ != net::OK) |
| 60 return; |
| 61 |
| 62 // Makes the second load for Link Doctor pages act as a normal load. |
| 63 // TODO(mmenke): Figure out if this affects any other cases. |
| 64 pending_error_code_ = net::OK; |
| 65 |
| 66 tab_reloader_->OnLoadStart(validated_url.SchemeIsSecure()); |
| 67 } |
| 68 |
| 69 void CaptivePortalTabHelper::DidCommitProvisionalLoadForFrame( |
| 70 int64 frame_id, |
| 71 bool is_main_frame, |
| 72 const GURL& url, |
| 73 content::PageTransition transition_type, |
| 74 content::RenderViewHost* render_view_host) { |
| 75 DCHECK(CalledOnValidThread()); |
| 76 |
| 77 // Ignore subframes. |
| 78 if (!is_main_frame) |
| 79 return; |
| 80 |
| 81 tab_reloader_->OnLoadCommitted(pending_error_code_); |
| 82 pending_error_code_ = net::OK; |
| 83 } |
| 84 |
| 85 void CaptivePortalTabHelper::DidFailProvisionalLoad( |
| 86 int64 frame_id, |
| 87 bool is_main_frame, |
| 88 const GURL& validated_url, |
| 89 int error_code, |
| 90 const string16& error_description, |
| 91 content::RenderViewHost* render_view_host) { |
| 92 DCHECK(CalledOnValidThread()); |
| 93 |
| 94 // Ignore subframes. |
| 95 if (!is_main_frame) |
| 96 return; |
| 97 |
| 98 // Aborts generally aren't followed by loading an error page, so go ahead and |
| 99 // reset the state now, to prevent any captive portal checks from triggering. |
| 100 if (error_code == net::ERR_ABORTED) { |
| 101 // May have been aborting the load of an error page. |
| 102 pending_error_code_ = net::OK; |
| 103 |
| 104 tab_reloader_->OnAbort(); |
| 105 return; |
| 106 } |
| 107 |
| 108 pending_error_code_ = error_code; |
| 109 } |
| 110 |
| 111 void CaptivePortalTabHelper::DidStopLoading() { |
| 112 DCHECK(CalledOnValidThread()); |
| 113 |
| 114 login_detector_->OnStoppedLoading(); |
| 115 } |
| 116 |
| 117 void CaptivePortalTabHelper::Observe( |
| 118 int type, |
| 119 const content::NotificationSource& source, |
| 120 const content::NotificationDetails& details) { |
| 121 DCHECK(CalledOnValidThread()); |
| 122 DCHECK_EQ(chrome::NOTIFICATION_CAPTIVE_PORTAL_CHECK_RESULT, type); |
| 123 DCHECK_EQ(profile_, content::Source<Profile>(source).ptr()); |
| 124 |
| 125 CaptivePortalService::Results* results = |
| 126 content::Details<CaptivePortalService::Results>(details).ptr(); |
| 127 |
| 128 tab_reloader_->OnCaptivePortalResults(results->previous_result, |
| 129 results->result); |
| 130 login_detector_->OnCaptivePortalResults(results->previous_result, |
| 131 results->result); |
| 132 } |
| 133 |
| 134 bool CaptivePortalTabHelper::IsLoginTab() const { |
| 135 return login_detector_->is_login_tab(); |
| 136 } |
| 137 |
| 138 void CaptivePortalTabHelper::SetIsLoginTab() { |
| 139 login_detector_->set_is_login_tab(); |
| 140 } |
| 141 |
| 142 void CaptivePortalTabHelper::SetTabReloaderForTest( |
| 143 CaptivePortalTabReloader* tab_reloader) { |
| 144 tab_reloader_.reset(tab_reloader); |
| 145 } |
| 146 |
| 147 CaptivePortalTabReloader* CaptivePortalTabHelper::GetTabReloaderForTest() { |
| 148 return tab_reloader_.get(); |
| 149 } |
| 150 |
| 151 void CaptivePortalTabHelper::OpenLoginTab() { |
| 152 Browser* browser = browser::FindTabbedBrowser(profile_, true); |
| 153 // If the Profile doesn't have a tabbed browser window open, do nothing. |
| 154 if (!browser) |
| 155 return; |
| 156 |
| 157 // Check if the Profile's topmost browser window already has a login tab. |
| 158 // If so, do nothing. |
| 159 // TODO(mmenke): Consider focusing that tab, at least if this is the tab |
| 160 // helper for the currently active tab for the profile. |
| 161 for (int i = 0; i < browser->tab_count(); ++i) { |
| 162 TabContents* tab_contents = browser->GetTabContentsWrapperAt(i); |
| 163 if (tab_contents->captive_portal_tab_helper()->IsLoginTab()) |
| 164 return; |
| 165 } |
| 166 |
| 167 // Otherwise, open a login tab. Only end up here when a captive portal result |
| 168 // was received, so it's safe to assume |profile_| has a CaptivePortalService. |
| 169 TabContents* tab_contents = |
| 170 browser->AddSelectedTabWithURL( |
| 171 CaptivePortalServiceFactory::GetForProfile(profile_)->test_url(), |
| 172 content::PAGE_TRANSITION_TYPED); |
| 173 tab_contents->captive_portal_tab_helper()->SetIsLoginTab(); |
| 174 } |
| 175 |
| 176 } // namespace captive_portal |
OLD | NEW |