Chromium Code Reviews| Index: components/omnibox/browser/physical_web_provider.cc |
| diff --git a/components/omnibox/browser/physical_web_provider.cc b/components/omnibox/browser/physical_web_provider.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..149d618bf27a0198b571e4c40239f9aea97e3629 |
| --- /dev/null |
| +++ b/components/omnibox/browser/physical_web_provider.cc |
| @@ -0,0 +1,142 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/strings/utf_string_conversions.h" |
| +#include "base/values.h" |
| +#include "components/omnibox/browser/autocomplete_provider_client.h" |
| +#include "components/omnibox/browser/autocomplete_provider_listener.h" |
| +#include "components/omnibox/browser/physical_web_provider.h" |
| +#include "components/physical_web/data_source/physical_web_data_source.h" |
| +#include "components/url_formatter/url_formatter.h" |
| +#include "grit/components_strings.h" |
| +#include "ui/base/l10n/l10n_util.h" |
| +#include "url/gurl.h" |
| + |
| +namespace { |
| + |
| +// The maximum number of match results to provide. If the number of nearby URLs |
| +// 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.
|
| +// item navigates to a page with the full list of nearby URLs. The overflow item |
| +// 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.
|
| +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.
|
| + |
| +// Relevance score of a Physical Web URL autocomplete match. |
| +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.
|
| + |
| +// Relevance score of the overflow item. |
| +static const int kOverflowItemRelevance = kPhysicalWebUrlRelevance - 1; |
| + |
| +} |
| + |
| +// static |
| +PhysicalWebProvider* PhysicalWebProvider::Create( |
| + AutocompleteProviderClient* client) { |
| + return new PhysicalWebProvider(client); |
| +} |
| + |
| +void PhysicalWebProvider::Start(const AutocompleteInput& input, |
| + bool minimal_changes) { |
| + Stop(false, false); |
| + |
| + done_ = false; |
| + matches_.clear(); |
| + |
| + PhysicalWebDataSource* data_source = client_->GetPhysicalWebDataSource(); |
| + if (!data_source) { |
| + done_ = true; |
| + return; |
| + } |
| + |
| + std::unique_ptr<base::ListValue> metadata_list = data_source->GetMetadata(); |
| + ConstructMatches(metadata_list.get()); |
| + AppendOverflowItemIfNecessary(metadata_list.get()); |
| + |
| + done_ = true; |
| +} |
| + |
| +void PhysicalWebProvider::Stop(bool clear_cached_results, |
| + bool due_to_user_inactivity) { |
| + done_ = true; |
| +} |
| + |
| +PhysicalWebProvider::PhysicalWebProvider(AutocompleteProviderClient* client) |
| + : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), |
| + client_(client) { |
| +} |
| + |
| +PhysicalWebProvider::~PhysicalWebProvider() { |
| +} |
| + |
| +void PhysicalWebProvider::ConstructMatches(base::ListValue* metadata_list) { |
| + const size_t metadata_count = metadata_list->GetSize(); |
| + |
| + // Leave room for an overflow item. |
| + const size_t max_rows = (metadata_count <= kPhysicalWebMaxMatches) ? |
| + metadata_count : kPhysicalWebMaxMatches - 1; |
| + |
| + for (size_t i = 0; i < metadata_count && matches_.size() < max_rows; ++i) { |
| + base::DictionaryValue* metadata_item = NULL; |
| + if (!metadata_list->GetDictionary(i, &metadata_item)) { |
| + continue; |
| + } |
| + |
| + std::string url_string; |
| + std::string title_string; |
| + if (!metadata_item->GetString("resolvedUrl", &url_string) || |
| + !metadata_item->GetString("title", &title_string)) { |
| + continue; |
| + } |
| + |
| + GURL url(url_string); |
| + base::string16 title = base::UTF8ToUTF16(title_string); |
| + |
| + AutocompleteMatch match(this, kPhysicalWebUrlRelevance, false, |
| + AutocompleteMatchType::PHYSICAL_WEB); |
| + match.destination_url = url; |
| + |
| + // 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
|
| + match.contents = url_formatter::FormatUrl(url, |
| + url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES, |
| + nullptr, nullptr, nullptr); |
| + match.fill_into_edit += |
| + AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| + url, match.contents, client_->GetSchemeClassifier()); |
| + |
| + AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
| + match.contents.length(), ACMatchClassification::URL, |
| + &match.contents_class); |
| + |
| + match.description = AutocompleteMatch::SanitizeString(title); |
| + AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
| + match.description.length(), ACMatchClassification::NONE, |
| + &match.description_class); |
| + |
| + match.allowed_to_be_default_match = matches_.empty(); |
| + |
| + matches_.push_back(match); |
| + } |
| +} |
| + |
| +void PhysicalWebProvider::AppendOverflowItemIfNecessary( |
| + base::ListValue* metadata_list) { |
| + const size_t previous_match_count = matches_.size(); |
| + const size_t additional_items = metadata_list->GetSize() - |
| + previous_match_count; |
| + |
| + if (additional_items > 0) { |
| + AutocompleteMatch match(this, kOverflowItemRelevance, false, |
| + AutocompleteMatchType::PHYSICAL_WEB); |
| + match.destination_url = GURL("chrome://physical-web"); |
| + match.description = l10n_util::GetPluralStringFUTF16( |
| + IDS_PHYSICAL_WEB_OVERFLOW, additional_items); |
| + AutocompleteMatch::ClassifyLocationInString(base::string16::npos, 0, |
| + match.description.length(), ACMatchClassification::NONE, |
| + &match.description_class); |
| + |
| + match.allowed_to_be_default_match = matches_.empty(); |
| + |
| + matches_.push_back(match); |
| + } |
| +} |