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.
5 #include "components/omnibox/browser/clipboard_url_provider.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/omnibox/browser/autocomplete_input.h"
10 #include "components/omnibox/browser/autocomplete_provider_client.h"
11 #include "components/omnibox/browser/verbatim_match.h"
12 #include "components/open_from_clipboard/clipboard_recent_content.h"
13 #include "components/url_formatter/url_formatter.h"
14 #include "grit/components_strings.h"
15 #include "ui/base/l10n/l10n_util.h"
17 ClipboardURLProvider::ClipboardURLProvider(
18 AutocompleteProviderClient
* client
,
19 ClipboardRecentContent
* clipboard_content
)
20 : AutocompleteProvider(AutocompleteProvider::TYPE_CLIPBOARD_URL
),
22 clipboard_content_(clipboard_content
) {
23 DCHECK(clipboard_content_
);
26 ClipboardURLProvider::~ClipboardURLProvider() {}
28 void ClipboardURLProvider::Start(const AutocompleteInput
& input
,
29 bool minimal_changes
) {
31 if (!input
.from_omnibox_focus())
35 if (!clipboard_content_
->GetRecentURLFromClipboard(&url
) ||
36 url
== input
.current_url())
39 DCHECK(url
.is_valid());
40 // Adds a default match. This match will be opened when the user presses "Go".
41 AutocompleteMatch verbatim_match
= VerbatimMatchForURL(
42 client_
, input
.text(), input
.current_page_classification(), -1);
43 if (verbatim_match
.destination_url
.is_valid())
44 matches_
.push_back(verbatim_match
);
46 // Add a clipboard match just below the verbatim match.
47 AutocompleteMatch
match(this, verbatim_match
.relevance
- 1, false,
48 AutocompleteMatchType::NAVSUGGEST
);
49 match
.destination_url
= url
;
50 match
.contents
.assign(url_formatter::FormatUrl(
51 url
, client_
->GetAcceptLanguages(), url_formatter::kFormatUrlOmitAll
,
52 net::UnescapeRule::SPACES
, nullptr, nullptr, nullptr));
53 AutocompleteMatch::ClassifyLocationInString(
54 base::string16::npos
, 0, match
.contents
.length(),
55 ACMatchClassification::URL
, &match
.contents_class
);
57 match
.description
.assign(l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD
));
58 AutocompleteMatch::ClassifyLocationInString(
59 base::string16::npos
, 0, match
.description
.length(),
60 ACMatchClassification::NONE
, &match
.description_class
);
62 // At least one match must be default, so if verbatim_match was invalid,
63 // the clipboard match is allowed to be default.
64 match
.allowed_to_be_default_match
= matches_
.empty();
65 matches_
.push_back(match
);