| OLD | NEW |
| 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 #import "components/open_from_clipboard/clipboard_recent_content_impl_ios.h" | 5 #import "components/open_from_clipboard/clipboard_recent_content_impl_ios.h" |
| 6 | 6 |
| 7 #import <CommonCrypto/CommonDigest.h> | 7 #import <CommonCrypto/CommonDigest.h> |
| 8 #import <UIKit/UIKit.h> | 8 #import <UIKit/UIKit.h> |
| 9 | 9 |
| 10 #import "base/mac/foundation_util.h" | 10 #import "base/mac/foundation_util.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #include "base/sys_info.h" | 12 #include "base/sys_info.h" |
| 13 | 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) | 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." | 15 #error "This file requires ARC support." |
| 16 #endif | 16 #endif |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 // Key used to store the pasteboard's current change count. If when resuming | 19 // Key used to store the pasteboard's current change count. If when resuming |
| 20 // chrome the pasteboard's change count is different from the stored one, then | 20 // chrome the pasteboard's change count is different from the stored one, then |
| 21 // it means that the pasteboard's content has changed. | 21 // it means that the pasteboard's content has changed. |
| 22 NSString* const kPasteboardChangeCountKey = @"PasteboardChangeCount"; | 22 NSString* const kPasteboardChangeCountKey = @"PasteboardChangeCount"; |
| 23 // Key used to store the last date at which it was detected that the pasteboard | 23 // Key used to store the last date at which it was detected that the pasteboard |
| 24 // changed. It is used to evaluate the age of the pasteboard's content. | 24 // changed. It is used to evaluate the age of the pasteboard's content. |
| 25 NSString* const kPasteboardChangeDateKey = @"PasteboardChangeDate"; | 25 NSString* const kPasteboardChangeDateKey = @"PasteboardChangeDate"; |
| 26 // Key used to store the hash of the content of the pasteboard. Whenever the | 26 // Key used to store the hash of the content of the pasteboard. Whenever the |
| 27 // hash changed, the pasteboard content is considered to have changed. | 27 // hash changed, the pasteboard content is considered to have changed. |
| 28 NSString* const kPasteboardEntryMD5Key = @"PasteboardEntryMD5"; | 28 NSString* const kPasteboardEntryMD5Key = @"PasteboardEntryMD5"; |
| 29 // Maximum age of clipboard in seconds. | |
| 30 NSTimeInterval const kMaximumAgeOfClipboard = 3 * 60 * 60; | |
| 31 | 29 |
| 32 // Compute a hash consisting of the first 4 bytes of the MD5 hash of |string|. | 30 // Compute a hash consisting of the first 4 bytes of the MD5 hash of |string|. |
| 33 // This value is used to detect pasteboard content change. Keeping only 4 bytes | 31 // This value is used to detect pasteboard content change. Keeping only 4 bytes |
| 34 // is a privacy requirement to introduce collision and allow deniability of | 32 // is a privacy requirement to introduce collision and allow deniability of |
| 35 // having copied a given string. | 33 // having copied a given string. |
| 36 NSData* WeakMD5FromNSString(NSString* string) { | 34 NSData* WeakMD5FromNSString(NSString* string) { |
| 37 unsigned char hash[CC_MD5_DIGEST_LENGTH]; | 35 unsigned char hash[CC_MD5_DIGEST_LENGTH]; |
| 38 const std::string clipboard = base::SysNSStringToUTF8(string); | 36 const std::string clipboard = base::SysNSStringToUTF8(string); |
| 39 const char* c_string = clipboard.c_str(); | 37 const char* c_string = clipboard.c_str(); |
| 40 CC_MD5(c_string, strlen(c_string), hash); | 38 CC_MD5(c_string, strlen(c_string), hash); |
| 41 NSData* data = [NSData dataWithBytes:hash length:4]; | 39 NSData* data = [NSData dataWithBytes:hash length:4]; |
| 42 return data; | 40 return data; |
| 43 } | 41 } |
| 44 | 42 |
| 45 } // namespace | 43 } // namespace |
| 46 | 44 |
| 47 @interface ClipboardRecentContentImplIOS () | 45 @interface ClipboardRecentContentImplIOS () |
| 48 | 46 |
| 49 // The user defaults from the app group used to optimize the pasteboard change | 47 // The user defaults from the app group used to optimize the pasteboard change |
| 50 // detection. | 48 // detection. |
| 51 @property(nonatomic, strong) NSUserDefaults* sharedUserDefaults; | 49 @property(nonatomic, strong) NSUserDefaults* sharedUserDefaults; |
| 52 // The pasteboard's change count. Increases everytime the pasteboard changes. | 50 // The pasteboard's change count. Increases everytime the pasteboard changes. |
| 53 @property(nonatomic) NSInteger lastPasteboardChangeCount; | 51 @property(nonatomic) NSInteger lastPasteboardChangeCount; |
| 54 // MD5 hash of the last registered pasteboard entry. | 52 // MD5 hash of the last registered pasteboard entry. |
| 55 @property(nonatomic, strong) NSData* lastPasteboardEntryMD5; | 53 @property(nonatomic, strong) NSData* lastPasteboardEntryMD5; |
| 56 // Contains the authorized schemes for URLs. | 54 // Contains the authorized schemes for URLs. |
| 57 @property(nonatomic, readonly) NSSet* authorizedSchemes; | 55 @property(nonatomic, readonly) NSSet* authorizedSchemes; |
| 58 // Delegate for metrics. | 56 // Delegate for metrics. |
| 59 @property(nonatomic, strong) id<ClipboardRecentContentDelegate> delegate; | 57 @property(nonatomic, strong) id<ClipboardRecentContentDelegate> delegate; |
| 58 // Maximum age of clipboard in seconds. |
| 59 @property(nonatomic, readonly) NSTimeInterval maximumAgeOfClipboard; |
| 60 | 60 |
| 61 // If the content of the pasteboard has changed, updates the change count, | 61 // If the content of the pasteboard has changed, updates the change count, |
| 62 // change date, and md5 of the latest pasteboard entry if necessary. | 62 // change date, and md5 of the latest pasteboard entry if necessary. |
| 63 - (void)updateIfNeeded; | 63 - (void)updateIfNeeded; |
| 64 | 64 |
| 65 // Returns whether the pasteboard changed since the last time a pasteboard | 65 // Returns whether the pasteboard changed since the last time a pasteboard |
| 66 // change was detected. | 66 // change was detected. |
| 67 - (BOOL)hasPasteboardChanged; | 67 - (BOOL)hasPasteboardChanged; |
| 68 | 68 |
| 69 // Loads information from the user defaults about the latest pasteboard entry. | 69 // Loads information from the user defaults about the latest pasteboard entry. |
| 70 - (void)loadFromUserDefaults; | 70 - (void)loadFromUserDefaults; |
| 71 | 71 |
| 72 // Returns the URL contained in the clipboard (if any). | 72 // Returns the URL contained in the clipboard (if any). |
| 73 - (NSURL*)URLFromPasteboard; | 73 - (NSURL*)URLFromPasteboard; |
| 74 | 74 |
| 75 // Returns the uptime. | 75 // Returns the uptime. |
| 76 - (NSTimeInterval)uptime; | 76 - (NSTimeInterval)uptime; |
| 77 | 77 |
| 78 @end | 78 @end |
| 79 | 79 |
| 80 @implementation ClipboardRecentContentImplIOS | 80 @implementation ClipboardRecentContentImplIOS |
| 81 | 81 |
| 82 @synthesize lastPasteboardChangeCount = _lastPasteboardChangeCount; | 82 @synthesize lastPasteboardChangeCount = _lastPasteboardChangeCount; |
| 83 @synthesize lastPasteboardChangeDate = _lastPasteboardChangeDate; | 83 @synthesize lastPasteboardChangeDate = _lastPasteboardChangeDate; |
| 84 @synthesize lastPasteboardEntryMD5 = _lastPasteboardEntryMD5; | 84 @synthesize lastPasteboardEntryMD5 = _lastPasteboardEntryMD5; |
| 85 @synthesize sharedUserDefaults = _sharedUserDefaults; | 85 @synthesize sharedUserDefaults = _sharedUserDefaults; |
| 86 @synthesize authorizedSchemes = _authorizedSchemes; | 86 @synthesize authorizedSchemes = _authorizedSchemes; |
| 87 @synthesize delegate = _delegate; | 87 @synthesize delegate = _delegate; |
| 88 @synthesize maximumAgeOfClipboard = _maximumAgeOfClipboard; |
| 88 | 89 |
| 89 - (instancetype)initWithAuthorizedSchemes:(NSSet<NSString*>*)authorizedSchemes | 90 - (instancetype)initWithMaxAge:(NSTimeInterval)maxAge |
| 90 userDefaults:(NSUserDefaults*)groupUserDefaults | 91 authorizedSchemes:(NSSet<NSString*>*)authorizedSchemes |
| 91 delegate:(id<ClipboardRecentContentDelegate>) | 92 userDefaults:(NSUserDefaults*)groupUserDefaults |
| 92 delegate { | 93 delegate:(id<ClipboardRecentContentDelegate>)delegate { |
| 93 self = [super init]; | 94 self = [super init]; |
| 94 if (self) { | 95 if (self) { |
| 96 _maximumAgeOfClipboard = maxAge; |
| 95 _delegate = delegate; | 97 _delegate = delegate; |
| 96 _authorizedSchemes = authorizedSchemes; | 98 _authorizedSchemes = authorizedSchemes; |
| 97 _sharedUserDefaults = groupUserDefaults; | 99 _sharedUserDefaults = groupUserDefaults; |
| 98 | 100 |
| 99 _lastPasteboardChangeCount = NSIntegerMax; | 101 _lastPasteboardChangeCount = NSIntegerMax; |
| 100 [self loadFromUserDefaults]; | 102 [self loadFromUserDefaults]; |
| 101 [self updateIfNeeded]; | 103 [self updateIfNeeded]; |
| 102 | 104 |
| 103 // Makes sure |last_pasteboard_change_count_| was properly initialized. | 105 // Makes sure |last_pasteboard_change_count_| was properly initialized. |
| 104 DCHECK_NE(_lastPasteboardChangeCount, NSIntegerMax); | 106 DCHECK_NE(_lastPasteboardChangeCount, NSIntegerMax); |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 return changeCountChanged; | 145 return changeCountChanged; |
| 144 } | 146 } |
| 145 | 147 |
| 146 BOOL md5Changed = | 148 BOOL md5Changed = |
| 147 ![[self getCurrentMD5] isEqualToData:self.lastPasteboardEntryMD5]; | 149 ![[self getCurrentMD5] isEqualToData:self.lastPasteboardEntryMD5]; |
| 148 return md5Changed; | 150 return md5Changed; |
| 149 } | 151 } |
| 150 | 152 |
| 151 - (NSURL*)recentURLFromClipboard { | 153 - (NSURL*)recentURLFromClipboard { |
| 152 [self updateIfNeeded]; | 154 [self updateIfNeeded]; |
| 153 if ([self clipboardContentAge] > kMaximumAgeOfClipboard) { | 155 if ([self clipboardContentAge] > self.maximumAgeOfClipboard) { |
| 154 return nil; | 156 return nil; |
| 155 } | 157 } |
| 156 return [self URLFromPasteboard]; | 158 return [self URLFromPasteboard]; |
| 157 } | 159 } |
| 158 | 160 |
| 159 - (NSTimeInterval)clipboardContentAge { | 161 - (NSTimeInterval)clipboardContentAge { |
| 160 return -[self.lastPasteboardChangeDate timeIntervalSinceNow]; | 162 return -[self.lastPasteboardChangeDate timeIntervalSinceNow]; |
| 161 } | 163 } |
| 162 | 164 |
| 163 - (void)suppressClipboardContent { | 165 - (void)suppressClipboardContent { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 forKey:kPasteboardChangeDateKey]; | 210 forKey:kPasteboardChangeDateKey]; |
| 209 [self.sharedUserDefaults setObject:self.lastPasteboardEntryMD5 | 211 [self.sharedUserDefaults setObject:self.lastPasteboardEntryMD5 |
| 210 forKey:kPasteboardEntryMD5Key]; | 212 forKey:kPasteboardEntryMD5Key]; |
| 211 } | 213 } |
| 212 | 214 |
| 213 - (NSTimeInterval)uptime { | 215 - (NSTimeInterval)uptime { |
| 214 return base::SysInfo::Uptime().InSecondsF(); | 216 return base::SysInfo::Uptime().InSecondsF(); |
| 215 } | 217 } |
| 216 | 218 |
| 217 @end | 219 @end |
| OLD | NEW |