| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/omnibox/browser/clipboard_url_provider.h" | 5 #include "components/omnibox/browser/clipboard_url_provider.h" |
| 6 | 6 |
| 7 #include <algorithm> |
| 8 |
| 7 #include "base/feature_list.h" | 9 #include "base/feature_list.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/metrics/histogram_macros.h" | 11 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 11 #include "components/omnibox/browser/autocomplete_input.h" | 13 #include "components/omnibox/browser/autocomplete_input.h" |
| 12 #include "components/omnibox/browser/autocomplete_provider_client.h" | 14 #include "components/omnibox/browser/autocomplete_provider_client.h" |
| 13 #include "components/omnibox/browser/omnibox_field_trial.h" | 15 #include "components/omnibox/browser/omnibox_field_trial.h" |
| 14 #include "components/omnibox/browser/verbatim_match.h" | 16 #include "components/omnibox/browser/verbatim_match.h" |
| 15 #include "components/open_from_clipboard/clipboard_recent_content.h" | 17 #include "components/open_from_clipboard/clipboard_recent_content.h" |
| 16 #include "components/strings/grit/components_strings.h" | 18 #include "components/strings/grit/components_strings.h" |
| 17 #include "components/url_formatter/url_formatter.h" | 19 #include "components/url_formatter/url_formatter.h" |
| 18 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 19 | 21 |
| 20 ClipboardURLProvider::ClipboardURLProvider( | 22 ClipboardURLProvider::ClipboardURLProvider( |
| 21 AutocompleteProviderClient* client, | 23 AutocompleteProviderClient* client, |
| 22 HistoryURLProvider* history_url_provider, | 24 HistoryURLProvider* history_url_provider, |
| 23 ClipboardRecentContent* clipboard_content) | 25 ClipboardRecentContent* clipboard_content) |
| 24 : AutocompleteProvider(AutocompleteProvider::TYPE_CLIPBOARD_URL), | 26 : AutocompleteProvider(AutocompleteProvider::TYPE_CLIPBOARD_URL), |
| 25 client_(client), | 27 client_(client), |
| 26 clipboard_content_(clipboard_content), | 28 clipboard_content_(clipboard_content), |
| 27 history_url_provider_(history_url_provider) { | 29 history_url_provider_(history_url_provider), |
| 30 current_url_suggested_times_(0) { |
| 28 DCHECK(clipboard_content_); | 31 DCHECK(clipboard_content_); |
| 29 } | 32 } |
| 30 | 33 |
| 31 ClipboardURLProvider::~ClipboardURLProvider() {} | 34 ClipboardURLProvider::~ClipboardURLProvider() {} |
| 32 | 35 |
| 33 void ClipboardURLProvider::Start(const AutocompleteInput& input, | 36 void ClipboardURLProvider::Start(const AutocompleteInput& input, |
| 34 bool minimal_changes) { | 37 bool minimal_changes) { |
| 35 matches_.clear(); | 38 matches_.clear(); |
| 36 | 39 |
| 37 // If the user started typing, do not offer clipboard based match. | 40 // If the user started typing, do not offer clipboard based match. |
| 38 if (!input.from_omnibox_focus()) | 41 if (!input.from_omnibox_focus()) |
| 39 return; | 42 return; |
| 40 | 43 |
| 41 GURL url; | 44 GURL url; |
| 42 // If the clipboard does not contain any URL, or the URL on the page is the | 45 // The clipboard does not contain a URL worth suggesting. |
| 43 // same as the URL in the clipboard, early return. | 46 if (!clipboard_content_->GetRecentURLFromClipboard(&url)) { |
| 44 if (!clipboard_content_->GetRecentURLFromClipboard(&url) || | 47 current_url_suggested_ = GURL(); |
| 45 url == input.current_url()) | 48 current_url_suggested_times_ = 0; |
| 49 return; |
| 50 } |
| 51 // The URL on the page is the same as the URL in the clipboard. Don't |
| 52 // bother suggesting it. |
| 53 if (url == input.current_url()) |
| 46 return; | 54 return; |
| 47 | 55 |
| 48 DCHECK(url.is_valid()); | 56 DCHECK(url.is_valid()); |
| 49 // If the omnibox is not empty, add a default match. | 57 // If the omnibox is not empty, add a default match. |
| 50 // This match will be opened when the user presses "Enter". | 58 // This match will be opened when the user presses "Enter". |
| 51 if (!input.text().empty()) { | 59 if (!input.text().empty()) { |
| 52 const base::string16 description = | 60 const base::string16 description = |
| 53 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl)) | 61 (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl)) |
| 54 ? input.current_title() | 62 ? input.current_title() |
| 55 : base::string16(); | 63 : base::string16(); |
| 56 AutocompleteMatch verbatim_match = | 64 AutocompleteMatch verbatim_match = |
| 57 VerbatimMatchForURL(client_, input, input.current_url(), description, | 65 VerbatimMatchForURL(client_, input, input.current_url(), description, |
| 58 history_url_provider_, -1); | 66 history_url_provider_, -1); |
| 59 matches_.push_back(verbatim_match); | 67 matches_.push_back(verbatim_match); |
| 60 } | 68 } |
| 61 UMA_HISTOGRAM_BOOLEAN("Omnibox.ClipboardSuggestionShownWithCurrentURL", | 69 UMA_HISTOGRAM_BOOLEAN("Omnibox.ClipboardSuggestionShownWithCurrentURL", |
| 62 !matches_.empty()); | 70 !matches_.empty()); |
| 63 UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionShownAge", | 71 UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionShownAge", |
| 64 clipboard_content_->GetClipboardContentAge()); | 72 clipboard_content_->GetClipboardContentAge()); |
| 73 // Record the number of times the currently-offered URL has been suggested. |
| 74 // This only works over this run of Chrome; if the URL was in the clipboard |
| 75 // on a previous run, those offerings will not be counted. |
| 76 if (url == current_url_suggested_) { |
| 77 current_url_suggested_times_++; |
| 78 } else { |
| 79 current_url_suggested_ = url; |
| 80 current_url_suggested_times_ = 1; |
| 81 } |
| 82 UMA_HISTOGRAM_SPARSE_SLOWLY( |
| 83 "Omnibox.ClipboardSuggestionShownNumTimes", |
| 84 std::min(current_url_suggested_times_, static_cast<size_t>(20))); |
| 65 | 85 |
| 66 // Add the clipboard match. The relevance is 800 to beat ZeroSuggest results. | 86 // Add the clipboard match. The relevance is 800 to beat ZeroSuggest results. |
| 67 AutocompleteMatch match(this, 800, false, AutocompleteMatchType::CLIPBOARD); | 87 AutocompleteMatch match(this, 800, false, AutocompleteMatchType::CLIPBOARD); |
| 68 match.destination_url = url; | 88 match.destination_url = url; |
| 69 match.contents.assign(url_formatter::FormatUrl( | 89 match.contents.assign(url_formatter::FormatUrl( |
| 70 url, url_formatter::kFormatUrlOmitAll, net::UnescapeRule::SPACES, | 90 url, url_formatter::kFormatUrlOmitAll, net::UnescapeRule::SPACES, |
| 71 nullptr, nullptr, nullptr)); | 91 nullptr, nullptr, nullptr)); |
| 72 AutocompleteMatch::ClassifyLocationInString( | 92 AutocompleteMatch::ClassifyLocationInString( |
| 73 base::string16::npos, 0, match.contents.length(), | 93 base::string16::npos, 0, match.contents.length(), |
| 74 ACMatchClassification::URL, &match.contents_class); | 94 ACMatchClassification::URL, &match.contents_class); |
| 75 | 95 |
| 76 match.description.assign(l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD)); | 96 match.description.assign(l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD)); |
| 77 AutocompleteMatch::ClassifyLocationInString( | 97 AutocompleteMatch::ClassifyLocationInString( |
| 78 base::string16::npos, 0, match.description.length(), | 98 base::string16::npos, 0, match.description.length(), |
| 79 ACMatchClassification::NONE, &match.description_class); | 99 ACMatchClassification::NONE, &match.description_class); |
| 80 | 100 |
| 81 matches_.push_back(match); | 101 matches_.push_back(match); |
| 82 } | 102 } |
| 103 |
| 104 void ClipboardURLProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 105 // If a URL wasn't suggested on this most recent focus event, don't bother |
| 106 // setting |times_returned_results_in_session|, as in effect this URL has |
| 107 // never been suggested during the current session. (For the purpose of |
| 108 // this provider, we define a session as intervals between when a URL |
| 109 // clipboard suggestion changes.) |
| 110 if (current_url_suggested_times_ == 0) |
| 111 return; |
| 112 provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); |
| 113 metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); |
| 114 new_entry.set_provider(AsOmniboxEventProviderType()); |
| 115 new_entry.set_times_returned_results_in_session(current_url_suggested_times_); |
| 116 } |
| OLD | NEW |