Bug 1085391 - Support Yosemite Handoff 'web browsing' activity type. r=spohl
authorPanos Astithas <past@mozilla.com>
Sat, 20 Oct 2018 04:47:38 +0000
changeset 490816 6ad9bd572bf46896dead808303c652bfb9f8d37a
parent 490815 501fffbf872d0a320069448010aa0706d3b81409
child 490817 bff46c89a68bebf27458b3f0aaa2715d6062c041
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewersspohl
bugs1085391
milestone65.0a1
Bug 1085391 - Support Yosemite Handoff 'web browsing' activity type. r=spohl Differential Revision: https://phabricator.services.mozilla.com/D8665
browser/app/macbuild/Contents/Info.plist.in
toolkit/xre/MacApplicationDelegate.mm
--- a/browser/app/macbuild/Contents/Info.plist.in
+++ b/browser/app/macbuild/Contents/Info.plist.in
@@ -216,16 +216,20 @@
 			<key>CFBundleURLSchemes</key>
 			<array>
 				<string>file</string>
 			</array>
 		</dict>
 	</array>
 	<key>CFBundleVersion</key>
 	<string>%MAC_BUNDLE_VERSION%</string>
+	<key>NSUserActivityTypes</key>
+	<array>
+		<string>NSUserActivityTypeBrowsingWeb</string>
+	</array>
 	<key>NSAppleScriptEnabled</key>
 	<true/>
 	<key>LSApplicationCategoryType</key>
 	<string>public.app-category.productivity</string>
 	<key>LSEnvironment</key>
 	<dict>
 		<key>MallocNanoZone</key>
 		<string>0</string>
--- a/toolkit/xre/MacApplicationDelegate.mm
+++ b/toolkit/xre/MacApplicationDelegate.mm
@@ -340,40 +340,19 @@ ProcessPendingGetURLAppleEvents()
   bool isGetURLEvent =
     ([event eventClass] == kInternetEventClass && [event eventID] == kAEGetURL);
   if (isGetURLEvent)
     sProcessedGetURLEvent = true;
 
   if (isGetURLEvent ||
       ([event eventClass] == 'WWW!' && [event eventID] == 'OURL')) {
     NSString* urlString = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
-
-    // don't open chrome URLs
-    NSString* schemeString = [[NSURL URLWithString:urlString] scheme];
-    if (!schemeString ||
-        [schemeString compare:@"chrome"
-                      options:NSCaseInsensitiveSearch
-                        range:NSMakeRange(0, [schemeString length])] == NSOrderedSame) {
-      return;
-    }
+    NSURL* url = [NSURL URLWithString:urlString];
 
-    // Add the URL to any command line we're currently setting up.
-    if (CommandLineServiceMac::AddURLToCurrentCommandLine([urlString UTF8String]))
-      return;
-
-    nsCOMPtr<nsICommandLineRunner> cmdLine(new nsCommandLine());
-    nsCOMPtr<nsIFile> workingDir;
-    nsresult rv = NS_GetSpecialDirectory(NS_OS_CURRENT_WORKING_DIR, getter_AddRefs(workingDir));
-    if (NS_FAILED(rv))
-      return;
-    const char *argv[3] = {nullptr, "-url", [urlString UTF8String]};
-    rv = cmdLine->Init(3, argv, workingDir, nsICommandLine::STATE_REMOTE_EXPLICIT);
-    if (NS_FAILED(rv))
-      return;
-    rv = cmdLine->Run();
+    [self openURL:url];
   }
   else if ([event eventClass] == kCoreEventClass && [event eventID] == kAEOpenDocuments) {
     NSAppleEventDescriptor* fileListDescriptor = [event paramDescriptorForKeyword:keyDirectObject];
     if (!fileListDescriptor)
       return;
 
     // Descriptor list indexing is one-based...
     NSInteger numberOfFiles = [fileListDescriptor numberOfItems];
@@ -387,9 +366,65 @@ ProcessPendingGetURLAppleEvents()
       if (!url)
         continue;
 
       [self application:NSApp openFile:[url path]];
     }
   }
 }
 
+- (BOOL)application:(NSApplication*)application
+    willContinueUserActivityWithType:(NSString*)userActivityType
+{
+  return [userActivityType isEqualToString:NSUserActivityTypeBrowsingWeb];
+}
+
+- (BOOL)application:(NSApplication*)application
+   continueUserActivity:(NSUserActivity*)userActivity
+     restorationHandler:(void (^)(NSArray*))restorationHandler
+{
+  if (![userActivity.activityType isEqualToString:NSUserActivityTypeBrowsingWeb]) {
+    return NO;
+  }
+
+  return [self openURL:userActivity.webpageURL];
+}
+
+- (void)application:(NSApplication*)application
+    didFailToContinueUserActivityWithType:(NSString*)userActivityType
+                                    error:(NSError*)error
+{
+  NSLog(@"Failed to continue user activity %@: %@", userActivityType, error);
+}
+
+- (BOOL)openURL:(NSURL*)url
+{
+  if (!url || !url.scheme || [url.scheme caseInsensitiveCompare:@"chrome"] == NSOrderedSame) {
+    return NO;
+  }
+
+  const char* const urlString = [[url absoluteString] UTF8String];
+  // Add the URL to any command line we're currently setting up.
+  if (CommandLineServiceMac::AddURLToCurrentCommandLine(urlString)) {
+    return NO;
+  }
+
+  nsCOMPtr<nsICommandLineRunner> cmdLine(new nsCommandLine());
+  nsCOMPtr<nsIFile> workingDir;
+  nsresult rv = NS_GetSpecialDirectory(NS_OS_CURRENT_WORKING_DIR,
+                                       getter_AddRefs(workingDir));
+  if (NS_FAILED(rv)) {
+    return NO;
+  }
+  const char* argv[3] = {nullptr, "-url", urlString};
+  rv = cmdLine->Init(3, argv, workingDir, nsICommandLine::STATE_REMOTE_EXPLICIT);
+  if (NS_FAILED(rv)) {
+    return NO;
+  }
+  rv = cmdLine->Run();
+  if (NS_FAILED(rv)) {
+    return NO;
+  }
+
+  return YES;
+}
+
 @end