| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/search/instant_search_prerenderer.h" | 5 #include "chrome/browser/ui/search/instant_search_prerenderer.h" |
| 6 | 6 |
| 7 #include "chrome/browser/autocomplete/autocomplete_match.h" | 7 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 8 #include "chrome/browser/prerender/prerender_handle.h" | 8 #include "chrome/browser/prerender/prerender_handle.h" |
| 9 #include "chrome/browser/prerender/prerender_manager.h" | 9 #include "chrome/browser/prerender/prerender_manager.h" |
| 10 #include "chrome/browser/prerender/prerender_manager_factory.h" | 10 #include "chrome/browser/prerender/prerender_manager_factory.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "chrome/browser/search/instant_service.h" | 12 #include "chrome/browser/search/instant_service.h" |
| 13 #include "chrome/browser/search/instant_service_factory.h" | 13 #include "chrome/browser/search/instant_service_factory.h" |
| 14 #include "chrome/browser/search/search.h" | 14 #include "chrome/browser/search/search.h" |
| 15 #include "chrome/browser/search_engines/template_url_service.h" |
| 16 #include "chrome/browser/search_engines/template_url_service_factory.h" |
| 15 #include "chrome/browser/ui/browser_navigator.h" | 17 #include "chrome/browser/ui/browser_navigator.h" |
| 16 #include "chrome/browser/ui/search/search_tab_helper.h" | 18 #include "chrome/browser/ui/search/search_tab_helper.h" |
| 17 | 19 |
| 18 namespace { | 20 namespace { |
| 19 | 21 |
| 20 // Returns true if the underlying page supports Instant search. | 22 // Returns true if the underlying page supports Instant search. |
| 21 bool PageSupportsInstantSearch(content::WebContents* contents) { | 23 bool PageSupportsInstantSearch(content::WebContents* contents) { |
| 22 // Search results page supports Instant search. | 24 // Search results page supports Instant search. |
| 23 return SearchTabHelper::FromWebContents(contents)->IsSearchResultsPage(); | 25 return SearchTabHelper::FromWebContents(contents)->IsSearchResultsPage(); |
| 24 } | 26 } |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 116 } | 118 } |
| 117 | 119 |
| 118 // InstantSearchPrerenderer can commit query to the prerendered page only if | 120 // InstantSearchPrerenderer can commit query to the prerendered page only if |
| 119 // the underlying |source| page doesn't support Instant search. | 121 // the underlying |source| page doesn't support Instant search. |
| 120 return !PageSupportsInstantSearch(source); | 122 return !PageSupportsInstantSearch(source); |
| 121 } | 123 } |
| 122 | 124 |
| 123 bool InstantSearchPrerenderer::UsePrerenderedPage( | 125 bool InstantSearchPrerenderer::UsePrerenderedPage( |
| 124 const GURL& url, | 126 const GURL& url, |
| 125 chrome::NavigateParams* params) { | 127 chrome::NavigateParams* params) { |
| 126 base::string16 search_terms = chrome::GetSearchTermsFromURL(profile_, url); | 128 base::string16 search_terms = GetSearchTermsFromURL(url); |
| 127 prerender::PrerenderManager* prerender_manager = | 129 prerender::PrerenderManager* prerender_manager = |
| 128 prerender::PrerenderManagerFactory::GetForProfile(profile_); | 130 prerender::PrerenderManagerFactory::GetForProfile(profile_); |
| 129 if (search_terms.empty() || !params->target_contents || | 131 if (search_terms.empty() || !params->target_contents || |
| 130 !prerender_contents() || !prerender_manager || | 132 !prerender_contents() || !prerender_manager || |
| 131 !QueryMatchesPrefetch(search_terms)) { | 133 !QueryMatchesPrefetch(search_terms)) { |
| 132 Cancel(); | 134 Cancel(); |
| 133 return false; | 135 return false; |
| 134 } | 136 } |
| 135 | 137 |
| 136 bool success = prerender_manager->MaybeUsePrerenderedPage( | 138 bool success = prerender_manager->MaybeUsePrerenderedPage( |
| 137 prerender_contents()->GetURL(), params); | 139 prerender_contents()->GetURL(), params); |
| 138 prerender_handle_.reset(); | 140 prerender_handle_.reset(); |
| 139 return success; | 141 return success; |
| 140 } | 142 } |
| 141 | 143 |
| 142 bool InstantSearchPrerenderer::IsAllowed(const AutocompleteMatch& match, | 144 bool InstantSearchPrerenderer::IsAllowed(const AutocompleteMatch& match, |
| 143 content::WebContents* source) const { | 145 content::WebContents* source) const { |
| 144 return source && AutocompleteMatch::IsSearchType(match.type) && | 146 return source && AutocompleteMatch::IsSearchType(match.type) && |
| 145 !PageSupportsInstantSearch(source); | 147 !PageSupportsInstantSearch(source); |
| 146 } | 148 } |
| 147 | 149 |
| 150 base::string16 InstantSearchPrerenderer::GetSearchTermsFromURL( |
| 151 const GURL& url) const { |
| 152 base::string16 search_terms; |
| 153 TemplateURLService* template_url_service = |
| 154 TemplateURLServiceFactory::GetForProfile(profile_); |
| 155 if (!template_url_service) |
| 156 return search_terms; |
| 157 |
| 158 TemplateURL* template_url = template_url_service->GetDefaultSearchProvider(); |
| 159 if (template_url) |
| 160 template_url->ExtractSearchTermsFromURL(url, &search_terms); |
| 161 return search_terms; |
| 162 } |
| 163 |
| 148 content::WebContents* InstantSearchPrerenderer::prerender_contents() const { | 164 content::WebContents* InstantSearchPrerenderer::prerender_contents() const { |
| 149 return (prerender_handle_ && prerender_handle_->contents()) ? | 165 return (prerender_handle_ && prerender_handle_->contents()) ? |
| 150 prerender_handle_->contents()->prerender_contents() : NULL; | 166 prerender_handle_->contents()->prerender_contents() : NULL; |
| 151 } | 167 } |
| 152 | 168 |
| 153 bool InstantSearchPrerenderer::QueryMatchesPrefetch( | 169 bool InstantSearchPrerenderer::QueryMatchesPrefetch( |
| 154 const base::string16& query) const { | 170 const base::string16& query) const { |
| 155 if (chrome::ShouldReuseInstantSearchBasePage()) | 171 if (chrome::ShouldReuseInstantSearchBasePage()) |
| 156 return true; | 172 return true; |
| 157 return last_instant_suggestion_.text == query; | 173 return last_instant_suggestion_.text == query; |
| 158 } | 174 } |
| OLD | NEW |