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.
5 #include "chrome/browser/ui/omnibox/omnibox_navigation_observer.h"
7 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
8 #include "chrome/browser/infobars/infobar_service.h"
9 #include "chrome/browser/intranet_redirect_detector.h"
10 #include "chrome/browser/ui/omnibox/alternate_nav_infobar_delegate.h"
11 #include "components/omnibox/shortcuts_backend.h"
12 #include "content/public/browser/browser_context.h"
13 #include "content/public/browser/navigation_controller.h"
14 #include "content/public/browser/navigation_details.h"
15 #include "content/public/browser/navigation_entry.h"
16 #include "content/public/browser/notification_service.h"
17 #include "content/public/browser/notification_types.h"
18 #include "content/public/browser/render_frame_host.h"
19 #include "content/public/browser/web_contents.h"
20 #include "net/base/load_flags.h"
21 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
22 #include "net/url_request/url_fetcher.h"
23 #include "net/url_request/url_request.h"
26 // Helpers --------------------------------------------------------------------
30 // HTTP 2xx, 401, and 407 all indicate that the target address exists.
31 bool ResponseCodeIndicatesSuccess(int response_code
) {
32 return ((response_code
/ 100) == 2) || (response_code
== 401) ||
33 (response_code
== 407);
36 // Returns true if |final_url| doesn't represent an ISP hijack of
37 // |original_url|, based on the IntranetRedirectDetector's RedirectOrigin().
38 bool IsValidNavigation(const GURL
& original_url
, const GURL
& final_url
) {
39 const GURL
& redirect_url(IntranetRedirectDetector::RedirectOrigin());
40 return !redirect_url
.is_valid() ||
41 net::registry_controlled_domains::SameDomainOrHost(
42 original_url
, final_url
,
43 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
) ||
44 !net::registry_controlled_domains::SameDomainOrHost(
45 final_url
, redirect_url
,
46 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES
);
52 // OmniboxNavigationObserver --------------------------------------------------
54 OmniboxNavigationObserver::OmniboxNavigationObserver(
56 const base::string16
& text
,
57 const AutocompleteMatch
& match
,
58 const AutocompleteMatch
& alternate_nav_match
)
61 alternate_nav_match_(alternate_nav_match
),
62 shortcuts_backend_(ShortcutsBackendFactory::GetForProfile(profile
)),
63 load_state_(LOAD_NOT_SEEN
),
64 fetch_state_(FETCH_NOT_COMPLETE
) {
65 if (alternate_nav_match_
.destination_url
.is_valid()) {
66 fetcher_
= net::URLFetcher::Create(alternate_nav_match_
.destination_url
,
67 net::URLFetcher::HEAD
, this);
68 fetcher_
->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES
);
69 fetcher_
->SetStopOnRedirect(true);
71 // We need to start by listening to AllSources, since we don't know which tab
72 // the navigation might occur in.
73 registrar_
.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING
,
74 content::NotificationService::AllSources());
77 OmniboxNavigationObserver::~OmniboxNavigationObserver() {
80 void OmniboxNavigationObserver::OnSuccessfulNavigation() {
81 if (shortcuts_backend_
.get())
82 shortcuts_backend_
->AddOrUpdateShortcut(text_
, match_
);
85 void OmniboxNavigationObserver::Observe(
87 const content::NotificationSource
& source
,
88 const content::NotificationDetails
& details
) {
89 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_PENDING
, type
);
91 // It's possible for an attempted omnibox navigation to cause the extensions
92 // system to synchronously navigate an extension background page. Not only is
93 // this navigation not the one we want to observe, the associated WebContents
94 // is invisible and has no InfoBarService, so trying to show an infobar in it
95 // later will crash. Just ignore this navigation and keep listening.
96 content::NavigationController
* controller
=
97 content::Source
<content::NavigationController
>(source
).ptr();
98 content::WebContents
* web_contents
= controller
->GetWebContents();
99 if (!InfoBarService::FromWebContents(web_contents
))
102 // Ignore navgiations to the wrong URL.
103 // This shouldn't actually happen, but right now it's possible because the
104 // prerenderer doesn't properly notify us when it swaps in a prerendered page.
105 // Plus, the swap-in can trigger instant to kick off a new background
106 // prerender, which we _do_ get notified about. Once crbug.com/247848 is
107 // fixed, this conditional should be able to be replaced with a [D]CHECK;
108 // until then we ignore the incorrect navigation (and will be torn down
109 // without having received the correct notification).
110 if (match_
.destination_url
!=
111 content::Details
<content::NavigationEntry
>(details
)->GetVirtualURL())
114 registrar_
.Remove(this, content::NOTIFICATION_NAV_ENTRY_PENDING
,
115 content::NotificationService::AllSources());
117 fetcher_
->SetRequestContext(
118 controller
->GetBrowserContext()->GetRequestContext());
120 WebContentsObserver::Observe(web_contents
);
121 // DidStartNavigationToPendingEntry() will be called for this load as well.
124 void OmniboxNavigationObserver::DidStartNavigationToPendingEntry(
126 content::NavigationController::ReloadType reload_type
) {
127 if (load_state_
== LOAD_NOT_SEEN
) {
128 load_state_
= LOAD_PENDING
;
136 void OmniboxNavigationObserver::DidFailProvisionalLoad(
137 content::RenderFrameHost
* render_frame_host
,
138 const GURL
& validated_url
,
140 const base::string16
& error_description
) {
141 if ((load_state_
!= LOAD_COMMITTED
) && !render_frame_host
->GetParent())
145 void OmniboxNavigationObserver::NavigationEntryCommitted(
146 const content::LoadCommittedDetails
& load_details
) {
147 load_state_
= LOAD_COMMITTED
;
148 if (ResponseCodeIndicatesSuccess(load_details
.http_status_code
) &&
149 IsValidNavigation(match_
.destination_url
,
150 load_details
.entry
->GetVirtualURL()))
151 OnSuccessfulNavigation();
152 if (!fetcher_
|| (fetch_state_
!= FETCH_NOT_COMPLETE
))
153 OnAllLoadingFinished(); // deletes |this|!
156 void OmniboxNavigationObserver::WebContentsDestroyed() {
160 void OmniboxNavigationObserver::OnURLFetchComplete(
161 const net::URLFetcher
* source
) {
162 DCHECK_EQ(fetcher_
.get(), source
);
163 const net::URLRequestStatus
& status
= source
->GetStatus();
164 int response_code
= source
->GetResponseCode();
166 (status
.is_success() && ResponseCodeIndicatesSuccess(response_code
)) ||
167 ((status
.status() == net::URLRequestStatus::CANCELED
) &&
168 ((response_code
/ 100) == 3) &&
169 IsValidNavigation(alternate_nav_match_
.destination_url
,
171 FETCH_SUCCEEDED
: FETCH_FAILED
;
172 if (load_state_
== LOAD_COMMITTED
)
173 OnAllLoadingFinished(); // deletes |this|!
176 void OmniboxNavigationObserver::OnAllLoadingFinished() {
177 if (fetch_state_
== FETCH_SUCCEEDED
) {
178 AlternateNavInfoBarDelegate::Create(
179 web_contents(), text_
, alternate_nav_match_
, match_
.destination_url
);