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 "ui/base/clipboard/clipboard.h" |
| 8 |
| 9 ClipboardRecentContentGeneric::ClipboardRecentContentGeneric() {} |
| 10 |
| 11 bool ClipboardRecentContentGeneric::GetRecentURLFromClipboard(GURL* url) { |
| 12 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread(); |
| 13 base::Time last_modified_time = clipboard->GetClipboardLastModifiedTime(); |
| 14 if (!last_modified_time_to_suppress_.is_null() && |
| 15 (last_modified_time == last_modified_time_to_suppress_)) |
| 16 return false; |
| 17 |
| 18 if (GetClipboardContentAge() > kMaximumAgeOfClipboard) |
| 19 return false; |
| 20 |
| 21 // Interpret the clipboard as a URL if possible. |
| 22 std::string gurl_string; |
| 23 clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &gurl_string); |
| 24 DCHECK(url); |
| 25 if (!gurl_string.empty()) { |
| 26 *url = GURL(gurl_string); |
| 27 } else { |
| 28 // Fall back to unicode / utf16, as some URLs may use international domain |
| 29 // names, not punycode. |
| 30 base::string16 gurl_string16; |
| 31 clipboard->ReadText(ui::CLIPBOARD_TYPE_COPY_PASTE, &gurl_string16); |
| 32 if (!gurl_string16.empty()) |
| 33 *url = GURL(gurl_string16); |
| 34 } |
| 35 return url->is_valid() && IsAppropriateSuggestion(*url); |
| 36 } |
| 37 |
| 38 base::TimeDelta ClipboardRecentContentGeneric::GetClipboardContentAge() const { |
| 39 const base::Time last_modified_time = |
| 40 ui::Clipboard::GetForCurrentThread()->GetClipboardLastModifiedTime(); |
| 41 const base::Time now = base::Time::Now(); |
| 42 // In case of system clock change, assume the last modified time is now. |
| 43 // (Don't return a negative age, i.e., a time in the future.) |
| 44 if (last_modified_time > now) |
| 45 return base::TimeDelta(); |
| 46 return now - last_modified_time; |
| 47 } |
| 48 |
| 49 void ClipboardRecentContentGeneric::SuppressClipboardContent() { |
| 50 // User cleared the user data. The pasteboard entry must be removed from the |
| 51 // omnibox list. Do this by suppressing all clipboard content with the |
| 52 // current clipboard content's time. Then we only suggest the clipboard |
| 53 // content later if the time changed. |
| 54 last_modified_time_to_suppress_ = |
| 55 ui::Clipboard::GetForCurrentThread()->GetClipboardLastModifiedTime(); |
| 56 } |
OLD | NEW |