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

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: changes for pkasting@ 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 // Physical Web matches should never be default. If the omnibox input is
72 // non-empty and we have at least one Physical Web match, add the current URL
73 // as the default so that hitting enter after focusing the omnibox causes the
74 // current page to reload. If the input field is empty, no default match is
75 // required.
76 if (!matches_.empty() && !input.text().empty()) {
77 matches_.push_back(VerbatimMatchForURL(client_, input, input.current_url(),
78 history_url_provider_, -1));
79 }
80
68 done_ = true; 81 done_ = true;
69 } 82 }
70 83
71 void PhysicalWebProvider::Stop(bool clear_cached_results, 84 void PhysicalWebProvider::Stop(bool clear_cached_results,
72 bool due_to_user_inactivity) { 85 bool due_to_user_inactivity) {
73 done_ = true; 86 done_ = true;
74 } 87 }
75 88
76 PhysicalWebProvider::PhysicalWebProvider(AutocompleteProviderClient* client) 89 PhysicalWebProvider::PhysicalWebProvider(
90 AutocompleteProviderClient* client,
91 HistoryURLProvider* history_url_provider)
77 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), 92 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB),
78 client_(client) { 93 client_(client),
79 } 94 history_url_provider_(history_url_provider) {}
80 95
81 PhysicalWebProvider::~PhysicalWebProvider() { 96 PhysicalWebProvider::~PhysicalWebProvider() {
82 } 97 }
83 98
84 void PhysicalWebProvider::ConstructMatches(base::ListValue* metadata_list) { 99 void PhysicalWebProvider::ConstructMatches(base::ListValue* metadata_list) {
85 const size_t metadata_count = metadata_list->GetSize(); 100 const size_t metadata_count = metadata_list->GetSize();
101 size_t used_slots = 0;
86 102
87 for (size_t i = 0; i < metadata_count; ++i) { 103 for (size_t i = 0; i < metadata_count; ++i) {
88 base::DictionaryValue* metadata_item = NULL; 104 base::DictionaryValue* metadata_item = NULL;
89 if (!metadata_list->GetDictionary(i, &metadata_item)) { 105 if (!metadata_list->GetDictionary(i, &metadata_item)) {
90 continue; 106 continue;
91 } 107 }
92 108
93 std::string url_string; 109 std::string url_string;
94 std::string title_string; 110 std::string title_string;
95 if (!metadata_item->GetString("resolvedUrl", &url_string) || 111 if (!metadata_item->GetString("resolvedUrl", &url_string) ||
96 !metadata_item->GetString("title", &title_string)) { 112 !metadata_item->GetString("title", &title_string)) {
97 continue; 113 continue;
98 } 114 }
99 115
100 // Add match items with decreasing relevance to preserve the ordering in 116 // Add match items with decreasing relevance to preserve the ordering in
101 // the metadata list. 117 // the metadata list.
102 int relevance = kPhysicalWebUrlBaseRelevance - matches_.size(); 118 int relevance = kPhysicalWebUrlBaseRelevance - used_slots;
103 119
104 // Append an overflow item if creating a match for each metadata item would 120 // Append an overflow item if creating a match for each metadata item would
105 // exceed the match limit. 121 // exceed the match limit.
106 const size_t remaining_slots = kPhysicalWebMaxMatches - matches_.size(); 122 const size_t remaining_slots = kPhysicalWebMaxMatches - used_slots;
107 const size_t remaining_metadata = metadata_count - i; 123 const size_t remaining_metadata = metadata_count - i;
108 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { 124 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) {
109 AppendOverflowItem(remaining_metadata, relevance); 125 AppendOverflowItem(remaining_metadata, relevance);
110 return; 126 return;
111 } 127 }
112 128
113 GURL url(url_string); 129 GURL url(url_string);
114 base::string16 title = base::UTF8ToUTF16(title_string); 130 base::string16 title = base::UTF8ToUTF16(title_string);
115 131
116 AutocompleteMatch match(this, relevance, false, 132 AutocompleteMatch match(this, relevance, false,
117 AutocompleteMatchType::PHYSICAL_WEB); 133 AutocompleteMatchType::PHYSICAL_WEB);
118 match.destination_url = url; 134 match.destination_url = url;
119 135
120 // Physical Web results should omit http:// (but not https://) and never 136 // Physical Web results should omit http:// (but not https://) and never
121 // appear bold. 137 // appear bold.
122 match.contents = url_formatter::FormatUrl(url, 138 match.contents = url_formatter::FormatUrl(url,
123 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES, 139 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES,
124 nullptr, nullptr, nullptr); 140 nullptr, nullptr, nullptr);
125 match.contents_class.push_back( 141 match.contents_class.push_back(
126 ACMatchClassification(0, ACMatchClassification::URL)); 142 ACMatchClassification(0, ACMatchClassification::URL));
127 143
128 match.fill_into_edit = 144 match.fill_into_edit =
129 AutocompleteInput::FormattedStringWithEquivalentMeaning( 145 AutocompleteInput::FormattedStringWithEquivalentMeaning(
130 url, match.contents, client_->GetSchemeClassifier()); 146 url, match.contents, client_->GetSchemeClassifier());
131 147
132 match.description = AutocompleteMatch::SanitizeString(title); 148 match.description = AutocompleteMatch::SanitizeString(title);
133 match.description_class.push_back( 149 match.description_class.push_back(
134 ACMatchClassification(0, ACMatchClassification::NONE)); 150 ACMatchClassification(0, ACMatchClassification::NONE));
135 151
136 match.allowed_to_be_default_match = matches_.empty();
137
138 matches_.push_back(match); 152 matches_.push_back(match);
153 ++used_slots;
139 } 154 }
140 } 155 }
141 156
142 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count, 157 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count,
143 int relevance) { 158 int relevance) {
144 std::string url_string = "chrome://physical-web"; 159 std::string url_string = "chrome://physical-web";
145 GURL url(url_string); 160 GURL url(url_string);
146 161
147 AutocompleteMatch match(this, relevance, false, 162 AutocompleteMatch match(this, relevance, false,
148 AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW); 163 AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW);
149 match.destination_url = url; 164 match.destination_url = url;
150 165
151 // Don't omit the chrome:// scheme when displaying the WebUI URL. 166 // Don't omit the chrome:// scheme when displaying the WebUI URL.
152 match.contents = url_formatter::FormatUrl(url, 167 match.contents = url_formatter::FormatUrl(url,
153 url_formatter::kFormatUrlOmitNothing, net::UnescapeRule::SPACES, 168 url_formatter::kFormatUrlOmitNothing, net::UnescapeRule::SPACES,
154 nullptr, nullptr, nullptr); 169 nullptr, nullptr, nullptr);
155 match.contents_class.push_back( 170 match.contents_class.push_back(
156 ACMatchClassification(0, ACMatchClassification::URL)); 171 ACMatchClassification(0, ACMatchClassification::URL));
157 172
158 match.fill_into_edit = 173 match.fill_into_edit =
159 AutocompleteInput::FormattedStringWithEquivalentMeaning( 174 AutocompleteInput::FormattedStringWithEquivalentMeaning(
160 url, match.contents, client_->GetSchemeClassifier()); 175 url, match.contents, client_->GetSchemeClassifier());
161 176
162 match.description = l10n_util::GetPluralStringFUTF16( 177 match.description = l10n_util::GetPluralStringFUTF16(
163 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count); 178 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count);
164 match.description_class.push_back( 179 match.description_class.push_back(
165 ACMatchClassification(0, ACMatchClassification::NONE)); 180 ACMatchClassification(0, ACMatchClassification::NONE));
166 181
167 match.allowed_to_be_default_match = matches_.empty();
168
169 matches_.push_back(match); 182 matches_.push_back(match);
170 } 183 }
OLDNEW
« no previous file with comments | « components/omnibox/browser/physical_web_provider.h ('k') | components/omnibox/browser/physical_web_provider_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698