1 // Copyright 2014 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 "chrome/browser/autocomplete/chrome_autocomplete_provider_client.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chrome/browser/autocomplete/autocomplete_classifier_factory.h"
10 #include "chrome/browser/autocomplete/in_memory_url_index_factory.h"
11 #include "chrome/browser/autocomplete/shortcuts_backend_factory.h"
12 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h"
13 #include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h"
14 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
15 #include "chrome/browser/chrome_notification_types.h"
16 #include "chrome/browser/history/history_service_factory.h"
17 #include "chrome/browser/history/top_sites_factory.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/search_engines/template_url_service_factory.h"
20 #include "chrome/browser/sync/profile_sync_service.h"
21 #include "chrome/browser/sync/profile_sync_service_factory.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/common/url_constants.h"
24 #include "components/history/core/browser/history_service.h"
25 #include "components/omnibox/browser/autocomplete_classifier.h"
26 #include "components/sync_driver/sync_service_utils.h"
27 #include "content/public/browser/notification_service.h"
29 #if defined(ENABLE_EXTENSIONS)
30 #include "chrome/browser/autocomplete/keyword_extensions_delegate_impl.h"
33 #if !defined(OS_ANDROID)
36 // This list should be kept in sync with chrome/common/url_constants.h.
37 // Only include useful sub-pages, confirmation alerts are not useful.
38 const char* const kChromeSettingsSubPages
[] = {
39 chrome::kAutofillSubPage
,
40 chrome::kClearBrowserDataSubPage
,
41 chrome::kContentSettingsSubPage
,
42 chrome::kContentSettingsExceptionsSubPage
,
43 chrome::kImportDataSubPage
,
44 chrome::kLanguageOptionsSubPage
,
45 chrome::kPasswordManagerSubPage
,
46 chrome::kResetProfileSettingsSubPage
,
47 chrome::kSearchEnginesSubPage
,
48 chrome::kSyncSetupSubPage
,
49 #if defined(OS_CHROMEOS)
50 chrome::kInternetOptionsSubPage
,
55 #endif // !defined(OS_ANDROID)
57 ChromeAutocompleteProviderClient::ChromeAutocompleteProviderClient(
60 scheme_classifier_(profile
),
61 search_terms_data_(profile_
) {
64 ChromeAutocompleteProviderClient::~ChromeAutocompleteProviderClient() {
67 net::URLRequestContextGetter
*
68 ChromeAutocompleteProviderClient::GetRequestContext() {
69 return profile_
->GetRequestContext();
72 PrefService
* ChromeAutocompleteProviderClient::GetPrefs() {
73 return profile_
->GetPrefs();
76 const AutocompleteSchemeClassifier
&
77 ChromeAutocompleteProviderClient::GetSchemeClassifier() const {
78 return scheme_classifier_
;
81 AutocompleteClassifier
*
82 ChromeAutocompleteProviderClient::GetAutocompleteClassifier() {
83 return AutocompleteClassifierFactory::GetForProfile(profile_
);
86 history::HistoryService
* ChromeAutocompleteProviderClient::GetHistoryService() {
87 return HistoryServiceFactory::GetForProfile(
88 profile_
, ServiceAccessType::EXPLICIT_ACCESS
);
91 scoped_refptr
<history::TopSites
>
92 ChromeAutocompleteProviderClient::GetTopSites() {
93 return TopSitesFactory::GetForProfile(profile_
);
96 bookmarks::BookmarkModel
* ChromeAutocompleteProviderClient::GetBookmarkModel() {
97 return BookmarkModelFactory::GetForProfile(profile_
);
100 history::URLDatabase
* ChromeAutocompleteProviderClient::GetInMemoryDatabase() {
101 history::HistoryService
* history_service
= GetHistoryService();
103 // This method is called in unit test contexts where the HistoryService isn't
105 return history_service
? history_service
->InMemoryDatabase() : NULL
;
108 InMemoryURLIndex
* ChromeAutocompleteProviderClient::GetInMemoryURLIndex() {
109 return InMemoryURLIndexFactory::GetForProfile(profile_
);
112 TemplateURLService
* ChromeAutocompleteProviderClient::GetTemplateURLService() {
113 return TemplateURLServiceFactory::GetForProfile(profile_
);
116 const TemplateURLService
*
117 ChromeAutocompleteProviderClient::GetTemplateURLService() const {
118 return TemplateURLServiceFactory::GetForProfile(profile_
);
122 SearchTermsData
& ChromeAutocompleteProviderClient::GetSearchTermsData() const {
123 return search_terms_data_
;
126 scoped_refptr
<ShortcutsBackend
>
127 ChromeAutocompleteProviderClient::GetShortcutsBackend() {
128 return ShortcutsBackendFactory::GetForProfile(profile_
);
131 scoped_refptr
<ShortcutsBackend
>
132 ChromeAutocompleteProviderClient::GetShortcutsBackendIfExists() {
133 return ShortcutsBackendFactory::GetForProfileIfExists(profile_
);
136 scoped_ptr
<KeywordExtensionsDelegate
>
137 ChromeAutocompleteProviderClient::GetKeywordExtensionsDelegate(
138 KeywordProvider
* keyword_provider
) {
139 #if defined(ENABLE_EXTENSIONS)
140 return make_scoped_ptr(
141 new KeywordExtensionsDelegateImpl(profile_
, keyword_provider
));
147 std::string
ChromeAutocompleteProviderClient::GetAcceptLanguages() const {
148 return profile_
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
152 ChromeAutocompleteProviderClient::GetEmbedderRepresentationOfAboutScheme() {
153 return content::kChromeUIScheme
;
156 std::vector
<base::string16
> ChromeAutocompleteProviderClient::GetBuiltinURLs() {
157 std::vector
<std::string
> chrome_builtins(
158 chrome::kChromeHostURLs
,
159 chrome::kChromeHostURLs
+ chrome::kNumberOfChromeHostURLs
);
160 std::sort(chrome_builtins
.begin(), chrome_builtins
.end());
162 std::vector
<base::string16
> builtins
;
164 for (std::vector
<std::string
>::iterator
i(chrome_builtins
.begin());
165 i
!= chrome_builtins
.end(); ++i
)
166 builtins
.push_back(base::ASCIIToUTF16(*i
));
168 #if !defined(OS_ANDROID)
169 base::string16
settings(base::ASCIIToUTF16(chrome::kChromeUISettingsHost
) +
170 base::ASCIIToUTF16("/"));
171 for (size_t i
= 0; i
< arraysize(kChromeSettingsSubPages
); i
++) {
172 builtins
.push_back(settings
+
173 base::ASCIIToUTF16(kChromeSettingsSubPages
[i
]));
180 std::vector
<base::string16
>
181 ChromeAutocompleteProviderClient::GetBuiltinsToProvideAsUserTypes() {
182 std::vector
<base::string16
> builtins_to_provide
;
183 builtins_to_provide
.push_back(
184 base::ASCIIToUTF16(chrome::kChromeUIChromeURLsURL
));
185 #if !defined(OS_ANDROID)
186 builtins_to_provide
.push_back(
187 base::ASCIIToUTF16(chrome::kChromeUISettingsURL
));
189 builtins_to_provide
.push_back(
190 base::ASCIIToUTF16(chrome::kChromeUIVersionURL
));
191 return builtins_to_provide
;
194 bool ChromeAutocompleteProviderClient::IsOffTheRecord() const {
195 return profile_
->IsOffTheRecord();
198 bool ChromeAutocompleteProviderClient::SearchSuggestEnabled() const {
199 return profile_
->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled
);
202 bool ChromeAutocompleteProviderClient::BookmarkBarIsVisible() const {
203 return profile_
->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar
);
206 bool ChromeAutocompleteProviderClient::TabSyncEnabledAndUnencrypted() const {
207 return sync_driver::IsTabSyncEnabledAndUnencrypted(
208 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_
),
209 profile_
->GetPrefs());
212 void ChromeAutocompleteProviderClient::Classify(
213 const base::string16
& text
,
215 bool allow_exact_keyword_match
,
216 metrics::OmniboxEventProto::PageClassification page_classification
,
217 AutocompleteMatch
* match
,
218 GURL
* alternate_nav_url
) {
219 AutocompleteClassifier
* classifier
= GetAutocompleteClassifier();
221 classifier
->Classify(text
, prefer_keyword
, allow_exact_keyword_match
,
222 page_classification
, match
, alternate_nav_url
);
225 void ChromeAutocompleteProviderClient::DeleteMatchingURLsForKeywordFromHistory(
226 history::KeywordID keyword_id
,
227 const base::string16
& term
) {
228 GetHistoryService()->DeleteMatchingURLsForKeyword(keyword_id
, term
);
231 void ChromeAutocompleteProviderClient::PrefetchImage(const GURL
& url
) {
232 BitmapFetcherService
* image_service
=
233 BitmapFetcherServiceFactory::GetForBrowserContext(profile_
);
234 DCHECK(image_service
);
235 image_service
->Prefetch(url
);
238 void ChromeAutocompleteProviderClient::OnAutocompleteControllerResultReady(
239 AutocompleteController
* controller
) {
240 content::NotificationService::current()->Notify(
241 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
242 content::Source
<AutocompleteController
>(controller
),
243 content::NotificationService::NoDetails());