Implement RemoveAsDefaultProtocolClient on OS X
authorBen Gotow <bengotow@gmail.com>
Sat, 7 May 2016 18:07:58 +0000 (11:07 -0700)
committerBen Gotow <bengotow@gmail.com>
Sat, 7 May 2016 18:07:58 +0000 (11:07 -0700)
atom/browser/browser_mac.mm

index eb7191b..767ddee 100644 (file)
@@ -43,7 +43,28 @@ void Browser::ClearRecentDocuments() {
 }
 
 bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol) {
-  return false;
+  NSString* identifier = [base::mac::MainBundle() bundleIdentifier];
+  if (!identifier)
+    return false;
+
+  NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
+  CFStringRef protocol_cf = base::mac::NSToCFCast(protocol_ns);
+  CFArrayRef bundleList = LSCopyAllHandlersForURLScheme(protocol_cf);
+  if (!bundleList) {
+    return false;
+  }
+  // On Mac OS X, we can't query the default, but the handlers list seems to put
+  // Apple's defaults first, so we'll use the first option that isn't our bundle
+  CFStringRef other = nil;
+  for (CFIndex i = 0; i < CFArrayGetCount(bundleList); i++) {
+    other = (CFStringRef)CFArrayGetValueAtIndex(bundleList, i);
+    if (![identifier isEqualToString: (__bridge NSString *)other]) {
+      break;
+    }
+  }
+
+  OSStatus return_code = LSSetDefaultHandlerForURLScheme(protocol_cf, other);
+  return return_code == noErr;
 }
 
 bool Browser::SetAsDefaultProtocolClient(const std::string& protocol) {