From ca971e978e6e82c93a18534c80b596fb715c1522 Mon Sep 17 00:00:00 2001 From: Samuel Attard Date: Thu, 18 Aug 2016 18:02:13 +1000 Subject: [PATCH] Return boolean instead of empty string when error occurs. Update docs --- atom/browser/browser_win.cc | 30 ++++++++++++++++-------------- docs/api/app.md | 12 ++++++++---- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/atom/browser/browser_win.cc b/atom/browser/browser_win.cc index 16c6314e8e16..97b2d24c3f35 100644 --- a/atom/browser/browser_win.cc +++ b/atom/browser/browser_win.cc @@ -125,7 +125,7 @@ bool Browser::SetUserTasks(const std::vector& tasks) { return SUCCEEDED(destinations->CommitList()); } -std::wstring protocolLaunchPath(std::string protocol, mate::Arguments* args) { +bool GetProtocolLaunchPath(std::string protocol, mate::Arguments* args, std::wstring* exe) { // Read in optional exe path arg std::wstring exePath; std::string rawExePath; @@ -135,7 +135,7 @@ std::wstring protocolLaunchPath(std::string protocol, mate::Arguments* args) { if (!args->GetNext(&rawExePath)) { if (!PathService::Get(base::FILE_EXE, &path)) { LOG(ERROR) << "Error getting app exe path"; - return L""; + return false; } // Executable Path exePath = path.value(); @@ -147,19 +147,17 @@ std::wstring protocolLaunchPath(std::string protocol, mate::Arguments* args) { std::vector launchArgs; args->GetNext(&launchArgs); - std::wstring exe = L"\"" + exePath + L"\" "; + *exe = L"\"" + exePath + L"\" "; // Parse launch args into a string of space spearated args if (launchArgs.size() != 0) { - std::string launchArgString = ""; - for (std::string launchArg : launchArgs) { - launchArgString = launchArgString + launchArg + " "; - } + std::string launchArgString = base::JoinString(launchArgs, " "); std::wstring wLaunchArgString; wLaunchArgString.assign(launchArgString.begin(), launchArgString.end()); - exe = exe + L"\"" + wLaunchArgString + L"\""; + *exe = *exe + L"\"" + wLaunchArgString + L"\""; } - return exe + L"\"%1\""; + *exe = *exe + L"\"%1\""; + return true; } bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol, @@ -191,7 +189,10 @@ bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol, // Default value not set, we can confirm that it is not set return true; - std::wstring exe = protocolLaunchPath(protocol, args); + std::wstring exe; + if (!GetProtocolLaunchPath(protocol, args, &exe)) { + return false; + } if (exe == L"") return false; if (keyVal == exe) { @@ -224,8 +225,8 @@ bool Browser::SetAsDefaultProtocolClient(const std::string& protocol, if (protocol.empty()) return false; - std::wstring exe = protocolLaunchPath(protocol, args); - if (exe == L"") { + std::wstring exe; + if (!GetProtocolLaunchPath(protocol, args, &exe)) { return false; } @@ -258,9 +259,10 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol, if (protocol.empty()) return false; - std::wstring exe = protocolLaunchPath(protocol, args); - if (exe == L"") + std::wstring exe; + if (!GetProtocolLaunchPath(protocol, args, &exe)) { return false; + } // Main Registry Key HKEY root = HKEY_CURRENT_USER; diff --git a/docs/api/app.md b/docs/api/app.md index ea922749db2e..e7eb194eb073 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -454,7 +454,7 @@ Clears the recent documents list. app to handle `electron://` links, call this method with `electron` as the parameter. * `path` String (optional) _Windows_ - Defaults to `process.execPath` -* `args` Array (options) _Windows_ - Defaults to an empty array +* `args` Array (optional) _Windows_ - Defaults to an empty array This method sets the current executable as the default handler for a protocol (aka URI scheme). It allows you to integrate your app deeper into the operating @@ -462,7 +462,7 @@ system. Once registered, all links with `your-protocol://` will be opened with the current executable. The whole link, including protocol, will be passed to your application as a parameter. -On windows you can provide optional parameters path, the path to your executable, +On Windows you can provide optional parameters path, the path to your executable, and args, an array of arguments to be passed to your executable when it launches. Returns `true` when the call succeeded, otherwise returns `false`. @@ -474,18 +474,22 @@ Please refer to [Apple's documentation][CFBundleURLTypes] for details. The API uses the Windows Registry and LSSetDefaultHandlerForURLScheme internally. -### `app.removeAsDefaultProtocolClient(protocol)` _macOS_ _Windows_ +### `app.removeAsDefaultProtocolClient(protocol[, path, args])` _macOS_ _Windows_ * `protocol` String - The name of your protocol, without `://`. +* `path` String (optional) _Windows_ - Defaults to `process.execPath` +* `args` Array (optional) _Windows_ - Defaults to an empty array This method checks if the current executable as the default handler for a protocol (aka URI scheme). If so, it will remove the app as the default handler. Returns `true` when the call succeeded, otherwise returns `false`. -### `app.isDefaultProtocolClient(protocol)` _macOS_ _Windows_ +### `app.isDefaultProtocolClient(protocol[, path, args])` _macOS_ _Windows_ * `protocol` String - The name of your protocol, without `://`. +* `path` String (optional) _Windows_ - Defaults to `process.execPath` +* `args` Array (optional) _Windows_ - Defaults to an empty array 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.