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 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 49 | 49 |
| 50 void PhysicalWebProvider::Start(const AutocompleteInput& input, | 50 void PhysicalWebProvider::Start(const AutocompleteInput& input, |
| 51 bool minimal_changes) { | 51 bool minimal_changes) { |
| 52 DCHECK(kPhysicalWebMaxMatches < kMaxMatches); | 52 DCHECK(kPhysicalWebMaxMatches < kMaxMatches); |
| 53 | 53 |
| 54 Stop(false, false); | 54 Stop(false, false); |
| 55 | 55 |
| 56 done_ = false; | 56 done_ = false; |
| 57 matches_.clear(); | 57 matches_.clear(); |
| 58 | 58 |
| 59 const bool input_from_focus = input.from_omnibox_focus(); | |
|
Mark P
2017/02/23 00:38:59
optional nit: I don't think this alias buys you mu
mattreynolds
2017/02/23 02:03:06
Removed
| |
| 60 | |
| 59 had_physical_web_suggestions_ = false; | 61 had_physical_web_suggestions_ = false; |
| 60 if (input.from_omnibox_focus()) | 62 if (input_from_focus) { |
|
Mark P
2017/02/23 00:38:59
nit: no need for { } here
mattreynolds
2017/02/23 02:03:06
Removed
| |
| 61 had_physical_web_suggestions_at_focus_or_later_ = false; | 63 BeginOmniboxSession(); |
| 64 } | |
| 62 | 65 |
| 63 // Don't provide suggestions in incognito mode. | 66 // Don't provide suggestions in incognito mode. |
|
Mark P
2017/02/23 00:38:59
nit: combine 66-72 with 74-81, as the result is th
mattreynolds
2017/02/23 02:03:06
Done.
| |
| 64 if (client_->IsOffTheRecord()) { | 67 if (client_->IsOffTheRecord()) { |
| 65 done_ = true; | 68 done_ = true; |
| 66 nearby_url_count_ = 0; | 69 nearby_url_count_ = 0; |
| 70 nearby_url_count_at_focus_ = 0; | |
|
Mark P
2017/02/23 00:38:59
Should this
had_physical_web_suggestions_at_focus_
mattreynolds
2017/02/23 02:03:06
Yep, added
| |
| 67 return; | 71 return; |
| 68 } | 72 } |
| 69 | 73 |
| 70 physical_web::PhysicalWebDataSource* data_source = | 74 physical_web::PhysicalWebDataSource* data_source = |
| 71 client_->GetPhysicalWebDataSource(); | 75 client_->GetPhysicalWebDataSource(); |
| 72 if (!data_source) { | 76 if (!data_source) { |
| 73 done_ = true; | 77 done_ = true; |
| 74 nearby_url_count_ = 0; | 78 nearby_url_count_ = 0; |
| 79 nearby_url_count_at_focus_ = 0; | |
| 75 return; | 80 return; |
| 76 } | 81 } |
| 77 | 82 |
| 78 const bool input_from_focus = input.from_omnibox_focus(); | 83 auto metadata_list = data_source->GetMetadataList(); |
| 84 nearby_url_count_ = metadata_list->size(); | |
| 85 if (input_from_focus) { | |
|
Mark P
2017/02/23 00:38:59
nit: {} unnecessary
mattreynolds
2017/02/23 02:03:06
Removed
| |
| 86 nearby_url_count_at_focus_ = nearby_url_count_; | |
| 87 } | |
| 88 | |
| 79 const bool empty_input_from_user = !input_from_focus && input.text().empty(); | 89 const bool empty_input_from_user = !input_from_focus && input.text().empty(); |
| 80 | 90 |
| 81 if (input_from_focus || empty_input_from_user) { | 91 if (input_from_focus || empty_input_from_user) { |
| 82 ConstructZeroSuggestMatches(data_source->GetMetadataList()); | 92 ConstructZeroSuggestMatches(std::move(metadata_list)); |
| 83 | 93 |
| 84 if (!matches_.empty()) { | 94 if (!matches_.empty()) { |
| 85 had_physical_web_suggestions_ = true; | 95 had_physical_web_suggestions_ = true; |
| 86 had_physical_web_suggestions_at_focus_or_later_ = true; | 96 had_physical_web_suggestions_at_focus_or_later_ = true; |
| 87 } | 97 } |
| 88 | 98 |
| 89 // Don't show zero-suggest suggestions unless the PhysicalWebZeroSuggest | 99 // Don't show zero-suggest suggestions unless the PhysicalWebZeroSuggest |
| 90 // omnibox experiment parameter is enabled. If the omnibox input is empty | 100 // omnibox experiment parameter is enabled. If the omnibox input is empty |
| 91 // because the user cleared it, also require that PhysicalWebAfterTyping is | 101 // because the user cleared it, also require that PhysicalWebAfterTyping is |
| 92 // enabled. | 102 // enabled. |
| 93 if (!zero_suggest_enabled_ || | 103 if (!zero_suggest_enabled_ || |
| 94 (empty_input_from_user && !after_typing_enabled_)) { | 104 (empty_input_from_user && !after_typing_enabled_)) { |
| 95 matches_.clear(); | 105 matches_.clear(); |
| 96 } | 106 } |
| 97 | 107 |
| 98 // In zero-suggest, Physical Web matches should never be default. If the | 108 // 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, | 109 // 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 | 110 // 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 | 111 // the omnibox causes the current page to reload. If the input field is |
| 102 // empty, no default match is required. | 112 // empty, no default match is required. |
| 103 if (!matches_.empty() && !input.text().empty()) { | 113 if (!matches_.empty() && !input.text().empty()) { |
| 104 matches_.push_back(VerbatimMatchForURL( | 114 matches_.push_back(VerbatimMatchForURL( |
| 105 client_, input, input.current_url(), history_url_provider_, -1)); | 115 client_, input, input.current_url(), history_url_provider_, -1)); |
| 106 } | 116 } |
| 107 } else { | 117 } else { |
| 108 ConstructQuerySuggestMatches(data_source->GetMetadataList(), input); | 118 ConstructQuerySuggestMatches(std::move(metadata_list), input); |
| 109 | 119 |
| 110 if (!matches_.empty()) { | 120 if (!matches_.empty()) { |
| 111 had_physical_web_suggestions_ = true; | 121 had_physical_web_suggestions_ = true; |
| 112 had_physical_web_suggestions_at_focus_or_later_ = true; | 122 had_physical_web_suggestions_at_focus_or_later_ = true; |
| 113 } | 123 } |
| 114 | 124 |
| 115 // Don't show Physical Web suggestions after the user starts typing unless | 125 // Don't show Physical Web suggestions after the user starts typing unless |
| 116 // the PhysicalWebAfterTyping omnibox experiment parameter is enabled. | 126 // the PhysicalWebAfterTyping omnibox experiment parameter is enabled. |
| 117 if (!after_typing_enabled_) { | 127 if (!after_typing_enabled_) { |
| 118 matches_.clear(); | 128 matches_.clear(); |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 141 if (had_physical_web_suggestions_) | 151 if (had_physical_web_suggestions_) |
| 142 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); | 152 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); |
| 143 if (had_physical_web_suggestions_at_focus_or_later_) { | 153 if (had_physical_web_suggestions_at_focus_or_later_) { |
| 144 new_entry.mutable_field_trial_triggered_in_session()->Add( | 154 new_entry.mutable_field_trial_triggered_in_session()->Add( |
| 145 field_trial_hashes[i]); | 155 field_trial_hashes[i]); |
| 146 } | 156 } |
| 147 } | 157 } |
| 148 | 158 |
| 149 // When the user accepts an autocomplete suggestion, record the number of | 159 // When the user accepts an autocomplete suggestion, record the number of |
| 150 // nearby Physical Web URLs at the time the provider last constructed matches. | 160 // nearby Physical Web URLs at the time the provider last constructed matches. |
| 151 UMA_HISTOGRAM_EXACT_LINEAR("Omnibox.SuggestionUsed.NearbyURLCount", | 161 UMA_HISTOGRAM_EXACT_LINEAR( |
| 152 nearby_url_count_, 50); | 162 "Omnibox.SuggestionUsed.NearbyURLCount.AtMatchCreation", |
| 163 nearby_url_count_, 50); | |
| 164 | |
| 165 // On Android, it is somehow possible to accept an omnibox suggestion without | |
| 166 // first focusing the omnibox. In this situation, the nearby URL count at | |
| 167 // focus will be invalid because the omnibox was never focused. In an earlier | |
|
Mark P
2017/02/23 00:38:59
omit the sentence "In an earlier version" and revi
mattreynolds
2017/02/23 02:03:07
Done.
| |
| 168 // version, this caused invalid data to be recorded by the histogram. Now we | |
| 169 // guard against recording the invalid data and instead record that we hit | |
| 170 // this corner case. | |
| 171 // TODO(crbug.com/691059): Remove once the focus-less suggestion mystery is | |
| 172 // solved. | |
| 173 bool suggestion_used_without_focus = | |
|
Mark P
2017/02/23 00:38:59
nit: const
mattreynolds
2017/02/23 02:03:06
Done.
| |
| 174 (nearby_url_count_at_focus_ == string::npos); | |
| 175 UMA_HISTOGRAM_BOOLEAN( | |
| 176 "Omnibox.PhysicalWebProvider.SuggestionUsedWithoutOmniboxFocus", | |
| 177 suggestion_used_without_focus); | |
| 178 | |
| 179 if (!suggestion_used_without_focus) { | |
| 180 // When the user accepts an autocomplete suggestion, record the number of | |
| 181 // nearby Physical Web URLs at the time the omnibox input was last focused. | |
| 182 UMA_HISTOGRAM_EXACT_LINEAR("Omnibox.SuggestionUsed.NearbyURLCount.AtFocus", | |
| 183 nearby_url_count_at_focus_, 50); | |
| 184 } | |
| 153 } | 185 } |
| 154 | 186 |
| 155 PhysicalWebProvider::PhysicalWebProvider( | 187 PhysicalWebProvider::PhysicalWebProvider( |
| 156 AutocompleteProviderClient* client, | 188 AutocompleteProviderClient* client, |
| 157 HistoryURLProvider* history_url_provider) | 189 HistoryURLProvider* history_url_provider) |
| 158 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), | 190 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), |
| 159 client_(client), | 191 client_(client), |
| 160 history_url_provider_(history_url_provider), | 192 history_url_provider_(history_url_provider), |
| 193 nearby_url_count_(string::npos), | |
| 194 nearby_url_count_at_focus_(string::npos), | |
| 161 zero_suggest_enabled_( | 195 zero_suggest_enabled_( |
| 162 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()), | 196 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()), |
| 163 after_typing_enabled_( | 197 after_typing_enabled_( |
| 164 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), | 198 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), |
| 165 zero_suggest_base_relevance_( | 199 zero_suggest_base_relevance_( |
| 166 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), | 200 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), |
| 167 after_typing_base_relevance_( | 201 after_typing_base_relevance_( |
| 168 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()) {} | 202 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()), |
| 203 had_physical_web_suggestions_(false), | |
| 204 had_physical_web_suggestions_at_focus_or_later_(false) {} | |
| 169 | 205 |
| 170 PhysicalWebProvider::~PhysicalWebProvider() { | 206 PhysicalWebProvider::~PhysicalWebProvider() { |
| 171 } | 207 } |
| 172 | 208 |
| 209 void PhysicalWebProvider::BeginOmniboxSession() { | |
| 210 // Some metrics are tracked per-session and must be reset each time a new | |
| 211 // session begins. Per-session metrics are recorded in AddProviderInfo. | |
|
Mark P
2017/02/23 00:38:59
Add something like
// Both these variables are set
mattreynolds
2017/02/23 02:03:06
Done.
| |
| 212 nearby_url_count_at_focus_ = string::npos; | |
| 213 had_physical_web_suggestions_at_focus_or_later_ = false; | |
| 214 } | |
| 215 | |
| 173 void PhysicalWebProvider::ConstructZeroSuggestMatches( | 216 void PhysicalWebProvider::ConstructZeroSuggestMatches( |
| 174 std::unique_ptr<physical_web::MetadataList> metadata_list) { | 217 std::unique_ptr<physical_web::MetadataList> metadata_list) { |
| 175 nearby_url_count_ = metadata_list->size(); | 218 size_t nearby_url_count = metadata_list->size(); |
| 176 size_t used_slots = 0; | 219 size_t used_slots = 0; |
| 177 | 220 |
| 178 for (size_t i = 0; i < nearby_url_count_; ++i) { | 221 for (size_t i = 0; i < nearby_url_count; ++i) { |
| 179 const auto& metadata_item = (*metadata_list)[i]; | 222 const auto& metadata_item = (*metadata_list)[i]; |
| 180 std::string url_string = metadata_item.resolved_url.spec(); | 223 std::string url_string = metadata_item.resolved_url.spec(); |
| 181 std::string title_string = metadata_item.title; | 224 std::string title_string = metadata_item.title; |
| 182 base::string16 title = | 225 base::string16 title = |
| 183 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string)); | 226 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string)); |
| 184 | 227 |
| 185 // Add match items with decreasing relevance to preserve the ordering in | 228 // Add match items with decreasing relevance to preserve the ordering in |
| 186 // the metadata list. | 229 // the metadata list. |
| 187 int relevance = zero_suggest_base_relevance_ - used_slots; | 230 int relevance = zero_suggest_base_relevance_ - used_slots; |
| 188 | 231 |
| 189 // Append an overflow item if creating a match for each metadata item would | 232 // Append an overflow item if creating a match for each metadata item would |
| 190 // exceed the match limit. | 233 // exceed the match limit. |
| 191 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; | 234 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; |
| 192 const size_t remaining_metadata = nearby_url_count_ - i; | 235 const size_t remaining_metadata = nearby_url_count - i; |
| 193 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { | 236 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { |
| 194 AppendOverflowItem(remaining_metadata, relevance, title); | 237 AppendOverflowItem(remaining_metadata, relevance, title); |
| 195 break; | 238 break; |
| 196 } | 239 } |
| 197 | 240 |
| 198 GURL url(url_string); | 241 GURL url(url_string); |
| 199 | 242 |
| 200 AutocompleteMatch match(this, relevance, false, | 243 AutocompleteMatch match(this, relevance, false, |
| 201 AutocompleteMatchType::PHYSICAL_WEB); | 244 AutocompleteMatchType::PHYSICAL_WEB); |
| 202 match.destination_url = url; | 245 match.destination_url = url; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 283 AutocompleteInput::FormattedStringWithEquivalentMeaning( | 326 AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 284 url, match.contents, client_->GetSchemeClassifier()); | 327 url, match.contents, client_->GetSchemeClassifier()); |
| 285 | 328 |
| 286 match.description = | 329 match.description = |
| 287 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION); | 330 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION); |
| 288 match.description_class.push_back( | 331 match.description_class.push_back( |
| 289 ACMatchClassification(0, ACMatchClassification::NONE)); | 332 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 290 | 333 |
| 291 matches_.push_back(match); | 334 matches_.push_back(match); |
| 292 } | 335 } |
| OLD | NEW |