From 77dfd3a76c34a3ca65a6ac233ccb80b0266b4f41 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 22 Aug 2016 09:50:58 +0900 Subject: [PATCH 1/2] Do not add extra space when args is empty --- atom/browser/browser.h | 4 +- atom/browser/browser_linux.cc | 2 +- atom/browser/browser_mac.mm | 2 +- atom/browser/browser_win.cc | 69 +++++++++++++++-------------------- 4 files changed, 34 insertions(+), 43 deletions(-) diff --git a/atom/browser/browser.h b/atom/browser/browser.h index efaa48437e20..b8b151d7bafa 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -77,7 +77,7 @@ class Browser : public WindowListObserver { // Remove the default protocol handler registry key bool RemoveAsDefaultProtocolClient(const std::string& protocol, - mate::Arguments* args); + mate::Arguments* args); // Set as default handler for a protocol. bool SetAsDefaultProtocolClient(const std::string& protocol, @@ -85,7 +85,7 @@ class Browser : public WindowListObserver { // Query the current state of default handler for a protocol. bool IsDefaultProtocolClient(const std::string& protocol, - mate::Arguments* args); + mate::Arguments* args); // Set/Get the badge count. bool SetBadgeCount(int count); diff --git a/atom/browser/browser_linux.cc b/atom/browser/browser_linux.cc index bcd37b170abc..b5926f451534 100644 --- a/atom/browser/browser_linux.cc +++ b/atom/browser/browser_linux.cc @@ -41,7 +41,7 @@ bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol, } bool Browser::SetAsDefaultProtocolClient(const std::string& protocol, - mate::Arguments* args) { + mate::Arguments* args) { return false; } diff --git a/atom/browser/browser_mac.mm b/atom/browser/browser_mac.mm index a5dde9a62243..00b0f8ff21b8 100644 --- a/atom/browser/browser_mac.mm +++ b/atom/browser/browser_mac.mm @@ -76,7 +76,7 @@ bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol, } bool Browser::SetAsDefaultProtocolClient(const std::string& protocol, - mate::Arguments* args) { + mate::Arguments* args) { if (protocol.empty()) return false; diff --git a/atom/browser/browser_win.cc b/atom/browser/browser_win.cc index 1e5d2e54a6c5..4b82d618cbb9 100644 --- a/atom/browser/browser_win.cc +++ b/atom/browser/browser_win.cc @@ -10,6 +10,8 @@ #include #include +#include "atom/common/atom_version.h" +#include "atom/common/native_mate_converters/string16_converter.h" #include "base/base_paths.h" #include "base/file_version_info.h" #include "base/files/file_path.h" @@ -20,8 +22,6 @@ #include "base/win/win_util.h" #include "base/win/registry.h" #include "base/win/windows_version.h" -#include "atom/common/atom_version.h" -#include "atom/common/native_mate_converters/string16_converter.h" namespace atom { @@ -42,6 +42,30 @@ BOOL CALLBACK WindowsEnumerationHandler(HWND hwnd, LPARAM param) { return TRUE; } +bool GetProtocolLaunchPath(mate::Arguments* args, base::string16* exe) { + // Executable Path + if (!args->GetNext(exe)) { + base::FilePath path; + if (!PathService::Get(base::FILE_EXE, &path)) { + LOG(ERROR) << "Error getting app exe path"; + return false; + } + *exe = path.value(); + } + + // Read in optional args arg + std::vector launch_args; + if (args->GetNext(&launch_args) && !launch_args.empty()) + *exe = base::StringPrintf(L"\"%s\" %s \"%%1\"", + exe->c_str(), + base::JoinString(launch_args, L" ").c_str()); + else + *exe = base::StringPrintf(L"\"%s\" \"%%1\"", exe->c_str()); + LOG(ERROR) << *exe; + return false; + return true; +} + } // namespace void Browser::Focus() { @@ -126,35 +150,6 @@ bool Browser::SetUserTasks(const std::vector& tasks) { return SUCCEEDED(destinations->CommitList()); } -bool GetProtocolLaunchPath(mate::Arguments* args, base::string16* exe) { - // Read in optional exe path arg - base::string16 exePath; - - base::FilePath path; - - if (!args->GetNext(&exePath)) { - if (!PathService::Get(base::FILE_EXE, &path)) { - LOG(ERROR) << "Error getting app exe path"; - return false; - } - // Executable Path - exePath = path.value(); - } - - // Read in optional args arg - std::vector launchArgs; - args->GetNext(&launchArgs); - - // Parse launch args into a string of space spearated args - base::string16 launchArgString; - if (launchArgs.size() != 0) { - launchArgString = base::JoinString(launchArgs, L" "); - } - *exe = base::StringPrintf(L"\"%s\" %s \"%%1\"", - exePath.c_str(), launchArgString.c_str()); - return true; -} - bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol, mate::Arguments* args) { if (protocol.empty()) @@ -185,11 +180,9 @@ bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol, return true; std::wstring exe; - if (!GetProtocolLaunchPath(args, &exe)) { - return false; - } - if (exe == L"") + if (!GetProtocolLaunchPath(args, &exe)) return false; + if (keyVal == exe) { // Let's kill the key if (FAILED(key.DeleteKey(L"shell"))) @@ -221,9 +214,8 @@ bool Browser::SetAsDefaultProtocolClient(const std::string& protocol, return false; std::wstring exe; - if (!GetProtocolLaunchPath(args, &exe)) { + if (!GetProtocolLaunchPath(args, &exe)) return false; - } // Main Registry Key HKEY root = HKEY_CURRENT_USER; @@ -255,9 +247,8 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol, return false; std::wstring exe; - if (!GetProtocolLaunchPath(args, &exe)) { + if (!GetProtocolLaunchPath(args, &exe)) return false; - } // Main Registry Key HKEY root = HKEY_CURRENT_USER; From 31b82731ac528b68e96e77e29ed3690089bc7aa7 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 22 Aug 2016 09:57:52 +0900 Subject: [PATCH 2/2] Cleanup the usages of std::wstring --- atom/browser/browser_win.cc | 51 ++++++++++++++----------------------- 1 file changed, 19 insertions(+), 32 deletions(-) diff --git a/atom/browser/browser_win.cc b/atom/browser/browser_win.cc index 4b82d618cbb9..40db4be9ecb8 100644 --- a/atom/browser/browser_win.cc +++ b/atom/browser/browser_win.cc @@ -61,8 +61,6 @@ bool GetProtocolLaunchPath(mate::Arguments* args, base::string16* exe) { base::JoinString(launch_args, L" ").c_str()); else *exe = base::StringPrintf(L"\"%s\" \"%%1\"", exe->c_str()); - LOG(ERROR) << *exe; - return false; return true; } @@ -157,12 +155,10 @@ bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol, // Main Registry Key HKEY root = HKEY_CURRENT_USER; - std::string keyPathStr = "Software\\Classes\\" + protocol; - std::wstring keyPath = std::wstring(keyPathStr.begin(), keyPathStr.end()); + base::string16 keyPath = base::UTF8ToUTF16("Software\\Classes\\" + protocol); // Command Key - std::string cmdPathStr = keyPathStr + "\\shell\\open\\command"; - std::wstring cmdPath = std::wstring(cmdPathStr.begin(), cmdPathStr.end()); + base::string16 cmdPath = keyPath + L"\\shell\\open\\command"; base::win::RegKey key; base::win::RegKey commandKey; @@ -174,12 +170,12 @@ bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol, // Key doesn't even exist, we can confirm that it is not set return true; - std::wstring keyVal; + base::string16 keyVal; if (FAILED(commandKey.ReadValue(L"", &keyVal))) // Default value not set, we can confirm that it is not set return true; - std::wstring exe; + base::string16 exe; if (!GetProtocolLaunchPath(args, &exe)) return false; @@ -213,20 +209,17 @@ bool Browser::SetAsDefaultProtocolClient(const std::string& protocol, if (protocol.empty()) return false; - std::wstring exe; + base::string16 exe; if (!GetProtocolLaunchPath(args, &exe)) 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()); - std::string urlDeclStr = "URL:" + protocol; - std::wstring urlDecl = std::wstring(urlDeclStr.begin(), urlDeclStr.end()); + base::string16 keyPath = base::UTF8ToUTF16("Software\\Classes\\" + protocol); + base::string16 urlDecl = base::UTF8ToUTF16("URL:" + protocol); // Command Key - std::string cmdPathStr = keyPathStr + "\\shell\\open\\command"; - std::wstring cmdPath = std::wstring(cmdPathStr.begin(), cmdPathStr.end()); + base::string16 cmdPath = keyPath + L"\\shell\\open\\command"; // Write information to registry base::win::RegKey key(root, keyPath.c_str(), KEY_ALL_ACCESS); @@ -246,18 +239,16 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol, if (protocol.empty()) return false; - std::wstring exe; + base::string16 exe; if (!GetProtocolLaunchPath(args, &exe)) 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()); + base::string16 keyPath = base::UTF8ToUTF16("Software\\Classes\\" + protocol); // Command Key - std::string cmdPathStr = keyPathStr + "\\shell\\open\\command"; - std::wstring cmdPath = std::wstring(cmdPathStr.begin(), cmdPathStr.end()); + base::string16 cmdPath = keyPath + L"\\shell\\open\\command"; base::win::RegKey key; base::win::RegKey commandKey; @@ -269,17 +260,13 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol, // Key doesn't exist, we can confirm that it is not set return false; - std::wstring keyVal; + base::string16 keyVal; if (FAILED(commandKey.ReadValue(L"", &keyVal))) // Default value not set, we can confirm that it is not set return false; - if (keyVal == exe) { - // Default value is the same as current file path - return true; - } else { - return false; - } + // Default value is the same as current file path + return keyVal == exe; } bool Browser::SetBadgeCount(int count) { @@ -287,13 +274,13 @@ bool Browser::SetBadgeCount(int count) { } void Browser::SetLoginItemSettings(LoginItemSettings settings) { - std::wstring keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; + base::string16 keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS); if (settings.open_at_login) { base::FilePath path; if (PathService::Get(base::FILE_EXE, &path)) { - std::wstring exePath(path.value()); + base::string16 exePath(path.value()); key.WriteValue(GetAppUserModelID(), exePath.c_str()); } } else { @@ -303,14 +290,14 @@ void Browser::SetLoginItemSettings(LoginItemSettings settings) { Browser::LoginItemSettings Browser::GetLoginItemSettings() { LoginItemSettings settings; - std::wstring keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; + base::string16 keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS); - std::wstring keyVal; + base::string16 keyVal; if (!FAILED(key.ReadValue(GetAppUserModelID(), &keyVal))) { base::FilePath path; if (PathService::Get(base::FILE_EXE, &path)) { - std::wstring exePath(path.value()); + base::string16 exePath(path.value()); settings.open_at_login = keyVal == exePath; } }