Add API: IsDefaultProtocolClient

This commit is contained in:
Rita Zhang 2016-04-24 22:17:01 -07:00
parent 7b8a05f01f
commit df2141d9e6
6 changed files with 91 additions and 0 deletions

View file

@ -451,6 +451,8 @@ void App::BuildPrototype(
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",

View file

@ -82,6 +82,9 @@ class Browser : public WindowListObserver {
// 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();

View file

@ -42,6 +42,10 @@ bool Browser::SetAsDefaultProtocolClient(const std::string& protocol) {
return false;
}
bool Browser::IsDefaultProtocolClient(const std::string& protocol) {
return false;
}
std::string Browser::GetExecutableFileVersion() const {
return brightray::GetApplicationVersion();
}

View file

@ -60,6 +60,30 @@ bool Browser::SetAsDefaultProtocolClient(const std::string& protocol) {
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) {
}

View file

@ -225,6 +225,50 @@ bool Browser::SetAsDefaultProtocolClient(const std::string& protocol) {
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(

View file

@ -384,6 +384,18 @@ This method checks if the current executable as the default handler for a protoc
**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
@ -556,3 +568,5 @@ Sets the `image` associated with this dock icon.
[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