base::Bind(&Browser::ClearRecentDocuments, browser))
.SetMethod("setAppUserModelId",
base::Bind(&Browser::SetAppUserModelID, browser))
+ .SetMethod("isDefaultProtocolClient",
+ base::Bind(&Browser::IsDefaultProtocolClient, browser))
.SetMethod("setAsDefaultProtocolClient",
base::Bind(&Browser::SetAsDefaultProtocolClient, browser))
.SetMethod("removeAsDefaultProtocolClient",
// Set as default handler for a protocol.
bool SetAsDefaultProtocolClient(const std::string& protocol);
+ // Query the current state of default handler for a protocol.
+ bool IsDefaultProtocolClient(const std::string& protocol);
+
#if defined(OS_MACOSX)
// Hide the application.
void Hide();
return false;
}
+bool Browser::IsDefaultProtocolClient(const std::string& protocol) {
+ return false;
+}
+
std::string Browser::GetExecutableFileVersion() const {
return brightray::GetApplicationVersion();
}
return return_code == noErr;
}
+bool Browser::IsDefaultProtocolClient(const std::string& protocol) {
+ if (protocol.empty())
+ return false;
+
+ NSString* identifier = [base::mac::MainBundle() bundleIdentifier];
+ if (!identifier)
+ return false;
+
+ NSString* protocol_ns = [NSString stringWithUTF8String:protocol.c_str()];
+
+ CFStringRef bundle =
+ LSCopyDefaultHandlerForURLScheme(base::mac::NSToCFCast(protocol_ns));
+ NSString* bundleId = static_cast<NSString*>(
+ base::mac::CFTypeRefToNSObjectAutorelease(bundle));
+ if (!bundleId)
+ return false;
+
+ // Ensure the comparison is case-insensitive
+ // as LS does not persist the case of the bundle id.
+ NSComparisonResult result =
+ [bundleId caseInsensitiveCompare:identifier];
+ return result == NSOrderedSame;
+}
+
void Browser::SetAppUserModelID(const base::string16& name) {
}
return true;
}
+bool Browser::IsDefaultProtocolClient(const std::string& protocol) {
+ if (protocol.empty())
+ return false;
+
+ base::FilePath path;
+ if (!PathService::Get(base::FILE_EXE, &path)) {
+ LOG(ERROR) << "Error getting app exe path";
+ return false;
+ }
+
+ // Main Registry Key
+ HKEY root = HKEY_CURRENT_USER;
+ std::string keyPathStr = "Software\\Classes\\" + protocol;
+ std::wstring keyPath = std::wstring(keyPathStr.begin(), keyPathStr.end());
+
+ // Command Key
+ std::string cmdPathStr = keyPathStr + "\\shell\\open\\command";
+ std::wstring cmdPath = std::wstring(cmdPathStr.begin(), cmdPathStr.end());
+
+ base::win::RegKey key;
+ base::win::RegKey commandKey;
+ if (FAILED(key.Open(root, keyPath.c_str(), KEY_ALL_ACCESS)))
+ // Key doesn't exist, we can confirm that it is not set
+ return false;
+
+ if (FAILED(commandKey.Open(root, cmdPath.c_str(), KEY_ALL_ACCESS)))
+ // Key doesn't exist, we can confirm that it is not set
+ return false;
+
+ std::wstring keyVal;
+ if (FAILED(commandKey.ReadValue(L"", &keyVal)))
+ // Default value not set, we can confirm that it is not set
+ return false;
+
+ std::wstring exePath(path.value());
+ std::wstring exe = L"\"" + exePath + L"\" \"%1\"";
+ if (keyVal == exe) {
+ // Default value is the same as current file path
+ return true;
+ } else {
+ return false;
+ }
+}
+
PCWSTR Browser::GetAppUserModelID() {
if (app_user_model_id_.empty()) {
SetAppUserModelID(base::ReplaceStringPlaceholders(
**Note:** On OS X, removing the app will automatically remove the app as the
default protocol handler.
+### `app.isDefaultProtocolClient(protocol)` _OS X_ _Windows_
+
+* `protocol` String - The name of your protocol, without `://`.
+
+This method checks if the current executable is the default handler for a protocol
+(aka URI scheme). If so, it will return true. Otherwise, it will return false.
+
+**Note:** On OS X, you can use this method to check if the app has been registered as the default protocol handler for a protocol. You can also verify this by checking `~/Library/Preferences/com.apple.LaunchServices.plist` on the OS X machine.
+Please refer to [Apple's documentation][LSCopyDefaultHandlerForURLScheme] for details.
+
+The API uses the Windows Registry and LSCopyDefaultHandlerForURLScheme internally.
+
### `app.setUserTasks(tasks)` _Windows_
* `tasks` Array - Array of `Task` objects
[tasks]:http://msdn.microsoft.com/en-us/library/windows/desktop/dd378460(v=vs.85).aspx#tasks
[app-user-model-id]: https://msdn.microsoft.com/en-us/library/windows/desktop/dd378459(v=vs.85).aspx
[CFBundleURLTypes]: https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html#//apple_ref/doc/uid/TP40009249-102207-TPXREF115
+[LSCopyDefaultHandlerForURLScheme]:
+https://developer.apple.com/library/mac/documentation/Carbon/Reference/LaunchServicesReference/#//apple_ref/c/func/LSCopyDefaultHandlerForURLScheme