Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "url/gurl.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 // The maximum number of match results to provide, including the overflow item. | |
| 18 static const int kPhysicalWebMaxMatches = 1; | |
|
Mark P
2016/08/08 20:41:37
side comment: later you might want to put this und
Mark P
2016/08/08 20:41:38
If this is set to 1, does that mean that if there'
mattreynolds
2016/08/09 21:42:02
With kPhysicalWebMaxMatches == 1, there should be
| |
| 19 | |
| 20 // Relevance score of a Physical Web URL autocomplete match. | |
|
Mark P
2016/08/08 20:41:37
Please comment, justifying why this is intentional
mattreynolds
2016/08/09 21:42:03
The current value was chosen arbitrarily, I think
| |
| 21 static const int kPhysicalWebUrlRelevance = 1000; | |
| 22 | |
| 23 // Relevance score of the overflow item. | |
|
Mark P
2016/08/08 20:41:38
Please explain what an "overflow" item is or point
mattreynolds
2016/08/09 21:42:02
I added some explanation to the comment above kPhy
| |
| 24 static const int kOverflowItemRelevance = kPhysicalWebUrlRelevance - 1; | |
| 25 | |
| 26 // The path to the Physical Web WebUI page. | |
| 27 const char kChromeUIPhysicalWebURL[] = "chrome://physical-web"; | |
| 28 | |
| 29 } | |
| 30 | |
| 31 //static | |
|
Mark P
2016/08/08 20:41:37
nit: space between "//" and "static"
mattreynolds
2016/08/09 21:42:03
Done.
| |
| 32 PhysicalWebProvider* PhysicalWebProvider::Create( | |
| 33 AutocompleteProviderClient* client) { | |
| 34 return new PhysicalWebProvider(client); | |
| 35 } | |
| 36 | |
| 37 void PhysicalWebProvider::Start(const AutocompleteInput& input, | |
| 38 bool minimal_changes) { | |
| 39 Stop(false, false); | |
| 40 | |
| 41 done_ = false; | |
| 42 matches_.clear(); | |
| 43 | |
| 44 PhysicalWebDataSource* data_source = client_->GetPhysicalWebDataSource(); | |
| 45 std::unique_ptr<base::ListValue> metadata_list; | |
| 46 if (data_source) { | |
| 47 metadata_list = data_source->GetMetadata(); | |
| 48 } else { | |
| 49 metadata_list = base::MakeUnique<base::ListValue>(); | |
| 50 } | |
|
Mark P
2016/08/08 20:41:37
Why don't you simply return early if you have no d
mattreynolds
2016/08/09 21:42:03
Done.
| |
| 51 | |
| 52 ConstructMatches(metadata_list.get()); | |
| 53 AppendOverflowItemIfNecessary(metadata_list.get()); | |
| 54 | |
| 55 done_ = true; | |
| 56 } | |
| 57 | |
| 58 void PhysicalWebProvider::Stop(bool clear_cached_results, | |
| 59 bool due_to_user_inactivity) { | |
| 60 done_ = true; | |
| 61 } | |
| 62 | |
| 63 PhysicalWebProvider::PhysicalWebProvider(AutocompleteProviderClient* client) | |
| 64 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), | |
| 65 client_(client) { | |
| 66 } | |
| 67 | |
| 68 PhysicalWebProvider::~PhysicalWebProvider() { | |
| 69 } | |
| 70 | |
| 71 void PhysicalWebProvider::ConstructMatches(base::ListValue* metadata_list) { | |
| 72 int metadata_count = metadata_list->GetSize(); | |
|
Mark P
2016/08/08 20:41:37
general comment: most of your ints throughout shou
Mark P
2016/08/08 20:41:37
nit: const
mattreynolds
2016/08/09 21:42:03
Done x2
| |
| 73 | |
| 74 // Leave room for an overflow item. | |
| 75 int max_rows = (metadata_count <= kPhysicalWebMaxMatches) | |
| 76 ? metadata_count : kPhysicalWebMaxMatches - 1; | |
|
Mark P
2016/08/08 20:41:37
nit: operators go at end of lines, not beginning o
mattreynolds
2016/08/09 21:42:03
Done.
| |
| 77 | |
| 78 for (int i = 0; i < metadata_count && (int)matches_.size() < max_rows; ++i) { | |
|
Mark P
2016/08/08 20:41:37
Don't all these Value getters have return values (
mattreynolds
2016/08/09 21:42:03
Added validation. I think in general it's fine to
| |
| 79 base::DictionaryValue* metadata_item = NULL; | |
| 80 metadata_list->GetDictionary(i, &metadata_item); | |
| 81 | |
| 82 std::string url_string; | |
| 83 std::string title_string; | |
| 84 metadata_item->GetString("resolvedUrl", &url_string); | |
| 85 metadata_item->GetString("title", &title_string); | |
| 86 | |
| 87 GURL url(url_string); | |
| 88 base::string16 title = base::UTF8ToUTF16(title_string); | |
| 89 | |
| 90 AutocompleteMatch match(this, kPhysicalWebUrlRelevance, false, | |
| 91 AutocompleteMatchType::PHYSICAL_WEB); | |
| 92 match.destination_url = url; | |
| 93 | |
| 94 // Physical Web results should always omit protocols and never appear bold | |
|
Mark P
2016/08/08 20:41:37
I am skeptical of this omitting protocols. This p
mattreynolds
2016/08/09 21:42:03
Changed to kFormatUrlOmitHttp.
| |
| 95 match.contents = url_formatter::FormatUrl(url, | |
| 96 url_formatter::kFormatUrlOmitAll, net::UnescapeRule::SPACES, nullptr, | |
| 97 nullptr, nullptr); | |
| 98 match.fill_into_edit += | |
| 99 AutocompleteInput::FormattedStringWithEquivalentMeaning( | |
| 100 url, match.contents, client_->GetSchemeClassifier()); | |
| 101 | |
| 102 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, | |
| 103 match.contents.length(), ACMatchClassification::URL, | |
| 104 &match.contents_class); | |
| 105 | |
| 106 match.description = AutocompleteMatch::SanitizeString(title); | |
| 107 AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, | |
| 108 match.description.length(), ACMatchClassification::NONE, | |
| 109 &match.description_class); | |
| 110 | |
| 111 match.allowed_to_be_default_match = matches_.empty(); | |
| 112 | |
| 113 matches_.push_back(match); | |
| 114 } | |
| 115 } | |
| 116 | |
| 117 void PhysicalWebProvider::AppendOverflowItemIfNecessary( | |
| 118 base::ListValue* metadata_list) { | |
| 119 int previous_match_count = matches_.size(); | |
|
Mark P
2016/08/08 20:41:37
nit: const
mattreynolds
2016/08/09 21:42:03
Done.
| |
| 120 int additional_items = metadata_list->GetSize() - previous_match_count; | |
| 121 if (additional_items > 0) { | |
| 122 GURL url(kChromeUIPhysicalWebURL); | |
|
Mark P
2016/08/08 20:41:37
only used in one place; no need to declare a varia
mattreynolds
2016/08/09 21:42:02
Done.
| |
| 123 std::ostringstream contents_buf; | |
| 124 contents_buf << additional_items | |
| 125 << ((previous_match_count > 0) ? " more" : "") | |
| 126 << " web page" << ((additional_items == 1) ? "" : "s") | |
| 127 << " found"; | |
| 128 | |
| 129 AutocompleteMatch match(this, kOverflowItemRelevance, false, | |
| 130 AutocompleteMatchType::PHYSICAL_WEB); | |
| 131 match.destination_url = url; | |
| 132 | |
| 133 match.description = base::UTF8ToUTF16(contents_buf.str().c_str()); | |
| 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(); | |
|
Mark P
2016/08/08 20:41:37
I think the overflow item should never be allowed
mattreynolds
2016/08/09 21:42:03
In the case where we only have one "slot" to fill
Mark P
2016/08/10 23:33:19
Acknowledged.
| |
| 139 | |
| 140 matches_.push_back(match); | |
| 141 } | |
| 142 } | |
| OLD | NEW |