| 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(); |
| 60 |
| 59 had_physical_web_suggestions_ = false; | 61 had_physical_web_suggestions_ = false; |
| 60 if (input.from_omnibox_focus()) | 62 if (input_from_focus) { |
| 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. |
| 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; |
| 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) { |
| 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 24 matching lines...) Expand all Loading... |
| 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("Omnibox.SuggestionUsed.NearbyURLCount", |
| 152 nearby_url_count_, 50); | 162 nearby_url_count_, 50); |
| 163 |
| 164 // When the user accepts an autocomplete suggestion, record the number of |
| 165 // nearby Physical Web URLs at the time the omnibox input was last focused. |
| 166 UMA_HISTOGRAM_EXACT_LINEAR("Omnibox.SuggestionUsed.NearbyURLCount.AtFocus", |
| 167 nearby_url_count_at_focus_, 50); |
| 153 } | 168 } |
| 154 | 169 |
| 155 PhysicalWebProvider::PhysicalWebProvider( | 170 PhysicalWebProvider::PhysicalWebProvider( |
| 156 AutocompleteProviderClient* client, | 171 AutocompleteProviderClient* client, |
| 157 HistoryURLProvider* history_url_provider) | 172 HistoryURLProvider* history_url_provider) |
| 158 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), | 173 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), |
| 159 client_(client), | 174 client_(client), |
| 160 history_url_provider_(history_url_provider), | 175 history_url_provider_(history_url_provider), |
| 176 nearby_url_count_(0), |
| 177 nearby_url_count_at_focus_(0), |
| 161 zero_suggest_enabled_( | 178 zero_suggest_enabled_( |
| 162 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()), | 179 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()), |
| 163 after_typing_enabled_( | 180 after_typing_enabled_( |
| 164 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), | 181 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), |
| 165 zero_suggest_base_relevance_( | 182 zero_suggest_base_relevance_( |
| 166 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), | 183 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), |
| 167 after_typing_base_relevance_( | 184 after_typing_base_relevance_( |
| 168 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()) {} | 185 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()), |
| 186 had_physical_web_suggestions_(false), |
| 187 had_physical_web_suggestions_at_focus_or_later_(false) {} |
| 169 | 188 |
| 170 PhysicalWebProvider::~PhysicalWebProvider() { | 189 PhysicalWebProvider::~PhysicalWebProvider() { |
| 171 } | 190 } |
| 172 | 191 |
| 192 void PhysicalWebProvider::BeginOmniboxSession() { |
| 193 // Some metrics are tracked per-session and must be reset each time a new |
| 194 // session begins. Per-session metrics are recorded in AddProviderInfo. |
| 195 nearby_url_count_at_focus_ = 0; |
| 196 had_physical_web_suggestions_at_focus_or_later_ = false; |
| 197 } |
| 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 |