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/autocomplete/shortcuts_backend.h" | 7 #include "chrome/browser/autocomplete/shortcuts_backend.h" |
8 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" | 8 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h" |
9 #include "chrome/browser/infobars/infobar_service.h" | 9 #include "chrome/browser/infobars/infobar_service.h" |
10 #include "chrome/browser/intranet_redirect_detector.h" | 10 #include "chrome/browser/intranet_redirect_detector.h" |
11 #include "chrome/browser/ui/omnibox/alternate_nav_infobar_delegate.h" | 11 #include "chrome/browser/ui/omnibox/alternate_nav_infobar_delegate.h" |
12 #include "content/public/browser/browser_context.h" | 12 #include "content/public/browser/browser_context.h" |
13 #include "content/public/browser/navigation_controller.h" | 13 #include "content/public/browser/navigation_controller.h" |
14 #include "content/public/browser/navigation_details.h" | 14 #include "content/public/browser/navigation_details.h" |
15 #include "content/public/browser/navigation_entry.h" | 15 #include "content/public/browser/navigation_entry.h" |
16 #include "content/public/browser/notification_service.h" | 16 #include "content/public/browser/notification_service.h" |
17 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
18 #include "content/public/browser/render_frame_host.h" | |
18 #include "content/public/browser/web_contents.h" | 19 #include "content/public/browser/web_contents.h" |
19 #include "net/base/load_flags.h" | 20 #include "net/base/load_flags.h" |
20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 21 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
21 #include "net/url_request/url_fetcher.h" | 22 #include "net/url_request/url_fetcher.h" |
22 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
23 | 24 |
24 | 25 |
25 // Helpers -------------------------------------------------------------------- | 26 // Helpers -------------------------------------------------------------------- |
26 | 27 |
27 namespace { | 28 namespace { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 fetch_state_(FETCH_NOT_COMPLETE) { | 64 fetch_state_(FETCH_NOT_COMPLETE) { |
64 if (alternate_nav_match_.destination_url.is_valid()) { | 65 if (alternate_nav_match_.destination_url.is_valid()) { |
65 fetcher_.reset(net::URLFetcher::Create(alternate_nav_match_.destination_url, | 66 fetcher_.reset(net::URLFetcher::Create(alternate_nav_match_.destination_url, |
66 net::URLFetcher::HEAD, this)); | 67 net::URLFetcher::HEAD, this)); |
67 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); | 68 fetcher_->SetLoadFlags(net::LOAD_DO_NOT_SAVE_COOKIES); |
68 fetcher_->SetStopOnRedirect(true); | 69 fetcher_->SetStopOnRedirect(true); |
69 } | 70 } |
70 // We need to start by listening to AllSources, since we don't know which tab | 71 // We need to start by listening to AllSources, since we don't know which tab |
71 // the navigation might occur in. | 72 // the navigation might occur in. |
72 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, | 73 registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_PENDING, |
73 content::NotificationService::AllSources()); | 74 content::NotificationService::AllSources()); |
Charlie Reis
2015/01/23 06:34:28
I'm guessing we can't enumerate the possible tabs
| |
74 } | 75 } |
75 | 76 |
76 OmniboxNavigationObserver::~OmniboxNavigationObserver() { | 77 OmniboxNavigationObserver::~OmniboxNavigationObserver() { |
77 } | 78 } |
78 | 79 |
79 void OmniboxNavigationObserver::OnSuccessfulNavigation() { | 80 void OmniboxNavigationObserver::OnSuccessfulNavigation() { |
80 if (shortcuts_backend_.get()) | 81 if (shortcuts_backend_.get()) |
81 shortcuts_backend_->AddOrUpdateShortcut(text_, match_); | 82 shortcuts_backend_->AddOrUpdateShortcut(text_, match_); |
82 } | 83 } |
83 | 84 |
84 void OmniboxNavigationObserver::Observe( | 85 void OmniboxNavigationObserver::Observe( |
85 int type, | 86 int type, |
86 const content::NotificationSource& source, | 87 const content::NotificationSource& source, |
87 const content::NotificationDetails& details) { | 88 const content::NotificationDetails& details) { |
88 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_PENDING, type); | 89 DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_PENDING, type); |
90 | |
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)) | |
100 return; | |
101 | |
102 CHECK_EQ(match_.destination_url, | |
103 content::Details<content::NavigationEntry>( | |
104 details)->GetVirtualURL()); | |
89 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_PENDING, | 105 registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_PENDING, |
90 content::NotificationService::AllSources()); | 106 content::NotificationService::AllSources()); |
91 content::NavigationController* controller = | |
92 content::Source<content::NavigationController>(source).ptr(); | |
93 if (fetcher_) { | 107 if (fetcher_) { |
94 fetcher_->SetRequestContext( | 108 fetcher_->SetRequestContext( |
95 controller->GetBrowserContext()->GetRequestContext()); | 109 controller->GetBrowserContext()->GetRequestContext()); |
96 } | 110 } |
97 WebContentsObserver::Observe(controller->GetWebContents()); | 111 WebContentsObserver::Observe(web_contents); |
98 // DidStartNavigationToPendingEntry() will be called for this load as well. | 112 // DidStartNavigationToPendingEntry() will be called for this load as well. |
99 } | 113 } |
100 | 114 |
115 void OmniboxNavigationObserver::DidStartNavigationToPendingEntry( | |
116 const GURL& url, | |
117 content::NavigationController::ReloadType reload_type) { | |
118 if (load_state_ == LOAD_NOT_SEEN) { | |
119 load_state_ = LOAD_PENDING; | |
120 if (fetcher_) | |
121 fetcher_->Start(); | |
122 } else { | |
123 delete this; | |
124 } | |
125 } | |
126 | |
127 void OmniboxNavigationObserver::DidFailProvisionalLoad( | |
128 content::RenderFrameHost* render_frame_host, | |
129 const GURL& validated_url, | |
130 int error_code, | |
131 const base::string16& error_description) { | |
132 if ((load_state_ != LOAD_COMMITTED) && !render_frame_host->GetParent()) | |
133 delete this; | |
134 } | |
135 | |
101 void OmniboxNavigationObserver::NavigationEntryCommitted( | 136 void OmniboxNavigationObserver::NavigationEntryCommitted( |
102 const content::LoadCommittedDetails& load_details) { | 137 const content::LoadCommittedDetails& load_details) { |
103 load_state_ = LOAD_COMMITTED; | 138 load_state_ = LOAD_COMMITTED; |
104 if (ResponseCodeIndicatesSuccess(load_details.http_status_code) && | 139 if (ResponseCodeIndicatesSuccess(load_details.http_status_code) && |
105 IsValidNavigation(match_.destination_url, load_details.entry->GetURL())) | 140 IsValidNavigation(match_.destination_url, |
141 load_details.entry->GetVirtualURL())) | |
106 OnSuccessfulNavigation(); | 142 OnSuccessfulNavigation(); |
107 if (!fetcher_ || (fetch_state_ != FETCH_NOT_COMPLETE)) | 143 if (!fetcher_ || (fetch_state_ != FETCH_NOT_COMPLETE)) |
108 OnAllLoadingFinished(); // deletes |this|! | 144 OnAllLoadingFinished(); // deletes |this|! |
109 } | 145 } |
110 | 146 |
111 void OmniboxNavigationObserver::WebContentsDestroyed() { | 147 void OmniboxNavigationObserver::WebContentsDestroyed() { |
112 delete this; | 148 delete this; |
113 } | 149 } |
114 | 150 |
115 void OmniboxNavigationObserver::DidStartNavigationToPendingEntry( | |
116 const GURL& url, | |
117 content::NavigationController::ReloadType reload_type) { | |
118 if (load_state_ == LOAD_NOT_SEEN) { | |
119 load_state_ = LOAD_PENDING; | |
120 if (fetcher_) | |
121 fetcher_->Start(); | |
122 } else { | |
123 delete this; | |
124 } | |
125 } | |
126 | |
127 void OmniboxNavigationObserver::OnURLFetchComplete( | 151 void OmniboxNavigationObserver::OnURLFetchComplete( |
128 const net::URLFetcher* source) { | 152 const net::URLFetcher* source) { |
129 DCHECK_EQ(fetcher_.get(), source); | 153 DCHECK_EQ(fetcher_.get(), source); |
130 const net::URLRequestStatus& status = source->GetStatus(); | 154 const net::URLRequestStatus& status = source->GetStatus(); |
131 int response_code = source->GetResponseCode(); | 155 int response_code = source->GetResponseCode(); |
132 fetch_state_ = | 156 fetch_state_ = |
133 (status.is_success() && ResponseCodeIndicatesSuccess(response_code)) || | 157 (status.is_success() && ResponseCodeIndicatesSuccess(response_code)) || |
134 ((status.status() == net::URLRequestStatus::CANCELED) && | 158 ((status.status() == net::URLRequestStatus::CANCELED) && |
135 ((response_code / 100) == 3) && | 159 ((response_code / 100) == 3) && |
136 IsValidNavigation(alternate_nav_match_.destination_url, | 160 IsValidNavigation(alternate_nav_match_.destination_url, |
137 source->GetURL())) ? | 161 source->GetURL())) ? |
138 FETCH_SUCCEEDED : FETCH_FAILED; | 162 FETCH_SUCCEEDED : FETCH_FAILED; |
139 if (load_state_ == LOAD_COMMITTED) | 163 if (load_state_ == LOAD_COMMITTED) |
140 OnAllLoadingFinished(); // deletes |this|! | 164 OnAllLoadingFinished(); // deletes |this|! |
141 } | 165 } |
142 | 166 |
143 void OmniboxNavigationObserver::OnAllLoadingFinished() { | 167 void OmniboxNavigationObserver::OnAllLoadingFinished() { |
144 if (fetch_state_ == FETCH_SUCCEEDED) { | 168 if (fetch_state_ == FETCH_SUCCEEDED) { |
145 AlternateNavInfoBarDelegate::Create( | 169 AlternateNavInfoBarDelegate::Create( |
146 web_contents(), text_, alternate_nav_match_, match_.destination_url); | 170 web_contents(), text_, alternate_nav_match_, match_.destination_url); |
147 } | 171 } |
148 delete this; | 172 delete this; |
149 } | 173 } |
OLD | NEW |