blob: 8fd7612dec3fa4fd6817b48e3082bf866efa962e [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
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"
16
17ClipboardURLProvider::ClipboardURLProvider(
18 AutocompleteProviderClient* client,
19 ClipboardRecentContent* clipboard_content)
20 : AutocompleteProvider(AutocompleteProvider::TYPE_CLIPBOARD_URL),
21 client_(client),
22 clipboard_content_(clipboard_content) {
23 DCHECK(clipboard_content_);
24}
25
26ClipboardURLProvider::~ClipboardURLProvider() {}
27
28void ClipboardURLProvider::Start(const AutocompleteInput& input,
29 bool minimal_changes) {
30 matches_.clear();
31 if (!input.from_omnibox_focus())
32 return;
33
34 GURL url;
35 if (!clipboard_content_->GetRecentURLFromClipboard(&url) ||
36 url == input.current_url())
37 return;
38
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(), 0);
43 if (verbatim_match.destination_url.is_valid())
44 matches_.push_back(verbatim_match);
45
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);
56
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);
61
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);
66}