Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(322)

Side by Side Diff: components/open_from_clipboard/clipboard_recent_content_generic.cc

Issue 2812773002: Refactor Clipboard Last Modified Time Storage (Closed)
Patch Set: itri-state enum Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 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 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/open_from_clipboard/clipboard_recent_content_generic.h" 5 #include "components/open_from_clipboard/clipboard_recent_content_generic.h"
6 6
7 #include "base/strings/string_util.h" 7 #include "base/strings/string_util.h"
8 #include "ui/base/clipboard/clipboard.h" 8 #include "ui/base/clipboard/clipboard.h"
9 9
10 ClipboardRecentContentGeneric::ClipboardRecentContentGeneric() {} 10 ClipboardRecentContentGeneric::ClipboardRecentContentGeneric() {}
11 11
12 bool ClipboardRecentContentGeneric::GetRecentURLFromClipboard(GURL* url) { 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() > MaximumAgeOfClipboard()) 13 if (GetClipboardContentAge() > MaximumAgeOfClipboard())
20 return false; 14 return false;
21 15
22 // Get and clean up the clipboard before processing. 16 // Get and clean up the clipboard before processing.
23 std::string gurl_string; 17 std::string gurl_string;
18 ui::Clipboard* clipboard = ui::Clipboard::GetForCurrentThread();
24 clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &gurl_string); 19 clipboard->ReadAsciiText(ui::CLIPBOARD_TYPE_COPY_PASTE, &gurl_string);
25 base::TrimWhitespaceASCII(gurl_string, base::TrimPositions::TRIM_ALL, 20 base::TrimWhitespaceASCII(gurl_string, base::TrimPositions::TRIM_ALL,
26 &gurl_string); 21 &gurl_string);
27 22
28 // Interpret the clipboard as a URL if possible. 23 // Interpret the clipboard as a URL if possible.
29 DCHECK(url); 24 DCHECK(url);
30 // If there is mid-string whitespace, don't attempt to interpret the string 25 // If there is mid-string whitespace, don't attempt to interpret the string
31 // as a URL. (Otherwise gurl will happily try to convert 26 // as a URL. (Otherwise gurl will happily try to convert
32 // "http://example.com extra words" into "http://example.com%20extra%20words", 27 // "http://example.com extra words" into "http://example.com%20extra%20words",
33 // which is not likely to be a useful or intended destination.) 28 // which is not likely to be a useful or intended destination.)
(...skipping 12 matching lines...) Expand all
46 std::string::npos) 41 std::string::npos)
47 return false; 42 return false;
48 if (!gurl_string16.empty()) 43 if (!gurl_string16.empty())
49 *url = GURL(gurl_string16); 44 *url = GURL(gurl_string16);
50 } 45 }
51 return url->is_valid() && IsAppropriateSuggestion(*url); 46 return url->is_valid() && IsAppropriateSuggestion(*url);
52 } 47 }
53 48
54 base::TimeDelta ClipboardRecentContentGeneric::GetClipboardContentAge() const { 49 base::TimeDelta ClipboardRecentContentGeneric::GetClipboardContentAge() const {
55 const base::Time last_modified_time = 50 const base::Time last_modified_time =
56 ui::Clipboard::GetForCurrentThread()->GetClipboardLastModifiedTime(); 51 ui::Clipboard::GetForCurrentThread()->GetLastModifiedTime();
57 const base::Time now = base::Time::Now(); 52 const base::Time now = base::Time::Now();
58 // In case of system clock change, assume the last modified time is now. 53 // 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.) 54 // (Don't return a negative age, i.e., a time in the future.)
60 if (last_modified_time > now) 55 if (last_modified_time > now)
61 return base::TimeDelta(); 56 return base::TimeDelta();
62 return now - last_modified_time; 57 return now - last_modified_time;
63 } 58 }
64 59
65 void ClipboardRecentContentGeneric::SuppressClipboardContent() { 60 void ClipboardRecentContentGeneric::SuppressClipboardContent() {
66 // User cleared the user data. The pasteboard entry must be removed from the 61 // 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 62 // omnibox list. Do this by pretending the current clipboard is ancient,
68 // current clipboard content's time. Then we only suggest the clipboard 63 // not recent.
69 // content later if the time changed. 64 ui::Clipboard::GetForCurrentThread()->ClearLastModifiedTime();
70 last_modified_time_to_suppress_ =
71 ui::Clipboard::GetForCurrentThread()->GetClipboardLastModifiedTime();
72 } 65 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698