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/test_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" |
10 #include "ui/app_list/search_result.h" | 18 #include "ui/app_list/search_result.h" |
11 #include "url/gurl.h" | 19 #include "url/gurl.h" |
12 | 20 |
13 namespace athena { | 21 namespace athena { |
14 | 22 |
15 namespace { | 23 namespace { |
16 | 24 |
| 25 // The templateURLServiceClient for Athena. Mainly for the interaction with |
| 26 // history module (see chrome/browser/search_engines for Chrome implementation). |
| 27 // TODO(mukai): Implement the contents of this class when it's necessary. |
| 28 class AthenaTemplateURLServiceClient : public TemplateURLServiceClient { |
| 29 public: |
| 30 AthenaTemplateURLServiceClient() {} |
| 31 virtual ~AthenaTemplateURLServiceClient() {} |
| 32 |
| 33 private: |
| 34 // TemplateURLServiceClient: |
| 35 virtual void SetOwner(TemplateURLService* owner) OVERRIDE { |
| 36 } |
| 37 virtual void DeleteAllSearchTermsForKeyword(TemplateURLID id) OVERRIDE { |
| 38 } |
| 39 virtual void SetKeywordSearchTermsForURL( |
| 40 const GURL& url, |
| 41 TemplateURLID id, |
| 42 const base::string16& term) OVERRIDE { |
| 43 } |
| 44 virtual void AddKeywordGeneratedVisit(const GURL& url) OVERRIDE { |
| 45 } |
| 46 |
| 47 DISALLOW_COPY_AND_ASSIGN(AthenaTemplateURLServiceClient); |
| 48 }; |
| 49 |
17 class UrlSearchResult : public app_list::SearchResult { | 50 class UrlSearchResult : public app_list::SearchResult { |
18 public: | 51 public: |
19 UrlSearchResult(content::BrowserContext* browser_context, | 52 UrlSearchResult(content::BrowserContext* browser_context, |
| 53 TemplateURLService* template_url_service, |
20 const base::string16& query) | 54 const base::string16& query) |
21 : browser_context_(browser_context), url_(query) { | 55 : browser_context_(browser_context), |
| 56 template_url_service_(template_url_service), |
| 57 input_(query, |
| 58 base::string16::npos /* cursor_position */, |
| 59 base::string16() /* desired_tld */, |
| 60 GURL() /* current_url */, |
| 61 metrics::OmniboxEventProto::INVALID_SPEC, |
| 62 false /* prevent_inline_autocomplete */, |
| 63 false /* prefer_keyword */, |
| 64 true /* allow_extract_keyword_match */, |
| 65 true /* want_asynchronous_matches */, |
| 66 TestSchemeClassifier()) { |
22 set_title(query); | 67 set_title(query); |
23 app_list::SearchResult::Tags title_tags; | 68 app_list::SearchResult::Tags title_tags; |
| 69 if (input_.type() == metrics::OmniboxInputType::URL) { |
| 70 title_tags.push_back(app_list::SearchResult::Tag( |
| 71 app_list::SearchResult::Tag::URL, 0, query.size())); |
| 72 } |
24 title_tags.push_back(app_list::SearchResult::Tag( | 73 title_tags.push_back(app_list::SearchResult::Tag( |
25 app_list::SearchResult::Tag::URL, 0, query.size())); | 74 app_list::SearchResult::Tag::MATCH, 0, query.size())); |
26 set_title_tags(title_tags); | 75 set_title_tags(title_tags); |
27 set_id(base::UTF16ToUTF8(query)); | 76 set_id(base::UTF16ToUTF8(query)); |
28 } | 77 } |
29 | 78 |
30 private: | 79 private: |
31 // Overriddenn from app_list::SearchResult: | 80 // Overriddenn from app_list::SearchResult: |
32 virtual void Open(int event_flags) OVERRIDE { | 81 virtual void Open(int event_flags) OVERRIDE { |
| 82 GURL url; |
| 83 if (input_.type() == metrics::OmniboxInputType::URL) { |
| 84 url = input_.canonicalized_url(); |
| 85 } else { |
| 86 TemplateURL* template_url = |
| 87 template_url_service_->GetDefaultSearchProvider(); |
| 88 const TemplateURLRef& search_url = template_url->url_ref(); |
| 89 TemplateURLRef::SearchTermsArgs search_terms_args(input_.text()); |
| 90 url = GURL(search_url.ReplaceSearchTerms( |
| 91 search_terms_args, template_url_service_->search_terms_data())); |
| 92 } |
33 ActivityManager::Get()->AddActivity( | 93 ActivityManager::Get()->AddActivity( |
34 ActivityFactory::Get()->CreateWebActivity(browser_context_, url_)); | 94 ActivityFactory::Get()->CreateWebActivity(browser_context_, url)); |
35 } | 95 } |
36 | 96 |
37 content::BrowserContext* browser_context_; | 97 content::BrowserContext* browser_context_; |
38 const GURL url_; | 98 TemplateURLService* template_url_service_; |
| 99 AutocompleteInput input_; |
39 | 100 |
40 DISALLOW_COPY_AND_ASSIGN(UrlSearchResult); | 101 DISALLOW_COPY_AND_ASSIGN(UrlSearchResult); |
41 }; | 102 }; |
42 | 103 |
43 } // namespace | 104 } // namespace |
44 | 105 |
45 UrlSearchProvider::UrlSearchProvider(content::BrowserContext* browser_context) | 106 UrlSearchProvider::UrlSearchProvider(content::BrowserContext* browser_context) |
46 : browser_context_(browser_context) { | 107 : browser_context_(browser_context), |
| 108 // TODO(mukai): introduce the real parameters when it's necessary. |
| 109 template_url_service_(new TemplateURLService( |
| 110 NULL /* prefs */, |
| 111 make_scoped_ptr(new SearchTermsData()), |
| 112 NULL /* KeywordWebDataService */, |
| 113 scoped_ptr<TemplateURLServiceClient>( |
| 114 new AthenaTemplateURLServiceClient()), |
| 115 NULL /*GoogleURLTracker */, |
| 116 NULL /* RapporService */, |
| 117 base::Closure() /* dsp_change_callback */)) { |
| 118 template_url_service_->Load(); |
47 } | 119 } |
48 | 120 |
49 UrlSearchProvider::~UrlSearchProvider() { | 121 UrlSearchProvider::~UrlSearchProvider() { |
50 } | 122 } |
51 | 123 |
52 void UrlSearchProvider::Start(const base::string16& query) { | 124 void UrlSearchProvider::Start(const base::string16& query) { |
53 ClearResults(); | 125 ClearResults(); |
54 Add(scoped_ptr<app_list::SearchResult>( | 126 Add(scoped_ptr<app_list::SearchResult>(new UrlSearchResult( |
55 new UrlSearchResult(browser_context_, query))); | 127 browser_context_, template_url_service_.get(), query))); |
56 } | 128 } |
57 | 129 |
58 void UrlSearchProvider::Stop() { | 130 void UrlSearchProvider::Stop() { |
59 } | 131 } |
60 | 132 |
61 } // namespace athena | 133 } // namespace athena |
OLD | NEW |