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