| 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/ui/omnibox/omnibox_navigation_observer.h" | 5 #include "chrome/browser/ui/omnibox/omnibox_navigation_observer.h" |
| 6 | 6 |
| 7 #include "chrome/browser/history/shortcuts_backend.h" |
| 8 #include "chrome/browser/history/shortcuts_backend_factory.h" |
| 7 #include "chrome/browser/infobars/infobar_service.h" | 9 #include "chrome/browser/infobars/infobar_service.h" |
| 8 #include "chrome/browser/intranet_redirect_detector.h" | 10 #include "chrome/browser/intranet_redirect_detector.h" |
| 9 #include "chrome/browser/ui/omnibox/alternate_nav_infobar_delegate.h" | 11 #include "chrome/browser/ui/omnibox/alternate_nav_infobar_delegate.h" |
| 10 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
| 11 #include "content/public/browser/navigation_controller.h" | 13 #include "content/public/browser/navigation_controller.h" |
| 12 #include "content/public/browser/navigation_details.h" | 14 #include "content/public/browser/navigation_details.h" |
| 13 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
| 14 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
| 15 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
| 16 #include "content/public/browser/web_contents.h" | 18 #include "content/public/browser/web_contents.h" |
| (...skipping 25 matching lines...) Expand all Loading... |
| 42 final_url, redirect_url, | 44 final_url, redirect_url, |
| 43 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); | 45 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); |
| 44 } | 46 } |
| 45 | 47 |
| 46 } // namespace | 48 } // namespace |
| 47 | 49 |
| 48 | 50 |
| 49 // OmniboxNavigationObserver -------------------------------------------------- | 51 // OmniboxNavigationObserver -------------------------------------------------- |
| 50 | 52 |
| 51 OmniboxNavigationObserver::OmniboxNavigationObserver( | 53 OmniboxNavigationObserver::OmniboxNavigationObserver( |
| 54 Profile* profile, |
| 55 const string16& text, |
| 56 const AutocompleteMatch& match, |
| 52 const GURL& alternate_nav_url) | 57 const GURL& alternate_nav_url) |
| 53 : alternate_nav_url_(alternate_nav_url), | 58 : text_(text), |
| 59 match_(match), |
| 60 alternate_nav_url_(alternate_nav_url), |
| 61 shortcuts_backend_(ShortcutsBackendFactory::GetForProfile(profile)), |
| 54 load_state_(LOAD_NOT_SEEN), | 62 load_state_(LOAD_NOT_SEEN), |
| 55 fetch_state_(FETCH_NOT_COMPLETE) { | 63 fetch_state_(FETCH_NOT_COMPLETE) { |
| 56 if (alternate_nav_url_.is_valid()) { | 64 if (alternate_nav_url_.is_valid()) { |
| 57 fetcher_.reset(net::URLFetcher::Create(alternate_nav_url, | 65 fetcher_.reset(net::URLFetcher::Create(alternate_nav_url, |
| 58 net::URLFetcher::HEAD, this)); | 66 net::URLFetcher::HEAD, this)); |
| 59 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 67 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
| 60 fetcher_->SetStopOnRedirect(true); | 68 fetcher_->SetStopOnRedirect(true); |
| 61 } | 69 } |
| 62 // We need to start by listening to AllSources, since we don't know which tab | 70 // We need to start by listening to AllSources, since we don't know which tab |
| 63 // the navigation might occur in. | 71 // the navigation might occur in. |
| 64 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, | 72 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, |
| 65 content::NotificationService::AllSources()); | 73 content::NotificationService::AllSources()); |
| 66 } | 74 } |
| 67 | 75 |
| 68 OmniboxNavigationObserver::~OmniboxNavigationObserver() { | 76 OmniboxNavigationObserver::~OmniboxNavigationObserver() { |
| 69 } | 77 } |
| 70 | 78 |
| 79 void OmniboxNavigationObserver::OnSuccessfulNavigation() { |
| 80 shortcuts_backend_->OnOmniboxNavigation(text_, match_); |
| 81 } |
| 82 |
| 71 void OmniboxNavigationObserver::Observe( | 83 void OmniboxNavigationObserver::Observe( |
| 72 int type, | 84 int type, |
| 73 const content::NotificationSource& source, | 85 const content::NotificationSource& source, |
| 74 const content::NotificationDetails& details) { | 86 const content::NotificationDetails& details) { |
| 75 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_PENDING, type); | 87 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_PENDING, type); |
| 76 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_PENDING, | 88 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_PENDING, |
| 77 content::NotificationService::AllSources()); | 89 content::NotificationService::AllSources()); |
| 78 content::NavigationController* controller = | 90 content::NavigationController* controller = |
| 79 content::Source<content::NavigationController>(source).ptr(); | 91 content::Source<content::NavigationController>(source).ptr(); |
| 80 if (fetcher_) { | 92 if (fetcher_) { |
| 81 fetcher_->SetRequestContext( | 93 fetcher_->SetRequestContext( |
| 82 controller->GetBrowserContext()->GetRequestContext()); | 94 controller->GetBrowserContext()->GetRequestContext()); |
| 83 } | 95 } |
| 84 WebContentsObserver::Observe(controller->GetWebContents()); | 96 WebContentsObserver::Observe(controller->GetWebContents()); |
| 85 // NavigateToPendingEntry() will be called for this load as well. | 97 // NavigateToPendingEntry() will be called for this load as well. |
| 86 } | 98 } |
| 87 | 99 |
| 88 void OmniboxNavigationObserver::NavigationEntryCommitted( | 100 void OmniboxNavigationObserver::NavigationEntryCommitted( |
| 89 const content::LoadCommittedDetails& load_details) { | 101 const content::LoadCommittedDetails& load_details) { |
| 90 load_state_ = LOAD_COMMITTED; | 102 load_state_ = LOAD_COMMITTED; |
| 103 if (ResponseCodeIndicatesSuccess(load_details.http_status_code) && |
| 104 IsValidNavigation(match_.destination_url, load_details.entry->GetURL())) |
| 105 OnSuccessfulNavigation(); |
| 91 if (!fetcher_ || (fetch_state_ != FETCH_NOT_COMPLETE)) | 106 if (!fetcher_ || (fetch_state_ != FETCH_NOT_COMPLETE)) |
| 92 OnAllLoadingFinished(); // deletes |this|! | 107 OnAllLoadingFinished(); // deletes |this|! |
| 93 } | 108 } |
| 94 | 109 |
| 95 void OmniboxNavigationObserver::WebContentsDestroyed( | 110 void OmniboxNavigationObserver::WebContentsDestroyed( |
| 96 content::WebContents* web_contents) { | 111 content::WebContents* web_contents) { |
| 97 delete this; | 112 delete this; |
| 98 } | 113 } |
| 99 | 114 |
| 100 void OmniboxNavigationObserver::NavigateToPendingEntry( | 115 void OmniboxNavigationObserver::NavigateToPendingEntry( |
| 101 const GURL& url, | 116 const GURL& url, |
| 102 content::NavigationController::ReloadType reload_type) { | 117 content::NavigationController::ReloadType reload_type) { |
| 103 if (load_state_ == LOAD_NOT_SEEN) { | 118 if (load_state_ == LOAD_NOT_SEEN) { |
| 104 load_state_ = LOAD_PENDING; | 119 load_state_ = LOAD_PENDING; |
| 105 if (fetcher_) | 120 if (fetcher_) |
| 106 fetcher_->Start(); | 121 fetcher_->Start(); |
| 107 } else { | 122 } else { |
| 108 delete this; | 123 delete this; |
| 109 } | 124 } |
| 110 } | 125 } |
| 111 | 126 |
| 112 void OmniboxNavigationObserver::OnURLFetchComplete( | 127 void OmniboxNavigationObserver::OnURLFetchComplete( |
| 113 const net::URLFetcher* source) { | 128 const net::URLFetcher* source) { |
| 129 DCHECK_EQ(fetcher_.get(), source); |
| 114 const net::URLRequestStatus& status = source->GetStatus(); | 130 const net::URLRequestStatus& status = source->GetStatus(); |
| 115 int response_code = source->GetResponseCode(); | 131 int response_code = source->GetResponseCode(); |
| 116 fetch_state_ = | 132 fetch_state_ = |
| 117 (status.is_success() && ResponseCodeIndicatesSuccess(response_code)) || | 133 (status.is_success() && ResponseCodeIndicatesSuccess(response_code)) || |
| 118 ((status.status() == net::URLRequestStatus::CANCELED) && | 134 ((status.status() == net::URLRequestStatus::CANCELED) && |
| 119 ((response_code / 100) == 3) && | 135 ((response_code / 100) == 3) && |
| 120 IsValidNavigation(alternate_nav_url_, source->GetURL())) ? | 136 IsValidNavigation(alternate_nav_url_, source->GetURL())) ? |
| 121 FETCH_SUCCEEDED : FETCH_FAILED; | 137 FETCH_SUCCEEDED : FETCH_FAILED; |
| 122 if (load_state_ == LOAD_COMMITTED) | 138 if (load_state_ == LOAD_COMMITTED) |
| 123 OnAllLoadingFinished(); // deletes |this|! | 139 OnAllLoadingFinished(); // deletes |this|! |
| 124 } | 140 } |
| 125 | 141 |
| 126 void OmniboxNavigationObserver::OnAllLoadingFinished() { | 142 void OmniboxNavigationObserver::OnAllLoadingFinished() { |
| 127 if (fetch_state_ == FETCH_SUCCEEDED) { | 143 if (fetch_state_ == FETCH_SUCCEEDED) { |
| 128 AlternateNavInfoBarDelegate::Create( | 144 AlternateNavInfoBarDelegate::Create( |
| 129 InfoBarService::FromWebContents(web_contents()), alternate_nav_url_); | 145 InfoBarService::FromWebContents(web_contents()), alternate_nav_url_); |
| 130 } | 146 } |
| 131 delete this; | 147 delete this; |
| 132 } | 148 } |
| OLD | NEW |