Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 "components/omnibox/browser/physical_web_provider.h" | 5 #include "components/omnibox/browser/physical_web_provider.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/metrics/histogram_macros.h" | 8 #include "base/metrics/histogram_macros.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 71 client_->GetPhysicalWebDataSource(); | 71 client_->GetPhysicalWebDataSource(); |
| 72 if (!data_source) { | 72 if (!data_source) { |
| 73 done_ = true; | 73 done_ = true; |
| 74 nearby_url_count_ = 0; | 74 nearby_url_count_ = 0; |
| 75 return; | 75 return; |
| 76 } | 76 } |
| 77 | 77 |
| 78 const bool input_from_focus = input.from_omnibox_focus(); | 78 const bool input_from_focus = input.from_omnibox_focus(); |
| 79 const bool empty_input_from_user = !input_from_focus && input.text().empty(); | 79 const bool empty_input_from_user = !input_from_focus && input.text().empty(); |
| 80 | 80 |
| 81 auto metadata_list = data_source->GetMetadataList(); | |
| 82 nearby_url_count_ = metadata_list->size(); | |
| 83 | |
| 81 if (input_from_focus || empty_input_from_user) { | 84 if (input_from_focus || empty_input_from_user) { |
| 82 ConstructZeroSuggestMatches(data_source->GetMetadataList()); | 85 ConstructZeroSuggestMatches(std::move(metadata_list)); |
|
Mark P
2017/02/11 05:17:39
I don't understand this move operation. Then agai
mattreynolds
2017/02/13 19:00:04
This preserves the same behavior as before. In the
Mark P
2017/02/15 06:45:53
Ah, okay. Thanks for the explanation.
| |
| 83 | 86 |
| 84 if (!matches_.empty()) { | 87 if (!matches_.empty()) { |
| 85 had_physical_web_suggestions_ = true; | 88 had_physical_web_suggestions_ = true; |
| 86 had_physical_web_suggestions_at_focus_or_later_ = true; | 89 had_physical_web_suggestions_at_focus_or_later_ = true; |
| 87 } | 90 } |
| 88 | 91 |
| 89 // Don't show zero-suggest suggestions unless the PhysicalWebZeroSuggest | 92 // Don't show zero-suggest suggestions unless the PhysicalWebZeroSuggest |
| 90 // omnibox experiment parameter is enabled. If the omnibox input is empty | 93 // omnibox experiment parameter is enabled. If the omnibox input is empty |
| 91 // because the user cleared it, also require that PhysicalWebAfterTyping is | 94 // because the user cleared it, also require that PhysicalWebAfterTyping is |
| 92 // enabled. | 95 // enabled. |
| 93 if (!zero_suggest_enabled_ || | 96 if (!zero_suggest_enabled_ || |
| 94 (empty_input_from_user && !after_typing_enabled_)) { | 97 (empty_input_from_user && !after_typing_enabled_)) { |
| 95 matches_.clear(); | 98 matches_.clear(); |
| 96 } | 99 } |
| 97 | 100 |
| 98 // In zero-suggest, Physical Web matches should never be default. If the | 101 // In zero-suggest, Physical Web matches should never be default. If the |
| 99 // omnibox input is non-empty and we have at least one Physical Web match, | 102 // omnibox input is non-empty and we have at least one Physical Web match, |
| 100 // add the current URL as the default so that hitting enter after focusing | 103 // add the current URL as the default so that hitting enter after focusing |
| 101 // the omnibox causes the current page to reload. If the input field is | 104 // the omnibox causes the current page to reload. If the input field is |
| 102 // empty, no default match is required. | 105 // empty, no default match is required. |
| 103 if (!matches_.empty() && !input.text().empty()) { | 106 if (!matches_.empty() && !input.text().empty()) { |
| 104 matches_.push_back(VerbatimMatchForURL( | 107 matches_.push_back(VerbatimMatchForURL( |
| 105 client_, input, input.current_url(), history_url_provider_, -1)); | 108 client_, input, input.current_url(), history_url_provider_, -1)); |
| 106 } | 109 } |
| 107 } else { | 110 } else { |
| 108 ConstructQuerySuggestMatches(data_source->GetMetadataList(), input); | 111 ConstructQuerySuggestMatches(std::move(metadata_list), input); |
| 109 | 112 |
| 110 if (!matches_.empty()) { | 113 if (!matches_.empty()) { |
| 111 had_physical_web_suggestions_ = true; | 114 had_physical_web_suggestions_ = true; |
| 112 had_physical_web_suggestions_at_focus_or_later_ = true; | 115 had_physical_web_suggestions_at_focus_or_later_ = true; |
| 113 } | 116 } |
| 114 | 117 |
| 115 // Don't show Physical Web suggestions after the user starts typing unless | 118 // Don't show Physical Web suggestions after the user starts typing unless |
| 116 // the PhysicalWebAfterTyping omnibox experiment parameter is enabled. | 119 // the PhysicalWebAfterTyping omnibox experiment parameter is enabled. |
| 117 if (!after_typing_enabled_) { | 120 if (!after_typing_enabled_) { |
| 118 matches_.clear(); | 121 matches_.clear(); |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 151 UMA_HISTOGRAM_EXACT_LINEAR("Omnibox.SuggestionUsed.NearbyURLCount", | 154 UMA_HISTOGRAM_EXACT_LINEAR("Omnibox.SuggestionUsed.NearbyURLCount", |
| 152 nearby_url_count_, 50); | 155 nearby_url_count_, 50); |
| 153 } | 156 } |
| 154 | 157 |
| 155 PhysicalWebProvider::PhysicalWebProvider( | 158 PhysicalWebProvider::PhysicalWebProvider( |
| 156 AutocompleteProviderClient* client, | 159 AutocompleteProviderClient* client, |
| 157 HistoryURLProvider* history_url_provider) | 160 HistoryURLProvider* history_url_provider) |
| 158 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), | 161 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), |
| 159 client_(client), | 162 client_(client), |
| 160 history_url_provider_(history_url_provider), | 163 history_url_provider_(history_url_provider), |
| 164 nearby_url_count_(0), | |
| 161 zero_suggest_enabled_( | 165 zero_suggest_enabled_( |
| 162 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()), | 166 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()), |
| 163 after_typing_enabled_( | 167 after_typing_enabled_( |
| 164 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), | 168 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), |
| 165 zero_suggest_base_relevance_( | 169 zero_suggest_base_relevance_( |
| 166 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), | 170 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), |
| 167 after_typing_base_relevance_( | 171 after_typing_base_relevance_( |
| 168 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()) {} | 172 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()) {} |
| 169 | 173 |
| 170 PhysicalWebProvider::~PhysicalWebProvider() { | 174 PhysicalWebProvider::~PhysicalWebProvider() { |
| 171 } | 175 } |
| 172 | 176 |
| 173 void PhysicalWebProvider::ConstructZeroSuggestMatches( | 177 void PhysicalWebProvider::ConstructZeroSuggestMatches( |
| 174 std::unique_ptr<physical_web::MetadataList> metadata_list) { | 178 std::unique_ptr<physical_web::MetadataList> metadata_list) { |
| 175 nearby_url_count_ = metadata_list->size(); | 179 size_t nearby_url_count = metadata_list->size(); |
| 176 size_t used_slots = 0; | 180 size_t used_slots = 0; |
| 177 | 181 |
| 178 for (size_t i = 0; i < nearby_url_count_; ++i) { | 182 for (size_t i = 0; i < nearby_url_count; ++i) { |
| 179 const auto& metadata_item = (*metadata_list)[i]; | 183 const auto& metadata_item = (*metadata_list)[i]; |
| 180 std::string url_string = metadata_item.resolved_url.spec(); | 184 std::string url_string = metadata_item.resolved_url.spec(); |
| 181 std::string title_string = metadata_item.title; | 185 std::string title_string = metadata_item.title; |
| 182 base::string16 title = | 186 base::string16 title = |
| 183 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string)); | 187 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string)); |
| 184 | 188 |
| 185 // Add match items with decreasing relevance to preserve the ordering in | 189 // Add match items with decreasing relevance to preserve the ordering in |
| 186 // the metadata list. | 190 // the metadata list. |
| 187 int relevance = zero_suggest_base_relevance_ - used_slots; | 191 int relevance = zero_suggest_base_relevance_ - used_slots; |
| 188 | 192 |
| 189 // Append an overflow item if creating a match for each metadata item would | 193 // Append an overflow item if creating a match for each metadata item would |
| 190 // exceed the match limit. | 194 // exceed the match limit. |
| 191 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; | 195 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; |
| 192 const size_t remaining_metadata = nearby_url_count_ - i; | 196 const size_t remaining_metadata = nearby_url_count - i; |
| 193 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { | 197 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { |
| 194 AppendOverflowItem(remaining_metadata, relevance, title); | 198 AppendOverflowItem(remaining_metadata, relevance, title); |
| 195 break; | 199 break; |
| 196 } | 200 } |
| 197 | 201 |
| 198 GURL url(url_string); | 202 GURL url(url_string); |
| 199 | 203 |
| 200 AutocompleteMatch match(this, relevance, false, | 204 AutocompleteMatch match(this, relevance, false, |
| 201 AutocompleteMatchType::PHYSICAL_WEB); | 205 AutocompleteMatchType::PHYSICAL_WEB); |
| 202 match.destination_url = url; | 206 match.destination_url = url; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 AutocompleteInput::FormattedStringWithEquivalentMeaning( | 287 AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 284 url, match.contents, client_->GetSchemeClassifier()); | 288 url, match.contents, client_->GetSchemeClassifier()); |
| 285 | 289 |
| 286 match.description = | 290 match.description = |
| 287 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION); | 291 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION); |
| 288 match.description_class.push_back( | 292 match.description_class.push_back( |
| 289 ACMatchClassification(0, ACMatchClassification::NONE)); | 293 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 290 | 294 |
| 291 matches_.push_back(match); | 295 matches_.push_back(match); |
| 292 } | 296 } |
| OLD | NEW |