Revert "Fix broken channel icon in chrome://help on CrOS" and try again
[chromium-blink-merge.git] / ios / chrome / browser / search / search_util.cc
blob079c53f6f5cb623e49efa3efef758d4c7f94c017
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.
5 #include "ios/chrome/browser/search/search_util.h"
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"
17 namespace {
19 TemplateURL* GetDefaultSearchProviderTemplateURL(
20 ios::ChromeBrowserState* browser_state) {
21 if (!browser_state)
22 return nullptr;
24 TemplateURLService* template_url_service =
25 ios::TemplateURLServiceFactory::GetForBrowserState(browser_state);
26 return template_url_service ? template_url_service->GetDefaultSearchProvider()
27 : nullptr;
30 } // namespace
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);
41 return search_terms;
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);
51 base::string16 GetSearchTerms(web::WebState* web_state) {
52 if (!web_state)
53 return base::string16();
55 web::NavigationItem* item =
56 web_state->GetNavigationManager()->GetVisibleItem();
57 if (!item)
58 return base::string16();
60 if (!search::IsQueryExtractionEnabled())
61 return base::string16();
63 ios::ChromeBrowserState* browser_state =
64 ios::ChromeBrowserState::FromBrowserState(web_state->GetBrowserState());
65 if (!IsQueryExtractionAllowedForURL(browser_state, item->GetVirtualURL()))
66 return base::string16();
68 return ExtractSearchTermsFromURL(browser_state, item->GetVirtualURL());