OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 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/open_from_clipboard/clipboard_recent_content_generic.h" |
| 6 |
| 7 #include "base/strings/string_util.h" |
| 8 #include "ui/base/clipboard/clipboard.h" |
| 9 |
| 10 ClipboardRecentContentGeneric::ClipboardRecentContentGeneric() {} |
| 11 |
| 12 bool ClipboardRecentContentGeneric::GetRecentURLFromClipboard(GURL* url) { |
| 13 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
| 14 base::Time last_modified_time = clipboard->GetClipboardLastModifiedTime(); |
| 15 if (!last_modified_time_to_suppress_.is_null() && |
| 16 (last_modified_time == last_modified_time_to_suppress_)) |
| 17 return false; |
| 18 |
| 19 if (GetClipboardContentAge() > kMaximumAgeOfClipboard) |
| 20 return false; |
| 21 |
| 22 // Get and clean up the clipboard before processing. |
| 23 std::string gurl_string; |
| 24 clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &gurl_string); |
| 25 base::TrimWhitespaceASCII(gurl_string, base::TrimPositions::TRIM_ALL, |
| 26 &gurl_string); |
| 27 |
| 28 // Interpret the clipboard as a URL if possible. |
| 29 DCHECK(url); |
| 30 // If there is mid-string whitespace, don't attempt to interpret the string |
| 31 // as a URL. (Otherwise gurl will happily try to convert |
| 32 // "http://example.com extra words" into "http://example.com%20extra%20words", |
| 33 // which is not likely to be a useful or intended destination.) |
| 34 if (gurl_string.find_first_of(base::kWhitespaceASCII) != std::string::npos) |
| 35 return false; |
| 36 if (!gurl_string.empty()) { |
| 37 *url = GURL(gurl_string); |
| 38 } else { |
| 39 // Fall back to unicode / UTF16, as some URLs may use international domain |
| 40 // names, not punycode. |
| 41 base::string16 gurl_string16; |
| 42 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &gurl_string16); |
| 43 base::TrimWhitespace(gurl_string16, base::TrimPositions::TRIM_ALL, |
| 44 &gurl_string16); |
| 45 if (gurl_string16.find_first_of(base::kWhitespaceUTF16) != |
| 46 std::string::npos) |
| 47 return false; |
| 48 if (!gurl_string16.empty()) |
| 49 *url = GURL(gurl_string16); |
| 50 } |
| 51 return url->is_valid() && IsAppropriateSuggestion(*url); |
| 52 } |
| 53 |
| 54 base::TimeDelta ClipboardRecentContentGeneric::GetClipboardContentAge() const { |
| 55 const base::Time last_modified_time = |
| 56 ui::Clipboard::GetForCurrentThread()->GetClipboardLastModifiedTime(); |
| 57 const base::Time now = base::Time::Now(); |
| 58 // In case of system clock change, assume the last modified time is now. |
| 59 // (Don't return a negative age, i.e., a time in the future.) |
| 60 if (last_modified_time > now) |
| 61 return base::TimeDelta(); |
| 62 return now - last_modified_time; |
| 63 } |
| 64 |
| 65 void ClipboardRecentContentGeneric::SuppressClipboardContent() { |
| 66 // User cleared the user data. The pasteboard entry must be removed from the |
| 67 // omnibox list. Do this by suppressing all clipboard content with the |
| 68 // current clipboard content's time. Then we only suggest the clipboard |
| 69 // content later if the time changed. |
| 70 last_modified_time_to_suppress_ = |
| 71 ui::Clipboard::GetForCurrentThread()->GetClipboardLastModifiedTime(); |
| 72 } |
OLD | NEW |