123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559 |
- // Copyright 2017 The Chromium Authors. All rights reserved.
- // 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/mac/foundation_util.h"
- #include "base/notreached.h"
- #include "base/strings/sys_string_conversions.h"
- #include "base/system/sys_info.h"
- #if !defined(__has_feature) || !__has_feature(objc_arc)
- #error "This file requires ARC support."
- #endif
- 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;
- // 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;
- // If the content of the pasteboard has changed, updates the change count
- // and change date.
- - (void)updateIfNeeded;
- // Returns whether the pasteboard changed since the last time a pasteboard
- // change was detected.
- - (BOOL)hasPasteboardChanged;
- // Loads information from the user defaults about the latest pasteboard entry.
- - (void)loadFromUserDefaults;
- // Returns the URL contained in the clipboard (if any).
- - (NSURL*)URLFromPasteboard;
- // Returns the uptime.
- - (NSTimeInterval)uptime;
- // Returns whether the value of the clipboard should be returned.
- - (BOOL)shouldReturnValueOfClipboard;
- // Calls |completionHandler| with the result of whether or not the clipboard
- // currently contains data matching |contentType|.
- - (void)checkForContentType:(ContentType)contentType
- 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
- results:
- (NSMutableDictionary<ContentType, NSNumber*>*)
- results
- completionHandler:
- (void (^)(NSSet<ContentType>*))completionHandler;
- @end
- @implementation ClipboardRecentContentImplIOS
- - (instancetype)initWithMaxAge:(NSTimeInterval)maxAge
- authorizedSchemes:(NSSet<NSString*>*)authorizedSchemes
- userDefaults:(NSUserDefaults*)groupUserDefaults
- delegate:(id<ClipboardRecentContentDelegate>)delegate {
- self = [super init];
- if (self) {
- _maximumAgeOfClipboard = maxAge;
- _delegate = delegate;
- _authorizedSchemes = authorizedSchemes;
- _sharedUserDefaults = groupUserDefaults;
- _lastPasteboardChangeCount = NSIntegerMax;
- [self loadFromUserDefaults];
- [self updateIfNeeded];
- [self updateCachedClipboardState];
- // Makes sure |last_pasteboard_change_count_| was properly initialized.
- DCHECK_NE(_lastPasteboardChangeCount, NSIntegerMax);
- [[NSNotificationCenter defaultCenter]
- addObserver:self
- selector:@selector(didBecomeActive:)
- name:UIApplicationDidBecomeActiveNotification
- object:nil];
- [[NSNotificationCenter defaultCenter]
- addObserver:self
- selector:@selector(pasteboardDidChange:)
- name:UIPasteboardChangedNotification
- object:nil];
- }
- return self;
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)didBecomeActive:(NSNotification*)notification {
- [self loadFromUserDefaults];
- [self updateIfNeeded];
- [self updateCachedClipboardState];
- }
- - (BOOL)hasPasteboardChanged {
- return UIPasteboard.generalPasteboard.changeCount !=
- self.lastPasteboardChangeCount;
- }
- - (void)pasteboardDidChange:(NSNotification*)notification {
- [self updateCachedClipboardState];
- }
- - (NSURL*)recentURLFromClipboard {
- [self updateIfNeeded];
- if (![self shouldReturnValueOfClipboard])
- return nil;
- if (@available(iOS 14, *)) {
- // On iOS 14, don't actually access the pasteboard in this method. This
- // prevents the pasteboard access notification from appearing.
- } else {
- if (!self.cachedURL) {
- self.cachedURL = [self URLFromPasteboard];
- }
- }
- return self.cachedURL;
- }
- - (NSString*)recentTextFromClipboard {
- [self updateIfNeeded];
- if (![self shouldReturnValueOfClipboard])
- return nil;
- if (@available(iOS 14, *)) {
- // On iOS 14, don't actually access the pasteboard in this method. This
- // prevents the pasteboard access notification from appearing.
- } else {
- if (!self.cachedText) {
- self.cachedText = UIPasteboard.generalPasteboard.string;
- }
- }
- return self.cachedText;
- }
- - (UIImage*)recentImageFromClipboard {
- [self updateIfNeeded];
- if (![self shouldReturnValueOfClipboard])
- return nil;
- if (@available(iOS 14, *)) {
- // On iOS 14, don't actually access the pasteboard in this method. This
- // prevents the pasteboard access notification from appearing.
- } else {
- if (!self.cachedImage) {
- self.cachedImage = UIPasteboard.generalPasteboard.image;
- }
- }
- return self.cachedImage;
- }
- - (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;
- }];
- }
- - (NSSet<ContentType>*)cachedClipboardContentTypes {
- if (![self shouldReturnValueOfClipboard])
- return nil;
- return self.cachedContentTypes;
- }
- - (void)hasContentMatchingTypes:(NSSet<ContentType>*)types
- completionHandler:
- (void (^)(NSSet<ContentType>*))completionHandler {
- [self updateIfNeeded];
- if (![self shouldReturnValueOfClipboard] || ![types count]) {
- completionHandler([NSSet set]);
- return;
- }
- [self hasContentMatchingRemainingTypes:types
- results:[[NSMutableDictionary alloc] init]
- completionHandler:completionHandler];
- }
- - (void)
- hasContentMatchingRemainingTypes:(NSSet<ContentType>*)types
- 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
- completionHandler:^(BOOL hasType) {
- results[type] = @(hasType);
- NSMutableSet* remainingTypes = [types mutableCopy];
- [remainingTypes removeObject:type];
- [weakSelf hasContentMatchingRemainingTypes:remainingTypes
- results:results
- completionHandler:completionHandler];
- }];
- }
- - (void)checkForContentType:(ContentType)contentType
- completionHandler:(void (^)(BOOL))completionHandler {
- if ([contentType isEqualToString:ContentTypeText]) {
- [self hasRecentTextFromClipboardInternal:^(BOOL hasText) {
- completionHandler(hasText);
- }];
- } else if ([contentType isEqualToString:ContentTypeURL]) {
- [self hasRecentURLFromClipboardInternal:^(BOOL hasURL) {
- completionHandler(hasURL);
- }];
- } else if ([contentType isEqualToString:ContentTypeImage]) {
- [self hasRecentImageFromClipboardInternal:^(BOOL hasImage) {
- completionHandler(hasImage);
- }];
- } else {
- NOTREACHED() << contentType;
- }
- }
- - (void)hasRecentURLFromClipboardInternal:(void (^)(BOOL))callback {
- DCHECK(callback);
- if (@available(iOS 14, *)) {
- // Use cached value if it exists
- if (self.cachedURL) {
- callback(YES);
- return;
- }
- #if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
- NSSet<UIPasteboardDetectionPattern>* urlPattern =
- [NSSet setWithObject:UIPasteboardDetectionPatternProbableWebURL];
- [UIPasteboard.generalPasteboard
- detectPatternsForPatterns:urlPattern
- completionHandler:^(
- NSSet<UIPasteboardDetectionPattern>* patterns,
- NSError* error) {
- callback([patterns
- containsObject:
- UIPasteboardDetectionPatternProbableWebURL]);
- }];
- #else
- // To prevent clipboard notification from appearing on iOS 14 with iOS 13
- // SDK, use the -hasURLs property to check for URL existence. This will
- // cause crbug.com/1033935 to reappear in code using this method (also see
- // the comments in -URLFromPasteboard in this file), but that is preferable
- // to the notificatio appearing when it shouldn't.
- callback(UIPasteboard.generalPasteboard.hasURLs);
- #endif
- } else {
- callback([self recentURLFromClipboard] != nil);
- }
- }
- - (void)hasRecentTextFromClipboardInternal:(void (^)(BOOL))callback {
- DCHECK(callback);
- if (@available(iOS 14, *)) {
- // Use cached value if it exists
- if (self.cachedText) {
- callback(YES);
- return;
- }
- #if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
- NSSet<UIPasteboardDetectionPattern>* textPattern =
- [NSSet setWithObject:UIPasteboardDetectionPatternProbableWebSearch];
- [UIPasteboard.generalPasteboard
- detectPatternsForPatterns:textPattern
- completionHandler:^(
- NSSet<UIPasteboardDetectionPattern>* patterns,
- NSError* error) {
- callback([patterns
- containsObject:
- UIPasteboardDetectionPatternProbableWebSearch]);
- }];
- #else
- callback(UIPasteboard.generalPasteboard.hasStrings);
- #endif
- } else {
- callback([self recentTextFromClipboard] != nil);
- }
- }
- - (void)hasRecentImageFromClipboardInternal:(void (^)(BOOL))callback {
- DCHECK(callback);
- if (@available(iOS 14, *)) {
- // Use cached value if it exists
- if (self.cachedImage) {
- callback(YES);
- return;
- }
- callback(UIPasteboard.generalPasteboard.hasImages);
- } else {
- callback([self recentImageFromClipboard] != nil);
- }
- }
- - (void)recentURLFromClipboardAsync:(void (^)(NSURL*))callback {
- DCHECK(callback);
- if (@available(iOS 14, *)) {
- [self updateIfNeeded];
- if (![self shouldReturnValueOfClipboard]) {
- callback(nil);
- return;
- }
- // Use cached value if it exists.
- if (self.cachedURL) {
- callback(self.cachedURL);
- return;
- }
- #if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
- __weak __typeof(self) weakSelf = self;
- NSSet<UIPasteboardDetectionPattern>* urlPattern =
- [NSSet setWithObject:UIPasteboardDetectionPatternProbableWebURL];
- [UIPasteboard.generalPasteboard
- detectValuesForPatterns:urlPattern
- completionHandler:^(
- NSDictionary<UIPasteboardDetectionPattern, id>* values,
- NSError* error) {
- // On iOS 16, users can deny access to the clipboard.
- if (error) {
- weakSelf.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]) {
- weakSelf.cachedURL = nil;
- callback(nil);
- } else {
- weakSelf.cachedURL = url;
- callback(url);
- }
- }];
- #else
- callback([self recentURLFromClipboard]);
- #endif
- } else {
- callback([self recentURLFromClipboard]);
- }
- }
- - (void)recentTextFromClipboardAsync:(void (^)(NSString*))callback {
- DCHECK(callback);
- if (@available(iOS 14, *)) {
- [self updateIfNeeded];
- if (![self shouldReturnValueOfClipboard]) {
- callback(nil);
- return;
- }
- // Use cached value if it exists.
- if (self.cachedText) {
- callback(self.cachedText);
- return;
- }
- #if defined(__IPHONE_14_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0
- __weak __typeof(self) weakSelf = self;
- NSSet<UIPasteboardDetectionPattern>* textPattern =
- [NSSet setWithObject:UIPasteboardDetectionPatternProbableWebSearch];
- [UIPasteboard.generalPasteboard
- detectValuesForPatterns:textPattern
- completionHandler:^(
- NSDictionary<UIPasteboardDetectionPattern, id>* values,
- NSError* error) {
- NSString* text =
- values[UIPasteboardDetectionPatternProbableWebSearch];
- weakSelf.cachedText = text;
- callback(text);
- }];
- #else
- callback([self recentTextFromClipboard]);
- #endif
- } else {
- callback([self recentTextFromClipboard]);
- }
- }
- - (void)recentImageFromClipboardAsync:(void (^)(UIImage*))callback {
- DCHECK(callback);
- [self updateIfNeeded];
- if (![self shouldReturnValueOfClipboard]) {
- callback(nil);
- return;
- }
- if (!self.cachedImage) {
- self.cachedImage = UIPasteboard.generalPasteboard.image;
- }
- callback(self.cachedImage);
- }
- - (NSTimeInterval)clipboardContentAge {
- return -[self.lastPasteboardChangeDate timeIntervalSinceNow];
- }
- - (BOOL)shouldReturnValueOfClipboard {
- 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 =
- [[UIPasteboard generalPasteboard] pasteboardTypes];
- if ([types containsObject:@"org.nspasteboard.ConcealedType"])
- return NO;
- return YES;
- }
- - (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)updateIfNeeded {
- if (![self hasPasteboardChanged]) {
- return;
- }
- self.lastPasteboardChangeDate = [NSDate date];
- self.lastPasteboardChangeCount = [UIPasteboard generalPasteboard].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 {
- NSURL* url = [UIPasteboard generalPasteboard].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:UIPasteboard.generalPasteboard.string];
- }
- if (![self.authorizedSchemes containsObject:url.scheme]) {
- return nil;
- }
- return url;
- }
- - (void)loadFromUserDefaults {
- self.lastPasteboardChangeCount =
- [self.sharedUserDefaults integerForKey:kPasteboardChangeCountKey];
- self.lastPasteboardChangeDate = base::mac::ObjCCastStrict<NSDate>(
- [self.sharedUserDefaults objectForKey:kPasteboardChangeDateKey]);
- }
- - (void)saveToUserDefaults {
- [self.sharedUserDefaults setInteger:self.lastPasteboardChangeCount
- forKey:kPasteboardChangeCountKey];
- [self.sharedUserDefaults setObject:self.lastPasteboardChangeDate
- forKey:kPasteboardChangeDateKey];
- }
- - (NSTimeInterval)uptime {
- return base::SysInfo::Uptime().InSecondsF();
- }
- @end
|