| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/alternate_nav_url_fetcher.h" | 5 #include "chrome/browser/alternate_nav_url_fetcher.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/infobars/infobar_tab_helper.h" | 8 #include "chrome/browser/infobars/infobar_tab_helper.h" |
| 9 #include "chrome/browser/intranet_redirect_detector.h" | 9 #include "chrome/browser/intranet_redirect_detector.h" |
| 10 #include "chrome/browser/profiles/profile.h" | 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/tab_contents/link_infobar_delegate.h" | 11 #include "chrome/browser/tab_contents/link_infobar_delegate.h" |
| 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 12 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 13 #include "chrome/common/chrome_notification_types.h" | 13 #include "chrome/common/chrome_notification_types.h" |
| 14 #include "content/public/browser/navigation_controller.h" | 14 #include "content/public/browser/navigation_controller.h" |
| 15 #include "content/public/browser/notification_service.h" | 15 #include "content/public/browser/notification_service.h" |
| 16 #include "content/public/common/url_fetcher.h" | 16 #include "content/public/common/url_fetcher.h" |
| 17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 19 #include "grit/theme_resources_standard.h" | 19 #include "grit/theme_resources_standard.h" |
| 20 #include "net/base/registry_controlled_domain.h" | 20 #include "net/base/registry_controlled_domain.h" |
| 21 #include "net/url_request/url_request.h" | 21 #include "net/url_request/url_request.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 22 #include "ui/base/l10n/l10n_util.h" |
| 23 #include "ui/base/resource/resource_bundle.h" | 23 #include "ui/base/resource/resource_bundle.h" |
| 24 | 24 |
| 25 using content::NavigationController; |
| 25 using content::OpenURLParams; | 26 using content::OpenURLParams; |
| 26 using content::Referrer; | 27 using content::Referrer; |
| 27 | 28 |
| 28 // AlternateNavInfoBarDelegate ------------------------------------------------ | 29 // AlternateNavInfoBarDelegate ------------------------------------------------ |
| 29 | 30 |
| 30 class AlternateNavInfoBarDelegate : public LinkInfoBarDelegate { | 31 class AlternateNavInfoBarDelegate : public LinkInfoBarDelegate { |
| 31 public: | 32 public: |
| 32 AlternateNavInfoBarDelegate(InfoBarTabHelper* owner, | 33 AlternateNavInfoBarDelegate(InfoBarTabHelper* owner, |
| 33 const GURL& alternate_nav_url); | 34 const GURL& alternate_nav_url); |
| 34 virtual ~AlternateNavInfoBarDelegate(); | 35 virtual ~AlternateNavInfoBarDelegate(); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 113 |
| 113 void AlternateNavURLFetcher::Observe( | 114 void AlternateNavURLFetcher::Observe( |
| 114 int type, | 115 int type, |
| 115 const content::NotificationSource& source, | 116 const content::NotificationSource& source, |
| 116 const content::NotificationDetails& details) { | 117 const content::NotificationDetails& details) { |
| 117 switch (type) { | 118 switch (type) { |
| 118 case content::NOTIFICATION_NAV_ENTRY_PENDING: { | 119 case content::NOTIFICATION_NAV_ENTRY_PENDING: { |
| 119 // If we've already received a notification for the same controller, we | 120 // If we've already received a notification for the same controller, we |
| 120 // should delete ourselves as that indicates that the page is being | 121 // should delete ourselves as that indicates that the page is being |
| 121 // re-loaded so this instance is now stale. | 122 // re-loaded so this instance is now stale. |
| 122 content::NavigationController* controller = | 123 NavigationController* controller = |
| 123 content::Source<content::NavigationController>(source).ptr(); | 124 content::Source<NavigationController>(source).ptr(); |
| 124 if (controller_ == controller) { | 125 if (controller_ == controller) { |
| 125 delete this; | 126 delete this; |
| 126 } else if (!controller_) { | 127 } else if (!controller_) { |
| 127 // Start listening for the commit notification. | 128 // Start listening for the commit notification. |
| 128 DCHECK(controller->GetPendingEntry()); | 129 DCHECK(controller->GetPendingEntry()); |
| 129 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 130 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 130 content::Source<content::NavigationController>( | 131 content::Source<NavigationController>( |
| 131 controller)); | 132 controller)); |
| 132 StartFetch(controller); | 133 StartFetch(controller); |
| 133 } | 134 } |
| 134 break; | 135 break; |
| 135 } | 136 } |
| 136 | 137 |
| 137 case chrome::NOTIFICATION_INSTANT_COMMITTED: { | 138 case chrome::NOTIFICATION_INSTANT_COMMITTED: { |
| 138 // See above. | 139 // See above. |
| 139 content::NavigationController* controller = | 140 NavigationController* controller = |
| 140 &content::Source<TabContentsWrapper>(source)-> | 141 &content::Source<TabContentsWrapper>(source)-> |
| 141 web_contents()->GetController(); | 142 web_contents()->GetController(); |
| 142 if (controller_ == controller) { | 143 if (controller_ == controller) { |
| 143 delete this; | 144 delete this; |
| 144 } else if (!controller_) { | 145 } else if (!controller_) { |
| 145 navigated_to_entry_ = true; | 146 navigated_to_entry_ = true; |
| 146 StartFetch(controller); | 147 StartFetch(controller); |
| 147 } | 148 } |
| 148 break; | 149 break; |
| 149 } | 150 } |
| 150 | 151 |
| 151 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: | 152 case content::NOTIFICATION_NAV_ENTRY_COMMITTED: |
| 152 // The page was navigated, we can show the infobar now if necessary. | 153 // The page was navigated, we can show the infobar now if necessary. |
| 153 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, | 154 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
| 154 content::Source<content::NavigationController>( | 155 content::Source<NavigationController>(controller_)); |
| 155 controller_)); | |
| 156 navigated_to_entry_ = true; | 156 navigated_to_entry_ = true; |
| 157 ShowInfobarIfPossible(); | 157 ShowInfobarIfPossible(); |
| 158 // WARNING: |this| may be deleted! | 158 // WARNING: |this| may be deleted! |
| 159 break; | 159 break; |
| 160 | 160 |
| 161 case content::NOTIFICATION_TAB_CLOSED: | 161 case content::NOTIFICATION_TAB_CLOSED: |
| 162 // We have been closed. In order to prevent the URLFetcher from trying to | 162 // We have been closed. In order to prevent the URLFetcher from trying to |
| 163 // access the controller that will be invalid, we delete ourselves. | 163 // access the controller that will be invalid, we delete ourselves. |
| 164 // This deletes the URLFetcher and insures its callback won't be called. | 164 // This deletes the URLFetcher and insures its callback won't be called. |
| 165 delete this; | 165 delete this; |
| 166 break; | 166 break; |
| 167 | 167 |
| 168 default: | 168 default: |
| 169 NOTREACHED(); | 169 NOTREACHED(); |
| 170 } | 170 } |
| 171 } | 171 } |
| 172 | 172 |
| 173 void AlternateNavURLFetcher::OnURLFetchComplete( | 173 void AlternateNavURLFetcher::OnURLFetchComplete( |
| 174 const content::URLFetcher* source) { | 174 const content::URLFetcher* source) { |
| 175 DCHECK_EQ(fetcher_.get(), source); | 175 DCHECK_EQ(fetcher_.get(), source); |
| 176 SetStatusFromURLFetch( | 176 SetStatusFromURLFetch( |
| 177 source->GetURL(), source->GetStatus(), source->GetResponseCode()); | 177 source->GetURL(), source->GetStatus(), source->GetResponseCode()); |
| 178 ShowInfobarIfPossible(); | 178 ShowInfobarIfPossible(); |
| 179 // WARNING: |this| may be deleted! | 179 // WARNING: |this| may be deleted! |
| 180 } | 180 } |
| 181 | 181 |
| 182 void AlternateNavURLFetcher::StartFetch( | 182 void AlternateNavURLFetcher::StartFetch(NavigationController* controller) { |
| 183 content::NavigationController* controller) { | |
| 184 controller_ = controller; | 183 controller_ = controller; |
| 185 registrar_.Add(this, content::NOTIFICATION_TAB_CLOSED, | 184 registrar_.Add(this, content::NOTIFICATION_TAB_CLOSED, |
| 186 content::Source<content::NavigationController>(controller_)); | 185 content::Source<NavigationController>(controller_)); |
| 187 | 186 |
| 188 DCHECK_EQ(NOT_STARTED, state_); | 187 DCHECK_EQ(NOT_STARTED, state_); |
| 189 state_ = IN_PROGRESS; | 188 state_ = IN_PROGRESS; |
| 190 fetcher_.reset(content::URLFetcher::Create( | 189 fetcher_.reset(content::URLFetcher::Create( |
| 191 GURL(alternate_nav_url_), content::URLFetcher::HEAD, this)); | 190 GURL(alternate_nav_url_), content::URLFetcher::HEAD, this)); |
| 192 fetcher_->SetRequestContext( | 191 fetcher_->SetRequestContext( |
| 193 controller_->GetBrowserContext()->GetRequestContext()); | 192 controller_->GetBrowserContext()->GetRequestContext()); |
| 194 fetcher_->Start(); | 193 fetcher_->Start(); |
| 195 } | 194 } |
| 196 | 195 |
| (...skipping 29 matching lines...) Expand all Loading... |
| 226 return; | 225 return; |
| 227 } | 226 } |
| 228 | 227 |
| 229 InfoBarTabHelper* infobar_helper = | 228 InfoBarTabHelper* infobar_helper = |
| 230 TabContentsWrapper::GetCurrentWrapperForContents( | 229 TabContentsWrapper::GetCurrentWrapperForContents( |
| 231 controller_->GetWebContents())->infobar_tab_helper(); | 230 controller_->GetWebContents())->infobar_tab_helper(); |
| 232 infobar_helper->AddInfoBar( | 231 infobar_helper->AddInfoBar( |
| 233 new AlternateNavInfoBarDelegate(infobar_helper, alternate_nav_url_)); | 232 new AlternateNavInfoBarDelegate(infobar_helper, alternate_nav_url_)); |
| 234 delete this; | 233 delete this; |
| 235 } | 234 } |
| OLD | NEW |