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