OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "athena/main/url_search_provider.h" | 5 #include "athena/main/url_search_provider.h" |
6 | 6 |
7 #include "athena/activity/public/activity_factory.h" | 7 #include "athena/activity/public/activity_factory.h" |
8 #include "athena/activity/public/activity_manager.h" | 8 #include "athena/activity/public/activity_manager.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
| 10 #include "components/autocomplete/autocomplete_input.h" |
| 11 #include "components/autocomplete/autocomplete_scheme_classifier.h" |
| 12 #include "components/metrics/proto/omnibox_event.pb.h" |
| 13 #include "components/metrics/proto/omnibox_input_type.pb.h" |
| 14 #include "components/search_engines/search_terms_data.h" |
| 15 #include "components/search_engines/template_url.h" |
| 16 #include "components/search_engines/template_url_service.h" |
| 17 #include "components/search_engines/template_url_service_client.h" |
| 18 #include "net/url_request/url_request.h" |
10 #include "ui/app_list/search_result.h" | 19 #include "ui/app_list/search_result.h" |
11 #include "url/gurl.h" | 20 #include "url/gurl.h" |
12 | 21 |
13 namespace athena { | 22 namespace athena { |
14 | 23 |
15 namespace { | 24 namespace { |
16 | 25 |
| 26 // The AutocompleteSchemeClassifier implementation for Athena. |
| 27 // TODO(mukai): Introduce supports of normal schemes like about: or blob: |
| 28 class AthenaSchemeClassifier : public AutocompleteSchemeClassifier { |
| 29 public: |
| 30 AthenaSchemeClassifier() {} |
| 31 |
| 32 // AutocompleteSchemeClassifier: |
| 33 virtual metrics::OmniboxInputType::Type GetInputTypeForScheme( |
| 34 const std::string& scheme) const OVERRIDE { |
| 35 if (net::URLRequest::IsHandledProtocol(scheme)) |
| 36 return metrics::OmniboxInputType::URL; |
| 37 return metrics::OmniboxInputType::INVALID; |
| 38 } |
| 39 }; |
| 40 |
| 41 // The templateURLServiceClient for Athena. Mainly for the interaction with |
| 42 // history module (see chrome/browser/search_engines for Chrome implementation). |
| 43 // TODO(mukai): Implement the contents of this class when it's necessary. |
| 44 class AthenaTemplateURLServiceClient : public TemplateURLServiceClient { |
| 45 public: |
| 46 AthenaTemplateURLServiceClient() {} |
| 47 virtual ~AthenaTemplateURLServiceClient() {} |
| 48 |
| 49 private: |
| 50 // TemplateURLServiceClient: |
| 51 virtual void SetOwner(TemplateURLService* owner) OVERRIDE {} |
| 52 virtual void DeleteAllSearchTermsForKeyword(TemplateURLID id) OVERRIDE {} |
| 53 virtual void SetKeywordSearchTermsForURL( |
| 54 const GURL& url, |
| 55 TemplateURLID id, |
| 56 const base::string16& term) OVERRIDE {} |
| 57 virtual void AddKeywordGeneratedVisit(const GURL& url) OVERRIDE {} |
| 58 |
| 59 DISALLOW_COPY_AND_ASSIGN(AthenaTemplateURLServiceClient); |
| 60 }; |
| 61 |
17 class UrlSearchResult : public app_list::SearchResult { | 62 class UrlSearchResult : public app_list::SearchResult { |
18 public: | 63 public: |
19 UrlSearchResult(content::BrowserContext* browser_context, | 64 UrlSearchResult(content::BrowserContext* browser_context, |
| 65 TemplateURLService* template_url_service, |
20 const base::string16& query) | 66 const base::string16& query) |
21 : browser_context_(browser_context), url_(query) { | 67 : browser_context_(browser_context), |
| 68 template_url_service_(template_url_service), |
| 69 input_(query, |
| 70 base::string16::npos /* cursor_position */, |
| 71 base::string16() /* desired_tld */, |
| 72 GURL() /* current_url */, |
| 73 metrics::OmniboxEventProto::INVALID_SPEC, |
| 74 false /* prevent_inline_autocomplete */, |
| 75 false /* prefer_keyword */, |
| 76 true /* allow_extract_keyword_match */, |
| 77 true /* want_asynchronous_matches */, |
| 78 AthenaSchemeClassifier()) { |
22 set_title(query); | 79 set_title(query); |
23 app_list::SearchResult::Tags title_tags; | 80 app_list::SearchResult::Tags title_tags; |
| 81 if (input_.type() == metrics::OmniboxInputType::URL) { |
| 82 title_tags.push_back(app_list::SearchResult::Tag( |
| 83 app_list::SearchResult::Tag::URL, 0, query.size())); |
| 84 } |
24 title_tags.push_back(app_list::SearchResult::Tag( | 85 title_tags.push_back(app_list::SearchResult::Tag( |
25 app_list::SearchResult::Tag::URL, 0, query.size())); | 86 app_list::SearchResult::Tag::MATCH, 0, query.size())); |
26 set_title_tags(title_tags); | 87 set_title_tags(title_tags); |
27 set_id(base::UTF16ToUTF8(query)); | 88 set_id(base::UTF16ToUTF8(query)); |
28 } | 89 } |
29 | 90 |
30 private: | 91 private: |
31 // Overriddenn from app_list::SearchResult: | 92 // Overriddenn from app_list::SearchResult: |
32 virtual void Open(int event_flags) OVERRIDE { | 93 virtual void Open(int event_flags) OVERRIDE { |
| 94 GURL url; |
| 95 if (input_.type() == metrics::OmniboxInputType::URL) { |
| 96 url = input_.canonicalized_url(); |
| 97 } else { |
| 98 TemplateURL* template_url = |
| 99 template_url_service_->GetDefaultSearchProvider(); |
| 100 const TemplateURLRef& search_url = template_url->url_ref(); |
| 101 TemplateURLRef::SearchTermsArgs search_terms_args(input_.text()); |
| 102 url = GURL(search_url.ReplaceSearchTerms( |
| 103 search_terms_args, template_url_service_->search_terms_data())); |
| 104 } |
33 ActivityManager::Get()->AddActivity( | 105 ActivityManager::Get()->AddActivity( |
34 ActivityFactory::Get()->CreateWebActivity(browser_context_, url_)); | 106 ActivityFactory::Get()->CreateWebActivity(browser_context_, url)); |
35 } | 107 } |
36 | 108 |
37 content::BrowserContext* browser_context_; | 109 content::BrowserContext* browser_context_; |
38 const GURL url_; | 110 TemplateURLService* template_url_service_; |
| 111 AutocompleteInput input_; |
39 | 112 |
40 DISALLOW_COPY_AND_ASSIGN(UrlSearchResult); | 113 DISALLOW_COPY_AND_ASSIGN(UrlSearchResult); |
41 }; | 114 }; |
42 | 115 |
43 } // namespace | 116 } // namespace |
44 | 117 |
45 UrlSearchProvider::UrlSearchProvider(content::BrowserContext* browser_context) | 118 UrlSearchProvider::UrlSearchProvider(content::BrowserContext* browser_context) |
46 : browser_context_(browser_context) { | 119 : browser_context_(browser_context), |
| 120 // TODO(mukai): introduce the real parameters when it's necessary. |
| 121 template_url_service_( |
| 122 new TemplateURLService(NULL /* prefs */, |
| 123 make_scoped_ptr(new SearchTermsData()), |
| 124 NULL /* KeywordWebDataService */, |
| 125 scoped_ptr<TemplateURLServiceClient>( |
| 126 new AthenaTemplateURLServiceClient()), |
| 127 NULL /*GoogleURLTracker */, |
| 128 NULL /* RapporService */, |
| 129 base::Closure() /* dsp_change_callback */)) { |
| 130 template_url_service_->Load(); |
47 } | 131 } |
48 | 132 |
49 UrlSearchProvider::~UrlSearchProvider() { | 133 UrlSearchProvider::~UrlSearchProvider() { |
50 } | 134 } |
51 | 135 |
52 void UrlSearchProvider::Start(const base::string16& query) { | 136 void UrlSearchProvider::Start(const base::string16& query) { |
53 ClearResults(); | 137 ClearResults(); |
54 Add(scoped_ptr<app_list::SearchResult>( | 138 Add(scoped_ptr<app_list::SearchResult>(new UrlSearchResult( |
55 new UrlSearchResult(browser_context_, query))); | 139 browser_context_, template_url_service_.get(), query))); |
56 } | 140 } |
57 | 141 |
58 void UrlSearchProvider::Stop() { | 142 void UrlSearchProvider::Stop() { |
59 } | 143 } |
60 | 144 |
61 } // namespace athena | 145 } // namespace athena |
OLD | NEW |