Chromium Code Reviews| 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 #import "components/open_from_clipboard/clipboard_recent_content_ios_impl.h" | |
| 6 | |
| 7 #import <CommonCrypto/CommonDigest.h> | |
| 8 #import <UIKit/UIKit.h> | |
| 9 | |
| 10 #include "base/strings/sys_string_conversions.h" | |
| 11 #include "base/sys_info.h" | |
| 12 | |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 14 #error "This file requires ARC support." | |
| 15 #endif | |
| 16 | |
| 17 namespace { | |
| 18 // Key used to store the pasteboard's current change count. If when resuming | |
| 19 // chrome the pasteboard's change count is different from the stored one, then | |
| 20 // it means that the pasteboard's content has changed. | |
| 21 NSString* kPasteboardChangeCountKey = @"PasteboardChangeCount"; | |
| 22 // Key used to store the last date at which it was detected that the pasteboard | |
| 23 // changed. It is used to evaluate the age of the pasteboard's content. | |
| 24 NSString* kPasteboardChangeDateKey = @"PasteboardChangeDate"; | |
| 25 // Key used to store the hash of the content of the pasteboard. Whenever the | |
| 26 // hash changed, the pasteboard content is considered to have changed. | |
| 27 NSString* kPasteboardEntryMD5Key = @"PasteboardEntryMD5"; | |
| 28 // Maximum age of clipboard in seconds. | |
| 29 NSTimeInterval kMaximumAgeOfClipboard = 3 * 60 * 60; | |
| 30 | |
| 31 // Compute a hash consisting of the first 4 bytes of the MD5 hash of |string|. | |
| 32 // This value is used to detect pasteboard content change. Keeping only 4 bytes | |
| 33 // is a privacy requirement to introduce collision and allow deniability of | |
| 34 // having copied a given string. | |
| 35 NSData* WeakMD5FromNSString(NSString* string) { | |
| 36 unsigned char hash[CC_MD5_DIGEST_LENGTH]; | |
| 37 const std::string clipboard = base::SysNSStringToUTF8(string); | |
| 38 const char* c_string = clipboard.c_str(); | |
| 39 CC_MD5(c_string, strlen(c_string), hash); | |
| 40 NSData* data = [NSData dataWithBytes:hash length:4]; | |
| 41 return data; | |
| 42 } | |
| 43 | |
| 44 } // namespace | |
| 45 | |
| 46 @interface ClipboardRecentContentIOSImpl () | |
| 47 | |
| 48 // The user defaults from the app group used to optimize the pasteboard change | |
| 49 // detection. | |
| 50 @property(nonatomic, strong) NSUserDefaults* sharedUserDefaults; | |
| 51 // The pasteboard's change count. Increases everytime the pasteboard changes. | |
| 52 @property(nonatomic) NSInteger lastPasteboardChangeCount; | |
| 53 // MD5 hash of the last registered pasteboard entry. | |
| 54 @property(nonatomic, strong) NSData* lastPasteboardEntryMD5; | |
| 55 // Contains the authorized schemes for URLs. | |
| 56 @property(nonatomic, strong) NSArray* authorizedSchemes; | |
| 57 // Delegate for metrics. | |
| 58 @property(nonatomic, strong) id<ClipboardRecentContentMetricsDelegate> delegate; | |
| 59 | |
| 60 // If the content of the pasteboard has changed, updates the change count, | |
| 61 // change date, and md5 of the latest pasteboard entry if necessary. | |
| 62 - (void)updateIfNeeded; | |
| 63 | |
| 64 // Returns whether the pasteboard changed since the last time a pasteboard | |
| 65 // change was detected. | |
| 66 - (BOOL)hasPasteboardChanged; | |
| 67 | |
| 68 // Loads information from the user defaults about the latest pasteboard entry. | |
| 69 - (void)loadFromUserDefaults; | |
| 70 | |
| 71 // Returns the URL contained in the clipboard (if any). | |
| 72 - (NSURL*)URLFromPasteboard; | |
| 73 | |
| 74 @end | |
| 75 | |
| 76 @implementation ClipboardRecentContentIOSImpl | |
| 77 | |
| 78 @synthesize lastPasteboardChangeCount = _lastPasteboardChangeCount; | |
| 79 @synthesize lastPasteboardChangeDate = _lastPasteboardChangeDate; | |
| 80 @synthesize lastPasteboardEntryMD5 = _lastPasteboardEntryMD5; | |
| 81 @synthesize sharedUserDefaults = _sharedUserDefaults; | |
| 82 @synthesize authorizedSchemes = _authorizedSchemes; | |
| 83 @synthesize delegate = _delegate; | |
| 84 | |
| 85 - (instancetype)initWithDelegate: | |
| 86 (id<ClipboardRecentContentMetricsDelegate>)delegate | |
| 87 authorizedSchemes:(NSArray*)authorizedSchemes | |
| 88 userDefaults:(NSUserDefaults*)groupUserDefaults { | |
| 89 self = [super init]; | |
| 90 if (self) { | |
| 91 _delegate = delegate; | |
| 92 _authorizedSchemes = authorizedSchemes; | |
| 93 _sharedUserDefaults = groupUserDefaults; | |
| 94 | |
| 95 _lastPasteboardChangeCount = NSIntegerMax; | |
| 96 [self loadFromUserDefaults]; | |
| 97 [self updateIfNeeded]; | |
| 98 | |
| 99 // Makes sure |last_pasteboard_change_count_| was properly initialized. | |
| 100 DCHECK_NE(_lastPasteboardChangeCount, NSIntegerMax); | |
| 101 [[NSNotificationCenter defaultCenter] | |
| 102 addObserver:self | |
| 103 selector:@selector(didBecomeActive:) | |
| 104 name:UIApplicationDidBecomeActiveNotification | |
| 105 object:nil]; | |
| 106 } | |
| 107 return self; | |
| 108 } | |
| 109 | |
| 110 - (void)dealloc { | |
| 111 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
|
jif
2017/03/29 14:17:56
[super dealloc];
(if the compiler does not complai
lody
2017/03/30 13:36:34
This file is using ARC
jif
2017/03/31 13:14:47
Ack.
The problem was in my knowledge of ARC.
| |
| 112 } | |
| 113 | |
| 114 - (void)didBecomeActive:(NSNotification*)notification { | |
| 115 [self loadFromUserDefaults]; | |
| 116 [self updateIfNeeded]; | |
| 117 } | |
| 118 | |
| 119 - (BOOL)hasPasteboardChanged { | |
| 120 // If |MD5Changed|, we know for sure there has been at least one pasteboard | |
| 121 // copy since last time it was checked. | |
| 122 // If the pasteboard content is still the same but the device was not | |
| 123 // rebooted, the change count can be checked to see if it changed. | |
| 124 // Note: due to a mismatch between the actual behavior and documentation, and | |
| 125 // lack of consistency on different reboot scenarios, the change count cannot | |
| 126 // be checked after a reboot. | |
| 127 // See radar://21833556 for more information. | |
| 128 NSInteger changeCount = [UIPasteboard generalPasteboard].changeCount; | |
| 129 bool changeCountChanged = changeCount != self.lastPasteboardChangeCount; | |
| 130 | |
| 131 bool notRebooted = [self uptime] > [self getClipboardContentAge]; | |
| 132 if (notRebooted) | |
| 133 return changeCountChanged; | |
| 134 | |
| 135 NSString* pasteboardString = [UIPasteboard generalPasteboard].string; | |
| 136 if (!pasteboardString) { | |
| 137 pasteboardString = @""; | |
| 138 } | |
| 139 NSData* md5 = WeakMD5FromNSString(pasteboardString); | |
| 140 BOOL md5Changed = ![md5 isEqualToData:self.lastPasteboardEntryMD5]; | |
| 141 | |
| 142 return md5Changed; | |
| 143 } | |
| 144 | |
| 145 - (NSURL*)getRecentURLFromClipboard { | |
| 146 [self updateIfNeeded]; | |
| 147 if ([self getClipboardContentAge] > kMaximumAgeOfClipboard) { | |
| 148 return nil; | |
| 149 } | |
| 150 | |
| 151 NSURL* urlFromPasteboard = [self URLFromPasteboard]; | |
| 152 if (urlFromPasteboard) { | |
| 153 return urlFromPasteboard; | |
| 154 } | |
| 155 return nil; | |
| 156 } | |
| 157 | |
| 158 - (NSTimeInterval)getClipboardContentAge { | |
| 159 return -[self.lastPasteboardChangeDate timeIntervalSinceNow]; | |
| 160 } | |
| 161 | |
| 162 - (void)suppressClipboardContent { | |
| 163 // User cleared the user data. The pasteboard entry must be removed from the | |
| 164 // omnibox list. Force entry expiration by setting copy date to 1970. | |
| 165 self.lastPasteboardChangeDate = | |
| 166 [[NSDate alloc] initWithTimeIntervalSince1970:0]; | |
| 167 [self saveToUserDefaults]; | |
| 168 } | |
| 169 | |
| 170 - (void)updateIfNeeded { | |
| 171 if (![self hasPasteboardChanged]) | |
| 172 return; | |
| 173 | |
| 174 [self.delegate onClipboardChanged]; | |
| 175 | |
| 176 self.lastPasteboardChangeDate = [NSDate date]; | |
| 177 self.lastPasteboardChangeCount = [UIPasteboard generalPasteboard].changeCount; | |
| 178 NSString* pasteboardString = [UIPasteboard generalPasteboard].string; | |
| 179 if (!pasteboardString) { | |
| 180 pasteboardString = @""; | |
| 181 } | |
| 182 NSData* MD5 = WeakMD5FromNSString(pasteboardString); | |
| 183 self.lastPasteboardEntryMD5 = [MD5 init]; | |
| 184 [self saveToUserDefaults]; | |
| 185 } | |
| 186 | |
| 187 - (NSURL*)URLFromPasteboard { | |
| 188 NSString* clipboardString = [UIPasteboard generalPasteboard].string; | |
| 189 if (!clipboardString) { | |
| 190 return nil; | |
| 191 } | |
| 192 | |
| 193 NSURL* url = [NSURL URLWithString:clipboardString]; | |
| 194 if (url) { | |
| 195 for (NSString* scheme in self.authorizedSchemes) { | |
| 196 if ([url.scheme isEqualToString:scheme]) { | |
| 197 return url; | |
| 198 } | |
| 199 } | |
| 200 } | |
| 201 | |
| 202 return nil; | |
| 203 } | |
| 204 | |
| 205 - (void)loadFromUserDefaults { | |
| 206 self.lastPasteboardChangeCount = | |
| 207 [self.sharedUserDefaults integerForKey:kPasteboardChangeCountKey]; | |
| 208 self.lastPasteboardChangeDate = | |
| 209 [self.sharedUserDefaults objectForKey:kPasteboardChangeDateKey]; | |
| 210 self.lastPasteboardEntryMD5 = | |
| 211 [self.sharedUserDefaults objectForKey:kPasteboardEntryMD5Key]; | |
| 212 | |
| 213 DCHECK(!self.lastPasteboardChangeDate || | |
| 214 [self.lastPasteboardChangeDate isKindOfClass:[NSDate class]]); | |
| 215 } | |
| 216 | |
| 217 - (void)saveToUserDefaults { | |
| 218 [self.sharedUserDefaults setInteger:self.lastPasteboardChangeCount | |
| 219 forKey:kPasteboardChangeCountKey]; | |
| 220 [self.sharedUserDefaults setObject:self.lastPasteboardChangeDate | |
| 221 forKey:kPasteboardChangeDateKey]; | |
| 222 [self.sharedUserDefaults setObject:self.lastPasteboardEntryMD5 | |
| 223 forKey:kPasteboardEntryMD5Key]; | |
| 224 } | |
| 225 | |
| 226 - (NSTimeInterval)uptime { | |
| 227 return base::SysInfo::Uptime().InSecondsF(); | |
| 228 } | |
| 229 | |
| 230 @end | |
| OLD | NEW |