| 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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 had_physical_web_suggestions_ = false; | 59 had_physical_web_suggestions_ = false; |
| 60 if (input.from_omnibox_focus()) | |
| 61 had_physical_web_suggestions_at_focus_or_later_ = false; | |
| 62 | 60 |
| 63 // Don't provide suggestions in incognito mode. | 61 physical_web::PhysicalWebDataSource* data_source = |
| 64 if (client_->IsOffTheRecord()) { | 62 client_->GetPhysicalWebDataSource(); |
| 63 |
| 64 // Don't provide results in incognito mode or if the data source could not be |
| 65 // created. |
| 66 if (client_->IsOffTheRecord() || !data_source) { |
| 65 done_ = true; | 67 done_ = true; |
| 66 nearby_url_count_ = 0; | 68 nearby_url_count_ = 0; |
| 69 nearby_url_count_at_focus_ = 0; |
| 70 had_physical_web_suggestions_at_focus_or_later_ = false; |
| 67 return; | 71 return; |
| 68 } | 72 } |
| 69 | 73 |
| 70 physical_web::PhysicalWebDataSource* data_source = | 74 auto metadata_list = data_source->GetMetadataList(); |
| 71 client_->GetPhysicalWebDataSource(); | 75 nearby_url_count_ = metadata_list->size(); |
| 72 if (!data_source) { | 76 if (input.from_omnibox_focus()) |
| 73 done_ = true; | 77 nearby_url_count_at_focus_ = nearby_url_count_; |
| 74 nearby_url_count_ = 0; | |
| 75 return; | |
| 76 } | |
| 77 | 78 |
| 78 const bool input_from_focus = input.from_omnibox_focus(); | 79 const bool empty_input_from_user = |
| 79 const bool empty_input_from_user = !input_from_focus && input.text().empty(); | 80 !input.from_omnibox_focus() && input.text().empty(); |
| 80 | 81 |
| 81 if (input_from_focus || empty_input_from_user) { | 82 if (input.from_omnibox_focus() || empty_input_from_user) { |
| 82 ConstructZeroSuggestMatches(data_source->GetMetadataList()); | 83 ConstructZeroSuggestMatches(std::move(metadata_list)); |
| 83 | 84 |
| 84 if (!matches_.empty()) { | 85 if (!matches_.empty()) { |
| 85 had_physical_web_suggestions_ = true; | 86 had_physical_web_suggestions_ = true; |
| 86 had_physical_web_suggestions_at_focus_or_later_ = true; | 87 had_physical_web_suggestions_at_focus_or_later_ = true; |
| 87 } | 88 } |
| 88 | 89 |
| 89 // Don't show zero-suggest suggestions unless the PhysicalWebZeroSuggest | 90 // Don't show zero-suggest suggestions unless the PhysicalWebZeroSuggest |
| 90 // omnibox experiment parameter is enabled. If the omnibox input is empty | 91 // omnibox experiment parameter is enabled. If the omnibox input is empty |
| 91 // because the user cleared it, also require that PhysicalWebAfterTyping is | 92 // because the user cleared it, also require that PhysicalWebAfterTyping is |
| 92 // enabled. | 93 // enabled. |
| 93 if (!zero_suggest_enabled_ || | 94 if (!zero_suggest_enabled_ || |
| 94 (empty_input_from_user && !after_typing_enabled_)) { | 95 (empty_input_from_user && !after_typing_enabled_)) { |
| 95 matches_.clear(); | 96 matches_.clear(); |
| 96 } | 97 } |
| 97 | 98 |
| 98 // In zero-suggest, Physical Web matches should never be default. If the | 99 // 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, | 100 // 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 | 101 // 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 | 102 // the omnibox causes the current page to reload. If the input field is |
| 102 // empty, no default match is required. | 103 // empty, no default match is required. |
| 103 if (!matches_.empty() && !input.text().empty()) { | 104 if (!matches_.empty() && !input.text().empty()) { |
| 104 matches_.push_back(VerbatimMatchForURL( | 105 matches_.push_back(VerbatimMatchForURL( |
| 105 client_, input, input.current_url(), history_url_provider_, -1)); | 106 client_, input, input.current_url(), history_url_provider_, -1)); |
| 106 } | 107 } |
| 107 } else { | 108 } else { |
| 108 ConstructQuerySuggestMatches(data_source->GetMetadataList(), input); | 109 ConstructQuerySuggestMatches(std::move(metadata_list), input); |
| 109 | 110 |
| 110 if (!matches_.empty()) { | 111 if (!matches_.empty()) { |
| 111 had_physical_web_suggestions_ = true; | 112 had_physical_web_suggestions_ = true; |
| 112 had_physical_web_suggestions_at_focus_or_later_ = true; | 113 had_physical_web_suggestions_at_focus_or_later_ = true; |
| 113 } | 114 } |
| 114 | 115 |
| 115 // Don't show Physical Web suggestions after the user starts typing unless | 116 // Don't show Physical Web suggestions after the user starts typing unless |
| 116 // the PhysicalWebAfterTyping omnibox experiment parameter is enabled. | 117 // the PhysicalWebAfterTyping omnibox experiment parameter is enabled. |
| 117 if (!after_typing_enabled_) { | 118 if (!after_typing_enabled_) { |
| 118 matches_.clear(); | 119 matches_.clear(); |
| (...skipping 22 matching lines...) Expand all Loading... |
| 141 if (had_physical_web_suggestions_) | 142 if (had_physical_web_suggestions_) |
| 142 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); | 143 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); |
| 143 if (had_physical_web_suggestions_at_focus_or_later_) { | 144 if (had_physical_web_suggestions_at_focus_or_later_) { |
| 144 new_entry.mutable_field_trial_triggered_in_session()->Add( | 145 new_entry.mutable_field_trial_triggered_in_session()->Add( |
| 145 field_trial_hashes[i]); | 146 field_trial_hashes[i]); |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| 149 // When the user accepts an autocomplete suggestion, record the number of | 150 // When the user accepts an autocomplete suggestion, record the number of |
| 150 // nearby Physical Web URLs at the time the provider last constructed matches. | 151 // nearby Physical Web URLs at the time the provider last constructed matches. |
| 151 UMA_HISTOGRAM_EXACT_LINEAR("Omnibox.SuggestionUsed.NearbyURLCount", | 152 UMA_HISTOGRAM_EXACT_LINEAR( |
| 152 nearby_url_count_, 50); | 153 "Omnibox.SuggestionUsed.NearbyURLCount.AtMatchCreation", |
| 154 nearby_url_count_, 50); |
| 155 |
| 156 // On Android, it is somehow possible to accept an omnibox suggestion without |
| 157 // first focusing the omnibox. In this situation, the nearby URL count at |
| 158 // focus will be invalid because the omnibox was never focused. We guard |
| 159 // against recording the invalid data and instead record that we hit this |
| 160 // corner case. |
| 161 // TODO(crbug.com/691059): Remove once the focus-less suggestion mystery is |
| 162 // solved. |
| 163 const bool suggestion_used_without_focus = |
| 164 (nearby_url_count_at_focus_ == std::string::npos); |
| 165 UMA_HISTOGRAM_BOOLEAN( |
| 166 "Omnibox.PhysicalWebProvider.SuggestionUsedWithoutOmniboxFocus", |
| 167 suggestion_used_without_focus); |
| 168 |
| 169 if (!suggestion_used_without_focus) { |
| 170 // When the user accepts an autocomplete suggestion, record the number of |
| 171 // nearby Physical Web URLs at the time the omnibox input was last focused. |
| 172 UMA_HISTOGRAM_EXACT_LINEAR("Omnibox.SuggestionUsed.NearbyURLCount.AtFocus", |
| 173 nearby_url_count_at_focus_, 50); |
| 174 } |
| 153 } | 175 } |
| 154 | 176 |
| 155 PhysicalWebProvider::PhysicalWebProvider( | 177 PhysicalWebProvider::PhysicalWebProvider( |
| 156 AutocompleteProviderClient* client, | 178 AutocompleteProviderClient* client, |
| 157 HistoryURLProvider* history_url_provider) | 179 HistoryURLProvider* history_url_provider) |
| 158 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), | 180 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), |
| 159 client_(client), | 181 client_(client), |
| 160 history_url_provider_(history_url_provider), | 182 history_url_provider_(history_url_provider), |
| 183 nearby_url_count_(std::string::npos), |
| 184 nearby_url_count_at_focus_(std::string::npos), |
| 161 zero_suggest_enabled_( | 185 zero_suggest_enabled_( |
| 162 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()), | 186 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()), |
| 163 after_typing_enabled_( | 187 after_typing_enabled_( |
| 164 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), | 188 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), |
| 165 zero_suggest_base_relevance_( | 189 zero_suggest_base_relevance_( |
| 166 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), | 190 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), |
| 167 after_typing_base_relevance_( | 191 after_typing_base_relevance_( |
| 168 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()) {} | 192 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()), |
| 193 had_physical_web_suggestions_(false), |
| 194 had_physical_web_suggestions_at_focus_or_later_(false) {} |
| 169 | 195 |
| 170 PhysicalWebProvider::~PhysicalWebProvider() { | 196 PhysicalWebProvider::~PhysicalWebProvider() { |
| 171 } | 197 } |
| 172 | 198 |
| 173 void PhysicalWebProvider::ConstructZeroSuggestMatches( | 199 void PhysicalWebProvider::ConstructZeroSuggestMatches( |
| 174 std::unique_ptr<physical_web::MetadataList> metadata_list) { | 200 std::unique_ptr<physical_web::MetadataList> metadata_list) { |
| 175 nearby_url_count_ = metadata_list->size(); | 201 size_t nearby_url_count = metadata_list->size(); |
| 176 size_t used_slots = 0; | 202 size_t used_slots = 0; |
| 177 | 203 |
| 178 for (size_t i = 0; i < nearby_url_count_; ++i) { | 204 for (size_t i = 0; i < nearby_url_count; ++i) { |
| 179 const auto& metadata_item = (*metadata_list)[i]; | 205 const auto& metadata_item = (*metadata_list)[i]; |
| 180 std::string url_string = metadata_item.resolved_url.spec(); | 206 std::string url_string = metadata_item.resolved_url.spec(); |
| 181 std::string title_string = metadata_item.title; | 207 std::string title_string = metadata_item.title; |
| 182 base::string16 title = | 208 base::string16 title = |
| 183 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string)); | 209 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string)); |
| 184 | 210 |
| 185 // Add match items with decreasing relevance to preserve the ordering in | 211 // Add match items with decreasing relevance to preserve the ordering in |
| 186 // the metadata list. | 212 // the metadata list. |
| 187 int relevance = zero_suggest_base_relevance_ - used_slots; | 213 int relevance = zero_suggest_base_relevance_ - used_slots; |
| 188 | 214 |
| 189 // Append an overflow item if creating a match for each metadata item would | 215 // Append an overflow item if creating a match for each metadata item would |
| 190 // exceed the match limit. | 216 // exceed the match limit. |
| 191 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; | 217 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; |
| 192 const size_t remaining_metadata = nearby_url_count_ - i; | 218 const size_t remaining_metadata = nearby_url_count - i; |
| 193 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { | 219 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { |
| 194 AppendOverflowItem(remaining_metadata, relevance, title); | 220 AppendOverflowItem(remaining_metadata, relevance, title); |
| 195 break; | 221 break; |
| 196 } | 222 } |
| 197 | 223 |
| 198 GURL url(url_string); | 224 GURL url(url_string); |
| 199 | 225 |
| 200 AutocompleteMatch match(this, relevance, false, | 226 AutocompleteMatch match(this, relevance, false, |
| 201 AutocompleteMatchType::PHYSICAL_WEB); | 227 AutocompleteMatchType::PHYSICAL_WEB); |
| 202 match.destination_url = url; | 228 match.destination_url = url; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 AutocompleteInput::FormattedStringWithEquivalentMeaning( | 309 AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 284 url, match.contents, client_->GetSchemeClassifier()); | 310 url, match.contents, client_->GetSchemeClassifier()); |
| 285 | 311 |
| 286 match.description = | 312 match.description = |
| 287 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION); | 313 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION); |
| 288 match.description_class.push_back( | 314 match.description_class.push_back( |
| 289 ACMatchClassification(0, ACMatchClassification::NONE)); | 315 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 290 | 316 |
| 291 matches_.push_back(match); | 317 matches_.push_back(match); |
| 292 } | 318 } |
| OLD | NEW |