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 "athena/main/url_search_provider.h"
7 #include "athena/activity/public/activity_factory.h"
8 #include "athena/activity/public/activity_manager.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "base/values.h"
11 #include "components/metrics/proto/omnibox_event.pb.h"
12 #include "components/metrics/proto/omnibox_input_type.pb.h"
13 #include "components/omnibox/autocomplete_input.h"
14 #include "components/omnibox/autocomplete_provider_client.h"
15 #include "components/omnibox/autocomplete_scheme_classifier.h"
16 #include "components/omnibox/search_provider.h"
17 #include "components/search_engines/search_terms_data.h"
18 #include "components/search_engines/template_url_service.h"
19 #include "components/search_engines/template_url_service_client.h"
20 #include "content/public/browser/browser_context.h"
21 #include "ui/app_list/search_result.h"
22 #include "ui/base/resource/resource_bundle.h"
29 // This constant was copied from HistoryURLProvider.
30 // TODO(hashimoto): Componentize HistoryURLProvider and delete this.
31 const int kScoreForWhatYouTypedResult
= 1203;
33 // The SearchTermsData implementation for Athena.
34 class AthenaSearchTermsData
: public SearchTermsData
{
37 virtual std::string
GetSuggestClient() const OVERRIDE
{
42 // The AutocompleteSchemeClassifier implementation for Athena.
43 // TODO(mukai): Introduce supports of normal schemes like about: or blob:
44 class AthenaSchemeClassifier
: public AutocompleteSchemeClassifier
{
46 AthenaSchemeClassifier() {}
48 // AutocompleteSchemeClassifier:
49 virtual metrics::OmniboxInputType::Type
GetInputTypeForScheme(
50 const std::string
& scheme
) const OVERRIDE
{
51 if (net::URLRequest::IsHandledProtocol(scheme
))
52 return metrics::OmniboxInputType::URL
;
53 return metrics::OmniboxInputType::INVALID
;
57 // The templateURLServiceClient for Athena. Mainly for the interaction with
58 // history module (see chrome/browser/search_engines for Chrome implementation).
59 // TODO(mukai): Implement the contents of this class when it's necessary.
60 class AthenaTemplateURLServiceClient
: public TemplateURLServiceClient
{
62 AthenaTemplateURLServiceClient() {}
63 virtual ~AthenaTemplateURLServiceClient() {}
66 // TemplateURLServiceClient:
67 virtual void SetOwner(TemplateURLService
* owner
) OVERRIDE
{}
68 virtual void DeleteAllSearchTermsForKeyword(TemplateURLID id
) OVERRIDE
{}
69 virtual void SetKeywordSearchTermsForURL(
72 const base::string16
& term
) OVERRIDE
{}
73 virtual void AddKeywordGeneratedVisit(const GURL
& url
) OVERRIDE
{}
74 virtual void RestoreExtensionInfoIfNecessary(
75 TemplateURL
* template_url
) OVERRIDE
{}
77 DISALLOW_COPY_AND_ASSIGN(AthenaTemplateURLServiceClient
);
80 // The AutocompleteProviderClient for Athena.
81 class AthenaAutocompleteProviderClient
: public AutocompleteProviderClient
{
83 explicit AthenaAutocompleteProviderClient(
84 content::BrowserContext
* browser_context
)
85 : browser_context_(browser_context
) {}
86 virtual ~AthenaAutocompleteProviderClient() {}
88 virtual net::URLRequestContextGetter
* RequestContext() OVERRIDE
{
89 return browser_context_
->GetRequestContext();
91 virtual bool IsOffTheRecord() OVERRIDE
{
92 return browser_context_
->IsOffTheRecord();
94 virtual std::string
AcceptLanguages() OVERRIDE
{
95 // TODO(hashimoto): Return the value stored in the prefs.
98 virtual bool SearchSuggestEnabled() OVERRIDE
{ return true; }
99 virtual bool ShowBookmarkBar() OVERRIDE
{ return false; }
100 virtual const AutocompleteSchemeClassifier
& SchemeClassifier() OVERRIDE
{
101 return scheme_classifier_
;
103 virtual void Classify(
104 const base::string16
& text
,
106 bool allow_exact_keyword_match
,
107 metrics::OmniboxEventProto::PageClassification page_classification
,
108 AutocompleteMatch
* match
,
109 GURL
* alternate_nav_url
) OVERRIDE
{}
110 virtual history::URLDatabase
* InMemoryDatabase() OVERRIDE
{ return NULL
; }
111 virtual void DeleteMatchingURLsForKeywordFromHistory(
112 history::KeywordID keyword_id
,
113 const base::string16
& term
) OVERRIDE
{}
114 virtual bool TabSyncEnabledAndUnencrypted() OVERRIDE
{ return false; }
115 virtual void PrefetchImage(const GURL
& url
) OVERRIDE
{}
118 content::BrowserContext
* browser_context_
;
119 AthenaSchemeClassifier scheme_classifier_
;
121 DISALLOW_COPY_AND_ASSIGN(AthenaAutocompleteProviderClient
);
124 int ACMatchStyleToTagStyle(int styles
) {
126 if (styles
& ACMatchClassification::URL
)
127 tag_styles
|= app_list::SearchResult::Tag::URL
;
128 if (styles
& ACMatchClassification::MATCH
)
129 tag_styles
|= app_list::SearchResult::Tag::MATCH
;
130 if (styles
& ACMatchClassification::DIM
)
131 tag_styles
|= app_list::SearchResult::Tag::DIM
;
136 // Translates ACMatchClassifications into SearchResult tags.
137 void ACMatchClassificationsToTags(
138 const base::string16
& text
,
139 const ACMatchClassifications
& text_classes
,
140 app_list::SearchResult::Tags
* tags
) {
141 int tag_styles
= app_list::SearchResult::Tag::NONE
;
142 size_t tag_start
= 0;
144 for (size_t i
= 0; i
< text_classes
.size(); ++i
) {
145 const ACMatchClassification
& text_class
= text_classes
[i
];
147 // Closes current tag.
148 if (tag_styles
!= app_list::SearchResult::Tag::NONE
) {
149 tags
->push_back(app_list::SearchResult::Tag(
150 tag_styles
, tag_start
, text_class
.offset
));
151 tag_styles
= app_list::SearchResult::Tag::NONE
;
154 if (text_class
.style
== ACMatchClassification::NONE
)
157 tag_start
= text_class
.offset
;
158 tag_styles
= ACMatchStyleToTagStyle(text_class
.style
);
161 if (tag_styles
!= app_list::SearchResult::Tag::NONE
) {
162 tags
->push_back(app_list::SearchResult::Tag(
163 tag_styles
, tag_start
, text
.length()));
167 class UrlSearchResult
: public app_list::SearchResult
{
169 UrlSearchResult(content::BrowserContext
* browser_context
,
170 const AutocompleteMatch
& match
)
171 : browser_context_(browser_context
),
173 set_id(match_
.destination_url
.spec());
175 // Derive relevance from omnibox relevance and normalize it to [0, 1].
176 // The magic number 1500 is the highest score of an omnibox result.
177 // See comments in autocomplete_provider.h.
178 set_relevance(match_
.relevance
/ 1500.0);
181 UpdateTitleAndDetails();
184 virtual ~UrlSearchResult() {}
187 // Overriddenn from app_list::SearchResult:
188 virtual void Open(int event_flags
) OVERRIDE
{
189 ActivityManager::Get()->AddActivity(
190 ActivityFactory::Get()->CreateWebActivity(
191 browser_context_
, base::string16(), match_
.destination_url
));
195 SetIcon(*ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
196 AutocompleteMatch::TypeToIcon(match_
.type
)));
199 void UpdateTitleAndDetails() {
200 set_title(match_
.contents
);
201 SearchResult::Tags title_tags
;
202 ACMatchClassificationsToTags(match_
.contents
,
203 match_
.contents_class
,
205 set_title_tags(title_tags
);
207 set_details(match_
.description
);
208 SearchResult::Tags details_tags
;
209 ACMatchClassificationsToTags(match_
.description
,
210 match_
.description_class
,
212 set_details_tags(details_tags
);
215 content::BrowserContext
* browser_context_
;
216 AutocompleteMatch match_
;
218 DISALLOW_COPY_AND_ASSIGN(UrlSearchResult
);
223 UrlSearchProvider::UrlSearchProvider(content::BrowserContext
* browser_context
)
224 : browser_context_(browser_context
),
225 // TODO(mukai): introduce the real parameters when it's necessary.
226 template_url_service_(
227 new TemplateURLService(NULL
/* prefs */,
228 scoped_ptr
<SearchTermsData
>(
229 new AthenaSearchTermsData()),
230 NULL
/* KeywordWebDataService */,
231 scoped_ptr
<TemplateURLServiceClient
>(
232 new AthenaTemplateURLServiceClient()),
233 NULL
/*GoogleURLTracker */,
234 NULL
/* RapporService */,
235 base::Closure() /* dsp_change_callback */)),
236 provider_(new ::SearchProvider(
238 template_url_service_
.get(),
239 scoped_ptr
<AutocompleteProviderClient
>(
240 new AthenaAutocompleteProviderClient(browser_context_
)))) {
241 template_url_service_
->Load();
244 UrlSearchProvider::~UrlSearchProvider() {
247 void UrlSearchProvider::Start(const base::string16
& query
) {
248 const bool minimal_changes
= query
== input_
.text();
249 input_
= AutocompleteInput(query
,
250 base::string16::npos
/* cursor_position */,
251 base::string16() /* desired_tld */,
252 GURL() /* current_url */,
253 metrics::OmniboxEventProto::INVALID_SPEC
,
254 false /* prevent_inline_autocomplete */,
255 false /* prefer_keyword */,
256 true /* allow_extract_keyword_match */,
257 true /* want_asynchronous_matches */,
258 AthenaSchemeClassifier());
260 provider_
->Start(input_
, minimal_changes
);
263 void UrlSearchProvider::Stop() {
264 provider_
->Stop(false);
267 void UrlSearchProvider::OnProviderUpdate(bool updated_matches
) {
268 if (!updated_matches
)
273 if (input_
.type() == metrics::OmniboxInputType::URL
) {
274 // TODO(hashimoto): Componentize HistoryURLProvider and remove this code.
275 AutocompleteMatch
what_you_typed_match(
276 NULL
, 0, false, AutocompleteMatchType::URL_WHAT_YOU_TYPED
);
277 what_you_typed_match
.destination_url
= input_
.canonicalized_url();
278 what_you_typed_match
.contents
= input_
.text();
279 what_you_typed_match
.relevance
= kScoreForWhatYouTypedResult
;
280 Add(scoped_ptr
<app_list::SearchResult
>(new UrlSearchResult(
281 browser_context_
, what_you_typed_match
)));
284 const ACMatches
& matches
= provider_
->matches();
285 for (ACMatches::const_iterator it
= matches
.begin(); it
!= matches
.end();
287 if (!it
->destination_url
.is_valid())
290 Add(scoped_ptr
<app_list::SearchResult
>(new UrlSearchResult(
291 browser_context_
, *it
)));
295 } // namespace athena