Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Side by Side Diff: components/omnibox/browser/physical_web_provider.cc

Issue 2689803002: Ensure nearby URL count metric is properly initialized (Closed)
Patch Set: fix errors Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
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()) 60 if (input.from_omnibox_focus())
61 had_physical_web_suggestions_at_focus_or_later_ = false; 61 BeginOmniboxSession();
62 62
63 // Don't provide suggestions in incognito mode. 63 physical_web::PhysicalWebDataSource* data_source =
64 if (client_->IsOffTheRecord()) { 64 client_->GetPhysicalWebDataSource();
65
66 // Don't provide results in incognito mode or if the data source could not be
67 // created.
68 if (client_->IsOffTheRecord() || !data_source) {
65 done_ = true; 69 done_ = true;
66 nearby_url_count_ = 0; 70 nearby_url_count_ = 0;
71 nearby_url_count_at_focus_ = 0;
72 had_physical_web_suggestions_at_focus_or_later_ = false;
67 return; 73 return;
68 } 74 }
69 75
70 physical_web::PhysicalWebDataSource* data_source = 76 auto metadata_list = data_source->GetMetadataList();
71 client_->GetPhysicalWebDataSource(); 77 nearby_url_count_ = metadata_list->size();
72 if (!data_source) { 78 if (input.from_omnibox_focus())
73 done_ = true; 79 nearby_url_count_at_focus_ = nearby_url_count_;
74 nearby_url_count_ = 0;
75 return;
76 }
77 80
78 const bool input_from_focus = input.from_omnibox_focus(); 81 const bool empty_input_from_user =
79 const bool empty_input_from_user = !input_from_focus && input.text().empty(); 82 !input.from_omnibox_focus() && input.text().empty();
80 83
81 if (input_from_focus || empty_input_from_user) { 84 if (input.from_omnibox_focus() || empty_input_from_user) {
82 ConstructZeroSuggestMatches(data_source->GetMetadataList()); 85 ConstructZeroSuggestMatches(std::move(metadata_list));
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 22 matching lines...) Expand all
141 if (had_physical_web_suggestions_) 144 if (had_physical_web_suggestions_)
142 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]); 145 new_entry.mutable_field_trial_triggered()->Add(field_trial_hashes[i]);
143 if (had_physical_web_suggestions_at_focus_or_later_) { 146 if (had_physical_web_suggestions_at_focus_or_later_) {
144 new_entry.mutable_field_trial_triggered_in_session()->Add( 147 new_entry.mutable_field_trial_triggered_in_session()->Add(
145 field_trial_hashes[i]); 148 field_trial_hashes[i]);
146 } 149 }
147 } 150 }
148 151
149 // When the user accepts an autocomplete suggestion, record the number of 152 // When the user accepts an autocomplete suggestion, record the number of
150 // nearby Physical Web URLs at the time the provider last constructed matches. 153 // nearby Physical Web URLs at the time the provider last constructed matches.
151 UMA_HISTOGRAM_EXACT_LINEAR("Omnibox.SuggestionUsed.NearbyURLCount", 154 UMA_HISTOGRAM_EXACT_LINEAR(
152 nearby_url_count_, 50); 155 "Omnibox.SuggestionUsed.NearbyURLCount.AtMatchCreation",
156 nearby_url_count_, 50);
157
158 // On Android, it is somehow possible to accept an omnibox suggestion without
159 // first focusing the omnibox. In this situation, the nearby URL count at
160 // focus will be invalid because the omnibox was never focused. We guard
161 // against recording the invalid data and instead record that we hit this
162 // corner case.
163 // TODO(crbug.com/691059): Remove once the focus-less suggestion mystery is
164 // solved.
165 const bool suggestion_used_without_focus =
166 (nearby_url_count_at_focus_ == std::string::npos);
167 UMA_HISTOGRAM_BOOLEAN(
168 "Omnibox.PhysicalWebProvider.SuggestionUsedWithoutOmniboxFocus",
169 suggestion_used_without_focus);
170
171 if (!suggestion_used_without_focus) {
172 // When the user accepts an autocomplete suggestion, record the number of
173 // nearby Physical Web URLs at the time the omnibox input was last focused.
174 UMA_HISTOGRAM_EXACT_LINEAR("Omnibox.SuggestionUsed.NearbyURLCount.AtFocus",
175 nearby_url_count_at_focus_, 50);
176 }
153 } 177 }
154 178
155 PhysicalWebProvider::PhysicalWebProvider( 179 PhysicalWebProvider::PhysicalWebProvider(
156 AutocompleteProviderClient* client, 180 AutocompleteProviderClient* client,
157 HistoryURLProvider* history_url_provider) 181 HistoryURLProvider* history_url_provider)
158 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), 182 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB),
159 client_(client), 183 client_(client),
160 history_url_provider_(history_url_provider), 184 history_url_provider_(history_url_provider),
185 nearby_url_count_(std::string::npos),
186 nearby_url_count_at_focus_(std::string::npos),
161 zero_suggest_enabled_( 187 zero_suggest_enabled_(
162 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()), 188 OmniboxFieldTrial::InPhysicalWebZeroSuggestFieldTrial()),
163 after_typing_enabled_( 189 after_typing_enabled_(
164 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()), 190 OmniboxFieldTrial::InPhysicalWebAfterTypingFieldTrial()),
165 zero_suggest_base_relevance_( 191 zero_suggest_base_relevance_(
166 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()), 192 OmniboxFieldTrial::GetPhysicalWebZeroSuggestBaseRelevance()),
167 after_typing_base_relevance_( 193 after_typing_base_relevance_(
168 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()) {} 194 OmniboxFieldTrial::GetPhysicalWebAfterTypingBaseRelevance()),
195 had_physical_web_suggestions_(false),
196 had_physical_web_suggestions_at_focus_or_later_(false) {}
169 197
170 PhysicalWebProvider::~PhysicalWebProvider() { 198 PhysicalWebProvider::~PhysicalWebProvider() {
171 } 199 }
172 200
201 void PhysicalWebProvider::BeginOmniboxSession() {
202 // Some metrics are tracked per-session and must be reset each time a new
203 // session begins. Per-session metrics are recorded in AddProviderInfo.
204 // The values set here are intended to be overwritten in Start() when the user
205 // focuses the omnibox.
Mark P 2017/02/23 05:09:49 Now that I read this code closer, I don't think th
mattreynolds 2017/02/23 19:42:14 Makes sense, removed.
206 nearby_url_count_at_focus_ = std::string::npos;
207 had_physical_web_suggestions_at_focus_or_later_ = false;
208 }
209
173 void PhysicalWebProvider::ConstructZeroSuggestMatches( 210 void PhysicalWebProvider::ConstructZeroSuggestMatches(
174 std::unique_ptr<physical_web::MetadataList> metadata_list) { 211 std::unique_ptr<physical_web::MetadataList> metadata_list) {
175 nearby_url_count_ = metadata_list->size(); 212 size_t nearby_url_count = metadata_list->size();
176 size_t used_slots = 0; 213 size_t used_slots = 0;
177 214
178 for (size_t i = 0; i < nearby_url_count_; ++i) { 215 for (size_t i = 0; i < nearby_url_count; ++i) {
179 const auto& metadata_item = (*metadata_list)[i]; 216 const auto& metadata_item = (*metadata_list)[i];
180 std::string url_string = metadata_item.resolved_url.spec(); 217 std::string url_string = metadata_item.resolved_url.spec();
181 std::string title_string = metadata_item.title; 218 std::string title_string = metadata_item.title;
182 base::string16 title = 219 base::string16 title =
183 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string)); 220 AutocompleteMatch::SanitizeString(base::UTF8ToUTF16(title_string));
184 221
185 // Add match items with decreasing relevance to preserve the ordering in 222 // Add match items with decreasing relevance to preserve the ordering in
186 // the metadata list. 223 // the metadata list.
187 int relevance = zero_suggest_base_relevance_ - used_slots; 224 int relevance = zero_suggest_base_relevance_ - used_slots;
188 225
189 // Append an overflow item if creating a match for each metadata item would 226 // Append an overflow item if creating a match for each metadata item would
190 // exceed the match limit. 227 // exceed the match limit.
191 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots; 228 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots;
192 const size_t remaining_metadata = nearby_url_count_ - i; 229 const size_t remaining_metadata = nearby_url_count - i;
193 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { 230 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) {
194 AppendOverflowItem(remaining_metadata, relevance, title); 231 AppendOverflowItem(remaining_metadata, relevance, title);
195 break; 232 break;
196 } 233 }
197 234
198 GURL url(url_string); 235 GURL url(url_string);
199 236
200 AutocompleteMatch match(this, relevance, false, 237 AutocompleteMatch match(this, relevance, false,
201 AutocompleteMatchType::PHYSICAL_WEB); 238 AutocompleteMatchType::PHYSICAL_WEB);
202 match.destination_url = url; 239 match.destination_url = url;
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
283 AutocompleteInput::FormattedStringWithEquivalentMeaning( 320 AutocompleteInput::FormattedStringWithEquivalentMeaning(
284 url, match.contents, client_->GetSchemeClassifier()); 321 url, match.contents, client_->GetSchemeClassifier());
285 322
286 match.description = 323 match.description =
287 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION); 324 l10n_util::GetStringUTF16(IDS_PHYSICAL_WEB_OVERFLOW_DESCRIPTION);
288 match.description_class.push_back( 325 match.description_class.push_back(
289 ACMatchClassification(0, ACMatchClassification::NONE)); 326 ACMatchClassification(0, ACMatchClassification::NONE));
290 327
291 matches_.push_back(match); 328 matches_.push_back(match);
292 } 329 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698