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 #import "components/open_from_clipboard/clipboard_recent_content_ios.h" | 5 #import "components/open_from_clipboard/clipboard_recent_content_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/ios/ios_util.h" | 10 #import "base/ios/ios_util.h" |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 | 141 |
142 void ClipboardRecentContentIOS::SuppressClipboardContent() { | 142 void ClipboardRecentContentIOS::SuppressClipboardContent() { |
143 // User cleared the user data. The pasteboard entry must be removed from the | 143 // User cleared the user data. The pasteboard entry must be removed from the |
144 // omnibox list. Force entry expiration by setting copy date to 1970. | 144 // omnibox list. Force entry expiration by setting copy date to 1970. |
145 last_pasteboard_change_date_.reset( | 145 last_pasteboard_change_date_.reset( |
146 [[NSDate alloc] initWithTimeIntervalSince1970:0]); | 146 [[NSDate alloc] initWithTimeIntervalSince1970:0]); |
147 SaveToUserDefaults(); | 147 SaveToUserDefaults(); |
148 } | 148 } |
149 | 149 |
150 void ClipboardRecentContentIOS::PasteboardChanged() { | 150 void ClipboardRecentContentIOS::PasteboardChanged() { |
151 NSString* pasteboard_string = [[UIPasteboard generalPasteboard] string]; | |
152 if (!pasteboard_string) | |
153 return; | |
151 url_from_pasteboard_cache_ = URLFromPasteboard(); | 154 url_from_pasteboard_cache_ = URLFromPasteboard(); |
152 if (!url_from_pasteboard_cache_.is_empty()) { | 155 if (!url_from_pasteboard_cache_.is_empty()) { |
153 base::RecordAction( | 156 base::RecordAction( |
154 base::UserMetricsAction("MobileOmniboxClipboardChanged")); | 157 base::UserMetricsAction("MobileOmniboxClipboardChanged")); |
155 } | 158 } |
156 last_pasteboard_change_date_.reset([[NSDate date] retain]); | 159 last_pasteboard_change_date_.reset([[NSDate date] retain]); |
157 last_pasteboard_change_count_ = [UIPasteboard generalPasteboard].changeCount; | 160 last_pasteboard_change_count_ = [UIPasteboard generalPasteboard].changeCount; |
158 NSString* pasteboard_string = [[UIPasteboard generalPasteboard] string]; | |
159 if (!pasteboard_string) { | |
160 pasteboard_string = @""; | |
161 } | |
162 NSData* MD5 = WeakMD5FromNSString(pasteboard_string); | 161 NSData* MD5 = WeakMD5FromNSString(pasteboard_string); |
163 last_pasteboard_entry_md5_.reset([MD5 retain]); | 162 last_pasteboard_entry_md5_.reset([MD5 retain]); |
164 SaveToUserDefaults(); | 163 SaveToUserDefaults(); |
165 } | 164 } |
166 | 165 |
167 ClipboardRecentContentIOS::ClipboardRecentContentIOS( | 166 ClipboardRecentContentIOS::ClipboardRecentContentIOS( |
168 const std::string& application_scheme, | 167 const std::string& application_scheme, |
169 NSUserDefaults* group_user_defaults) | 168 NSUserDefaults* group_user_defaults) |
170 : application_scheme_(application_scheme), | 169 : application_scheme_(application_scheme), |
171 shared_user_defaults_([group_user_defaults retain]) { | 170 shared_user_defaults_([group_user_defaults retain]) { |
172 Init(base::TimeDelta::FromMilliseconds(base::SysInfo::Uptime())); | 171 Init(base::TimeDelta::FromMilliseconds(base::SysInfo::Uptime())); |
173 } | 172 } |
174 | 173 |
175 ClipboardRecentContentIOS::ClipboardRecentContentIOS( | 174 ClipboardRecentContentIOS::ClipboardRecentContentIOS( |
176 const std::string& application_scheme, | 175 const std::string& application_scheme, |
177 base::TimeDelta uptime) | 176 base::TimeDelta uptime) |
178 : application_scheme_(application_scheme), | 177 : application_scheme_(application_scheme), |
179 shared_user_defaults_([[NSUserDefaults standardUserDefaults] retain]) { | 178 shared_user_defaults_([[NSUserDefaults standardUserDefaults] retain]) { |
180 Init(uptime); | 179 Init(uptime); |
181 } | 180 } |
182 | 181 |
183 bool ClipboardRecentContentIOS::HasPasteboardChanged(base::TimeDelta uptime) { | 182 bool ClipboardRecentContentIOS::HasPasteboardChanged(base::TimeDelta uptime) { |
183 // If [[UIPasteboard generalPasteboard] string] is nil, the content of the | |
184 // pasteboard cannot be accessed. This case should not be considered as a | |
185 // pasteboard change. | |
186 NSString* pasteboard_string = [[UIPasteboard generalPasteboard] string]; | |
187 if (!pasteboard_string) | |
188 return NO; | |
189 | |
184 // If |MD5Changed|, we know for sure there has been at least one pasteboard | 190 // If |MD5Changed|, we know for sure there has been at least one pasteboard |
185 // copy since last time it was checked. | 191 // copy since last time it was checked. |
186 // If the pasteboard content is still the same but the device was not | 192 // If the pasteboard content is still the same but the device was not |
187 // rebooted, the change count can be checked to see if it changed. | 193 // rebooted, the change count can be checked to see if it changed. |
188 // Note: due to a mismatch between the actual behavior and documentation, and | 194 // Note: due to a mismatch between the actual behavior and documentation, and |
189 // lack of consistency on different reboot scenarios, the change count cannot | 195 // lack of consistency on different reboot scenarios, the change count cannot |
190 // be checked after a reboot. | 196 // be checked after a reboot. |
191 // See radar://21833556 for more information. | 197 // See radar://21833556 for more information. |
192 NSInteger change_count = [UIPasteboard generalPasteboard].changeCount; | 198 NSInteger change_count = [UIPasteboard generalPasteboard].changeCount; |
193 bool change_count_changed = change_count != last_pasteboard_change_count_; | 199 bool change_count_changed = change_count != last_pasteboard_change_count_; |
194 | 200 |
195 bool not_rebooted = uptime > GetClipboardContentAge(); | 201 bool not_rebooted = uptime > GetClipboardContentAge(); |
196 if (not_rebooted) | 202 if (not_rebooted) |
197 return change_count_changed; | 203 return change_count_changed; |
198 | 204 |
199 NSString* pasteboard_string = [[UIPasteboard generalPasteboard] string]; | |
200 if (!pasteboard_string) { | |
201 pasteboard_string = @""; | |
202 } | |
203 NSData* md5 = WeakMD5FromNSString(pasteboard_string); | 205 NSData* md5 = WeakMD5FromNSString(pasteboard_string); |
204 BOOL md5_changed = ![md5 isEqualToData:last_pasteboard_entry_md5_]; | 206 BOOL md5_changed = ![md5 isEqualToData:last_pasteboard_entry_md5_]; |
205 | 207 |
206 return md5_changed; | 208 return md5_changed; |
207 } | 209 } |
208 | 210 |
209 void ClipboardRecentContentIOS::Init(base::TimeDelta uptime) { | 211 void ClipboardRecentContentIOS::Init(base::TimeDelta uptime) { |
210 last_pasteboard_change_count_ = NSIntegerMax; | 212 last_pasteboard_change_count_ = NSIntegerMax; |
211 url_from_pasteboard_cache_ = URLFromPasteboard(); | 213 url_from_pasteboard_cache_ = URLFromPasteboard(); |
212 LoadFromUserDefaults(); | 214 LoadFromUserDefaults(); |
213 | 215 |
214 if (HasPasteboardChanged(uptime)) | 216 if (HasPasteboardChanged(uptime)) |
215 PasteboardChanged(); | 217 PasteboardChanged(); |
216 | 218 |
217 // Makes sure |last_pasteboard_change_count_| was properly initialized. | 219 // Makes sure |last_pasteboard_change_count_| was properly initialized. |
218 DCHECK_NE(last_pasteboard_change_count_, NSIntegerMax); | 220 DCHECK_NE(last_pasteboard_change_count_, NSIntegerMax); |
219 notification_bridge_.reset( | 221 notification_bridge_.reset( |
220 [[PasteboardNotificationListenerBridge alloc] initWithDelegate:this]); | 222 [[PasteboardNotificationListenerBridge alloc] initWithDelegate:this]); |
221 } | 223 } |
222 | 224 |
223 ClipboardRecentContentIOS::~ClipboardRecentContentIOS() { | 225 ClipboardRecentContentIOS::~ClipboardRecentContentIOS() { |
224 [notification_bridge_ disconnect]; | 226 [notification_bridge_ disconnect]; |
225 } | 227 } |
226 | 228 |
227 GURL ClipboardRecentContentIOS::URLFromPasteboard() { | 229 GURL ClipboardRecentContentIOS::URLFromPasteboard() { |
228 const std::string clipboard = | 230 NSString* clipboard_string = [[UIPasteboard generalPasteboard] string]; |
229 base::SysNSStringToUTF8([[UIPasteboard generalPasteboard] string]); | 231 if (!clipboard_string) { |
232 return GURL::EmptyGURL(); | |
233 } | |
234 const std::string clipboard = base::SysNSStringToUTF8(clipboard_string); | |
droger
2015/08/17 08:27:39
It seems this change does not affect the behavior
Olivier
2015/08/17 08:33:19
The result is the same.
But adding the early retur
| |
230 GURL gurl = GURL(clipboard); | 235 GURL gurl = GURL(clipboard); |
231 if (gurl.is_valid()) { | 236 if (gurl.is_valid()) { |
232 for (size_t i = 0; i < arraysize(kAuthorizedSchemes); ++i) { | 237 for (size_t i = 0; i < arraysize(kAuthorizedSchemes); ++i) { |
233 if (gurl.SchemeIs(kAuthorizedSchemes[i])) { | 238 if (gurl.SchemeIs(kAuthorizedSchemes[i])) { |
234 return gurl; | 239 return gurl; |
235 } | 240 } |
236 } | 241 } |
237 if (!application_scheme_.empty() && | 242 if (!application_scheme_.empty() && |
238 gurl.SchemeIs(application_scheme_.c_str())) { | 243 gurl.SchemeIs(application_scheme_.c_str())) { |
239 return gurl; | 244 return gurl; |
(...skipping 29 matching lines...) Expand all Loading... | |
269 void ClipboardRecentContentIOS::SaveToUserDefaults() { | 274 void ClipboardRecentContentIOS::SaveToUserDefaults() { |
270 [shared_user_defaults_ setInteger:last_pasteboard_change_count_ | 275 [shared_user_defaults_ setInteger:last_pasteboard_change_count_ |
271 forKey:kPasteboardChangeCountKey]; | 276 forKey:kPasteboardChangeCountKey]; |
272 [shared_user_defaults_ setObject:last_pasteboard_change_date_ | 277 [shared_user_defaults_ setObject:last_pasteboard_change_date_ |
273 forKey:kPasteboardChangeDateKey]; | 278 forKey:kPasteboardChangeDateKey]; |
274 [shared_user_defaults_ setObject:last_pasteboard_entry_md5_ | 279 [shared_user_defaults_ setObject:last_pasteboard_entry_md5_ |
275 forKey:kPasteboardEntryMD5Key]; | 280 forKey:kPasteboardEntryMD5Key]; |
276 [shared_user_defaults_ setObject:last_displayed_pasteboard_entry_ | 281 [shared_user_defaults_ setObject:last_displayed_pasteboard_entry_ |
277 forKey:kLastDisplayedPasteboardEntryKey]; | 282 forKey:kLastDisplayedPasteboardEntryKey]; |
278 } | 283 } |
OLD | NEW |