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/browser/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 const PlaceholderRequestCallback
& placeholder_match_getter
)
23 : AutocompleteProvider(AutocompleteProvider::TYPE_SHORTCUTS
),
24 clipboard_recent_content_(clipboard_recent_content
),
25 placeholder_match_getter_(placeholder_match_getter
) {
26 DCHECK(clipboard_recent_content_
);
29 ClipboardURLProvider::~ClipboardURLProvider() {
32 void ClipboardURLProvider::Start(const AutocompleteInput
& input
,
33 bool minimal_changes
) {
35 if (!input
.from_omnibox_focus())
37 // Attempt to add an AutocompleteMatch only if the user has not entered
38 // anything in the omnibox.
39 if (input
.cursor_position() == base::string16::npos
) {
41 // Add the suggestion only if the user is not already on the page stored
43 if (clipboard_recent_content_
->GetRecentURLFromClipboard(&url
) &&
44 url
!= input
.current_url()) {
45 DCHECK(url
.is_valid());
46 AutocompleteMatch
match(this, kClipboardURLProviderRelevance
, false,
47 AutocompleteMatchType::NAVSUGGEST_PERSONALIZED
);
48 match
.allowed_to_be_default_match
= true;
49 match
.destination_url
= url
;
50 match
.contents
.assign(base::UTF8ToUTF16(url
.spec()));
51 AutocompleteMatch::ClassifyLocationInString(
52 base::string16::npos
, 0, match
.contents
.length(),
53 ACMatchClassification::URL
, &match
.contents_class
);
55 match
.description
.assign(
56 l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD
));
57 AutocompleteMatch::ClassifyLocationInString(
58 base::string16::npos
, 0, match
.description
.length(),
59 ACMatchClassification::URL
, &match
.description_class
);
60 // Adds a default match. This match will be opened when the user presses
62 AutocompleteMatch
current_url_match(placeholder_match_getter_
.Run(input
));
63 if (current_url_match
.destination_url
.is_valid()) {
64 // Makes the placeholder match be above the clipboard match.
65 current_url_match
.relevance
= kClipboardURLProviderRelevance
+ 1;
66 matches_
.push_back(current_url_match
);
69 matches_
.push_back(match
);