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

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: make overflow item localizable 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, then an overflow item is created. Tapping the overflow
Mark P 2016/08/10 23:33:20 nit: omit "then"
mattreynolds 2016/08/11 01:25:02 Done.
21 // item navigates to a page with the full list of nearby URLs. The overflow item
22 // is counted as a match result for the purposes of the match limit.
Mark P 2016/08/10 23:33:20 Can you put this example of yours in the comment?
mattreynolds 2016/08/11 01:25:02 Done.
23 static const int kPhysicalWebMaxMatches = 1;
Mark P 2016/08/10 23:33:20 Can you put a DCHECK assertion somewhere that this
mattreynolds 2016/08/11 01:25:03 Sure, added it to Start.
24
25 // Relevance score of a Physical Web URL autocomplete match.
26 static const int kPhysicalWebUrlRelevance = 700;
Mark P 2016/08/10 23:33:21 I wrote: I think it's rarely above 600. I suggest
mattreynolds 2016/08/11 01:25:03 Done.
27
28 // Relevance score of the overflow item.
29 static const int kOverflowItemRelevance = kPhysicalWebUrlRelevance - 1;
30
31 }
32
33 // static
34 PhysicalWebProvider* PhysicalWebProvider::Create(
35 AutocompleteProviderClient* client) {
36 return new PhysicalWebProvider(client);
37 }
38
39 void PhysicalWebProvider::Start(const AutocompleteInput& input,
40 bool minimal_changes) {
41 Stop(false, false);
42
43 done_ = false;
44 matches_.clear();
45
46 PhysicalWebDataSource* data_source = client_->GetPhysicalWebDataSource();
47 if (!data_source) {
48 done_ = true;
49 return;
50 }
51
52 std::unique_ptr<base::ListValue> metadata_list = data_source->GetMetadata();
53 ConstructMatches(metadata_list.get());
54 AppendOverflowItemIfNecessary(metadata_list.get());
55
56 done_ = true;
57 }
58
59 void PhysicalWebProvider::Stop(bool clear_cached_results,
60 bool due_to_user_inactivity) {
61 done_ = true;
62 }
63
64 PhysicalWebProvider::PhysicalWebProvider(AutocompleteProviderClient* client)
65 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB),
66 client_(client) {
67 }
68
69 PhysicalWebProvider::~PhysicalWebProvider() {
70 }
71
72 void PhysicalWebProvider::ConstructMatches(base::ListValue* metadata_list) {
73 const size_t metadata_count = metadata_list->GetSize();
74
75 // Leave room for an overflow item.
76 const size_t max_rows = (metadata_count <= kPhysicalWebMaxMatches) ?
77 metadata_count : kPhysicalWebMaxMatches - 1;
78
79 for (size_t i = 0; i < metadata_count && matches_.size() < max_rows; ++i) {
80 base::DictionaryValue* metadata_item = NULL;
81 if (!metadata_list->GetDictionary(i, &metadata_item)) {
82 continue;
83 }
84
85 std::string url_string;
86 std::string title_string;
87 if (!metadata_item->GetString("resolvedUrl", &url_string) ||
88 !metadata_item->GetString("title", &title_string)) {
89 continue;
90 }
91
92 GURL url(url_string);
93 base::string16 title = base::UTF8ToUTF16(title_string);
94
95 AutocompleteMatch match(this, kPhysicalWebUrlRelevance, false,
96 AutocompleteMatchType::PHYSICAL_WEB);
97 match.destination_url = url;
98
99 // Physical Web results should never omit protocols and never appear bold
Mark P 2016/08/10 23:33:20 Please revise this comment; it's currently wrong.
mattreynolds 2016/08/11 01:25:02 Fixed
100 match.contents = url_formatter::FormatUrl(url,
101 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES,
102 nullptr, nullptr, nullptr);
103 match.fill_into_edit +=
104 AutocompleteInput::FormattedStringWithEquivalentMeaning(
105 url, match.contents, client_->GetSchemeClassifier());
106
107 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
108 match.contents.length(), ACMatchClassification::URL,
109 &match.contents_class);
110
111 match.description = AutocompleteMatch::SanitizeString(title);
112 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
113 match.description.length(), ACMatchClassification::NONE,
114 &match.description_class);
115
116 match.allowed_to_be_default_match = matches_.empty();
117
118 matches_.push_back(match);
119 }
120 }
121
122 void PhysicalWebProvider::AppendOverflowItemIfNecessary(
123 base::ListValue* metadata_list) {
124 const size_t previous_match_count = matches_.size();
125 const size_t additional_items = metadata_list->GetSize() -
126 previous_match_count;
127
128 if (additional_items > 0) {
129 AutocompleteMatch match(this, kOverflowItemRelevance, false,
130 AutocompleteMatchType::PHYSICAL_WEB);
131 match.destination_url = GURL("chrome://physical-web");
132 match.description = l10n_util::GetPluralStringFUTF16(
133 IDS_PHYSICAL_WEB_OVERFLOW, additional_items);
134 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0,
135 match.description.length(), ACMatchClassification::NONE,
136 &match.description_class);
137
138 match.allowed_to_be_default_match = matches_.empty();
139
140 matches_.push_back(match);
141 }
142 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698