| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "athena/main/url_search_provider.h" | |
| 6 | |
| 7 #include "athena/activity/public/activity.h" | |
| 8 #include "athena/activity/public/activity_factory.h" | |
| 9 #include "athena/content/public/scheme_classifier_factory.h" | |
| 10 #include "base/strings/utf_string_conversions.h" | |
| 11 #include "base/values.h" | |
| 12 #include "components/metrics/proto/omnibox_event.pb.h" | |
| 13 #include "components/metrics/proto/omnibox_input_type.pb.h" | |
| 14 #include "components/omnibox/autocomplete_input.h" | |
| 15 #include "components/omnibox/autocomplete_provider_client.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" | |
| 23 #include "url/gurl.h" | |
| 24 | |
| 25 namespace athena { | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 // This constant was copied from HistoryURLProvider. | |
| 30 // TODO(hashimoto): Componentize HistoryURLProvider and delete this. | |
| 31 const int kScoreForWhatYouTypedResult = 1203; | |
| 32 | |
| 33 // The SearchTermsData implementation for Athena. | |
| 34 class AthenaSearchTermsData : public SearchTermsData { | |
| 35 public: | |
| 36 // SearchTermsData: | |
| 37 virtual std::string GetSuggestClient() const override { | |
| 38 return "chrome"; | |
| 39 } | |
| 40 }; | |
| 41 | |
| 42 // The templateURLServiceClient for Athena. Mainly for the interaction with | |
| 43 // history module (see chrome/browser/search_engines for Chrome implementation). | |
| 44 // TODO(mukai): Implement the contents of this class when it's necessary. | |
| 45 class AthenaTemplateURLServiceClient : public TemplateURLServiceClient { | |
| 46 public: | |
| 47 AthenaTemplateURLServiceClient() {} | |
| 48 virtual ~AthenaTemplateURLServiceClient() {} | |
| 49 | |
| 50 private: | |
| 51 // TemplateURLServiceClient: | |
| 52 virtual void SetOwner(TemplateURLService* owner) override {} | |
| 53 virtual void DeleteAllSearchTermsForKeyword(TemplateURLID id) override {} | |
| 54 virtual void SetKeywordSearchTermsForURL( | |
| 55 const GURL& url, | |
| 56 TemplateURLID id, | |
| 57 const base::string16& term) override {} | |
| 58 virtual void AddKeywordGeneratedVisit(const GURL& url) override {} | |
| 59 virtual void RestoreExtensionInfoIfNecessary( | |
| 60 TemplateURL* template_url) override {} | |
| 61 | |
| 62 DISALLOW_COPY_AND_ASSIGN(AthenaTemplateURLServiceClient); | |
| 63 }; | |
| 64 | |
| 65 // The AutocompleteProviderClient for Athena. | |
| 66 class AthenaAutocompleteProviderClient : public AutocompleteProviderClient { | |
| 67 public: | |
| 68 explicit AthenaAutocompleteProviderClient( | |
| 69 content::BrowserContext* browser_context) | |
| 70 : browser_context_(browser_context), | |
| 71 scheme_classifier_(CreateSchemeClassifier(browser_context)) {} | |
| 72 virtual ~AthenaAutocompleteProviderClient() {} | |
| 73 | |
| 74 virtual net::URLRequestContextGetter* RequestContext() override { | |
| 75 return browser_context_->GetRequestContext(); | |
| 76 } | |
| 77 virtual bool IsOffTheRecord() override { | |
| 78 return browser_context_->IsOffTheRecord(); | |
| 79 } | |
| 80 virtual std::string AcceptLanguages() override { | |
| 81 // TODO(hashimoto): Return the value stored in the prefs. | |
| 82 return "en-US"; | |
| 83 } | |
| 84 virtual bool SearchSuggestEnabled() override { return true; } | |
| 85 virtual bool ShowBookmarkBar() override { return false; } | |
| 86 virtual const AutocompleteSchemeClassifier& SchemeClassifier() override { | |
| 87 return *scheme_classifier_; | |
| 88 } | |
| 89 virtual void Classify( | |
| 90 const base::string16& text, | |
| 91 bool prefer_keyword, | |
| 92 bool allow_exact_keyword_match, | |
| 93 metrics::OmniboxEventProto::PageClassification page_classification, | |
| 94 AutocompleteMatch* match, | |
| 95 GURL* alternate_nav_url) override {} | |
| 96 virtual history::URLDatabase* InMemoryDatabase() override { return NULL; } | |
| 97 virtual void DeleteMatchingURLsForKeywordFromHistory( | |
| 98 history::KeywordID keyword_id, | |
| 99 const base::string16& term) override {} | |
| 100 virtual bool TabSyncEnabledAndUnencrypted() override { return false; } | |
| 101 virtual void PrefetchImage(const GURL& url) override {} | |
| 102 | |
| 103 private: | |
| 104 content::BrowserContext* browser_context_; | |
| 105 scoped_ptr<AutocompleteSchemeClassifier> scheme_classifier_; | |
| 106 | |
| 107 DISALLOW_COPY_AND_ASSIGN(AthenaAutocompleteProviderClient); | |
| 108 }; | |
| 109 | |
| 110 int ACMatchStyleToTagStyle(int styles) { | |
| 111 int tag_styles = 0; | |
| 112 if (styles & ACMatchClassification::URL) | |
| 113 tag_styles |= app_list::SearchResult::Tag::URL; | |
| 114 if (styles & ACMatchClassification::MATCH) | |
| 115 tag_styles |= app_list::SearchResult::Tag::MATCH; | |
| 116 if (styles & ACMatchClassification::DIM) | |
| 117 tag_styles |= app_list::SearchResult::Tag::DIM; | |
| 118 | |
| 119 return tag_styles; | |
| 120 } | |
| 121 | |
| 122 // Translates ACMatchClassifications into SearchResult tags. | |
| 123 void ACMatchClassificationsToTags( | |
| 124 const base::string16& text, | |
| 125 const ACMatchClassifications& text_classes, | |
| 126 app_list::SearchResult::Tags* tags) { | |
| 127 int tag_styles = app_list::SearchResult::Tag::NONE; | |
| 128 size_t tag_start = 0; | |
| 129 | |
| 130 for (size_t i = 0; i < text_classes.size(); ++i) { | |
| 131 const ACMatchClassification& text_class = text_classes[i]; | |
| 132 | |
| 133 // Closes current tag. | |
| 134 if (tag_styles != app_list::SearchResult::Tag::NONE) { | |
| 135 tags->push_back(app_list::SearchResult::Tag( | |
| 136 tag_styles, tag_start, text_class.offset)); | |
| 137 tag_styles = app_list::SearchResult::Tag::NONE; | |
| 138 } | |
| 139 | |
| 140 if (text_class.style == ACMatchClassification::NONE) | |
| 141 continue; | |
| 142 | |
| 143 tag_start = text_class.offset; | |
| 144 tag_styles = ACMatchStyleToTagStyle(text_class.style); | |
| 145 } | |
| 146 | |
| 147 if (tag_styles != app_list::SearchResult::Tag::NONE) { | |
| 148 tags->push_back(app_list::SearchResult::Tag( | |
| 149 tag_styles, tag_start, text.length())); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 class UrlSearchResult : public app_list::SearchResult { | |
| 154 public: | |
| 155 UrlSearchResult(content::BrowserContext* browser_context, | |
| 156 const AutocompleteMatch& match) | |
| 157 : browser_context_(browser_context), | |
| 158 match_(match) { | |
| 159 set_id(match_.destination_url.spec()); | |
| 160 | |
| 161 // Derive relevance from omnibox relevance and normalize it to [0, 1]. | |
| 162 // The magic number 1500 is the highest score of an omnibox result. | |
| 163 // See comments in autocomplete_provider.h. | |
| 164 set_relevance(match_.relevance / 1500.0); | |
| 165 | |
| 166 UpdateIcon(); | |
| 167 UpdateTitleAndDetails(); | |
| 168 } | |
| 169 | |
| 170 virtual ~UrlSearchResult() {} | |
| 171 | |
| 172 private: | |
| 173 // Overridden from app_list::SearchResult: | |
| 174 virtual scoped_ptr<app_list::SearchResult> Duplicate() override { | |
| 175 return make_scoped_ptr(new UrlSearchResult(browser_context_, match_)); | |
| 176 } | |
| 177 | |
| 178 virtual void Open(int event_flags) override { | |
| 179 Activity* activity = ActivityFactory::Get()->CreateWebActivity( | |
| 180 browser_context_, base::string16(), match_.destination_url); | |
| 181 Activity::Show(activity); | |
| 182 } | |
| 183 | |
| 184 void UpdateIcon() { | |
| 185 SetIcon(*ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed( | |
| 186 AutocompleteMatch::TypeToIcon(match_.type))); | |
| 187 } | |
| 188 | |
| 189 void UpdateTitleAndDetails() { | |
| 190 set_title(match_.contents); | |
| 191 SearchResult::Tags title_tags; | |
| 192 ACMatchClassificationsToTags(match_.contents, | |
| 193 match_.contents_class, | |
| 194 &title_tags); | |
| 195 set_title_tags(title_tags); | |
| 196 | |
| 197 set_details(match_.description); | |
| 198 SearchResult::Tags details_tags; | |
| 199 ACMatchClassificationsToTags(match_.description, | |
| 200 match_.description_class, | |
| 201 &details_tags); | |
| 202 set_details_tags(details_tags); | |
| 203 } | |
| 204 | |
| 205 content::BrowserContext* browser_context_; | |
| 206 AutocompleteMatch match_; | |
| 207 | |
| 208 DISALLOW_COPY_AND_ASSIGN(UrlSearchResult); | |
| 209 }; | |
| 210 | |
| 211 } // namespace | |
| 212 | |
| 213 UrlSearchProvider::UrlSearchProvider(content::BrowserContext* browser_context) | |
| 214 : browser_context_(browser_context), | |
| 215 // TODO(mukai): introduce the real parameters when it's necessary. | |
| 216 template_url_service_( | |
| 217 new TemplateURLService(NULL /* prefs */, | |
| 218 scoped_ptr<SearchTermsData>( | |
| 219 new AthenaSearchTermsData()), | |
| 220 NULL /* KeywordWebDataService */, | |
| 221 scoped_ptr<TemplateURLServiceClient>( | |
| 222 new AthenaTemplateURLServiceClient()), | |
| 223 NULL /*GoogleURLTracker */, | |
| 224 NULL /* RapporService */, | |
| 225 base::Closure() /* dsp_change_callback */)), | |
| 226 provider_(new ::SearchProvider( | |
| 227 this, | |
| 228 template_url_service_.get(), | |
| 229 scoped_ptr<AutocompleteProviderClient>( | |
| 230 new AthenaAutocompleteProviderClient(browser_context_)))) { | |
| 231 template_url_service_->Load(); | |
| 232 } | |
| 233 | |
| 234 UrlSearchProvider::~UrlSearchProvider() { | |
| 235 } | |
| 236 | |
| 237 void UrlSearchProvider::Start(const base::string16& query) { | |
| 238 const bool minimal_changes = query == input_.text(); | |
| 239 scoped_ptr<AutocompleteSchemeClassifier> scheme_classifier( | |
| 240 CreateSchemeClassifier(browser_context_)); | |
| 241 input_ = AutocompleteInput(query, | |
| 242 base::string16::npos /* cursor_position */, | |
| 243 std::string() /* desired_tld */, | |
| 244 GURL() /* current_url */, | |
| 245 metrics::OmniboxEventProto::INVALID_SPEC, | |
| 246 false /* prevent_inline_autocomplete */, | |
| 247 false /* prefer_keyword */, | |
| 248 true /* allow_extract_keyword_match */, | |
| 249 true /* want_asynchronous_matches */, | |
| 250 *scheme_classifier); | |
| 251 | |
| 252 // Clearing results here may cause unexpected results. | |
| 253 // TODO(mukai): fix this by fixing crbug.com/415500 | |
| 254 if (!minimal_changes) | |
| 255 ClearResults(); | |
| 256 | |
| 257 if (input_.type() == metrics::OmniboxInputType::URL) { | |
| 258 // TODO(hashimoto): Componentize HistoryURLProvider and remove this code. | |
| 259 AutocompleteMatch what_you_typed_match( | |
| 260 NULL, 0, false, AutocompleteMatchType::URL_WHAT_YOU_TYPED); | |
| 261 what_you_typed_match.destination_url = input_.canonicalized_url(); | |
| 262 what_you_typed_match.contents = input_.text(); | |
| 263 what_you_typed_match.relevance = kScoreForWhatYouTypedResult; | |
| 264 Add(scoped_ptr<app_list::SearchResult>(new UrlSearchResult( | |
| 265 browser_context_, what_you_typed_match))); | |
| 266 } | |
| 267 | |
| 268 provider_->Start(input_, minimal_changes); | |
| 269 } | |
| 270 | |
| 271 void UrlSearchProvider::Stop() { | |
| 272 provider_->Stop(false); | |
| 273 } | |
| 274 | |
| 275 void UrlSearchProvider::OnProviderUpdate(bool updated_matches) { | |
| 276 if (!updated_matches) | |
| 277 return; | |
| 278 | |
| 279 const ACMatches& matches = provider_->matches(); | |
| 280 for (ACMatches::const_iterator it = matches.begin(); it != matches.end(); | |
| 281 ++it) { | |
| 282 if (!it->destination_url.is_valid()) | |
| 283 continue; | |
| 284 | |
| 285 Add(scoped_ptr<app_list::SearchResult>(new UrlSearchResult( | |
| 286 browser_context_, *it))); | |
| 287 } | |
| 288 } | |
| 289 | |
| 290 } // namespace athena | |
| OLD | NEW |