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/open_from_clipboard/clipboard_url_provider.h"
7 #include "base/logging.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "components/omnibox/autocomplete_input.h"
10 #include "components/open_from_clipboard/clipboard_recent_content.h"
11 #include "grit/components_strings.h"
12 #include "ui/base/l10n/l10n_util.h"
15 // Score defining the priority of the suggestion. 1600 guarantees that the
16 // suggestion will always be first. See table in autocomplete_provider.h.
17 const int kClipboardURLProviderRelevance
= 1600;
20 ClipboardURLProvider::ClipboardURLProvider(
21 ClipboardRecentContent
* clipboard_recent_content
)
22 : AutocompleteProvider(AutocompleteProvider::TYPE_SHORTCUTS
),
23 clipboard_recent_content_(clipboard_recent_content
) {
24 DCHECK(clipboard_recent_content_
);
27 ClipboardURLProvider::~ClipboardURLProvider() {
30 void ClipboardURLProvider::Start(const AutocompleteInput
& input
,
32 bool called_due_to_focus
) {
34 if (!called_due_to_focus
)
36 // Attempt to add an AutocompleteMatch only if the user has not entered
37 // anything in the omnibox.
38 if (input
.cursor_position() == base::string16::npos
) {
40 if (clipboard_recent_content_
->GetRecentURLFromClipboard(&url
)) {
41 DCHECK(url
.is_valid());
42 AutocompleteMatch
match(this, kClipboardURLProviderRelevance
, false,
43 AutocompleteMatchType::NAVSUGGEST_PERSONALIZED
);
44 match
.allowed_to_be_default_match
= true;
45 match
.destination_url
= url
;
46 match
.contents
.assign(base::UTF8ToUTF16(url
.spec()));
47 AutocompleteMatch::ClassifyLocationInString(
48 base::string16::npos
, 0, match
.contents
.length(),
49 ACMatchClassification::URL
, &match
.contents_class
);
51 match
.description
.assign(
52 l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD
));
53 AutocompleteMatch::ClassifyLocationInString(
54 base::string16::npos
, 0, match
.description
.length(),
55 ACMatchClassification::URL
, &match
.description_class
);
57 matches_
.push_back(match
);