From 5ba92b2fde37964f3a4e4429f064aff7933315a3 Mon Sep 17 00:00:00 2001 From: Robbie Gibson Date: Mon, 21 Mar 2022 19:44:57 +0000 Subject: [PATCH] [iOS] Use default scheme for copied URLs without schemes The iOS async clipboard APIs |detectPatternsForPatterns:| and |detectValuesForPatterns:| return YES and a value even if the copied data is a text url with no scheme. In this case, default to https when constructing a URL/GURL. Bug: 1306773 Change-Id: I649fa194a10a2ffc945335ba01d397744a3d2690 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3540620 Reviewed-by: Olivier Robin Commit-Queue: Robbie Gibson Cr-Commit-Position: refs/heads/main@{#983451} --- .../clipboard_recent_content_impl_ios.mm | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/components/open_from_clipboard/clipboard_recent_content_impl_ios.mm b/components/open_from_clipboard/clipboard_recent_content_impl_ios.mm index 2fe3035d0153a1..7f3573f5797255 100644 --- a/components/open_from_clipboard/clipboard_recent_content_impl_ios.mm +++ b/components/open_from_clipboard/clipboard_recent_content_impl_ios.mm @@ -28,6 +28,8 @@ // 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 @@ -392,6 +394,16 @@ - (void)recentURLFromClipboardAsync:(void (^)(NSURL*))callback { URLWithString: values[UIPasteboardDetectionPatternProbableWebURL]]; + // |detectValuesForPatterns:| will return a url even if the url + // is missing a scheme. In this case, default to https. + if (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);