sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 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. |
| 4 | |
Robbie Gibson | f4e78b8 | 2019-02-20 18:00:19 | [diff] [blame] | 5 | #include "components/omnibox/browser/clipboard_provider.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 6 | |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 7 | #include <algorithm> |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <utility> |
Gang Wu | ba18dba | 2019-09-25 03:43:21 | [diff] [blame] | 10 | #include <vector> |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 11 | |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 12 | #include "base/bind.h" |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 13 | #include "base/feature_list.h" |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 14 | #include "base/memory/ref_counted_memory.h" |
| 15 | #include "base/memory/weak_ptr.h" |
Robbie Gibson | 1a9405b | 2019-02-21 12:20:45 | [diff] [blame] | 16 | #include "base/metrics/field_trial_params.h" |
Ilya Sherman | 1edb6f18 | 2017-12-12 04:00:42 | [diff] [blame] | 17 | #include "base/metrics/histogram_functions.h" |
mpearson | ab376149 | 2017-03-31 17:29:51 | [diff] [blame] | 18 | #include "base/metrics/histogram_macros.h" |
Gang Wu | 730add3a | 2019-11-01 00:10:55 | [diff] [blame] | 19 | #include "base/metrics/user_metrics.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 20 | #include "base/strings/utf_string_conversions.h" |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 21 | #include "base/task/post_task.h" |
Gabriel Charette | afbec875 | 2020-05-29 04:34:11 | [diff] [blame] | 22 | #include "base/task/thread_pool.h" |
Gang Wu | 3804fbc26 | 2020-03-24 00:50:14 | [diff] [blame] | 23 | #include "build/build_config.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 24 | #include "components/omnibox/browser/autocomplete_input.h" |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 25 | #include "components/omnibox/browser/autocomplete_match.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 26 | #include "components/omnibox/browser/autocomplete_provider_client.h" |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 27 | #include "components/omnibox/browser/autocomplete_provider_listener.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 28 | #include "components/omnibox/browser/verbatim_match.h" |
Tomasz Wiszkowski | d938a111 | 2019-03-06 18:01:57 | [diff] [blame] | 29 | #include "components/omnibox/common/omnibox_features.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 30 | #include "components/open_from_clipboard/clipboard_recent_content.h" |
Tommy C. Li | 17b5ba11 | 2020-08-11 02:29:21 | [diff] [blame] | 31 | #include "components/search_engines/omnibox_focus_type.h" |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 32 | #include "components/search_engines/template_url_service.h" |
thakis | fe8fa0a | 2017-02-23 19:46:36 | [diff] [blame] | 33 | #include "components/strings/grit/components_strings.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 34 | #include "components/url_formatter/url_formatter.h" |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 35 | #include "third_party/abseil-cpp/absl/types/optional.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 36 | #include "ui/base/l10n/l10n_util.h" |
Gang Wu | 0539784 | 2020-07-17 17:06:44 | [diff] [blame] | 37 | #include "ui/gfx/image/image_skia.h" |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 38 | #include "ui/gfx/image/image_util.h" |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 39 | |
Gang Wu | ba18dba | 2019-09-25 03:43:21 | [diff] [blame] | 40 | namespace { |
| 41 | |
Gang Wu | 0db1f1fa | 2019-10-02 20:30:10 | [diff] [blame] | 42 | const size_t kMaxClipboardSuggestionShownNumTimesSimpleSize = 20; |
| 43 | |
Tomasz Wiszkowski | 4d43cef | 2020-11-24 19:47:00 | [diff] [blame] | 44 | // Clipboard suggestions should be placed above search and url suggestions |
| 45 | // (including MostVisited tiles), but below query tiles. |
| 46 | const int kClipboardMatchRelevanceScore = 1501; |
Gang Wu | 64198f200 | 2020-06-25 00:28:27 | [diff] [blame] | 47 | |
Gang Wu | ba18dba | 2019-09-25 03:43:21 | [diff] [blame] | 48 | bool IsMatchDeletionEnabled() { |
| 49 | return base::FeatureList::IsEnabled( |
| 50 | omnibox::kOmniboxRemoveSuggestionsFromClipboard); |
| 51 | } |
| 52 | |
Gang Wu | 0db1f1fa | 2019-10-02 20:30:10 | [diff] [blame] | 53 | void RecordCreatingClipboardSuggestionMetrics( |
| 54 | size_t current_url_suggested_times, |
| 55 | bool matches_is_empty, |
| 56 | AutocompleteMatchType::Type match_type, |
| 57 | const base::TimeDelta clipboard_contents_age) { |
| 58 | DCHECK(match_type == AutocompleteMatchType::CLIPBOARD_URL || |
| 59 | match_type == AutocompleteMatchType::CLIPBOARD_TEXT || |
| 60 | match_type == AutocompleteMatchType::CLIPBOARD_IMAGE); |
| 61 | |
| 62 | base::UmaHistogramSparse( |
| 63 | "Omnibox.ClipboardSuggestionShownNumTimes", |
| 64 | std::min(current_url_suggested_times, |
| 65 | kMaxClipboardSuggestionShownNumTimesSimpleSize)); |
| 66 | UMA_HISTOGRAM_BOOLEAN("Omnibox.ClipboardSuggestionShownWithCurrentURL", |
| 67 | !matches_is_empty); |
| 68 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionShownAge", |
| 69 | clipboard_contents_age); |
| 70 | if (match_type == AutocompleteMatchType::CLIPBOARD_URL) { |
| 71 | base::UmaHistogramSparse( |
| 72 | "Omnibox.ClipboardSuggestionShownNumTimes.URL", |
| 73 | std::min(current_url_suggested_times, |
| 74 | kMaxClipboardSuggestionShownNumTimesSimpleSize)); |
| 75 | UMA_HISTOGRAM_BOOLEAN("Omnibox.ClipboardSuggestionShownWithCurrentURL.URL", |
| 76 | !matches_is_empty); |
| 77 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionShownAge.URL", |
| 78 | clipboard_contents_age); |
| 79 | } else if (match_type == AutocompleteMatchType::CLIPBOARD_TEXT) { |
| 80 | base::UmaHistogramSparse( |
| 81 | "Omnibox.ClipboardSuggestionShownNumTimes.TEXT", |
| 82 | std::min(current_url_suggested_times, |
| 83 | kMaxClipboardSuggestionShownNumTimesSimpleSize)); |
| 84 | UMA_HISTOGRAM_BOOLEAN("Omnibox.ClipboardSuggestionShownWithCurrentURL.TEXT", |
| 85 | !matches_is_empty); |
| 86 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionShownAge.TEXT", |
| 87 | clipboard_contents_age); |
| 88 | } else if (match_type == AutocompleteMatchType::CLIPBOARD_IMAGE) { |
| 89 | base::UmaHistogramSparse( |
| 90 | "Omnibox.ClipboardSuggestionShownNumTimes.IMAGE", |
| 91 | std::min(current_url_suggested_times, |
| 92 | kMaxClipboardSuggestionShownNumTimesSimpleSize)); |
| 93 | UMA_HISTOGRAM_BOOLEAN( |
| 94 | "Omnibox.ClipboardSuggestionShownWithCurrentURL.IMAGE", |
| 95 | !matches_is_empty); |
| 96 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionShownAge.IMAGE", |
| 97 | clipboard_contents_age); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | void RecordDeletingClipboardSuggestionMetrics( |
| 102 | AutocompleteMatchType::Type match_type, |
| 103 | const base::TimeDelta clipboard_contents_age) { |
Gang Wu | 730add3a | 2019-11-01 00:10:55 | [diff] [blame] | 104 | base::RecordAction( |
| 105 | base::UserMetricsAction("Omnibox.ClipboardSuggestionRemoved")); |
| 106 | |
Gang Wu | 0db1f1fa | 2019-10-02 20:30:10 | [diff] [blame] | 107 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionRemovedAge", |
| 108 | clipboard_contents_age); |
| 109 | if (match_type == AutocompleteMatchType::CLIPBOARD_URL) { |
| 110 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionRemovedAge.URL", |
| 111 | clipboard_contents_age); |
| 112 | } else if (match_type == AutocompleteMatchType::CLIPBOARD_TEXT) { |
| 113 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionRemovedAge.TEXT", |
| 114 | clipboard_contents_age); |
Gang Wu | 77cb111 | 2020-04-06 23:21:43 | [diff] [blame] | 115 | } else if (match_type == AutocompleteMatchType::CLIPBOARD_IMAGE) { |
| 116 | UMA_HISTOGRAM_LONG_TIMES_100("Omnibox.ClipboardSuggestionRemovedAge.IMAGE", |
| 117 | clipboard_contents_age); |
Gang Wu | 0db1f1fa | 2019-10-02 20:30:10 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
Gang Wu | ba18dba | 2019-09-25 03:43:21 | [diff] [blame] | 121 | } // namespace |
| 122 | |
Robbie Gibson | f4e78b8 | 2019-02-20 18:00:19 | [diff] [blame] | 123 | ClipboardProvider::ClipboardProvider(AutocompleteProviderClient* client, |
| 124 | AutocompleteProviderListener* listener, |
| 125 | HistoryURLProvider* history_url_provider, |
| 126 | ClipboardRecentContent* clipboard_content) |
| 127 | : AutocompleteProvider(AutocompleteProvider::TYPE_CLIPBOARD), |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 128 | client_(client), |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 129 | listener_(listener), |
mpearson | 931028c | 2016-07-01 18:55:11 | [diff] [blame] | 130 | clipboard_content_(clipboard_content), |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 131 | history_url_provider_(history_url_provider), |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 132 | current_url_suggested_times_(0), |
Robbie Gibson | 1a9405b | 2019-02-21 12:20:45 | [diff] [blame] | 133 | field_trial_triggered_(false), |
Jeremy Roman | 5c341f6d | 2019-07-15 15:56:10 | [diff] [blame] | 134 | field_trial_triggered_in_session_(false) { |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 135 | DCHECK(clipboard_content_); |
| 136 | } |
| 137 | |
Robbie Gibson | f4e78b8 | 2019-02-20 18:00:19 | [diff] [blame] | 138 | ClipboardProvider::~ClipboardProvider() {} |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 139 | |
Robbie Gibson | f4e78b8 | 2019-02-20 18:00:19 | [diff] [blame] | 140 | void ClipboardProvider::Start(const AutocompleteInput& input, |
| 141 | bool minimal_changes) { |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 142 | matches_.clear(); |
Robbie Gibson | 1a9405b | 2019-02-21 12:20:45 | [diff] [blame] | 143 | field_trial_triggered_ = false; |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 144 | |
| 145 | // If the user started typing, do not offer clipboard based match. |
Tommy C. Li | 17b5ba11 | 2020-08-11 02:29:21 | [diff] [blame] | 146 | if (input.focus_type() == OmniboxFocusType::DEFAULT) |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 147 | return; |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 148 | |
| 149 | // Image matched was kicked off asynchronously, so proceed when that ends. |
Robbie Gibson | 200b1b6 | 2019-10-08 16:13:47 | [diff] [blame] | 150 | if (CreateImageMatch(input)) |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 151 | return; |
Robbie Gibson | 200b1b6 | 2019-10-08 16:13:47 | [diff] [blame] | 152 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 153 | bool read_clipboard_content = false; |
| 154 | bool read_clipboard_url; |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 155 | absl::optional<AutocompleteMatch> optional_match = |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 156 | CreateURLMatch(input, &read_clipboard_url); |
| 157 | read_clipboard_content |= read_clipboard_url; |
| 158 | if (!optional_match) { |
| 159 | bool read_clipboard_text; |
| 160 | optional_match = CreateTextMatch(input, &read_clipboard_text); |
| 161 | read_clipboard_content |= read_clipboard_text; |
| 162 | } |
Robbie Gibson | 200b1b6 | 2019-10-08 16:13:47 | [diff] [blame] | 163 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 164 | if (optional_match) { |
| 165 | AddCreatedMatchWithTracking(input, std::move(optional_match).value(), |
| 166 | clipboard_content_->GetClipboardContentAge()); |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 167 | return; |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 168 | } |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 169 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 170 | // If there was clipboard content, but no match, don't proceed. There was |
| 171 | // some other reason for not creating a match (e.g. copied URL but the URL was |
| 172 | // the same as the current URL). |
| 173 | if (read_clipboard_content) { |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | // On iOS 14, accessing the clipboard contents shows a notification to the |
| 178 | // user. To avoid this, all the methods above will not check the contents and |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 179 | // will return false/absl::nullopt. Instead, check the existence of content |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 180 | // without accessing the actual content and create blank matches. |
| 181 | done_ = false; |
| 182 | // Image matched was kicked off asynchronously, so proceed when that ends. |
| 183 | CheckClipboardContent(input); |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 184 | } |
| 185 | |
Robbie Gibson | f4e78b8 | 2019-02-20 18:00:19 | [diff] [blame] | 186 | void ClipboardProvider::Stop(bool clear_cached_results, |
| 187 | bool due_to_user_inactivity) { |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 188 | callback_weak_ptr_factory_.InvalidateWeakPtrs(); |
| 189 | AutocompleteProvider::Stop(clear_cached_results, due_to_user_inactivity); |
| 190 | } |
| 191 | |
Gang Wu | ba18dba | 2019-09-25 03:43:21 | [diff] [blame] | 192 | void ClipboardProvider::DeleteMatch(const AutocompleteMatch& match) { |
Gang Wu | 0db1f1fa | 2019-10-02 20:30:10 | [diff] [blame] | 193 | RecordDeletingClipboardSuggestionMetrics( |
| 194 | match.type, clipboard_content_->GetClipboardContentAge()); |
Gang Wu | ba18dba | 2019-09-25 03:43:21 | [diff] [blame] | 195 | clipboard_content_->ClearClipboardContent(); |
| 196 | |
| 197 | const auto pred = [&match](const AutocompleteMatch& i) { |
| 198 | return i.contents == match.contents && i.type == match.type; |
| 199 | }; |
| 200 | base::EraseIf(matches_, pred); |
| 201 | } |
| 202 | |
| 203 | void ClipboardProvider::AddProviderInfo(ProvidersInfo* provider_info) const { |
| 204 | // If a URL wasn't suggested on this most recent focus event, don't bother |
| 205 | // setting |times_returned_results_in_session|, as in effect this URL has |
| 206 | // never been suggested during the current session. (For the purpose of |
| 207 | // this provider, we define a session as intervals between when a URL |
| 208 | // clipboard suggestion changes.) |
| 209 | if (current_url_suggested_times_ == 0) |
| 210 | return; |
| 211 | provider_info->push_back(metrics::OmniboxEventProto_ProviderInfo()); |
| 212 | metrics::OmniboxEventProto_ProviderInfo& new_entry = provider_info->back(); |
| 213 | new_entry.set_provider(AsOmniboxEventProviderType()); |
| 214 | new_entry.set_provider_done(done_); |
| 215 | new_entry.set_times_returned_results_in_session(current_url_suggested_times_); |
| 216 | |
| 217 | if (field_trial_triggered_ || field_trial_triggered_in_session_) { |
| 218 | std::vector<uint32_t> field_trial_hashes; |
| 219 | OmniboxFieldTrial::GetActiveSuggestFieldTrialHashes(&field_trial_hashes); |
| 220 | for (uint32_t trial : field_trial_hashes) { |
| 221 | if (field_trial_triggered_) { |
| 222 | new_entry.mutable_field_trial_triggered()->Add(trial); |
| 223 | } |
| 224 | if (field_trial_triggered_in_session_) { |
| 225 | new_entry.mutable_field_trial_triggered_in_session()->Add(trial); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
Robbie Gibson | 1a9405b | 2019-02-21 12:20:45 | [diff] [blame] | 231 | void ClipboardProvider::ResetSession() { |
| 232 | field_trial_triggered_ = false; |
| 233 | field_trial_triggered_in_session_ = false; |
| 234 | } |
| 235 | |
Robbie Gibson | f4e78b8 | 2019-02-20 18:00:19 | [diff] [blame] | 236 | void ClipboardProvider::AddCreatedMatchWithTracking( |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 237 | const AutocompleteInput& input, |
Robbie Gibson | 50dce21 | 2019-02-19 16:42:25 | [diff] [blame] | 238 | const AutocompleteMatch& match, |
| 239 | const base::TimeDelta clipboard_contents_age) { |
Robbie Gibson | d2a2eee | 2019-01-25 14:38:10 | [diff] [blame] | 240 | // Record the number of times the currently-offered URL has been suggested. |
| 241 | // This only works over this run of Chrome; if the URL was in the clipboard |
| 242 | // on a previous run, those offerings will not be counted. |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 243 | if (match.destination_url == current_url_suggested_) { |
Robbie Gibson | d2a2eee | 2019-01-25 14:38:10 | [diff] [blame] | 244 | current_url_suggested_times_++; |
| 245 | } else { |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 246 | current_url_suggested_ = match.destination_url; |
Robbie Gibson | d2a2eee | 2019-01-25 14:38:10 | [diff] [blame] | 247 | current_url_suggested_times_ = 1; |
| 248 | } |
| 249 | |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 250 | // If the omnibox is not empty, add a default match. |
| 251 | // This match will be opened when the user presses "Enter". |
| 252 | if (!input.text().empty()) { |
Jan Wilken Dörrie | 624543a | 2021-03-12 04:18:09 | [diff] [blame] | 253 | const std::u16string description = |
gcomanici | 8cabc77f | 2017-04-27 20:04:54 | [diff] [blame] | 254 | (base::FeatureList::IsEnabled(omnibox::kDisplayTitleForCurrentUrl)) |
| 255 | ? input.current_title() |
Jan Wilken Dörrie | 624543a | 2021-03-12 04:18:09 | [diff] [blame] | 256 | : std::u16string(); |
Tomasz Wiszkowski | 913b806 | 2021-03-10 21:52:48 | [diff] [blame] | 257 | AutocompleteMatch verbatim_match = VerbatimMatchForURL( |
| 258 | this, client_, input, input.current_url(), description, -1); |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 259 | matches_.push_back(verbatim_match); |
jif | 3986ea50 | 2016-07-13 13:43:42 | [diff] [blame] | 260 | } |
Gang Wu | 0db1f1fa | 2019-10-02 20:30:10 | [diff] [blame] | 261 | |
| 262 | RecordCreatingClipboardSuggestionMetrics(current_url_suggested_times_, |
| 263 | matches_.empty(), match.type, |
| 264 | clipboard_contents_age); |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 265 | |
manuk | 21aa24d | 2019-05-01 15:08:16 | [diff] [blame] | 266 | matches_.push_back(match); |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 267 | } |
| 268 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 269 | bool ClipboardProvider::TemplateURLSupportsTextSearch() { |
| 270 | TemplateURLService* url_service = client_->GetTemplateURLService(); |
| 271 | const TemplateURL* default_url = url_service->GetDefaultSearchProvider(); |
| 272 | if (!default_url) |
| 273 | return false; |
| 274 | |
| 275 | DCHECK(!default_url->url().empty()); |
| 276 | DCHECK(default_url->url_ref().IsValid(url_service->search_terms_data())); |
| 277 | return true; |
| 278 | } |
| 279 | |
| 280 | bool ClipboardProvider::TemplateURLSupportsImageSearch() { |
| 281 | TemplateURLService* url_service = client_->GetTemplateURLService(); |
| 282 | const TemplateURL* default_url = url_service->GetDefaultSearchProvider(); |
| 283 | |
| 284 | return default_url && !default_url->image_url().empty() && |
| 285 | default_url->image_url_ref().IsValid(url_service->search_terms_data()); |
| 286 | } |
| 287 | |
| 288 | void ClipboardProvider::CheckClipboardContent(const AutocompleteInput& input) { |
| 289 | std::set<ClipboardContentType> desired_types; |
| 290 | desired_types.insert(ClipboardContentType::URL); |
| 291 | |
| 292 | if (TemplateURLSupportsTextSearch()) { |
| 293 | desired_types.insert(ClipboardContentType::Text); |
| 294 | } |
| 295 | |
Gang Wu | a801f8b | 2020-11-03 18:23:13 | [diff] [blame] | 296 | if (TemplateURLSupportsImageSearch()) { |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 297 | desired_types.insert(ClipboardContentType::Image); |
| 298 | } |
| 299 | |
| 300 | // We want to get the age here because the contents of the clipboard could |
| 301 | // change after this point. We want the age of the contents we actually use, |
| 302 | // not the age of whatever's on the clipboard when the histogram is created |
| 303 | // (i.e when the match is created). |
| 304 | base::TimeDelta clipboard_contents_age = |
| 305 | clipboard_content_->GetClipboardContentAge(); |
| 306 | clipboard_content_->HasRecentContentFromClipboard( |
| 307 | desired_types, |
| 308 | base::BindOnce(&ClipboardProvider::OnReceiveClipboardContent, |
| 309 | callback_weak_ptr_factory_.GetWeakPtr(), input, |
| 310 | clipboard_contents_age)); |
| 311 | } |
| 312 | |
| 313 | void ClipboardProvider::OnReceiveClipboardContent( |
| 314 | const AutocompleteInput& input, |
| 315 | base::TimeDelta clipboard_contents_age, |
| 316 | std::set<ClipboardContentType> matched_types) { |
| 317 | if (matched_types.find(ClipboardContentType::Image) != matched_types.end()) { |
| 318 | // The image content will be added in later. If the image is large, encoding |
| 319 | // the image may take some time, so just be wary whenever that step happens |
| 320 | // (e.g OmniboxView::OpenMatch). |
| 321 | AutocompleteMatch match = NewBlankImageMatch(); |
| 322 | field_trial_triggered_ = true; |
| 323 | field_trial_triggered_in_session_ = true; |
Gang Wu | a801f8b | 2020-11-03 18:23:13 | [diff] [blame] | 324 | AddCreatedMatchWithTracking(input, match, clipboard_contents_age); |
| 325 | listener_->OnProviderUpdate(true); |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 326 | } else if (matched_types.find(ClipboardContentType::URL) != |
| 327 | matched_types.end()) { |
| 328 | AutocompleteMatch match = NewBlankURLMatch(); |
| 329 | AddCreatedMatchWithTracking(input, match, clipboard_contents_age); |
| 330 | listener_->OnProviderUpdate(true); |
| 331 | } else if (matched_types.find(ClipboardContentType::Text) != |
| 332 | matched_types.end()) { |
| 333 | AutocompleteMatch match = NewBlankTextMatch(); |
| 334 | AddCreatedMatchWithTracking(input, match, clipboard_contents_age); |
| 335 | listener_->OnProviderUpdate(true); |
| 336 | } |
| 337 | done_ = true; |
| 338 | } |
| 339 | |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 340 | absl::optional<AutocompleteMatch> ClipboardProvider::CreateURLMatch( |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 341 | const AutocompleteInput& input, |
| 342 | bool* read_clipboard_content) { |
| 343 | *read_clipboard_content = false; |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 344 | if (base::FeatureList::IsEnabled( |
| 345 | omnibox::kClipboardSuggestionContentHidden)) { |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 346 | return absl::nullopt; |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 347 | } |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 348 | // The clipboard does not contain a URL worth suggesting. |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 349 | absl::optional<GURL> optional_gurl = |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 350 | clipboard_content_->GetRecentURLFromClipboard(); |
Robbie Gibson | 200b1b6 | 2019-10-08 16:13:47 | [diff] [blame] | 351 | if (!optional_gurl) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 352 | return absl::nullopt; |
Robbie Gibson | 200b1b6 | 2019-10-08 16:13:47 | [diff] [blame] | 353 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 354 | *read_clipboard_content = true; |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 355 | GURL url = std::move(optional_gurl).value(); |
| 356 | |
| 357 | // The URL on the page is the same as the URL in the clipboard. Don't |
| 358 | // bother suggesting it. |
| 359 | if (url == input.current_url()) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 360 | return absl::nullopt; |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 361 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 362 | return NewClipboardURLMatch(url); |
| 363 | } |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 364 | |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 365 | absl::optional<AutocompleteMatch> ClipboardProvider::CreateTextMatch( |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 366 | const AutocompleteInput& input, |
| 367 | bool* read_clipboard_content) { |
| 368 | *read_clipboard_content = false; |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 369 | if (base::FeatureList::IsEnabled( |
| 370 | omnibox::kClipboardSuggestionContentHidden)) { |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 371 | return absl::nullopt; |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 372 | } |
| 373 | |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 374 | absl::optional<std::u16string> optional_text = |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 375 | clipboard_content_->GetRecentTextFromClipboard(); |
| 376 | if (!optional_text) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 377 | return absl::nullopt; |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 378 | |
| 379 | *read_clipboard_content = true; |
Jan Wilken Dörrie | 624543a | 2021-03-12 04:18:09 | [diff] [blame] | 380 | std::u16string text = std::move(optional_text).value(); |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 381 | |
| 382 | // The clipboard can contain the empty string, which shouldn't be suggested. |
| 383 | if (text.empty()) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 384 | return absl::nullopt; |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 385 | |
| 386 | // The text in the clipboard is a url. We don't want to prompt the user to |
| 387 | // search for a url. |
| 388 | if (GURL(text).is_valid()) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 389 | return absl::nullopt; |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 390 | |
| 391 | return NewClipboardTextMatch(text); |
| 392 | } |
| 393 | |
| 394 | bool ClipboardProvider::CreateImageMatch(const AutocompleteInput& input) { |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 395 | if (base::FeatureList::IsEnabled( |
| 396 | omnibox::kClipboardSuggestionContentHidden)) { |
| 397 | return false; |
| 398 | } |
| 399 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 400 | if (!clipboard_content_->HasRecentImageFromClipboard()) { |
| 401 | return false; |
| 402 | } |
| 403 | |
| 404 | if (!TemplateURLSupportsImageSearch()) { |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | done_ = false; |
| 409 | |
| 410 | // We want to get the age here because the contents of the clipboard could |
| 411 | // change after this point. We want the age of the image we actually use, not |
| 412 | // the age of whatever's on the clipboard when the histogram is created (i.e |
| 413 | // when the match is created). |
| 414 | base::TimeDelta clipboard_contents_age = |
| 415 | clipboard_content_->GetClipboardContentAge(); |
| 416 | clipboard_content_->GetRecentImageFromClipboard(base::BindOnce( |
| 417 | &ClipboardProvider::CreateImageMatchCallback, |
| 418 | callback_weak_ptr_factory_.GetWeakPtr(), input, clipboard_contents_age)); |
| 419 | return true; |
| 420 | } |
| 421 | |
| 422 | void ClipboardProvider::CreateImageMatchCallback( |
| 423 | const AutocompleteInput& input, |
| 424 | const base::TimeDelta clipboard_contents_age, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 425 | absl::optional<gfx::Image> optional_image) { |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 426 | NewClipboardImageMatch( |
Gang Wu | 67a448d | 2021-04-06 17:15:57 | [diff] [blame] | 427 | optional_image, base::BindOnce(&ClipboardProvider::AddImageMatchCallback, |
| 428 | callback_weak_ptr_factory_.GetWeakPtr(), |
| 429 | input, clipboard_contents_age)); |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 430 | } |
Gang Wu | 67a448d | 2021-04-06 17:15:57 | [diff] [blame] | 431 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 432 | void ClipboardProvider::AddImageMatchCallback( |
| 433 | const AutocompleteInput& input, |
| 434 | const base::TimeDelta clipboard_contents_age, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 435 | absl::optional<AutocompleteMatch> match) { |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 436 | if (!match) { |
| 437 | return; |
| 438 | } |
| 439 | AddCreatedMatchWithTracking(input, match.value(), clipboard_contents_age); |
| 440 | listener_->OnProviderUpdate(true); |
| 441 | done_ = true; |
| 442 | } |
| 443 | |
| 444 | AutocompleteMatch ClipboardProvider::NewBlankURLMatch() { |
Gang Wu | 64198f200 | 2020-06-25 00:28:27 | [diff] [blame] | 445 | AutocompleteMatch match(this, kClipboardMatchRelevanceScore, |
| 446 | IsMatchDeletionEnabled(), |
Robbie Gibson | 3e74f7d | 2019-01-21 14:03:00 | [diff] [blame] | 447 | AutocompleteMatchType::CLIPBOARD_URL); |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 448 | |
| 449 | match.description.assign(l10n_util::GetStringUTF16(IDS_LINK_FROM_CLIPBOARD)); |
| 450 | if (!match.description.empty()) |
| 451 | match.description_class.push_back({0, ACMatchClassification::NONE}); |
| 452 | return match; |
| 453 | } |
| 454 | |
| 455 | AutocompleteMatch ClipboardProvider::NewClipboardURLMatch(GURL url) { |
| 456 | DCHECK(url.is_valid()); |
| 457 | |
| 458 | AutocompleteMatch match = NewBlankURLMatch(); |
| 459 | |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 460 | UpdateClipboardURLContent(url, &match); |
Robbie Gibson | 66a41d9 | 2019-11-27 10:04:11 | [diff] [blame] | 461 | |
Robbie Gibson | 0ab91ac | 2020-08-13 15:06:05 | [diff] [blame] | 462 | return match; |
| 463 | } |
Robbie Gibson | 146bab0 | 2019-04-02 22:29:34 | [diff] [blame] | 464 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 465 | AutocompleteMatch ClipboardProvider::NewBlankTextMatch() { |
| 466 | AutocompleteMatch match(this, kClipboardMatchRelevanceScore, |
| 467 | IsMatchDeletionEnabled(), |
| 468 | AutocompleteMatchType::CLIPBOARD_TEXT); |
Justin Cohen | 40f1bad | 2020-08-13 19:34:26 | [diff] [blame] | 469 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 470 | match.description.assign(l10n_util::GetStringUTF16(IDS_TEXT_FROM_CLIPBOARD)); |
| 471 | if (!match.description.empty()) |
| 472 | match.description_class.push_back({0, ACMatchClassification::NONE}); |
Justin Cohen | 40f1bad | 2020-08-13 19:34:26 | [diff] [blame] | 473 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 474 | match.transition = ui::PAGE_TRANSITION_GENERATED; |
| 475 | return match; |
| 476 | } |
Justin Cohen | 40f1bad | 2020-08-13 19:34:26 | [diff] [blame] | 477 | |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 478 | absl::optional<AutocompleteMatch> ClipboardProvider::NewClipboardTextMatch( |
Jan Wilken Dörrie | 624543a | 2021-03-12 04:18:09 | [diff] [blame] | 479 | std::u16string text) { |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 480 | AutocompleteMatch match = NewBlankTextMatch(); |
Robbie Gibson | 66a41d9 | 2019-11-27 10:04:11 | [diff] [blame] | 481 | |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 482 | if (!UpdateClipboardTextContent(text, &match)) |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 483 | return absl::nullopt; |
Robbie Gibson | 200b1b6 | 2019-10-08 16:13:47 | [diff] [blame] | 484 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 485 | return match; |
| 486 | } |
| 487 | |
| 488 | AutocompleteMatch ClipboardProvider::NewBlankImageMatch() { |
| 489 | AutocompleteMatch match(this, kClipboardMatchRelevanceScore, |
| 490 | IsMatchDeletionEnabled(), |
| 491 | AutocompleteMatchType::CLIPBOARD_IMAGE); |
| 492 | |
| 493 | match.description.assign(l10n_util::GetStringUTF16(IDS_IMAGE_FROM_CLIPBOARD)); |
manuk | 21aa24d | 2019-05-01 15:08:16 | [diff] [blame] | 494 | if (!match.description.empty()) |
| 495 | match.description_class.push_back({0, ACMatchClassification::NONE}); |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 496 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 497 | // This will end up being something like "Search for Copied Image." This may |
| 498 | // seem strange to use for |fill_into_edit|, but it is because iOS requires |
| 499 | // some text in the text field for the Enter key to work when using keyboard |
| 500 | // navigation. |
| 501 | match.fill_into_edit = match.description; |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 502 | match.transition = ui::PAGE_TRANSITION_GENERATED; |
| 503 | |
Robbie Gibson | e6915ce1 | 2018-12-26 12:08:10 | [diff] [blame] | 504 | return match; |
sdefresne | 70948d6 | 2015-08-11 10:46:35 | [diff] [blame] | 505 | } |
mpearson | ea5016a | 2017-06-01 22:20:32 | [diff] [blame] | 506 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 507 | void ClipboardProvider::NewClipboardImageMatch( |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 508 | absl::optional<gfx::Image> optional_image, |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 509 | ClipboardImageMatchCallback callback) { |
Robbie Gibson | 25d0ca2 | 2020-10-19 17:00:32 | [diff] [blame] | 510 | // ImageSkia::ToImageSkia should only be called if the gfx::Image is |
| 511 | // non-empty. It is unclear when the clipboard returns a non-optional but |
| 512 | // empty image. See crbug.com/1136759 for more details. |
| 513 | if (!optional_image || optional_image.value().IsEmpty()) { |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 514 | std::move(callback).Run(absl::nullopt); |
Maksim Ivanov | 8f2cf7fa | 2020-08-31 16:39:38 | [diff] [blame] | 515 | return; |
| 516 | } |
Gang Wu | 0539784 | 2020-07-17 17:06:44 | [diff] [blame] | 517 | gfx::ImageSkia image_skia = *optional_image.value().ToImageSkia(); |
| 518 | image_skia.MakeThreadSafe(); |
Gabriel Charette | afbec875 | 2020-05-29 04:34:11 | [diff] [blame] | 519 | base::ThreadPool::PostTaskAndReplyWithResult( |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 520 | FROM_HERE, |
Gang Wu | 0539784 | 2020-07-17 17:06:44 | [diff] [blame] | 521 | base::BindOnce(&ClipboardProvider::EncodeClipboardImage, image_skia), |
Robbie Gibson | f4e78b8 | 2019-02-20 18:00:19 | [diff] [blame] | 522 | base::BindOnce(&ClipboardProvider::ConstructImageMatchCallback, |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 523 | callback_weak_ptr_factory_.GetWeakPtr(), |
| 524 | std::move(callback))); |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 525 | } |
| 526 | |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 527 | void ClipboardProvider::UpdateClipboardMatchWithContent( |
| 528 | AutocompleteMatch* match, |
| 529 | ClipboardMatchCallback callback) { |
| 530 | DCHECK(match); |
| 531 | if (match->type == AutocompleteMatchType::CLIPBOARD_URL) { |
| 532 | clipboard_content_->GetRecentURLFromClipboard(base::BindOnce( |
| 533 | &ClipboardProvider::OnReceiveURLForMatchWithContent, |
| 534 | callback_weak_ptr_factory_.GetWeakPtr(), std::move(callback), match)); |
| 535 | return; |
| 536 | } else if (match->type == AutocompleteMatchType::CLIPBOARD_TEXT) { |
| 537 | clipboard_content_->GetRecentTextFromClipboard(base::BindOnce( |
| 538 | &ClipboardProvider::OnReceiveTextForMatchWithContent, |
| 539 | callback_weak_ptr_factory_.GetWeakPtr(), std::move(callback), match)); |
| 540 | return; |
| 541 | } else if (match->type == AutocompleteMatchType::CLIPBOARD_IMAGE) { |
| 542 | clipboard_content_->GetRecentImageFromClipboard(base::BindOnce( |
| 543 | &ClipboardProvider::OnReceiveImageForMatchWithContent, |
| 544 | callback_weak_ptr_factory_.GetWeakPtr(), std::move(callback), match)); |
| 545 | return; |
| 546 | } |
| 547 | } |
| 548 | |
Robbie Gibson | f4e78b8 | 2019-02-20 18:00:19 | [diff] [blame] | 549 | scoped_refptr<base::RefCountedMemory> ClipboardProvider::EncodeClipboardImage( |
Gang Wu | 0539784 | 2020-07-17 17:06:44 | [diff] [blame] | 550 | gfx::ImageSkia image_skia) { |
| 551 | gfx::Image resized_image = |
| 552 | gfx::ResizedImageForSearchByImage(gfx::Image(image_skia)); |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 553 | return resized_image.As1xPNGBytes(); |
| 554 | } |
| 555 | |
Robbie Gibson | f4e78b8 | 2019-02-20 18:00:19 | [diff] [blame] | 556 | void ClipboardProvider::ConstructImageMatchCallback( |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 557 | ClipboardImageMatchCallback callback, |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 558 | scoped_refptr<base::RefCountedMemory> image_bytes) { |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 559 | TemplateURLService* url_service = client_->GetTemplateURLService(); |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 560 | const TemplateURL* default_url = url_service->GetDefaultSearchProvider(); |
Robbie Gibson | 200b1b6 | 2019-10-08 16:13:47 | [diff] [blame] | 561 | DCHECK(default_url); |
Gang Wu | 64198f200 | 2020-06-25 00:28:27 | [diff] [blame] | 562 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 563 | AutocompleteMatch match = NewBlankImageMatch(); |
Robbie Gibson | 66a41d9 | 2019-11-27 10:04:11 | [diff] [blame] | 564 | |
Gang Wu | c0173f1 | 2020-05-27 06:10:15 | [diff] [blame] | 565 | match.search_terms_args = |
Jan Wilken Dörrie | 756999e | 2021-03-23 15:05:24 | [diff] [blame] | 566 | std::make_unique<TemplateURLRef::SearchTermsArgs>(u""); |
Gang Wu | c0173f1 | 2020-05-27 06:10:15 | [diff] [blame] | 567 | match.search_terms_args->image_thumbnail_content.assign( |
| 568 | image_bytes->front_as<char>(), image_bytes->size()); |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 569 | TemplateURLRef::PostContent post_content; |
| 570 | GURL result(default_url->image_url_ref().ReplaceSearchTerms( |
Gang Wu | c0173f1 | 2020-05-27 06:10:15 | [diff] [blame] | 571 | *match.search_terms_args.get(), url_service->search_terms_data(), |
| 572 | &post_content)); |
| 573 | |
Gang Wu | a801f8b | 2020-11-03 18:23:13 | [diff] [blame] | 574 | if (!base::FeatureList::IsEnabled(omnibox::kImageSearchSuggestionThumbnail)) { |
Gang Wu | c0173f1 | 2020-05-27 06:10:15 | [diff] [blame] | 575 | // If Omnibox image suggestion do not need thumbnail, release memory. |
| 576 | match.search_terms_args.reset(); |
| 577 | } |
Robbie Gibson | 71b0c52 | 2019-02-13 00:05:58 | [diff] [blame] | 578 | match.destination_url = result; |
| 579 | match.post_content = |
| 580 | std::make_unique<TemplateURLRef::PostContent>(post_content); |
| 581 | |
Robbie Gibson | c28f2fd4 | 2020-08-17 18:52:22 | [diff] [blame] | 582 | std::move(callback).Run(match); |
Robbie Gibson | e13f2e6 | 2019-01-09 11:00:24 | [diff] [blame] | 583 | } |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 584 | |
| 585 | void ClipboardProvider::OnReceiveURLForMatchWithContent( |
| 586 | ClipboardMatchCallback callback, |
| 587 | AutocompleteMatch* match, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 588 | absl::optional<GURL> optional_gurl) { |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 589 | if (!optional_gurl) |
| 590 | return; |
| 591 | |
| 592 | GURL url = std::move(optional_gurl).value(); |
| 593 | UpdateClipboardURLContent(url, match); |
| 594 | std::move(callback).Run(); |
| 595 | } |
| 596 | |
| 597 | void ClipboardProvider::OnReceiveTextForMatchWithContent( |
| 598 | ClipboardMatchCallback callback, |
| 599 | AutocompleteMatch* match, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 600 | absl::optional<std::u16string> optional_text) { |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 601 | if (!optional_text) |
| 602 | return; |
| 603 | |
| 604 | std::u16string text = std::move(optional_text).value(); |
| 605 | if (!UpdateClipboardTextContent(text, match)) |
| 606 | return; |
| 607 | |
| 608 | std::move(callback).Run(); |
| 609 | } |
| 610 | |
| 611 | void ClipboardProvider::OnReceiveImageForMatchWithContent( |
| 612 | ClipboardMatchCallback callback, |
| 613 | AutocompleteMatch* match, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 614 | absl::optional<gfx::Image> optional_image) { |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 615 | if (!optional_image) |
| 616 | return; |
| 617 | |
| 618 | gfx::Image image = std::move(optional_image).value(); |
| 619 | NewClipboardImageMatch( |
| 620 | image, |
| 621 | base::BindOnce(&ClipboardProvider::OnReceiveImageMatchForMatchWithContent, |
| 622 | callback_weak_ptr_factory_.GetWeakPtr(), |
| 623 | std::move(callback), match)); |
| 624 | } |
| 625 | |
| 626 | void ClipboardProvider::OnReceiveImageMatchForMatchWithContent( |
| 627 | ClipboardMatchCallback callback, |
| 628 | AutocompleteMatch* match, |
Anton Bikineev | 1156b5f | 2021-05-15 22:35:36 | [diff] [blame^] | 629 | absl::optional<AutocompleteMatch> optional_match) { |
Gang Wu | 6b5405d7 | 2021-04-09 21:48:20 | [diff] [blame] | 630 | DCHECK(match); |
| 631 | if (!optional_match) |
| 632 | return; |
| 633 | |
| 634 | match->destination_url = std::move(optional_match->destination_url); |
| 635 | match->post_content = std::move(optional_match->post_content); |
| 636 | match->search_terms_args = std::move(optional_match->search_terms_args); |
| 637 | |
| 638 | std::move(callback).Run(); |
| 639 | } |
| 640 | |
| 641 | void ClipboardProvider::UpdateClipboardURLContent(const GURL& url, |
| 642 | AutocompleteMatch* match) { |
| 643 | DCHECK(url.is_valid()); |
| 644 | DCHECK(match); |
| 645 | |
| 646 | match->destination_url = url; |
| 647 | |
| 648 | // Because the user did not type a related input to get this clipboard |
| 649 | // suggestion, preserve the subdomain so the user has extra context. |
| 650 | auto format_types = AutocompleteMatch::GetFormatTypes(false, true); |
| 651 | match->contents.assign(url_formatter::FormatUrl( |
| 652 | url, format_types, net::UnescapeRule::SPACES, nullptr, nullptr, nullptr)); |
| 653 | if (!match->contents.empty()) |
| 654 | match->contents_class.push_back({0, ACMatchClassification::URL}); |
| 655 | match->fill_into_edit = |
| 656 | AutocompleteInput::FormattedStringWithEquivalentMeaning( |
| 657 | url, match->contents, client_->GetSchemeClassifier(), nullptr); |
| 658 | } |
| 659 | |
| 660 | bool ClipboardProvider::UpdateClipboardTextContent(const std::u16string& text, |
| 661 | AutocompleteMatch* match) { |
| 662 | DCHECK(match); |
| 663 | |
| 664 | // The text in the clipboard is a url. We don't want to prompt the user to |
| 665 | // search for a url. |
| 666 | if (GURL(text).is_valid()) |
| 667 | return false; |
| 668 | |
| 669 | match->fill_into_edit = text; |
| 670 | |
| 671 | TemplateURLService* url_service = client_->GetTemplateURLService(); |
| 672 | const TemplateURL* default_url = url_service->GetDefaultSearchProvider(); |
| 673 | if (!default_url) |
| 674 | return false; |
| 675 | |
| 676 | DCHECK(!default_url->url().empty()); |
| 677 | DCHECK(default_url->url_ref().IsValid(url_service->search_terms_data())); |
| 678 | TemplateURLRef::SearchTermsArgs search_args(text); |
| 679 | GURL result(default_url->url_ref().ReplaceSearchTerms( |
| 680 | search_args, url_service->search_terms_data())); |
| 681 | |
| 682 | match->destination_url = result; |
| 683 | match->contents.assign(l10n_util::GetStringFUTF16( |
| 684 | IDS_COPIED_TEXT_FROM_CLIPBOARD, AutocompleteMatch::SanitizeString(text))); |
| 685 | if (!match->contents.empty()) |
| 686 | match->contents_class.push_back({0, ACMatchClassification::NONE}); |
| 687 | |
| 688 | match->keyword = default_url->keyword(); |
| 689 | |
| 690 | return true; |
| 691 | } |