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 | |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 9 | #include "base/feature_list.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 10 | #include "base/logging.h" |
Ilya Sherman | 1edb6f18 | 2017-12-12 04:00:42 | [diff] [blame] | 11 | #include "base/metrics/histogram_functions.h" |
mpearson | ab376149 | 2017-03-31 17:29:51 | [diff] [blame] | 12 | #include "base/metrics/histogram_macros.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
| 14 | #include "components/omnibox/browser/autocomplete_input.h" |
| 15 | #include "components/omnibox/browser/autocomplete_provider_client.h" |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 16 | #include "components/omnibox/browser/omnibox_field_trial.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 17 | #include "components/omnibox/browser/verbatim_match.h" |
| 18 | #include "components/open_from_clipboard/clipboard_recent_content.h" |
thakis | fe8fa0a | 2017-02-23 19:46:36 | [diff] [blame] | 19 | #include "components/strings/grit/components_strings.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 20 | #include "components/url_formatter/url_formatter.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 21 | #include "ui/base/l10n/l10n_util.h" |
| 22 | |
| 23 | ClipboardURLProvider::ClipboardURLProvider( |
| 24 | AutocompleteProviderClient* client, |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 25 | HistoryURLProvider* history_url_provider, |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 26 | ClipboardRecentContent* clipboard_content) |
| 27 | : AutocompleteProvider(AutocompleteProvider::TYPE_CLIPBOARD_URL), |
| 28 | client_(client), |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 29 | clipboard_content_(clipboard_content), |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 30 | history_url_provider_(history_url_provider), |
| 31 | current_url_suggested_times_(0) { |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 32 | DCHECK(clipboard_content_); |
| 33 | } |
| 34 | |
| 35 | ClipboardURLProvider::~ClipboardURLProvider() {} |
| 36 | |
| 37 | void ClipboardURLProvider::Start(const AutocompleteInput& input, |
| 38 | bool minimal_changes) { |
| 39 | matches_.clear(); |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 40 | |
| 41 | // If the user started typing, do not offer clipboard based match. |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 42 | if (!input.from_omnibox_focus()) |
| 43 | return; |
| 44 | |
| 45 | GURL url; |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 46 | // The clipboard does not contain a URL worth suggesting. |
| 47 | if (!clipboard_content_->GetRecentURLFromClipboard(&url)) { |
| 48 | current_url_suggested_ = GURL(); |
| 49 | current_url_suggested_times_ = 0; |
| 50 | return; |
| 51 | } |
| 52 | // The URL on the page is the same as the URL in the clipboard. Don't |
| 53 | // bother suggesting it. |
| 54 | if (url == input.current_url()) |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 55 | return; |
| 56 | |
| 57 | DCHECK(url.is_valid()); |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 58 | // If the omnibox is not empty, add a default match. |
| 59 | // This match will be opened when the user presses "Enter". |
| 60 | if (!input.text().empty()) { |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 61 | const base::string16 description = |
| 62 | (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl)) |
| 63 | ? input.current_title() |
| 64 | : base::string16(); |
| 65 | AutocompleteMatch verbatim_match = |
| 66 | VerbatimMatchForURL(client_, input, input.current_url(), description, |
| 67 | history_url_provider_, -1); |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 68 | matches_.push_back(verbatim_match); |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 69 | } |
mpearson | ab376149 | 2017-03-31 17:29:51 | [diff] [blame] | 70 | UMA_HISTOGRAM_BOOLEAN("Omnibox.ClipboardSuggestionShownWithCurrentURL", |
| 71 | !matches_.empty()); |
mpearson | d3dcdb0 | 2017-04-08 01:11:03 | [diff] [blame] | 72 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionShownAge", |
| 73 | clipboard_content_->GetClipboardContentAge()); |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 74 | // Record the number of times the currently-offered URL has been suggested. |
| 75 | // This only works over this run of Chrome; if the URL was in the clipboard |
| 76 | // on a previous run, those offerings will not be counted. |
| 77 | if (url == current_url_suggested_) { |
| 78 | current_url_suggested_times_++; |
| 79 | } else { |
| 80 | current_url_suggested_ = url; |
| 81 | current_url_suggested_times_ = 1; |
| 82 | } |
Ilya Sherman | 1edb6f18 | 2017-12-12 04:00:42 | [diff] [blame] | 83 | base::UmaHistogramSparse( |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 84 | "Omnibox.ClipboardSuggestionShownNumTimes", |
| 85 | std::min(current_url_suggested_times_, static_cast<size_t>(20))); |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 86 | |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 87 | // Add the clipboard match. The relevance is 800 to beat ZeroSuggest results. |
| 88 | AutocompleteMatch match(this, 800, false, AutocompleteMatchType::CLIPBOARD); |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 89 | match.destination_url = url; |
Tommy C. Li | c68f2e4 | 2017-12-19 20:43:45 | [diff] [blame] | 90 | // Because the user did not type a related input to get this clipboard |
| 91 | // suggestion, preserve the path and subdomain so the user has extra context. |
| 92 | auto format_types = AutocompleteMatch::GetFormatTypes(false, true, true); |
tommycli | 72014f6 | 2017-06-29 21:42:16 | [diff] [blame] | 93 | match.contents.assign(url_formatter::FormatUrl( |
| 94 | url, format_types, net::UnescapeRule::SPACES, nullptr, nullptr, nullptr)); |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 95 | AutocompleteMatch::ClassifyLocationInString( |
| 96 | base::string16::npos, 0, match.contents.length(), |
| 97 | ACMatchClassification::URL, &match.contents_class); |
| 98 | |
| 99 | match.description.assign(l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD)); |
| 100 | AutocompleteMatch::ClassifyLocationInString( |
| 101 | base::string16::npos, 0, match.description.length(), |
| 102 | ACMatchClassification::NONE, &match.description_class); |
| 103 | |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 104 | matches_.push_back(match); |
| 105 | } |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 106 | |
| 107 | void ClipboardURLProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 108 | // If a URL wasn't suggested on this most recent focus event, don't bother |
| 109 | // setting |times_returned_results_in_session|, as in effect this URL has |
| 110 | // never been suggested during the current session. (For the purpose of |
| 111 | // this provider, we define a session as intervals between when a URL |
| 112 | // clipboard suggestion changes.) |
| 113 | if (current_url_suggested_times_ == 0) |
| 114 | return; |
| 115 | provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); |
| 116 | metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); |
| 117 | new_entry.set_provider(AsOmniboxEventProviderType()); |
| 118 | new_entry.set_times_returned_results_in_session(current_url_suggested_times_); |
| 119 | } |