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"
19 TemplateURL
* GetDefaultSearchProviderTemplateURL(
20 ios::ChromeBrowserState
* browser_state
) {
24 TemplateURLService
* template_url_service
=
25 ios::TemplateURLServiceFactory::GetForBrowserState(browser_state
);
26 return template_url_service
? template_url_service
->GetDefaultSearchProvider()
32 base::string16
ExtractSearchTermsFromURL(ios::ChromeBrowserState
* browser_state
,
34 base::string16 search_terms
;
35 TemplateURL
* template_url
=
36 GetDefaultSearchProviderTemplateURL(browser_state
);
38 template_url
->ExtractSearchTermsFromURL(
39 url
, ios::UIThreadSearchTermsData(browser_state
), &search_terms
);
44 bool IsQueryExtractionAllowedForURL(ios::ChromeBrowserState
* browser_state
,
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
) {
53 return base::string16();
55 web::NavigationItem
* item
=
56 web_state
->GetNavigationManager()->GetVisibleItem();
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());