From 64f9c6a1d7ef9ec9ec251922379611beac5f474a Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Jul 2016 10:57:53 -0700 Subject: [PATCH 1/7] Add get/setLoginItemSettings on all platforms --- atom/browser/api/atom_api_app.cc | 8 ++++---- atom/browser/browser.h | 11 ++++------- atom/browser/browser_linux.cc | 7 +++++++ atom/browser/browser_win.cc | 8 ++++++++ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 43ba298ad0d3..654e9d716d2d 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -559,6 +559,10 @@ void App::BuildPrototype( base::Bind(&Browser::RemoveAsDefaultProtocolClient, browser)) .SetMethod("setBadgeCount", base::Bind(&Browser::SetBadgeCount, browser)) .SetMethod("getBadgeCount", base::Bind(&Browser::GetBadgeCount, browser)) + .SetMethod("getLoginItemSettings", + base::Bind(&Browser::GetLoginItemSettings, browser)) + .SetMethod("setLoginItemSettings", + base::Bind(&Browser::SetLoginItemSettings, browser)) #if defined(OS_MACOSX) .SetMethod("hide", base::Bind(&Browser::Hide, browser)) .SetMethod("show", base::Bind(&Browser::Show, browser)) @@ -566,10 +570,6 @@ void App::BuildPrototype( base::Bind(&Browser::SetUserActivity, browser)) .SetMethod("getCurrentActivityType", base::Bind(&Browser::GetCurrentActivityType, browser)) - .SetMethod("getLoginItemSettings", - base::Bind(&Browser::GetLoginItemSettings, browser)) - .SetMethod("setLoginItemSettings", - base::Bind(&Browser::SetLoginItemSettings, browser)) #endif #if defined(OS_WIN) .SetMethod("setUserTasks", base::Bind(&Browser::SetUserTasks, browser)) diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 7d493f054932..771a29a87a42 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -88,7 +88,7 @@ class Browser : public WindowListObserver { bool SetBadgeCount(int count); int GetBadgeCount(); -#if defined(OS_MACOSX) + // Set/Get the login item settings of the app struct LoginItemSettings { bool open_at_login = false; bool open_as_hidden = false; @@ -96,7 +96,10 @@ class Browser : public WindowListObserver { bool opened_at_login = false; bool opened_as_hidden = false; }; + void SetLoginItemSettings(LoginItemSettings settings); + LoginItemSettings GetLoginItemSettings(); +#if defined(OS_MACOSX) // Hide the application. void Hide(); @@ -139,12 +142,6 @@ class Browser : public WindowListObserver { // Set docks' icon. void DockSetIcon(const gfx::Image& image); - - // Get login item settings of app - LoginItemSettings GetLoginItemSettings(); - - // Set login item settings of app - void SetLoginItemSettings(LoginItemSettings settings); #endif // defined(OS_MACOSX) #if defined(OS_WIN) diff --git a/atom/browser/browser_linux.cc b/atom/browser/browser_linux.cc index fe1a0af9ba3a..bc2dc89ac33a 100644 --- a/atom/browser/browser_linux.cc +++ b/atom/browser/browser_linux.cc @@ -57,6 +57,13 @@ bool Browser::SetBadgeCount(int count) { } } +void Browser::SetLoginItemSettings(LoginItemSettings settings) { +} + +LoginItemSettings Browser::GetLoginItemSettings() { + return LoginItemSettings(); +} + std::string Browser::GetExecutableFileVersion() const { return brightray::GetApplicationVersion(); } diff --git a/atom/browser/browser_win.cc b/atom/browser/browser_win.cc index 5eb9275dcff6..a28dfc096adb 100644 --- a/atom/browser/browser_win.cc +++ b/atom/browser/browser_win.cc @@ -273,6 +273,14 @@ bool Browser::SetBadgeCount(int count) { return false; } +void Browser::SetLoginItemSettings(LoginItemSettings settings) { +} + +LoginItemSettings Browser::GetLoginItemSettings() { + return LoginItemSettings(); +} + + PCWSTR Browser::GetAppUserModelID() { if (app_user_model_id_.empty()) { SetAppUserModelID(base::ReplaceStringPlaceholders( From 5a36c5596cdef4a857a8541f18692572945a228f Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Jul 2016 11:12:26 -0700 Subject: [PATCH 2/7] Add initial windows login item implementation --- atom/browser/browser_win.cc | 39 ++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/atom/browser/browser_win.cc b/atom/browser/browser_win.cc index a28dfc096adb..88c2df3b6a13 100644 --- a/atom/browser/browser_win.cc +++ b/atom/browser/browser_win.cc @@ -274,10 +274,47 @@ bool Browser::SetBadgeCount(int count) { } void Browser::SetLoginItemSettings(LoginItemSettings settings) { + base::FilePath path; + if (!PathService::Get(base::FILE_EXE, &path)) { + LOG(ERROR) << "Error getting app exe path"; + return; + } + + // Main Registry Key + HKEY root = HKEY_CURRENT_USER; + std::string keyPathStr = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; + std::wstring keyPath = std::wstring(keyPathStr.begin(), keyPathStr.end()); + + // Executable Path + std::wstring exePath(path.value()); + base::win::RegKey key(root, keyPath.c_str(), KEY_ALL_ACCESS); + + if (settings.open_at_login) + key.WriteValue(GetAppUserModelID(), exePath.c_str()); + else { + key.DeleteValue(GetAppUserModelID()) + } } LoginItemSettings Browser::GetLoginItemSettings() { - return LoginItemSettings(); + LoginItemSettings settings; + + base::FilePath path; + if (!PathService::Get(base::FILE_EXE, &path)) { + LOG(ERROR) << "Error getting app exe path"; + return; + } + + // Main Registry Key + HKEY root = HKEY_CURRENT_USER; + std::string keyPathStr = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; + std::wstring keyPath = std::wstring(keyPathStr.begin(), keyPathStr.end()); + + // Executable Path + std::wstring exePath(path.value()); + base::win::RegKey key(root, keyPath.c_str(), KEY_ALL_ACCESS); + + settings.open_at_login = key.HasValue(GetAppUserModelID()); } From af907163a29f575e37a5c21d5ee201d64444d8f9 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Jul 2016 11:18:17 -0700 Subject: [PATCH 3/7] Enable LoginItemSettings converter on all platforms --- atom/browser/api/atom_api_app.cc | 3 --- 1 file changed, 3 deletions(-) diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 654e9d716d2d..fc3d9e12a07d 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -72,7 +72,6 @@ struct Converter { }; #endif -#if defined(OS_MACOSX) template<> struct Converter { static bool FromV8(v8::Isolate* isolate, v8::Local val, @@ -97,8 +96,6 @@ struct Converter { return dict.GetHandle(); } }; -#endif - } // namespace mate From 3d358ee7ea1c7438d1b266d7f1e44686bbef6cf4 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Jul 2016 12:29:01 -0700 Subject: [PATCH 4/7] Fix Windows compilation issues --- atom/browser/browser_win.cc | 54 +++++++++++++++---------------------- 1 file changed, 21 insertions(+), 33 deletions(-) diff --git a/atom/browser/browser_win.cc b/atom/browser/browser_win.cc index 88c2df3b6a13..522fffa0191f 100644 --- a/atom/browser/browser_win.cc +++ b/atom/browser/browser_win.cc @@ -274,47 +274,35 @@ bool Browser::SetBadgeCount(int count) { } void Browser::SetLoginItemSettings(LoginItemSettings settings) { - base::FilePath path; - if (!PathService::Get(base::FILE_EXE, &path)) { - LOG(ERROR) << "Error getting app exe path"; - return; - } + std::wstring keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; + base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS); - // Main Registry Key - HKEY root = HKEY_CURRENT_USER; - std::string keyPathStr = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; - std::wstring keyPath = std::wstring(keyPathStr.begin(), keyPathStr.end()); - - // Executable Path - std::wstring exePath(path.value()); - base::win::RegKey key(root, keyPath.c_str(), KEY_ALL_ACCESS); - - if (settings.open_at_login) - key.WriteValue(GetAppUserModelID(), exePath.c_str()); - else { - key.DeleteValue(GetAppUserModelID()) + if (settings.open_at_login) { + base::FilePath path; + if (PathService::Get(base::FILE_EXE, &path)) { + std::wstring exePath(path.value()); + key.WriteValue(GetAppUserModelID(), exePath.c_str()); + } + } else { + key.DeleteValue(GetAppUserModelID()); } } -LoginItemSettings Browser::GetLoginItemSettings() { +Browser::LoginItemSettings Browser::GetLoginItemSettings() { LoginItemSettings settings; + std::wstring keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; + base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS); + std::wstring keyVal; - base::FilePath path; - if (!PathService::Get(base::FILE_EXE, &path)) { - LOG(ERROR) << "Error getting app exe path"; - return; + if (!FAILED(key.ReadValue(GetAppUserModelID(), &keyVal))) { + base::FilePath path; + if (PathService::Get(base::FILE_EXE, &path)) { + std::wstring exePath(path.value()); + settings.open_at_login = keyVal == exePath; + } } - // Main Registry Key - HKEY root = HKEY_CURRENT_USER; - std::string keyPathStr = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; - std::wstring keyPath = std::wstring(keyPathStr.begin(), keyPathStr.end()); - - // Executable Path - std::wstring exePath(path.value()); - base::win::RegKey key(root, keyPath.c_str(), KEY_ALL_ACCESS); - - settings.open_at_login = key.HasValue(GetAppUserModelID()); + return settings; } From 64513f8c690603618b9d08af125a83e800193912 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Jul 2016 12:30:40 -0700 Subject: [PATCH 5/7] Enable login item spec on Windows --- spec/api-app-spec.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index f1e1b1b5a7cd..618919ec25ef 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -300,7 +300,7 @@ describe('app module', function () { }) describe('app.get/setLoginItemSettings API', function () { - if (process.platform !== 'darwin') return + if (process.platform === 'linux') return beforeEach(function () { app.setLoginItemSettings({openAtLogin: false}) @@ -323,7 +323,7 @@ describe('app module', function () { app.setLoginItemSettings({openAtLogin: true, openAsHidden: true}) assert.deepEqual(app.getLoginItemSettings(), { openAtLogin: true, - openAsHidden: true, + openAsHidden: process.platform === 'darwin', // Only available on macOS wasOpenedAtLogin: false, wasOpenedAsHidden: false, restoreState: false From 66d16b28718ae0f3d6b6efbee2c2c5231aaa14ac Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Jul 2016 12:36:06 -0700 Subject: [PATCH 6/7] Add Windows label to login items settings API --- docs/api/app.md | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/docs/api/app.md b/docs/api/app.md index d949744c0cf9..d69fd5d60b04 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -611,22 +611,24 @@ Returns the current value displayed in the counter badge. Returns whether current desktop environment is Unity launcher. -### `app.getLoginItemSettings()` _macOS_ +### `app.getLoginItemSettings()` _macOS_ _Windows_ Return an Object with the login item settings of the app. * `openAtLogin` Boolean - `true` if the app is set to open at login. * `openAsHidden` Boolean - `true` if the app is set to open as hidden at login. + This setting is only supported on macOS. * `wasOpenedAtLogin` Boolean - `true` if the app was opened at login - automatically. + automatically. This setting is only supported on macOS. * `wasOpenedAsHidden` Boolean - `true` if the app was opened as a hidden login item. This indicates that the app should not open any windows at startup. + This setting is only supported on macOS. * `restoreState` Boolean - `true` if the app was opened as a login item that should restore the state from the previous session. This indicates that the app should restore the windows that were open the last time the app was - closed. + closed. This setting is only supported on macOS. -### `app.setLoginItemSettings(settings)` _macOS_ +### `app.setLoginItemSettings(settings)` _macOS_ _Windows_ * `settings` Object * `openAtLogin` Boolean - `true` to open the app at login, `false` to remove @@ -634,7 +636,8 @@ Return an Object with the login item settings of the app. * `openAsHidden` Boolean - `true` to open the app as hidden. Defaults to `false`. The user can edit this setting from the System Preferences so `app.getLoginItemStatus().wasOpenedAsHidden` should be checked when the app - is opened to know the current value. + is opened to know the current value. This setting is only supported on + macOS. Set the app's login item settings. From afd5e51d6aa39b66187e41ce022485368f0ba4f7 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Mon, 11 Jul 2016 12:36:46 -0700 Subject: [PATCH 7/7] Add Browser namespace --- atom/browser/browser_linux.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/atom/browser/browser_linux.cc b/atom/browser/browser_linux.cc index bc2dc89ac33a..6aff6027266f 100644 --- a/atom/browser/browser_linux.cc +++ b/atom/browser/browser_linux.cc @@ -60,7 +60,7 @@ bool Browser::SetBadgeCount(int count) { void Browser::SetLoginItemSettings(LoginItemSettings settings) { } -LoginItemSettings Browser::GetLoginItemSettings() { +Browser::LoginItemSettings Browser::GetLoginItemSettings() { return LoginItemSettings(); }