File: clipboard_recent_content_impl_ios.mm

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (623 lines) | stat: -rw-r--r-- 23,335 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "components/open_from_clipboard/clipboard_recent_content_impl_ios.h"

#import <MobileCoreServices/MobileCoreServices.h>
#import <UIKit/UIKit.h>

#import "base/apple/foundation_util.h"
#import "base/functional/bind.h"
#include "base/notreached.h"
#include "base/strings/sys_string_conversions.h"
#include "base/system/sys_info.h"
#include "components/open_from_clipboard/clipboard_async_wrapper_ios.h"

ContentType const ContentTypeURL = @"ContentTypeURL";
ContentType const ContentTypeText = @"ContentTypeString";
ContentType const ContentTypeImage = @"ContentTypeImage";

namespace {
// Key used to store the pasteboard's current change count. If when resuming
// chrome the pasteboard's change count is different from the stored one, then
// it means that the pasteboard's content has changed.
NSString* const kPasteboardChangeCountKey = @"PasteboardChangeCount";
// Key used to store the last date at which it was detected that the pasteboard
// changed. It is used to evaluate the age of the pasteboard's content.
NSString* const kPasteboardChangeDateKey = @"PasteboardChangeDate";
// Default Scheme to use for urls with no scheme.
NSString* const kDefaultScheme = @"https";

}  // namespace

@interface ClipboardRecentContentImplIOS ()

// The user defaults from the app group used to optimize the pasteboard change
// detection.
@property(nonatomic, strong) NSUserDefaults* sharedUserDefaults;
// The pasteboard's change count. Increases everytime the pasteboard changes.
@property(nonatomic) NSInteger lastPasteboardChangeCount;
// Contains the authorized schemes for URLs.
@property(nonatomic, readonly) NSSet* authorizedSchemes;
// Delegate for metrics.
@property(nonatomic, strong) id<ClipboardRecentContentDelegate> delegate;
// Maximum age of clipboard in seconds.
@property(nonatomic, readonly) NSTimeInterval maximumAgeOfClipboard;
// Whether the clipboard should only be accessed asynchronously.
@property(nonatomic, assign) BOOL onlyUseClipboardAsync;

// A cached version of an already-retrieved URL. This prevents subsequent URL
// requests from triggering the iOS 14 pasteboard notification.
@property(nonatomic, strong) NSURL* cachedURL;
// A cached version of an already-retrieved string. This prevents subsequent
// string requests from triggering the iOS 14 pasteboard notification.
@property(nonatomic, copy) NSString* cachedText;
// A cached version of an already-retrieved image. This prevents subsequent
// image requests from triggering the iOS 14 pasteboard notification.
@property(nonatomic, strong) UIImage* cachedImage;
// A cached set of the content types currently being used for the clipboard.
@property(nonatomic, strong) NSSet<ContentType>* cachedContentTypes;

// Loads information from the user defaults about the latest pasteboard entry.
- (void)loadFromUserDefaults;

// Returns the URL contained in `pasteboard` (if any).
- (NSURL*)URLFromPasteboard:(UIPasteboard*)pasteboard;

// Returns the uptime.
- (NSTimeInterval)uptime;

// Calls |completionHandler| with the result of whether or not the clipboard
// currently contains data matching |contentType|.
- (void)checkForContentType:(ContentType)contentType
                 pasteboard:(UIPasteboard*)pasteboard
          completionHandler:(void (^)(BOOL))completionHandler;

// Checks the clipboard for content matching |types| and calls
// |completionHandler| once all types are checked. This method is called
// recursively and partial results are passed in |results| until all types have
// been checked.
- (void)
    hasContentMatchingRemainingTypes:(NSSet<ContentType>*)types
                          pasteboard:(UIPasteboard*)pasteboard
                             results:
                                 (NSMutableDictionary<ContentType, NSNumber*>*)
                                     results
                   completionHandler:
                       (void (^)(NSSet<ContentType>*))completionHandler;

@end

@implementation ClipboardRecentContentImplIOS

- (instancetype)initWithMaxAge:(NSTimeInterval)maxAge
             authorizedSchemes:(NSSet<NSString*>*)authorizedSchemes
                  userDefaults:(NSUserDefaults*)groupUserDefaults
         onlyUseClipboardAsync:(BOOL)onlyUseClipboardAsync
                      delegate:(id<ClipboardRecentContentDelegate>)delegate {
  self = [super init];
  if (self) {
    _maximumAgeOfClipboard = maxAge;
    _delegate = delegate;
    _authorizedSchemes = authorizedSchemes;
    _sharedUserDefaults = groupUserDefaults;
    _onlyUseClipboardAsync = onlyUseClipboardAsync;

    _lastPasteboardChangeCount = NSIntegerMax;
    [self loadFromUserDefaults];
    [self updateCachedClipboardState];

    [[NSNotificationCenter defaultCenter]
        addObserver:self
           selector:@selector(didBecomeActive:)
               name:UIApplicationDidBecomeActiveNotification
             object:nil];
    [[NSNotificationCenter defaultCenter]
        addObserver:self
           selector:@selector(pasteboardDidChange:)
               name:UIPasteboardChangedNotification
             object:nil];

    __weak __typeof(self) weakSelf = self;
    GetGeneralPasteboard(
        _onlyUseClipboardAsync, base::BindOnce(^(UIPasteboard* pasteboard) {
          [weakSelf updateIfNeededWithPasteboard:pasteboard];
          // Makes sure |last_pasteboard_change_count_| was properly
          // initialized.
          DCHECK_NE(weakSelf.lastPasteboardChangeCount, NSIntegerMax);
        }));
  }
  return self;
}

- (void)dealloc {
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didBecomeActive:(NSNotification*)notification {
  [self loadFromUserDefaults];
  [self updateCachedClipboardState];

  __weak __typeof(self) weakSelf = self;
  GetGeneralPasteboard(self.onlyUseClipboardAsync,
                       base::BindOnce(^(UIPasteboard* pasteboard) {
                         [weakSelf updateIfNeededWithPasteboard:pasteboard];
                       }));
}

#pragma mark - Public

- (NSURL*)recentURLFromClipboard {
  // If the clipboard can only be accessed asynchronously, then this method
  // cannot even check whether the existing cached URL is stil valid.
  if (self.onlyUseClipboardAsync) {
    return nil;
  }
  return [self recentURLFromPasteboard:UIPasteboard.generalPasteboard];
}

- (NSString*)recentTextFromClipboard {
  // If the clipboard can only be accessed asynchronously, then this method
  // cannot even check whether the existing cached text is stil valid.
  if (self.onlyUseClipboardAsync) {
    return nil;
  }

  return [self recentTextFromPasteboard:UIPasteboard.generalPasteboard];
}

- (UIImage*)recentImageFromClipboard {
  // If the clipboard can only be accessed asynchronously, then this method
  // cannot even check whether the existing cached image is stil valid.
  if (self.onlyUseClipboardAsync) {
    return nil;
  }

  return [self recentImageFromPasteboard:UIPasteboard.generalPasteboard];
}

- (NSSet<ContentType>*)cachedClipboardContentTypes {
  if (![self shouldReturnValueOfClipboard:nil]) {
    return nil;
  }
  return self.cachedContentTypes;
}

- (void)hasContentMatchingTypes:(NSSet<ContentType>*)types
              completionHandler:
                  (void (^)(NSSet<ContentType>*))completionHandler {
  __weak __typeof(self) weakSelf = self;
  GetGeneralPasteboard(self.onlyUseClipboardAsync,
                       base::BindOnce(^(UIPasteboard* pasteboard) {
                         [weakSelf hasContentMatchingTypes:types
                                                pasteboard:pasteboard
                                         completionHandler:completionHandler];
                       }));
}

- (void)recentURLFromClipboardAsync:(void (^)(NSURL*))callback {
  __weak __typeof(self) weakSelf = self;
  GetGeneralPasteboard(
      self.onlyUseClipboardAsync, base::BindOnce(^(UIPasteboard* pasteboard) {
        [weakSelf recentURLFromClipboardAsyncWithPasteboard:pasteboard
                                                   callback:callback];
      }));
}

- (void)recentTextFromClipboardAsync:(void (^)(NSString*))callback {
  __weak __typeof(self) weakSelf = self;
  GetGeneralPasteboard(
      self.onlyUseClipboardAsync, base::BindOnce(^(UIPasteboard* pasteboard) {
        [weakSelf recentTextFromClipboardAsyncWithPasteboard:pasteboard
                                                    callback:callback];
      }));
}

- (void)recentImageFromClipboardAsync:(void (^)(UIImage*))callback {
  __weak __typeof(self) weakSelf = self;
  GetGeneralPasteboard(
      self.onlyUseClipboardAsync, base::BindOnce(^(UIPasteboard* pasteboard) {
        [weakSelf recentImageFromClipboardAsyncWithPasteboard:pasteboard
                                                     callback:callback];
      }));
}

- (NSTimeInterval)clipboardContentAge {
  return -[self.lastPasteboardChangeDate timeIntervalSinceNow];
}

- (void)suppressClipboardContent {
  // User cleared the user data. The pasteboard entry must be removed from the
  // omnibox list. Force entry expiration by setting copy date to 1970.
  self.lastPasteboardChangeDate =
      [[NSDate alloc] initWithTimeIntervalSince1970:0];
  [self saveToUserDefaults];
}

- (void)saveToUserDefaults {
  [self.sharedUserDefaults setInteger:self.lastPasteboardChangeCount
                               forKey:kPasteboardChangeCountKey];
  [self.sharedUserDefaults setObject:self.lastPasteboardChangeDate
                              forKey:kPasteboardChangeDateKey];
}

#pragma mark - Private

// Returns whether the pasteboard changed since the last time a pasteboard
// change was detected.
- (BOOL)hasPasteboardChanged:(UIPasteboard*)pasteboard {
  return pasteboard.changeCount != self.lastPasteboardChangeCount;
}

- (void)pasteboardDidChange:(NSNotification*)notification {
  [self updateCachedClipboardState];
}

- (void)updateCachedClipboardState {
  self.cachedContentTypes = nil;

  NSSet<ContentType>* desiredContentTypes = [NSSet
      setWithArray:@[ ContentTypeImage, ContentTypeURL, ContentTypeText ]];
  __weak __typeof(self) weakSelf = self;
  [self hasContentMatchingTypes:desiredContentTypes
              completionHandler:^(NSSet<ContentType>* results) {
                weakSelf.cachedContentTypes = results;
              }];
}

// The synchronous version of this method is kept around for public APIs and old
// iOS versions.
- (NSURL*)recentURLFromPasteboard:(UIPasteboard*)pasteboard {
  [self updateIfNeededWithPasteboard:pasteboard];

  if (![self shouldReturnValueOfClipboard:pasteboard]) {
    return nil;
  }

  return self.cachedURL;
}

// The synchronous version of this method is kept around for public APIs and old
// iOS versions.
- (NSString*)recentTextFromPasteboard:(UIPasteboard*)pasteboard {
  [self updateIfNeededWithPasteboard:pasteboard];

  if (![self shouldReturnValueOfClipboard:pasteboard]) {
    return nil;
  }

  return self.cachedText;
}

// The synchronous version of this method is kept around for public APIs and old
// iOS versions.
- (UIImage*)recentImageFromPasteboard:(UIPasteboard*)pasteboard {
  [self updateIfNeededWithPasteboard:pasteboard];

  if (![self shouldReturnValueOfClipboard:pasteboard]) {
    return nil;
  }

  return self.cachedImage;
}

// Checks for if the given `pasteboard` has content matching the provided
// `types`.
- (void)hasContentMatchingTypes:(NSSet<ContentType>*)types
                     pasteboard:(UIPasteboard*)pasteboard
              completionHandler:
                  (void (^)(NSSet<ContentType>*))completionHandler {
  [self updateIfNeededWithPasteboard:pasteboard];
  if (![self shouldReturnValueOfClipboard:pasteboard] || ![types count]) {
    completionHandler([NSSet set]);
    return;
  }

  [self hasContentMatchingRemainingTypes:types
                              pasteboard:pasteboard
                                 results:[[NSMutableDictionary alloc] init]
                       completionHandler:completionHandler];
}

- (void)
    hasContentMatchingRemainingTypes:(NSSet<ContentType>*)types
                          pasteboard:(UIPasteboard*)pasteboard
                             results:
                                 (NSMutableDictionary<ContentType, NSNumber*>*)
                                     results
                   completionHandler:
                       (void (^)(NSSet<ContentType>*))completionHandler {
  if ([types count] == 0) {
    NSMutableSet<ContentType>* matchingTypes = [NSMutableSet set];
    for (ContentType type in results) {
      if ([results[type] boolValue]) {
        [matchingTypes addObject:type];
      }
    }
    completionHandler(matchingTypes);
    return;
  }

  __weak __typeof(self) weakSelf = self;
  ContentType type = [types anyObject];
  [self checkForContentType:type
                 pasteboard:pasteboard
          completionHandler:^(BOOL hasType) {
            results[type] = @(hasType);

            NSMutableSet* remainingTypes = [types mutableCopy];
            [remainingTypes removeObject:type];
            [weakSelf hasContentMatchingRemainingTypes:remainingTypes
                                            pasteboard:pasteboard
                                               results:results
                                     completionHandler:completionHandler];
          }];
}

- (void)checkForContentType:(ContentType)contentType
                 pasteboard:(UIPasteboard*)pasteboard
          completionHandler:(void (^)(BOOL))completionHandler {
  if ([contentType isEqualToString:ContentTypeText]) {
    [self hasRecentTextFromClipboardInternalWithPasteboard:pasteboard
                                                  callback:^(BOOL hasText) {
                                                    completionHandler(hasText);
                                                  }];
  } else if ([contentType isEqualToString:ContentTypeURL]) {
    [self hasRecentURLFromClipboardInternalWithPasteboard:pasteboard
                                                 callback:^(BOOL hasURL) {
                                                   completionHandler(hasURL);
                                                 }];
  } else if ([contentType isEqualToString:ContentTypeImage]) {
    [self
        hasRecentImageFromClipboardInternalWithPasteboard:pasteboard
                                                 callback:^(BOOL hasImage) {
                                                   completionHandler(hasImage);
                                                 }];
  } else {
    NOTREACHED() << contentType;
  }
}

// The underlying logic to check if the clipboard has a recent URL, with the
// addition of a `pasteboard` parameter to aid in forcing all pasteboard access
// to be async.
- (void)
    hasRecentURLFromClipboardInternalWithPasteboard:(UIPasteboard*)pasteboard
                                           callback:(void (^)(BOOL))callback {
  DCHECK(callback);
  // Use cached value if it exists.
  if (self.cachedURL) {
    callback(YES);
    return;
  }

  NSSet<UIPasteboardDetectionPattern>* urlPattern =
      [NSSet setWithObject:UIPasteboardDetectionPatternProbableWebURL];
  [pasteboard
      detectPatternsForPatterns:urlPattern
              completionHandler:^(NSSet<UIPasteboardDetectionPattern>* patterns,
                                  NSError* error) {
                callback([patterns
                    containsObject:UIPasteboardDetectionPatternProbableWebURL]);
              }];
}

// The underlying logic to check if the clipboard has recent text, with the
// addition of a `pasteboard` parameter to aid in forcing all pasteboard access
// to be async.
- (void)
    hasRecentTextFromClipboardInternalWithPasteboard:(UIPasteboard*)pasteboard
                                            callback:(void (^)(BOOL))callback {
  DCHECK(callback);
  // Use cached value if it exists.
  if (self.cachedText) {
    callback(YES);
    return;
  }

  NSSet<UIPasteboardDetectionPattern>* textPattern =
      [NSSet setWithObject:UIPasteboardDetectionPatternProbableWebSearch];
  [pasteboard
      detectPatternsForPatterns:textPattern
              completionHandler:^(NSSet<UIPasteboardDetectionPattern>* patterns,
                                  NSError* error) {
                callback([patterns
                    containsObject:
                        UIPasteboardDetectionPatternProbableWebSearch]);
              }];
}

// The underlying logic to check if the clipboard has a recent image, with the
// addition of a `pasteboard` parameter to aid in forcing all pasteboard access
// to be async.
- (void)
    hasRecentImageFromClipboardInternalWithPasteboard:(UIPasteboard*)pasteboard
                                             callback:(void (^)(BOOL))callback {
  DCHECK(callback);
  // Use cached value if it exists
  if (self.cachedImage) {
    callback(YES);
    return;
  }

  callback(pasteboard.hasImages);
}

// The underlying logic to check the recent url, with the addition of a
// `pasteboard` parameter to aid in forcing all pasteboard access to be async.
- (void)recentURLFromClipboardAsyncWithPasteboard:(UIPasteboard*)pasteboard
                                         callback:(void (^)(NSURL*))callback {
  DCHECK(callback);
  [self updateIfNeededWithPasteboard:pasteboard];
  if (![self shouldReturnValueOfClipboard:pasteboard]) {
    callback(nil);
    return;
  }

  // Use cached value if it exists.
  if (self.cachedURL) {
    callback(self.cachedURL);
    return;
  }

  __weak __typeof(self) weakSelf = self;
  NSSet<UIPasteboardDetectionPattern>* urlPattern =
      [NSSet setWithObject:UIPasteboardDetectionPatternProbableWebURL];
  [pasteboard detectValuesForPatterns:urlPattern
                    completionHandler:^(
                        NSDictionary<UIPasteboardDetectionPattern, id>* values,
                        NSError* error) {
                      [weakSelf callCompletionHandlerWithValues:values
                                                       callback:callback
                                                          error:error];
                    }];
}

// Helper method for completion handler block to ensure `self` isn't retained.
- (void)callCompletionHandlerWithValues:
            (NSDictionary<UIPasteboardDetectionPattern, id>*)values
                               callback:(void (^)(NSURL*))callback
                                  error:(NSError*)error {
  // On iOS 16, users can deny access to the clipboard.
  if (error) {
    self.cachedURL = nil;
    callback(nil);
    return;
  }
  NSURL* url =
      [NSURL URLWithString:values[UIPasteboardDetectionPatternProbableWebURL]];

  // |detectValuesForPatterns:| will return a url even if the url
  // is missing a scheme. In this case, default to https.
  if (url && url.scheme == nil) {
    NSURLComponents* components = [[NSURLComponents alloc] initWithURL:url
                                               resolvingAgainstBaseURL:NO];
    components.scheme = kDefaultScheme;
    url = components.URL;
  }

  if (![self.authorizedSchemes containsObject:url.scheme]) {
    self.cachedURL = nil;
    callback(nil);
  } else {
    self.cachedURL = url;
    callback(url);
  }
}

// The underlying logic to check the recent text, with the addition of a
// `pasteboard` parameter to aid in forcing all pasteboard access to be async.
- (void)recentTextFromClipboardAsyncWithPasteboard:(UIPasteboard*)pasteboard
                                          callback:
                                              (void (^)(NSString*))callback {
  DCHECK(callback);
  [self updateIfNeededWithPasteboard:pasteboard];
  if (![self shouldReturnValueOfClipboard:pasteboard]) {
    callback(nil);
    return;
  }

  // Use cached value if it exists.
  if (self.cachedText) {
    callback(self.cachedText);
    return;
  }

  __weak __typeof(self) weakSelf = self;
  NSSet<UIPasteboardDetectionPattern>* textPattern =
      [NSSet setWithObject:UIPasteboardDetectionPatternProbableWebSearch];
  [pasteboard detectValuesForPatterns:textPattern
                    completionHandler:^(
                        NSDictionary<UIPasteboardDetectionPattern, id>* values,
                        NSError* error) {
                      NSString* text =
                          values[UIPasteboardDetectionPatternProbableWebSearch];
                      weakSelf.cachedText = text;

                      callback(text);
                    }];
}

// The underlying logic to check the recent image, with the addition of a
// `pasteboard` parameter to aid in forcing all pasteboard access to be async.
- (void)recentImageFromClipboardAsyncWithPasteboard:(UIPasteboard*)pasteboard
                                           callback:
                                               (void (^)(UIImage*))callback {
  DCHECK(callback);
  [self updateIfNeededWithPasteboard:pasteboard];
  if (![self shouldReturnValueOfClipboard:pasteboard]) {
    callback(nil);
    return;
  }

  if (!self.cachedImage) {
    self.cachedImage = pasteboard.image;
  }
  callback(self.cachedImage);
}

// Returns whether the value of the given clipboard should be returned.
- (BOOL)shouldReturnValueOfClipboard:(UIPasteboard*)pasteboard {
  if ([self clipboardContentAge] > self.maximumAgeOfClipboard)
    return NO;

  // It is the common convention on iOS that password managers tag confidential
  // data with the flavor "org.nspasteboard.ConcealedType". Obey this
  // convention; the user doesn't want for their confidential data to be
  // suggested as a search, anyway. See http://nspasteboard.org/ for more info.
  NSArray<NSString*>* types = [pasteboard pasteboardTypes];
  if ([types containsObject:@"org.nspasteboard.ConcealedType"])
    return NO;

  return YES;
}

// If the content of the pasteboard has changed, updates the change count
// and change date.
- (void)updateIfNeededWithPasteboard:(UIPasteboard*)pasteboard {
  if (![self hasPasteboardChanged:pasteboard]) {
    return;
  }

  self.lastPasteboardChangeDate = [NSDate date];
  self.lastPasteboardChangeCount = pasteboard.changeCount;

  // Clear the cache because the pasteboard data has changed.
  self.cachedURL = nil;
  self.cachedText = nil;
  self.cachedImage = nil;

  [self.delegate onClipboardChanged];

  [self saveToUserDefaults];
}

- (NSURL*)URLFromPasteboard:(UIPasteboard*)pasteboard {
  NSURL* url = pasteboard.URL;
  // Usually, even if the user copies plaintext, if it looks like a URL, the URL
  // property is filled. Sometimes, this doesn't happen, for instance when the
  // pasteboard is sync'd from a Mac to the iOS simulator. In this case,
  // fallback and manually check whether the pasteboard contains a url-like
  // string.
  if (!url) {
    url = [NSURL URLWithString:pasteboard.string];
  }
  if (![self.authorizedSchemes containsObject:url.scheme]) {
    return nil;
  }
  return url;
}

- (void)loadFromUserDefaults {
  self.lastPasteboardChangeCount =
      [self.sharedUserDefaults integerForKey:kPasteboardChangeCountKey];
  self.lastPasteboardChangeDate = base::apple::ObjCCastStrict<NSDate>(
      [self.sharedUserDefaults objectForKey:kPasteboardChangeDateKey]);
}

- (NSTimeInterval)uptime {
  return base::SysInfo::Uptime().InSecondsF();
}

@end