OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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. |
| 4 |
| 5 #include "ios/chrome/browser/search/search_util.h" |
| 6 |
| 7 #include "components/search/search.h" |
| 8 #include "components/search_engines/template_url_service.h" |
| 9 #include "ios/chrome/browser/search_engines/template_url_service_factory.h" |
| 10 #include "ios/chrome/browser/search_engines/ui_thread_search_terms_data.h" |
| 11 #include "ios/public/provider/chrome/browser/browser_state/chrome_browser_state.
h" |
| 12 #include "ios/web/public/navigation_item.h" |
| 13 #include "ios/web/public/navigation_manager.h" |
| 14 #include "ios/web/public/web_state/web_state.h" |
| 15 #include "url/gurl.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 TemplateURL* GetDefaultSearchProviderTemplateURL( |
| 20 ios::ChromeBrowserState* browser_state) { |
| 21 if (!browser_state) |
| 22 return nullptr; |
| 23 |
| 24 TemplateURLService* template_url_service = |
| 25 ios::TemplateURLServiceFactory::GetForBrowserState(browser_state); |
| 26 return template_url_service ? template_url_service->GetDefaultSearchProvider() |
| 27 : nullptr; |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 base::string16 ExtractSearchTermsFromURL(ios::ChromeBrowserState* browser_state, |
| 33 const GURL& url) { |
| 34 base::string16 search_terms; |
| 35 TemplateURL* template_url = |
| 36 GetDefaultSearchProviderTemplateURL(browser_state); |
| 37 if (template_url) { |
| 38 template_url->ExtractSearchTermsFromURL( |
| 39 url, ios::UIThreadSearchTermsData(browser_state), &search_terms); |
| 40 } |
| 41 return search_terms; |
| 42 } |
| 43 |
| 44 bool IsQueryExtractionAllowedForURL(ios::ChromeBrowserState* browser_state, |
| 45 const GURL& url) { |
| 46 TemplateURL* template_url = |
| 47 GetDefaultSearchProviderTemplateURL(browser_state); |
| 48 return template_url && search::IsSuitableURLForInstant(url, template_url); |
| 49 } |
| 50 |
| 51 base::string16 GetSearchTerms(web::WebState* web_state) { |
| 52 if (!web_state) |
| 53 return base::string16(); |
| 54 |
| 55 web::NavigationItem* item = |
| 56 web_state->GetNavigationManager()->GetVisibleItem(); |
| 57 if (!item) |
| 58 return base::string16(); |
| 59 |
| 60 if (!search::IsQueryExtractionEnabled()) |
| 61 return base::string16(); |
| 62 |
| 63 ios::ChromeBrowserState* browser_state = |
| 64 ios::ChromeBrowserState::FromBrowserState(web_state->GetBrowserState()); |
| 65 if (!IsQueryExtractionAllowedForURL(browser_state, item->GetVirtualURL())) |
| 66 return base::string16(); |
| 67 |
| 68 return ExtractSearchTermsFromURL(browser_state, item->GetVirtualURL()); |
| 69 } |
OLD | NEW |