| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012 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 "chrome/browser/ui/views/ash/app_list/search_builder.h" |
| 6 |
| 7 #include "chrome/browser/autocomplete/autocomplete.h" |
| 8 #include "chrome/browser/autocomplete/autocomplete_match.h" |
| 9 #include "chrome/browser/extensions/extension_service.h" |
| 10 #include "chrome/browser/profiles/profile.h" |
| 11 #include "chrome/browser/search_engines/template_url.h" |
| 12 #include "chrome/browser/ui/browser_finder.h" |
| 13 #include "chrome/browser/ui/browser.h" |
| 14 #include "chrome/browser/ui/views/ash/extension_utils.h" |
| 15 #include "chrome/browser/ui/views/event_utils.h" |
| 16 #include "grit/generated_resources.h" |
| 17 #include "grit/theme_resources.h" |
| 18 #include "ui/app_list/search_box_model.h" |
| 19 #include "ui/app_list/search_result.h" |
| 20 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/base/resource/resource_bundle.h" |
| 22 |
| 23 namespace { |
| 24 |
| 25 int ACMatchStyleToTagStyle(int styles) { |
| 26 int tag_styles = 0; |
| 27 if (styles & ACMatchClassification::URL) |
| 28 tag_styles |= app_list::SearchResult::Tag::URL; |
| 29 if (styles & ACMatchClassification::MATCH) |
| 30 tag_styles |= app_list::SearchResult::Tag::MATCH; |
| 31 if (styles & ACMatchClassification::DIM) |
| 32 tag_styles |= app_list::SearchResult::Tag::DIM; |
| 33 |
| 34 return tag_styles; |
| 35 } |
| 36 |
| 37 // Translates ACMatchClassifications into SearchResult tags. |
| 38 void ACMatchClassificationsToTags( |
| 39 const string16& text, |
| 40 const ACMatchClassifications& text_classes, |
| 41 app_list::SearchResult::Tags* tags) { |
| 42 int tag_styles = app_list::SearchResult::Tag::NONE; |
| 43 size_t tag_start = 0; |
| 44 |
| 45 for (size_t i = 0; i < text_classes.size(); ++i) { |
| 46 const ACMatchClassification& text_class = text_classes[i]; |
| 47 |
| 48 // Closes current tag. |
| 49 if (tag_styles != app_list::SearchResult::Tag::NONE) { |
| 50 tags->push_back(app_list::SearchResult::Tag( |
| 51 tag_styles, tag_start, text_class.offset)); |
| 52 tag_styles = app_list::SearchResult::Tag::NONE; |
| 53 } |
| 54 |
| 55 if (text_class.style == ACMatchClassification::NONE) |
| 56 continue; |
| 57 |
| 58 tag_start = text_class.offset; |
| 59 tag_styles = ACMatchStyleToTagStyle(text_class.style); |
| 60 } |
| 61 |
| 62 if (tag_styles != app_list::SearchResult::Tag::NONE) { |
| 63 tags->push_back(app_list::SearchResult::Tag( |
| 64 tag_styles, tag_start, text.length())); |
| 65 } |
| 66 } |
| 67 |
| 68 // SearchBuildResult is an app list SearchResult built from an |
| 69 // AutocompleteMatch. |
| 70 class SearchBuilderResult : public app_list::SearchResult { |
| 71 public: |
| 72 SearchBuilderResult(Profile* profile, |
| 73 const AutocompleteMatch& match) |
| 74 : profile_(profile), |
| 75 match_(match) { |
| 76 UpdateIcon(); |
| 77 UpdateTitleAndDetails(); |
| 78 } |
| 79 |
| 80 const AutocompleteMatch& match() const { |
| 81 return match_; |
| 82 } |
| 83 |
| 84 private: |
| 85 void UpdateIcon() { |
| 86 const TemplateURL* template_url = match_.GetTemplateURL(profile_); |
| 87 if (template_url && template_url->IsExtensionKeyword()) { |
| 88 set_icon(profile_->GetExtensionService()->GetOmniboxPopupIcon( |
| 89 template_url->GetExtensionId())); |
| 90 return; |
| 91 } |
| 92 |
| 93 int resource_id = match_.starred ? |
| 94 IDR_OMNIBOX_STAR : AutocompleteMatch::TypeToIcon(match_.type); |
| 95 set_icon(*ui::ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 96 resource_id)); |
| 97 } |
| 98 |
| 99 void UpdateTitleAndDetails() { |
| 100 set_title(match_.contents); |
| 101 app_list::SearchResult::Tags title_tags; |
| 102 ACMatchClassificationsToTags(match_.contents, |
| 103 match_.contents_class, |
| 104 &title_tags); |
| 105 set_title_tags(title_tags); |
| 106 |
| 107 set_details(match_.description); |
| 108 app_list::SearchResult::Tags details_tags; |
| 109 ACMatchClassificationsToTags(match_.description, |
| 110 match_.description_class, |
| 111 &details_tags); |
| 112 set_details_tags(details_tags); |
| 113 } |
| 114 |
| 115 Profile* profile_; |
| 116 AutocompleteMatch match_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(SearchBuilderResult); |
| 119 }; |
| 120 |
| 121 } // namespace |
| 122 |
| 123 SearchBuilder::SearchBuilder( |
| 124 Profile* profile, |
| 125 app_list::SearchBoxModel* search_box, |
| 126 app_list::AppListModel::SearchResults* results) |
| 127 : profile_(profile), |
| 128 search_box_(search_box), |
| 129 results_(results), |
| 130 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 131 controller_(new AutocompleteController(profile, this))) { |
| 132 search_box_->SetHintText( |
| 133 l10n_util::GetStringUTF16(IDS_SEARCH_BOX_HINT)); |
| 134 search_box_->SetIcon(*ResourceBundle::GetSharedInstance(). |
| 135 GetBitmapNamed(IDR_OMNIBOX_SEARCH)); |
| 136 } |
| 137 |
| 138 SearchBuilder::~SearchBuilder() { |
| 139 } |
| 140 |
| 141 void SearchBuilder::StartSearch() { |
| 142 string16 user_text = search_box_->text(); |
| 143 string16 empty_string; |
| 144 |
| 145 // Omnibox features such as keyword selection/accepting and instant query |
| 146 // are not implemented. |
| 147 // TODO(xiyuan): Figure out the features that need to support here. |
| 148 controller_->Start(user_text, |
| 149 empty_string, // desired TLD. |
| 150 false, // don't prevent inline autocompletion |
| 151 false, // no preferred keyword provider |
| 152 true, // allow exact keyword matches |
| 153 AutocompleteInput::ALL_MATCHES); |
| 154 } |
| 155 |
| 156 void SearchBuilder::StopSearch() { |
| 157 controller_->Stop(true /* clear_result */); |
| 158 } |
| 159 |
| 160 void SearchBuilder::OpenResult(const app_list::SearchResult& result, |
| 161 int event_flags) { |
| 162 const SearchBuilderResult* builder_result = |
| 163 static_cast<const SearchBuilderResult*>(&result); |
| 164 const AutocompleteMatch& match = builder_result->match(); |
| 165 |
| 166 if (match.type == AutocompleteMatch::EXTENSION_APP) { |
| 167 ExtensionService* service = profile_->GetExtensionService(); |
| 168 const extensions::Extension* extension = |
| 169 service->GetInstalledApp(match.destination_url); |
| 170 if (extension) |
| 171 extension_utils::OpenExtension(profile_, extension, event_flags); |
| 172 } else { |
| 173 // TODO(xiyuan): What should we do for alternate url case? |
| 174 Browser* browser = browser::FindOrCreateTabbedBrowser(profile_); |
| 175 browser->OpenURL( |
| 176 content::OpenURLParams(match.destination_url, |
| 177 content::Referrer(), |
| 178 event_utils::DispositionFromEventFlags( |
| 179 event_flags), |
| 180 match.transition, |
| 181 false)); |
| 182 } |
| 183 } |
| 184 |
| 185 void SearchBuilder::OnResultChanged(bool default_match_changed) { |
| 186 // TODO(xiyuan): Handle default match properly. |
| 187 const AutocompleteResult& ac_result = controller_->result(); |
| 188 results_->DeleteAll(); |
| 189 for (ACMatches::const_iterator it = ac_result.begin(); |
| 190 it != ac_result.end(); |
| 191 ++it) { |
| 192 results_->Add(new SearchBuilderResult(profile_, *it)); |
| 193 } |
| 194 } |
| OLD | NEW |