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

Side by Side Diff: components/open_from_clipboard/clipboard_recent_content_ios_impl.mm

Issue 2782823003: Rewrite implementation of ClipboardRecentContent in Objective C. (Closed)
Patch Set: address comments 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
(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 // Returns the uptime.
75 - (NSTimeInterval)uptime;
76
77 @end
78
79 @implementation ClipboardRecentContentIOSImpl
80
81 @synthesize lastPasteboardChangeCount = _lastPasteboardChangeCount;
82 @synthesize lastPasteboardChangeDate = _lastPasteboardChangeDate;
83 @synthesize lastPasteboardEntryMD5 = _lastPasteboardEntryMD5;
84 @synthesize sharedUserDefaults = _sharedUserDefaults;
85 @synthesize authorizedSchemes = _authorizedSchemes;
86 @synthesize delegate = _delegate;
87
88 - (instancetype)initWithDelegate:
89 (id<ClipboardRecentContentMetricsDelegate>)delegate
90 authorizedSchemes:(NSArray*)authorizedSchemes
91 userDefaults:(NSUserDefaults*)groupUserDefaults {
92 self = [super init];
93 if (self) {
94 _delegate = delegate;
95 _authorizedSchemes = authorizedSchemes;
96 _sharedUserDefaults = groupUserDefaults;
97
98 _lastPasteboardChangeCount = NSIntegerMax;
99 [self loadFromUserDefaults];
100 [self updateIfNeeded];
101
102 // Makes sure |last_pasteboard_change_count_| was properly initialized.
103 DCHECK_NE(_lastPasteboardChangeCount, NSIntegerMax);
104 [[NSNotificationCenter defaultCenter]
105 addObserver:self
106 selector:@selector(didBecomeActive:)
107 name:UIApplicationDidBecomeActiveNotification
108 object:nil];
109 }
110 return self;
111 }
112
113 - (void)dealloc {
114 [[NSNotificationCenter defaultCenter] removeObserver:self];
115 }
116
117 - (void)didBecomeActive:(NSNotification*)notification {
118 [self loadFromUserDefaults];
119 [self updateIfNeeded];
120 }
121
122 - (BOOL)hasPasteboardChanged {
123 // If |MD5Changed|, we know for sure there has been at least one pasteboard
124 // copy since last time it was checked.
125 // If the pasteboard content is still the same but the device was not
126 // rebooted, the change count can be checked to see if it changed.
127 // Note: due to a mismatch between the actual behavior and documentation, and
128 // lack of consistency on different reboot scenarios, the change count cannot
129 // be checked after a reboot.
130 // See radar://21833556 for more information.
131 NSInteger changeCount = [UIPasteboard generalPasteboard].changeCount;
sdefresne 2017/04/03 09:27:17 It looks like changeCount and changeCountChanged a
lody 2017/04/04 13:42:18 Done.
132 bool changeCountChanged = changeCount != self.lastPasteboardChangeCount;
133
134 bool notRebooted = [self uptime] > [self getClipboardContentAge];
sdefresne 2017/04/03 09:27:17 Avoid using negative names for you variables. Inst
lody 2017/04/04 13:42:18 Done.
135 if (notRebooted)
136 return changeCountChanged;
137
138 NSString* pasteboardString = [UIPasteboard generalPasteboard].string;
139 if (!pasteboardString) {
sdefresne 2017/04/03 09:27:17 No need to check for nil here, WeakMD5FromNSString
lody 2017/04/04 13:42:18 Done.
140 pasteboardString = @"";
141 }
142 NSData* md5 = WeakMD5FromNSString(pasteboardString);
143 BOOL md5Changed = ![md5 isEqualToData:self.lastPasteboardEntryMD5];
144
145 return md5Changed;
146 }
147
148 - (NSURL*)getRecentURLFromClipboard {
149 [self updateIfNeeded];
150 if ([self getClipboardContentAge] > kMaximumAgeOfClipboard) {
151 return nil;
152 }
153
154 NSURL* urlFromPasteboard = [self URLFromPasteboard];
155 if (urlFromPasteboard) {
156 return urlFromPasteboard;
157 }
158 return nil;
159 }
160
161 - (NSTimeInterval)getClipboardContentAge {
162 return -[self.lastPasteboardChangeDate timeIntervalSinceNow];
163 }
164
165 - (void)suppressClipboardContent {
166 // User cleared the user data. The pasteboard entry must be removed from the
167 // omnibox list. Force entry expiration by setting copy date to 1970.
168 self.lastPasteboardChangeDate =
169 [[NSDate alloc] initWithTimeIntervalSince1970:0];
170 [self saveToUserDefaults];
171 }
172
173 - (void)updateIfNeeded {
174 if (![self hasPasteboardChanged])
175 return;
176
177 [self.delegate onClipboardChanged];
178
179 self.lastPasteboardChangeDate = [NSDate date];
180 self.lastPasteboardChangeCount = [UIPasteboard generalPasteboard].changeCount;
181 NSString* pasteboardString = [UIPasteboard generalPasteboard].string;
sdefresne 2017/04/03 09:27:17 This is duplicated with line 138-142. Can you extr
lody 2017/04/04 13:42:18 Done.
182 if (!pasteboardString) {
sdefresne 2017/04/03 09:27:17 ditto
lody 2017/04/04 13:42:19 Done.
183 pasteboardString = @"";
184 }
185 NSData* MD5 = WeakMD5FromNSString(pasteboardString);
186 self.lastPasteboardEntryMD5 = [MD5 init];
sdefresne 2017/04/03 09:27:17 You're sending -init to an already initialised Obj
lody 2017/04/04 13:42:19 Done.
187 [self saveToUserDefaults];
188 }
189
190 - (NSURL*)URLFromPasteboard {
191 NSString* clipboardString = [UIPasteboard generalPasteboard].string;
192 if (!clipboardString) {
sdefresne 2017/04/03 09:27:17 Braces for simple blocks are optional (simple bloc
lody 2017/04/04 13:42:19 Done.
193 return nil;
194 }
195
196 NSURL* url = [NSURL URLWithString:clipboardString];
197 if (url) {
sdefresne 2017/04/03 09:27:17 You can use an early return here to avoid deep nes
lody 2017/04/04 13:42:19 Done.
198 for (NSString* scheme in self.authorizedSchemes) {
199 if ([url.scheme isEqualToString:scheme]) {
200 return url;
201 }
202 }
203 }
204
205 return nil;
206 }
207
208 - (void)loadFromUserDefaults {
209 self.lastPasteboardChangeCount =
210 [self.sharedUserDefaults integerForKey:kPasteboardChangeCountKey];
211 self.lastPasteboardChangeDate =
212 [self.sharedUserDefaults objectForKey:kPasteboardChangeDateKey];
sdefresne 2017/04/03 09:27:17 You can use base::ObjCCastStrict here and remove t
lody 2017/04/04 13:42:19 Done.
213 self.lastPasteboardEntryMD5 =
214 [self.sharedUserDefaults objectForKey:kPasteboardEntryMD5Key];
215
216 DCHECK(!self.lastPasteboardChangeDate ||
217 [self.lastPasteboardChangeDate isKindOfClass:[NSDate class]]);
218 }
219
220 - (void)saveToUserDefaults {
221 [self.sharedUserDefaults setInteger:self.lastPasteboardChangeCount
222 forKey:kPasteboardChangeCountKey];
223 [self.sharedUserDefaults setObject:self.lastPasteboardChangeDate
224 forKey:kPasteboardChangeDateKey];
225 [self.sharedUserDefaults setObject:self.lastPasteboardEntryMD5
226 forKey:kPasteboardEntryMD5Key];
227 }
228
229 - (NSTimeInterval)uptime {
230 return base::SysInfo::Uptime().InSecondsF();
231 }
232
233 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698