| 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 "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 the first Physical Web URL autocomplete match. This score |
| 32 // is intended to be between ClipboardURLProvider and ZeroSuggestProvider. |
| 33 // Subsequent matches should decrease in relevance to preserve the ordering |
| 34 // in the metadata list. |
| 35 static const int kPhysicalWebUrlBaseRelevance = 700; |
| 36 |
| 37 } |
| 38 |
| 39 // static |
| 40 PhysicalWebProvider* PhysicalWebProvider::Create( |
| 41 AutocompleteProviderClient* client) { |
| 42 return new PhysicalWebProvider(client); |
| 43 } |
| 44 |
| 45 void PhysicalWebProvider::Start(const AutocompleteInput& input, |
| 46 bool minimal_changes) { |
| 47 DCHECK(kPhysicalWebMaxMatches < kMaxMatches); |
| 48 |
| 49 Stop(false, false); |
| 50 |
| 51 done_ = false; |
| 52 matches_.clear(); |
| 53 |
| 54 // Stop providing suggestions when the user enters text into the omnibox. |
| 55 if (!input.from_omnibox_focus()) { |
| 56 done_ = true; |
| 57 return; |
| 58 } |
| 59 |
| 60 PhysicalWebDataSource* data_source = client_->GetPhysicalWebDataSource(); |
| 61 if (!data_source) { |
| 62 done_ = true; |
| 63 return; |
| 64 } |
| 65 |
| 66 ConstructMatches(data_source->GetMetadata().get()); |
| 67 |
| 68 done_ = true; |
| 69 } |
| 70 |
| 71 void PhysicalWebProvider::Stop(bool clear_cached_results, |
| 72 bool due_to_user_inactivity) { |
| 73 done_ = true; |
| 74 } |
| 75 |
| 76 PhysicalWebProvider::PhysicalWebProvider(AutocompleteProviderClient* client) |
| 77 : AutocompleteProvider(AutocompleteProvider::TYPE_PHYSICAL_WEB), |
| 78 client_(client) { |
| 79 } |
| 80 |
| 81 PhysicalWebProvider::~PhysicalWebProvider() { |
| 82 } |
| 83 |
| 84 void PhysicalWebProvider::ConstructMatches(base::ListValue* metadata_list) { |
| 85 const size_t metadata_count = metadata_list->GetSize(); |
| 86 |
| 87 for (size_t i = 0; i < metadata_count; ++i) { |
| 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 // Add match items with decreasing relevance to preserve the ordering in |
| 101 // the metadata list. |
| 102 int relevance = kPhysicalWebUrlBaseRelevance - matches_.size(); |
| 103 |
| 104 // Append an overflow item if creating a match for each metadata item would |
| 105 // exceed the match limit. |
| 106 const size_t remaining_slots = kPhysicalWebMaxMatches - matches_.size(); |
| 107 const size_t remaining_metadata = metadata_count - i; |
| 108 if ((remaining_slots == 1) && (remaining_metadata > remaining_slots)) { |
| 109 AppendOverflowItem(remaining_metadata, relevance); |
| 110 return; |
| 111 } |
| 112 |
| 113 GURL url(url_string); |
| 114 base::string16 title = base::UTF8ToUTF16(title_string); |
| 115 |
| 116 AutocompleteMatch match(this, relevance, false, |
| 117 AutocompleteMatchType::PHYSICAL_WEB); |
| 118 match.destination_url = url; |
| 119 |
| 120 // Physical Web results should omit http:// (but not https://) and never |
| 121 // appear bold. |
| 122 match.contents = url_formatter::FormatUrl(url, |
| 123 url_formatter::kFormatUrlOmitHTTP, net::UnescapeRule::SPACES, |
| 124 nullptr, nullptr, nullptr); |
| 125 match.contents_class.push_back( |
| 126 ACMatchClassification(0, ACMatchClassification::URL)); |
| 127 |
| 128 match.fill_into_edit = |
| 129 AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 130 url, match.contents, client_->GetSchemeClassifier()); |
| 131 |
| 132 match.description = AutocompleteMatch::SanitizeString(title); |
| 133 match.description_class.push_back( |
| 134 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 135 |
| 136 match.allowed_to_be_default_match = matches_.empty(); |
| 137 |
| 138 matches_.push_back(match); |
| 139 } |
| 140 } |
| 141 |
| 142 void PhysicalWebProvider::AppendOverflowItem(int additional_url_count, |
| 143 int relevance) { |
| 144 AutocompleteMatch match(this, relevance, false, |
| 145 AutocompleteMatchType::PHYSICAL_WEB_OVERFLOW); |
| 146 match.destination_url = GURL("chrome://physical-web"); |
| 147 match.description = l10n_util::GetPluralStringFUTF16( |
| 148 IDS_PHYSICAL_WEB_OVERFLOW, additional_url_count); |
| 149 match.description_class.push_back( |
| 150 ACMatchClassification(0, ACMatchClassification::NONE)); |
| 151 |
| 152 match.allowed_to_be_default_match = matches_.empty(); |
| 153 |
| 154 matches_.push_back(match); |
| 155 } |
| OLD | NEW |