blob: 3fe1074966161124158d27f699cfd3e4a9a5e553 [file] [log] [blame]
sdefresne70948d62015-08-11 10:46:351// 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
gcomanici8cabc77f2017-04-27 20:04:547#include "base/feature_list.h"
sdefresne70948d62015-08-11 10:46:358#include "base/logging.h"
mpearsonab3761492017-03-31 17:29:519#include "base/metrics/histogram_macros.h"
sdefresne70948d62015-08-11 10:46:3510#include "base/strings/utf_string_conversions.h"
11#include "components/omnibox/browser/autocomplete_input.h"
12#include "components/omnibox/browser/autocomplete_provider_client.h"
gcomanici8cabc77f2017-04-27 20:04:5413#include "components/omnibox/browser/omnibox_field_trial.h"
sdefresne70948d62015-08-11 10:46:3514#include "components/omnibox/browser/verbatim_match.h"
15#include "components/open_from_clipboard/clipboard_recent_content.h"
thakisfe8fa0a2017-02-23 19:46:3616#include "components/strings/grit/components_strings.h"
sdefresne70948d62015-08-11 10:46:3517#include "components/url_formatter/url_formatter.h"
sdefresne70948d62015-08-11 10:46:3518#include "ui/base/l10n/l10n_util.h"
19
20ClipboardURLProvider::ClipboardURLProvider(
21 AutocompleteProviderClient* client,
mpearson931028c2016-07-01 18:55:1122 HistoryURLProvider* history_url_provider,
sdefresne70948d62015-08-11 10:46:3523 ClipboardRecentContent* clipboard_content)
24 : AutocompleteProvider(AutocompleteProvider::TYPE_CLIPBOARD_URL),
25 client_(client),
mpearson931028c2016-07-01 18:55:1126 clipboard_content_(clipboard_content),
27 history_url_provider_(history_url_provider) {
sdefresne70948d62015-08-11 10:46:3528 DCHECK(clipboard_content_);
29}
30
31ClipboardURLProvider::~ClipboardURLProvider() {}
32
33void ClipboardURLProvider::Start(const AutocompleteInput& input,
34 bool minimal_changes) {
35 matches_.clear();
jif3986ea502016-07-13 13:43:4236
37 // If the user started typing, do not offer clipboard based match.
sdefresne70948d62015-08-11 10:46:3538 if (!input.from_omnibox_focus())
39 return;
40
41 GURL url;
jif3986ea502016-07-13 13:43:4242 // 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.
sdefresne70948d62015-08-11 10:46:3544 if (!clipboard_content_->GetRecentURLFromClipboard(&url) ||
45 url == input.current_url())
46 return;
47
48 DCHECK(url.is_valid());
jif3986ea502016-07-13 13:43:4249 // 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()) {
gcomanici8cabc77f2017-04-27 20:04:5452 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);
sdefresne70948d62015-08-11 10:46:3559 matches_.push_back(verbatim_match);
jif3986ea502016-07-13 13:43:4260 }
mpearsonab3761492017-03-31 17:29:5161 UMA_HISTOGRAM_BOOLEAN("Omnibox.ClipboardSuggestionShownWithCurrentURL",
62 !matches_.empty());
mpearsond3dcdb02017-04-08 01:11:0363 UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionShownAge",
64 clipboard_content_->GetClipboardContentAge());
sdefresne70948d62015-08-11 10:46:3565
jif3986ea502016-07-13 13:43:4266 // Add the clipboard match. The relevance is 800 to beat ZeroSuggest results.
67 AutocompleteMatch match(this, 800, false, AutocompleteMatchType::CLIPBOARD);
sdefresne70948d62015-08-11 10:46:3568 match.destination_url = url;
69 match.contents.assign(url_formatter::FormatUrl(
jshin1fb76462016-04-05 22:13:0370 url, url_formatter::kFormatUrlOmitAll, net::UnescapeRule::SPACES,
71 nullptr, nullptr, nullptr));
sdefresne70948d62015-08-11 10:46:3572 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
sdefresne70948d62015-08-11 10:46:3581 matches_.push_back(match);
82}