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

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

Issue 2203993002: Add a Physical Web omnibox autocomplete provider (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: improved comments, AppendOverflowItem Created 4 years, 4 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/memory/ptr_util.h"
6 #include "base/strings/utf_string_conversions.h"
7 #include "base/values.h"
8 #include "components/omnibox/browser/autocomplete_provider_client.h"
9 #include "components/omnibox/browser/autocomplete_provider_listener.h"
10 #include "components/omnibox/browser/physical_web_provider.h"
11 #include "components/physical_web/data_source/physical_web_data_source.h"
12 #include "components/url_formatter/url_formatter.h"
13 #include "grit/components_strings.h"
14 #include "ui/base/l10n/l10n_util.h"
15 #include "url/gurl.h"
16
17 namespace {
18
19 // 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
21 // navigates to a page with the full list of nearby URLs. The overflow item is
22 // counted as a match result for the purposes of the match limit.
23 //
24 // ex: With kPhysicalWebMaxMatches == 1, there should be at most one suggestion
25 // created by this provider. If there is a single nearby URL, then the
26 // suggestion will be for that URL. If there are multiple nearby URLs, the
27 // suggestion will be the overflow item which navigates to the WebUI when
28 // tapped.
29 static const size_t kPhysicalWebMaxMatches = 1;
30
31 // Relevance score of a Physical Web URL autocomplete match. This score is
32 // intended to be between ClipboardURLProvider and ZeroSuggestProvider.
33 static const int kPhysicalWebUrlRelevance = 700;
34
35 // Relevance score of the overflow item.
36 static const int kOverflowItemRelevance = kPhysicalWebUrlRelevance - 1;
37
38 }
39
40 // static
41 PhysicalWebProvider* PhysicalWebProvider::Create(
42 AutocompleteProviderClient* client) {
43 return new PhysicalWebProvider(client);
44 }
45
46 void PhysicalWebProvider::Start(const AutocompleteInput& input,
47 bool minimal_changes) {
48 DCHECK(kPhysicalWebMaxMatches < kMaxMatches);
49
50 Stop(false, false);
51
52 done_ = false;
53 matches_.clear();
54
55 PhysicalWebDataSource* data_source = client_->GetPhysicalWebDataSource();
56 if (!data_source) {
57 done_ = true;
58 return;
59 }
60
61 ConstructMatches(data_source->GetMetadata().get());
62
63 done_ = true;
64 }
65
66 void PhysicalWebProvider::Stop(bool clear_cached_results,
67 bool due_to_user_inactivity) {
68 done_ = true;
69 }
70
71 PhysicalWebProvider::PhysicalWebProvider(AutocompleteProviderClient* client)
72 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB),
73 client_(client) {
74 }
75
76 PhysicalWebProvider::~PhysicalWebProvider() {
77 }
78
79 void PhysicalWebProvider::ConstructMatches(base::ListValue* metadata_list) {
80 const size_t metadata_count = metadata_list->GetSize();
81
82 // Leave room for an overflow item.
83 const bool needs_overflow = metadata_count > kPhysicalWebMaxMatches;
Mark P 2016/08/11 23:16:03 This needs overflow is no longer correct, as you m
mattreynolds 2016/08/16 20:35:25 Fixed so that we only count slots as used once we'
Mark P 2016/08/16 22:49:08 Acknowledged. This seems like small enough of a d
84 const size_t max_rows = needs_overflow ? kPhysicalWebMaxMatches - 1 :
85 metadata_count;
86
87 for (size_t i = 0; i < metadata_count && matches_.size() < max_rows; ++i) {
Mark P 2016/08/11 23:16:03 nit: parens around the comparison operators (but t
mattreynolds 2016/08/16 20:35:25 Acknowledged.
88 base::DictionaryValue* metadata_item = NULL;
89 if (!metadata_list->GetDictionary(i, &metadata_item)) {
90 continue;
91 }
92
93 std::string url_string;
94 std::string title_string;
95 if (!metadata_item->GetString("resolvedUrl", &url_string) ||
96 !metadata_item->GetString("title", &title_string)) {
97 continue;
98 }
99
100 GURL url(url_string);
101 base::string16 title = base::UTF8ToUTF16(title_string);
102
103 AutocompleteMatch match(this, kPhysicalWebUrlRelevance, false,
104 AutocompleteMatchType::PHYSICAL_WEB);
105 match.destination_url = url;
106
107 // Physical Web results should omit http:// (but not https://) and never
108 // appear bold.
109 match.contents = url_formatter::FormatUrl(url,
110 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES,
111 nullptr, nullptr, nullptr);
112 match.fill_into_edit +=
Mark P 2016/08/11 23:16:03 Why +=?
mattreynolds 2016/08/16 20:35:25 Not sure, I borrowed this code from ZeroSuggestPro
113 AutocompleteInput::FormattedStringWithEquivalentMeaning(
114 url, match.contents, client_->GetSchemeClassifier());
115
116 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
Mark P 2016/08/11 23:16:03 This more naturally immediately follows the initia
Mark P 2016/08/11 23:16:03 I think the first three parameters should actually
mattreynolds 2016/08/16 20:35:25 This was also copied from ZeroSuggestProvider::Nav
Mark P 2016/08/16 22:49:08 Well, that's a poorly commented behavior of Classi
117 match.contents.length(), ACMatchClassification::URL,
118 &match.contents_class);
119
120 match.description = AutocompleteMatch::SanitizeString(title);
121 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
122 match.description.length(), ACMatchClassification::NONE,
123 &match.description_class);
124
125 match.allowed_to_be_default_match = matches_.empty();
126
127 matches_.push_back(match);
Mark P 2016/08/11 23:16:03 I notice you give all results the same relevance s
mattreynolds 2016/08/16 20:35:25 Yes, we would like to preserve the order from the
128 }
129
130 if (needs_overflow) {
131 AppendOverflowItem(metadata_count - matches_.size());
132 }
133 }
134
135 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count) {
136 AutocompleteMatch match(this, kOverflowItemRelevance, false,
137 AutocompleteMatchType::PHYSICAL_WEB);
138 match.destination_url = GURL("chrome://physical-web");
139 match.description = l10n_util::GetPluralStringFUTF16(
140 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count);
141 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
142 match.description.length(), ACMatchClassification::NONE,
143 &match.description_class);
144
145 match.allowed_to_be_default_match = matches_.empty();
146
147 matches_.push_back(match);
148 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698