sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 1 | // Copyright 2015 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 "components/omnibox/browser/clipboard_url_provider.h" |
| 6 | |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame^] | 7 | #include "base/feature_list.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 8 | #include "base/logging.h" |
mpearson | ab376149 | 2017-03-31 17:29:51 | [diff] [blame] | 9 | #include "base/metrics/histogram_macros.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
| 11 | #include "components/omnibox/browser/autocomplete_input.h" |
| 12 | #include "components/omnibox/browser/autocomplete_provider_client.h" |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame^] | 13 | #include "components/omnibox/browser/omnibox_field_trial.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 14 | #include "components/omnibox/browser/verbatim_match.h" |
| 15 | #include "components/open_from_clipboard/clipboard_recent_content.h" |
thakis | fe8fa0a | 2017-02-23 19:46:36 | [diff] [blame] | 16 | #include "components/strings/grit/components_strings.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 17 | #include "components/url_formatter/url_formatter.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 18 | #include "ui/base/l10n/l10n_util.h" |
| 19 | |
| 20 | ClipboardURLProvider::ClipboardURLProvider( |
| 21 | AutocompleteProviderClient* client, |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 22 | HistoryURLProvider* history_url_provider, |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 23 | ClipboardRecentContent* clipboard_content) |
| 24 | : AutocompleteProvider(AutocompleteProvider::TYPE_CLIPBOARD_URL), |
| 25 | client_(client), |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 26 | clipboard_content_(clipboard_content), |
| 27 | history_url_provider_(history_url_provider) { |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 28 | DCHECK(clipboard_content_); |
| 29 | } |
| 30 | |
| 31 | ClipboardURLProvider::~ClipboardURLProvider() {} |
| 32 | |
| 33 | void ClipboardURLProvider::Start(const AutocompleteInput& input, |
| 34 | bool minimal_changes) { |
| 35 | matches_.clear(); |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 36 | |
| 37 | // If the user started typing, do not offer clipboard based match. |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 38 | if (!input.from_omnibox_focus()) |
| 39 | return; |
| 40 | |
| 41 | GURL url; |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 42 | // If the clipboard does not contain any URL, or the URL on the page is the |
| 43 | // same as the URL in the clipboard, early return. |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 44 | if (!clipboard_content_->GetRecentURLFromClipboard(&url) || |
| 45 | url == input.current_url()) |
| 46 | return; |
| 47 | |
| 48 | DCHECK(url.is_valid()); |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 49 | // If the omnibox is not empty, add a default match. |
| 50 | // This match will be opened when the user presses "Enter". |
| 51 | if (!input.text().empty()) { |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame^] | 52 | const base::string16 description = |
| 53 | (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl)) |
| 54 | ? input.current_title() |
| 55 | : base::string16(); |
| 56 | AutocompleteMatch verbatim_match = |
| 57 | VerbatimMatchForURL(client_, input, input.current_url(), description, |
| 58 | history_url_provider_, -1); |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 59 | matches_.push_back(verbatim_match); |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 60 | } |
mpearson | ab376149 | 2017-03-31 17:29:51 | [diff] [blame] | 61 | UMA_HISTOGRAM_BOOLEAN("Omnibox.ClipboardSuggestionShownWithCurrentURL", |
| 62 | !matches_.empty()); |
mpearson | d3dcdb0 | 2017-04-08 01:11:03 | [diff] [blame] | 63 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionShownAge", |
| 64 | clipboard_content_->GetClipboardContentAge()); |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 65 | |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 66 | // Add the clipboard match. The relevance is 800 to beat ZeroSuggest results. |
| 67 | AutocompleteMatch match(this, 800, false, AutocompleteMatchType::CLIPBOARD); |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 68 | match.destination_url = url; |
| 69 | match.contents.assign(url_formatter::FormatUrl( |
jshin | 1fb7646 | 2016-04-05 22:13:03 | [diff] [blame] | 70 | url, url_formatter::kFormatUrlOmitAll, net::UnescapeRule::SPACES, |
| 71 | nullptr, nullptr, nullptr)); |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 72 | AutocompleteMatch::ClassifyLocationInString( |
| 73 | base::string16::npos, 0, match.contents.length(), |
| 74 | ACMatchClassification::URL, &match.contents_class); |
| 75 | |
| 76 | match.description.assign(l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD)); |
| 77 | AutocompleteMatch::ClassifyLocationInString( |
| 78 | base::string16::npos, 0, match.description.length(), |
| 79 | ACMatchClassification::NONE, &match.description_class); |
| 80 | |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 81 | matches_.push_back(match); |
| 82 | } |