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

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

Issue 2266083002: Avoid DCHECK failure for chrome:// URLs in autocomplete suggestions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fix OmniboxViewViewsTest.CloseOmniboxPopupOnTextDrag Created 4 years, 3 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 "base/memory/ptr_util.h" 5 #include "base/memory/ptr_util.h"
6 #include "base/strings/utf_string_conversions.h" 6 #include "base/strings/utf_string_conversions.h"
7 #include "base/values.h" 7 #include "base/values.h"
8 #include "components/omnibox/browser/autocomplete_provider_client.h" 8 #include "components/omnibox/browser/autocomplete_provider_client.h"
9 #include "components/omnibox/browser/autocomplete_provider_listener.h" 9 #include "components/omnibox/browser/autocomplete_provider_listener.h"
10 #include "components/omnibox/browser/history_url_provider.h"
10 #include "components/omnibox/browser/physical_web_provider.h" 11 #include "components/omnibox/browser/physical_web_provider.h"
12 #include "components/omnibox/browser/verbatim_match.h"
11 #include "components/physical_web/data_source/physical_web_data_source.h" 13 #include "components/physical_web/data_source/physical_web_data_source.h"
12 #include "components/url_formatter/url_formatter.h" 14 #include "components/url_formatter/url_formatter.h"
13 #include "grit/components_strings.h" 15 #include "grit/components_strings.h"
14 #include "ui/base/l10n/l10n_util.h" 16 #include "ui/base/l10n/l10n_util.h"
15 #include "url/gurl.h" 17 #include "url/gurl.h"
16 18
17 namespace { 19 namespace {
18 20
19 // The maximum number of match results to provide. If the number of nearby URLs 21 // The maximum number of match results to provide. If the number of nearby URLs
20 // exceeds this limit, an overflow item is created. Tapping the overflow item 22 // exceeds this limit, an overflow item is created. Tapping the overflow item
(...skipping 10 matching lines...) Expand all
31 // Relevance score of the first Physical Web URL autocomplete match. This score 33 // Relevance score of the first Physical Web URL autocomplete match. This score
32 // is intended to be between ClipboardURLProvider and ZeroSuggestProvider. 34 // is intended to be between ClipboardURLProvider and ZeroSuggestProvider.
33 // Subsequent matches should decrease in relevance to preserve the ordering 35 // Subsequent matches should decrease in relevance to preserve the ordering
34 // in the metadata list. 36 // in the metadata list.
35 static const int kPhysicalWebUrlBaseRelevance = 700; 37 static const int kPhysicalWebUrlBaseRelevance = 700;
36 38
37 } 39 }
38 40
39 // static 41 // static
40 PhysicalWebProvider* PhysicalWebProvider::Create( 42 PhysicalWebProvider* PhysicalWebProvider::Create(
41 AutocompleteProviderClient* client) { 43 AutocompleteProviderClient* client,
42 return new PhysicalWebProvider(client); 44 HistoryURLProvider* history_url_provider) {
45 return new PhysicalWebProvider(client, history_url_provider);
43 } 46 }
44 47
45 void PhysicalWebProvider::Start(const AutocompleteInput& input, 48 void PhysicalWebProvider::Start(const AutocompleteInput& input,
46 bool minimal_changes) { 49 bool minimal_changes) {
47 DCHECK(kPhysicalWebMaxMatches < kMaxMatches); 50 DCHECK(kPhysicalWebMaxMatches < kMaxMatches);
48 51
49 Stop(false, false); 52 Stop(false, false);
50 53
51 done_ = false; 54 done_ = false;
52 matches_.clear(); 55 matches_.clear();
53 56
54 // Stop providing suggestions when the user enters text into the omnibox. 57 // Stop providing suggestions when the user enters text into the omnibox.
55 if (!input.from_omnibox_focus()) { 58 if (!input.from_omnibox_focus()) {
56 done_ = true; 59 done_ = true;
57 return; 60 return;
58 } 61 }
59 62
60 PhysicalWebDataSource* data_source = client_->GetPhysicalWebDataSource(); 63 PhysicalWebDataSource* data_source = client_->GetPhysicalWebDataSource();
61 if (!data_source) { 64 if (!data_source) {
62 done_ = true; 65 done_ = true;
63 return; 66 return;
64 } 67 }
65 68
66 ConstructMatches(data_source->GetMetadata().get()); 69 ConstructMatches(data_source->GetMetadata().get());
67 70
71 // If we're going to show matches and the omnibox is non-empty, add the
72 // current URL as the default match.
Peter Kasting 2016/09/01 22:06:35 Nit: Explain why
mattreynolds 2016/09/02 00:29:08 Done.
73 if (!matches_.empty() && !input.text().empty()) {
74 AutocompleteMatch verbatim_match = VerbatimMatchForURL(
75 client_, input, input.current_url(), history_url_provider_, -1);
76 matches_.push_back(verbatim_match);
Peter Kasting 2016/09/01 22:06:35 Nit: Just inline: matches_.push_back(Verbatim
mattreynolds 2016/09/02 00:29:08 Done.
77 }
78
68 done_ = true; 79 done_ = true;
69 } 80 }
70 81
71 void PhysicalWebProvider::Stop(bool clear_cached_results, 82 void PhysicalWebProvider::Stop(bool clear_cached_results,
72 bool due_to_user_inactivity) { 83 bool due_to_user_inactivity) {
73 done_ = true; 84 done_ = true;
74 } 85 }
75 86
76 PhysicalWebProvider::PhysicalWebProvider(AutocompleteProviderClient* client) 87 PhysicalWebProvider::PhysicalWebProvider(
88 AutocompleteProviderClient* client,
89 HistoryURLProvider* history_url_provider)
77 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), 90 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB),
78 client_(client) { 91 client_(client),
92 history_url_provider_(history_url_provider) {
79 } 93 }
80 94
81 PhysicalWebProvider::~PhysicalWebProvider() { 95 PhysicalWebProvider::~PhysicalWebProvider() {
82 } 96 }
83 97
84 void PhysicalWebProvider::ConstructMatches(base::ListValue* metadata_list) { 98 void PhysicalWebProvider::ConstructMatches(base::ListValue* metadata_list) {
85 const size_t metadata_count = metadata_list->GetSize(); 99 const size_t metadata_count = metadata_list->GetSize();
100 size_t used_slots = 0;
86 101
87 for (size_t i = 0; i < metadata_count; ++i) { 102 for (size_t i = 0; i < metadata_count; ++i) {
88 base::DictionaryValue* metadata_item = NULL; 103 base::DictionaryValue* metadata_item = NULL;
89 if (!metadata_list->GetDictionary(i, &metadata_item)) { 104 if (!metadata_list->GetDictionary(i, &metadata_item)) {
90 continue; 105 continue;
91 } 106 }
92 107
93 std::string url_string; 108 std::string url_string;
94 std::string title_string; 109 std::string title_string;
95 if (!metadata_item->GetString("resolvedUrl", &url_string) || 110 if (!metadata_item->GetString("resolvedUrl", &url_string) ||
96 !metadata_item->GetString("title", &title_string)) { 111 !metadata_item->GetString("title", &title_string)) {
97 continue; 112 continue;
98 } 113 }
99 114
100 // Add match items with decreasing relevance to preserve the ordering in 115 // Add match items with decreasing relevance to preserve the ordering in
101 // the metadata list. 116 // the metadata list.
102 int relevance = kPhysicalWebUrlBaseRelevance - matches_.size(); 117 int relevance = kPhysicalWebUrlBaseRelevance - used_slots;
103 118
104 // Append an overflow item if creating a match for each metadata item would 119 // Append an overflow item if creating a match for each metadata item would
105 // exceed the match limit. 120 // exceed the match limit.
106 const size_t remaining_slots = kPhysicalWebMaxMatches - matches_.size(); 121 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots;
107 const size_t remaining_metadata = metadata_count - i; 122 const size_t remaining_metadata = metadata_count - i;
108 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { 123 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) {
109 AppendOverflowItem(remaining_metadata, relevance); 124 AppendOverflowItem(remaining_metadata, relevance);
110 return; 125 return;
111 } 126 }
112 127
113 GURL url(url_string); 128 GURL url(url_string);
114 base::string16 title = base::UTF8ToUTF16(title_string); 129 base::string16 title = base::UTF8ToUTF16(title_string);
115 130
116 AutocompleteMatch match(this, relevance, false, 131 AutocompleteMatch match(this, relevance, false,
117 AutocompleteMatchType::PHYSICAL_WEB); 132 AutocompleteMatchType::PHYSICAL_WEB);
118 match.destination_url = url; 133 match.destination_url = url;
119 134
120 // Physical Web results should omit http:// (but not https://) and never 135 // Physical Web results should omit http:// (but not https://) and never
121 // appear bold. 136 // appear bold.
122 match.contents = url_formatter::FormatUrl(url, 137 match.contents = url_formatter::FormatUrl(url,
123 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES, 138 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES,
124 nullptr, nullptr, nullptr); 139 nullptr, nullptr, nullptr);
125 match.contents_class.push_back( 140 match.contents_class.push_back(
126 ACMatchClassification(0, ACMatchClassification::URL)); 141 ACMatchClassification(0, ACMatchClassification::URL));
127 142
128 match.fill_into_edit = 143 match.fill_into_edit =
129 AutocompleteInput::FormattedStringWithEquivalentMeaning( 144 AutocompleteInput::FormattedStringWithEquivalentMeaning(
130 url, match.contents, client_->GetSchemeClassifier()); 145 url, match.contents, client_->GetSchemeClassifier());
131 146
132 match.description = AutocompleteMatch::SanitizeString(title); 147 match.description = AutocompleteMatch::SanitizeString(title);
133 match.description_class.push_back( 148 match.description_class.push_back(
134 ACMatchClassification(0, ACMatchClassification::NONE)); 149 ACMatchClassification(0, ACMatchClassification::NONE));
135 150
136 match.allowed_to_be_default_match = matches_.empty();
137
138 matches_.push_back(match); 151 matches_.push_back(match);
152 ++used_slots;
139 } 153 }
140 } 154 }
141 155
142 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count, 156 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count,
143 int relevance) { 157 int relevance) {
144 std::string url_string = "chrome://physical-web"; 158 std::string url_string = "chrome://physical-web";
145 GURL url(url_string); 159 GURL url(url_string);
146 160
147 AutocompleteMatch match(this, relevance, false, 161 AutocompleteMatch match(this, relevance, false,
148 AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW); 162 AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW);
149 match.destination_url = url; 163 match.destination_url = url;
150 164
151 // Don't omit the chrome:// scheme when displaying the WebUI URL. 165 // Don't omit the chrome:// scheme when displaying the WebUI URL.
152 match.contents = url_formatter::FormatUrl(url, 166 match.contents = url_formatter::FormatUrl(url,
153 url_formatter::kFormatUrlOmitNothing, net::UnescapeRule::SPACES, 167 url_formatter::kFormatUrlOmitNothing, net::UnescapeRule::SPACES,
154 nullptr, nullptr, nullptr); 168 nullptr, nullptr, nullptr);
155 match.contents_class.push_back( 169 match.contents_class.push_back(
156 ACMatchClassification(0, ACMatchClassification::URL)); 170 ACMatchClassification(0, ACMatchClassification::URL));
157 171
158 match.fill_into_edit = 172 match.fill_into_edit =
159 AutocompleteInput::FormattedStringWithEquivalentMeaning( 173 AutocompleteInput::FormattedStringWithEquivalentMeaning(
160 url, match.contents, client_->GetSchemeClassifier()); 174 url, match.contents, client_->GetSchemeClassifier());
161 175
162 match.description = l10n_util::GetPluralStringFUTF16( 176 match.description = l10n_util::GetPluralStringFUTF16(
163 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count); 177 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count);
164 match.description_class.push_back( 178 match.description_class.push_back(
165 ACMatchClassification(0, ACMatchClassification::NONE)); 179 ACMatchClassification(0, ACMatchClassification::NONE));
166 180
167 match.allowed_to_be_default_match = matches_.empty();
168
169 matches_.push_back(match); 181 matches_.push_back(match);
170 } 182 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698