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 "content/public/browser/notification_service.h"
28 #if defined(ENABLE_EXTENSIONS)
29 #include "chrome/browser/autocomplete/keyword_extensions_delegate_impl.h"
32 #if !defined(OS_ANDROID)
35 // This list should be kept in sync with chrome/common/url_constants.h.
36 // Only include useful sub-pages, confirmation alerts are not useful.
37 const char* const kChromeSettingsSubPages
[] = {
38 chrome::kAutofillSubPage
,
39 chrome::kClearBrowserDataSubPage
,
40 chrome::kContentSettingsSubPage
,
41 chrome::kContentSettingsExceptionsSubPage
,
42 chrome::kImportDataSubPage
,
43 chrome::kLanguageOptionsSubPage
,
44 chrome::kPasswordManagerSubPage
,
45 chrome::kResetProfileSettingsSubPage
,
46 chrome::kSearchEnginesSubPage
,
47 chrome::kSyncSetupSubPage
,
48 #if defined(OS_CHROMEOS)
49 chrome::kInternetOptionsSubPage
,
54 #endif // !defined(OS_ANDROID)
56 ChromeAutocompleteProviderClient::ChromeAutocompleteProviderClient(
59 scheme_classifier_(profile
),
60 search_terms_data_(profile_
) {
63 ChromeAutocompleteProviderClient::~ChromeAutocompleteProviderClient() {
66 net::URLRequestContextGetter
*
67 ChromeAutocompleteProviderClient::GetRequestContext() {
68 return profile_
->GetRequestContext();
71 PrefService
* ChromeAutocompleteProviderClient::GetPrefs() {
72 return profile_
->GetPrefs();
75 const AutocompleteSchemeClassifier
&
76 ChromeAutocompleteProviderClient::GetSchemeClassifier() const {
77 return scheme_classifier_
;
80 AutocompleteClassifier
*
81 ChromeAutocompleteProviderClient::GetAutocompleteClassifier() {
82 return AutocompleteClassifierFactory::GetForProfile(profile_
);
85 history::HistoryService
* ChromeAutocompleteProviderClient::GetHistoryService() {
86 return HistoryServiceFactory::GetForProfile(
87 profile_
, ServiceAccessType::EXPLICIT_ACCESS
);
90 scoped_refptr
<history::TopSites
>
91 ChromeAutocompleteProviderClient::GetTopSites() {
92 return TopSitesFactory::GetForProfile(profile_
);
95 bookmarks::BookmarkModel
* ChromeAutocompleteProviderClient::GetBookmarkModel() {
96 return BookmarkModelFactory::GetForProfile(profile_
);
99 history::URLDatabase
* ChromeAutocompleteProviderClient::GetInMemoryDatabase() {
100 history::HistoryService
* history_service
= GetHistoryService();
102 // This method is called in unit test contexts where the HistoryService isn't
104 return history_service
? history_service
->InMemoryDatabase() : NULL
;
107 InMemoryURLIndex
* ChromeAutocompleteProviderClient::GetInMemoryURLIndex() {
108 return InMemoryURLIndexFactory::GetForProfile(profile_
);
111 TemplateURLService
* ChromeAutocompleteProviderClient::GetTemplateURLService() {
112 return TemplateURLServiceFactory::GetForProfile(profile_
);
115 const TemplateURLService
*
116 ChromeAutocompleteProviderClient::GetTemplateURLService() const {
117 return TemplateURLServiceFactory::GetForProfile(profile_
);
121 SearchTermsData
& ChromeAutocompleteProviderClient::GetSearchTermsData() const {
122 return search_terms_data_
;
125 scoped_refptr
<ShortcutsBackend
>
126 ChromeAutocompleteProviderClient::GetShortcutsBackend() {
127 return ShortcutsBackendFactory::GetForProfile(profile_
);
130 scoped_refptr
<ShortcutsBackend
>
131 ChromeAutocompleteProviderClient::GetShortcutsBackendIfExists() {
132 return ShortcutsBackendFactory::GetForProfileIfExists(profile_
);
135 scoped_ptr
<KeywordExtensionsDelegate
>
136 ChromeAutocompleteProviderClient::GetKeywordExtensionsDelegate(
137 KeywordProvider
* keyword_provider
) {
138 #if defined(ENABLE_EXTENSIONS)
139 return make_scoped_ptr(
140 new KeywordExtensionsDelegateImpl(profile_
, keyword_provider
));
146 std::string
ChromeAutocompleteProviderClient::GetAcceptLanguages() const {
147 return profile_
->GetPrefs()->GetString(prefs::kAcceptLanguages
);
151 ChromeAutocompleteProviderClient::GetEmbedderRepresentationOfAboutScheme() {
152 return content::kChromeUIScheme
;
155 std::vector
<base::string16
> ChromeAutocompleteProviderClient::GetBuiltinURLs() {
156 std::vector
<std::string
> chrome_builtins(
157 chrome::kChromeHostURLs
,
158 chrome::kChromeHostURLs
+ chrome::kNumberOfChromeHostURLs
);
159 std::sort(chrome_builtins
.begin(), chrome_builtins
.end());
161 std::vector
<base::string16
> builtins
;
163 for (std::vector
<std::string
>::iterator
i(chrome_builtins
.begin());
164 i
!= chrome_builtins
.end(); ++i
)
165 builtins
.push_back(base::ASCIIToUTF16(*i
));
167 #if !defined(OS_ANDROID)
168 base::string16
settings(base::ASCIIToUTF16(chrome::kChromeUISettingsHost
) +
169 base::ASCIIToUTF16("/"));
170 for (size_t i
= 0; i
< arraysize(kChromeSettingsSubPages
); i
++) {
171 builtins
.push_back(settings
+
172 base::ASCIIToUTF16(kChromeSettingsSubPages
[i
]));
179 std::vector
<base::string16
>
180 ChromeAutocompleteProviderClient::GetBuiltinsToProvideAsUserTypes() {
181 std::vector
<base::string16
> builtins_to_provide
;
182 builtins_to_provide
.push_back(
183 base::ASCIIToUTF16(chrome::kChromeUIChromeURLsURL
));
184 #if !defined(OS_ANDROID)
185 builtins_to_provide
.push_back(
186 base::ASCIIToUTF16(chrome::kChromeUISettingsURL
));
188 builtins_to_provide
.push_back(
189 base::ASCIIToUTF16(chrome::kChromeUIVersionURL
));
190 return builtins_to_provide
;
193 bool ChromeAutocompleteProviderClient::IsOffTheRecord() const {
194 return profile_
->IsOffTheRecord();
197 bool ChromeAutocompleteProviderClient::SearchSuggestEnabled() const {
198 return profile_
->GetPrefs()->GetBoolean(prefs::kSearchSuggestEnabled
);
201 bool ChromeAutocompleteProviderClient::BookmarkBarIsVisible() const {
202 return profile_
->GetPrefs()->GetBoolean(bookmarks::prefs::kShowBookmarkBar
);
205 bool ChromeAutocompleteProviderClient::TabSyncEnabledAndUnencrypted() const {
206 // Check field trials and settings allow sending the URL on suggest requests.
207 ProfileSyncService
* service
=
208 ProfileSyncServiceFactory::GetInstance()->GetForProfile(profile_
);
209 sync_driver::SyncPrefs
sync_prefs(profile_
->GetPrefs());
210 return service
&& service
->CanSyncStart() &&
211 sync_prefs
.GetPreferredDataTypes(syncer::UserTypes())
212 .Has(syncer::PROXY_TABS
) &&
213 !service
->GetEncryptedDataTypes().Has(syncer::SESSIONS
);
216 void ChromeAutocompleteProviderClient::Classify(
217 const base::string16
& text
,
219 bool allow_exact_keyword_match
,
220 metrics::OmniboxEventProto::PageClassification page_classification
,
221 AutocompleteMatch
* match
,
222 GURL
* alternate_nav_url
) {
223 AutocompleteClassifier
* classifier
= GetAutocompleteClassifier();
225 classifier
->Classify(text
, prefer_keyword
, allow_exact_keyword_match
,
226 page_classification
, match
, alternate_nav_url
);
229 void ChromeAutocompleteProviderClient::DeleteMatchingURLsForKeywordFromHistory(
230 history::KeywordID keyword_id
,
231 const base::string16
& term
) {
232 GetHistoryService()->DeleteMatchingURLsForKeyword(keyword_id
, term
);
235 void ChromeAutocompleteProviderClient::PrefetchImage(const GURL
& url
) {
236 BitmapFetcherService
* image_service
=
237 BitmapFetcherServiceFactory::GetForBrowserContext(profile_
);
238 DCHECK(image_service
);
239 image_service
->Prefetch(url
);
242 void ChromeAutocompleteProviderClient::OnAutocompleteControllerResultReady(
243 AutocompleteController
* controller
) {
244 content::NotificationService::current()->Notify(
245 chrome::NOTIFICATION_AUTOCOMPLETE_CONTROLLER_RESULT_READY
,
246 content::Source
<AutocompleteController
>(controller
),
247 content::NotificationService::NoDetails());