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/open_from_clipboard/clipboard_recent_content_ios.h" | 5 #include "components/open_from_clipboard/clipboard_recent_content_ios.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
12 #include "base/metrics/user_metrics.h" | 12 #include "base/metrics/user_metrics.h" |
13 #include "base/strings/sys_string_conversions.h" | 13 #include "base/strings/sys_string_conversions.h" |
| 14 #include "base/sys_info.h" |
14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
15 #include "url/url_constants.h" | 16 #include "url/url_constants.h" |
16 | 17 |
17 ClipboardRecentContent* ClipboardRecentContent::GetInstance() { | 18 ClipboardRecentContent* ClipboardRecentContent::GetInstance() { |
18 return ClipboardRecentContentIOS::GetInstance(); | 19 return ClipboardRecentContentIOS::GetInstance(); |
19 } | 20 } |
20 | 21 |
21 // Bridge that forwards pasteboard change notifications to its delegate. | 22 // Bridge that forwards pasteboard change notifications to its delegate. |
22 @interface PasteboardNotificationListenerBridge : NSObject { | 23 @interface PasteboardNotificationListenerBridge : NSObject { |
23 ClipboardRecentContentIOS* _delegate; | 24 ClipboardRecentContentIOS* _delegate; |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
109 lastPasteboardChangeDate_.reset([[NSDate date] retain]); | 110 lastPasteboardChangeDate_.reset([[NSDate date] retain]); |
110 lastPasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount; | 111 lastPasteboardChangeCount_ = [UIPasteboard generalPasteboard].changeCount; |
111 SaveToUserDefaults(); | 112 SaveToUserDefaults(); |
112 } | 113 } |
113 } | 114 } |
114 | 115 |
115 ClipboardRecentContentIOS::ClipboardRecentContentIOS() | 116 ClipboardRecentContentIOS::ClipboardRecentContentIOS() |
116 : ClipboardRecentContent() { | 117 : ClipboardRecentContent() { |
117 urlFromPasteboardCache_ = URLFromPasteboard(); | 118 urlFromPasteboardCache_ = URLFromPasteboard(); |
118 LoadFromUserDefaults(); | 119 LoadFromUserDefaults(); |
119 if ([UIPasteboard generalPasteboard].changeCount != | 120 // The pasteboard's changeCount is reset to zero when the device is restarted. |
120 lastPasteboardChangeCount_) { | 121 // This means that even if |changeCount| hasn't changed, the pasteboard |
121 PasteboardChanged(); | 122 // content could have changed. In order to avoid missing pasteboard changes, |
| 123 // the changeCount is reset if the device has restarted. |
| 124 NSInteger changeCount = [UIPasteboard generalPasteboard].changeCount; |
| 125 if (changeCount != lastPasteboardChangeCount_ || |
| 126 DeviceRestartedSincePasteboardChanged()) { |
| 127 PasteboardChanged(); |
122 } | 128 } |
123 notificationBridge_.reset( | 129 notificationBridge_.reset( |
124 [[PasteboardNotificationListenerBridge alloc] initWithDelegate:this]); | 130 [[PasteboardNotificationListenerBridge alloc] initWithDelegate:this]); |
125 } | 131 } |
126 | 132 |
127 ClipboardRecentContentIOS::~ClipboardRecentContentIOS() { | 133 ClipboardRecentContentIOS::~ClipboardRecentContentIOS() { |
128 } | 134 } |
129 | 135 |
130 GURL ClipboardRecentContentIOS::URLFromPasteboard() { | 136 GURL ClipboardRecentContentIOS::URLFromPasteboard() { |
131 const std::string clipboard = | 137 const std::string clipboard = |
(...skipping 21 matching lines...) Expand all Loading... |
153 DCHECK(!lastPasteboardChangeDate_ || | 159 DCHECK(!lastPasteboardChangeDate_ || |
154 [lastPasteboardChangeDate_ isKindOfClass:[NSDate class]]); | 160 [lastPasteboardChangeDate_ isKindOfClass:[NSDate class]]); |
155 } | 161 } |
156 | 162 |
157 void ClipboardRecentContentIOS::SaveToUserDefaults() { | 163 void ClipboardRecentContentIOS::SaveToUserDefaults() { |
158 [[NSUserDefaults standardUserDefaults] setInteger:lastPasteboardChangeCount_ | 164 [[NSUserDefaults standardUserDefaults] setInteger:lastPasteboardChangeCount_ |
159 forKey:kPasteboardChangeCountKey]; | 165 forKey:kPasteboardChangeCountKey]; |
160 [[NSUserDefaults standardUserDefaults] setObject:lastPasteboardChangeDate_ | 166 [[NSUserDefaults standardUserDefaults] setObject:lastPasteboardChangeDate_ |
161 forKey:kPasteboardChangeDateKey]; | 167 forKey:kPasteboardChangeDateKey]; |
162 } | 168 } |
| 169 |
| 170 bool ClipboardRecentContentIOS::DeviceRestartedSincePasteboardChanged() { |
| 171 int64 secondsSincePasteboardChange = |
| 172 -static_cast<int64>([lastPasteboardChangeDate_ timeIntervalSinceNow]); |
| 173 int64 secondsSinceLastDeviceRestart = base::SysInfo::Uptime() / 1000; |
| 174 return secondsSincePasteboardChange > secondsSinceLastDeviceRestart; |
| 175 } |
OLD | NEW |