refactor: extract-constant static Windows registry keys in Browser code (#47569)
* refactor: extract-constant for registry key in GetProcessExecPath() * refactor: extract-constant for registry key in Browser::SetLoginItemSettings() * refactor: extract-constant for registry key in Browser::SetLoginItemSettings() * refactor: extract-constant for registry key in Browser::GetLoginItemSettings() * chore: document the symbolic constants * refactor: prefer base::wcstring_view::c_str() to data() to make zero-termination clearer
This commit is contained in:
parent
b21c4b0f88
commit
2ba8fe9140
1 changed files with 19 additions and 25 deletions
|
@ -20,6 +20,7 @@
|
||||||
#include "base/files/file_path.h"
|
#include "base/files/file_path.h"
|
||||||
#include "base/logging.h"
|
#include "base/logging.h"
|
||||||
#include "base/path_service.h"
|
#include "base/path_service.h"
|
||||||
|
#include "base/strings/cstring_view.h"
|
||||||
#include "base/strings/strcat_win.h"
|
#include "base/strings/strcat_win.h"
|
||||||
#include "base/strings/string_util.h"
|
#include "base/strings/string_util.h"
|
||||||
#include "base/strings/utf_string_conversions.h"
|
#include "base/strings/utf_string_conversions.h"
|
||||||
|
@ -54,6 +55,14 @@ namespace electron {
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
// specifies what should run at user login
|
||||||
|
constexpr base::wcstring_view Run =
|
||||||
|
LR"(Software\Microsoft\Windows\CurrentVersion\Run)";
|
||||||
|
|
||||||
|
// controls whether each Run entry is enabled or disabled
|
||||||
|
constexpr base::wcstring_view StartupApprovedRun =
|
||||||
|
LR"(Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run)";
|
||||||
|
|
||||||
bool GetProcessExecPath(std::wstring* exe) {
|
bool GetProcessExecPath(std::wstring* exe) {
|
||||||
base::FilePath path;
|
base::FilePath path;
|
||||||
if (!base::PathService::Get(base::FILE_EXE, &path)) {
|
if (!base::PathService::Get(base::FILE_EXE, &path)) {
|
||||||
|
@ -193,19 +202,11 @@ std::vector<LaunchItem> GetLoginItemSettingsHelper(
|
||||||
|
|
||||||
// attempt to update launch_item.enabled if there is a matching key
|
// attempt to update launch_item.enabled if there is a matching key
|
||||||
// value entry in the StartupApproved registry
|
// value entry in the StartupApproved registry
|
||||||
|
const HKEY scope_key =
|
||||||
|
scope == L"user" ? HKEY_CURRENT_USER : HKEY_LOCAL_MACHINE;
|
||||||
HKEY hkey;
|
HKEY hkey;
|
||||||
// StartupApproved registry path
|
LONG res = RegOpenKeyEx(scope_key, StartupApprovedRun.c_str(), 0,
|
||||||
LPCTSTR path = TEXT(
|
KEY_QUERY_VALUE, &hkey);
|
||||||
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApp"
|
|
||||||
"roved\\Run");
|
|
||||||
LONG res;
|
|
||||||
if (scope == L"user") {
|
|
||||||
res =
|
|
||||||
RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_QUERY_VALUE, &hkey);
|
|
||||||
} else {
|
|
||||||
res =
|
|
||||||
RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_QUERY_VALUE, &hkey);
|
|
||||||
}
|
|
||||||
if (res == ERROR_SUCCESS) {
|
if (res == ERROR_SUCCESS) {
|
||||||
DWORD type, size;
|
DWORD type, size;
|
||||||
wchar_t startup_binary[12];
|
wchar_t startup_binary[12];
|
||||||
|
@ -615,14 +616,10 @@ void Browser::UpdateBadgeContents(
|
||||||
}
|
}
|
||||||
|
|
||||||
void Browser::SetLoginItemSettings(LoginItemSettings settings) {
|
void Browser::SetLoginItemSettings(LoginItemSettings settings) {
|
||||||
std::wstring key_path = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
|
base::win::RegKey key(HKEY_CURRENT_USER, Run.c_str(), KEY_ALL_ACCESS);
|
||||||
base::win::RegKey key(HKEY_CURRENT_USER, key_path.c_str(), KEY_ALL_ACCESS);
|
|
||||||
|
|
||||||
std::wstring startup_approved_key_path =
|
|
||||||
L"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApproved"
|
|
||||||
L"\\Run";
|
|
||||||
base::win::RegKey startup_approved_key(
|
base::win::RegKey startup_approved_key(
|
||||||
HKEY_CURRENT_USER, startup_approved_key_path.c_str(), KEY_ALL_ACCESS);
|
HKEY_CURRENT_USER, StartupApprovedRun.c_str(), KEY_ALL_ACCESS);
|
||||||
PCWSTR key_name =
|
PCWSTR key_name =
|
||||||
!settings.name.empty() ? settings.name.c_str() : GetAppUserModelID();
|
!settings.name.empty() ? settings.name.c_str() : GetAppUserModelID();
|
||||||
|
|
||||||
|
@ -635,9 +632,7 @@ void Browser::SetLoginItemSettings(LoginItemSettings settings) {
|
||||||
startup_approved_key.DeleteValue(key_name);
|
startup_approved_key.DeleteValue(key_name);
|
||||||
} else {
|
} else {
|
||||||
HKEY hard_key;
|
HKEY hard_key;
|
||||||
LPCTSTR path = TEXT(
|
constexpr LPCTSTR path = StartupApprovedRun.c_str();
|
||||||
"Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\StartupApp"
|
|
||||||
"roved\\Run");
|
|
||||||
LONG res =
|
LONG res =
|
||||||
RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_ALL_ACCESS, &hard_key);
|
RegOpenKeyEx(HKEY_CURRENT_USER, path, 0, KEY_ALL_ACCESS, &hard_key);
|
||||||
|
|
||||||
|
@ -660,8 +655,7 @@ void Browser::SetLoginItemSettings(LoginItemSettings settings) {
|
||||||
v8::Local<v8::Value> Browser::GetLoginItemSettings(
|
v8::Local<v8::Value> Browser::GetLoginItemSettings(
|
||||||
const LoginItemSettings& options) {
|
const LoginItemSettings& options) {
|
||||||
LoginItemSettings settings;
|
LoginItemSettings settings;
|
||||||
std::wstring keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
|
base::win::RegKey key(HKEY_CURRENT_USER, Run.c_str(), KEY_ALL_ACCESS);
|
||||||
base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS);
|
|
||||||
std::wstring keyVal;
|
std::wstring keyVal;
|
||||||
|
|
||||||
// keep old openAtLogin behaviour
|
// keep old openAtLogin behaviour
|
||||||
|
@ -678,9 +672,9 @@ v8::Local<v8::Value> Browser::GetLoginItemSettings(
|
||||||
boolean executable_will_launch_at_login = false;
|
boolean executable_will_launch_at_login = false;
|
||||||
std::vector<LaunchItem> launch_items;
|
std::vector<LaunchItem> launch_items;
|
||||||
base::win::RegistryValueIterator hkcu_iterator(HKEY_CURRENT_USER,
|
base::win::RegistryValueIterator hkcu_iterator(HKEY_CURRENT_USER,
|
||||||
keyPath.c_str());
|
Run.c_str());
|
||||||
base::win::RegistryValueIterator hklm_iterator(HKEY_LOCAL_MACHINE,
|
base::win::RegistryValueIterator hklm_iterator(HKEY_LOCAL_MACHINE,
|
||||||
keyPath.c_str());
|
Run.c_str());
|
||||||
|
|
||||||
launch_items = GetLoginItemSettingsHelper(
|
launch_items = GetLoginItemSettingsHelper(
|
||||||
&hkcu_iterator, &executable_will_launch_at_login, L"user", options);
|
&hkcu_iterator, &executable_will_launch_at_login, L"user", options);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue