diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index e0c5f7e92b61..99cd5ffa8b14 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -12,7 +12,6 @@ #include "atom/browser/api/atom_api_web_contents.h" #include "atom/browser/atom_browser_context.h" #include "atom/browser/atom_browser_main_parts.h" -#include "atom/browser/browser.h" #include "atom/browser/login_handler.h" #include "atom/browser/relauncher.h" #include "atom/common/atom_command_line.h" @@ -298,6 +297,8 @@ struct Converter { dict.Get("openAtLogin", &(out->open_at_login)); dict.Get("openAsHidden", &(out->open_as_hidden)); + dict.Get("path", &(out->path)); + dict.Get("args", &(out->args)); return true; } @@ -746,6 +747,12 @@ bool App::IsAccessibilitySupportEnabled() { return ax_state->IsAccessibleBrowser(); } +Browser::LoginItemSettings App::GetLoginItemSettings(mate::Arguments* args) { + Browser::LoginItemSettings options; + args->GetNext(&options); + return Browser::Get()->GetLoginItemSettings(options); +} + #if defined(USE_NSS_CERTS) void App::ImportCertificate( const base::DictionaryValue& options, @@ -867,8 +874,7 @@ 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("getLoginItemSettings", &App::GetLoginItemSettings) .SetMethod("setLoginItemSettings", base::Bind(&Browser::SetLoginItemSettings, browser)) #if defined(OS_MACOSX) diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index c72d3c1998ed..2bd977f46b05 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -10,6 +10,7 @@ #include "atom/browser/api/event_emitter.h" #include "atom/browser/atom_browser_client.h" +#include "atom/browser/browser.h" #include "atom/browser/browser_observer.h" #include "atom/common/native_mate_converters/callback.h" #include "chrome/browser/process_singleton.h" @@ -123,6 +124,7 @@ class App : public AtomBrowserClient::Delegate, bool Relaunch(mate::Arguments* args); void DisableHardwareAcceleration(mate::Arguments* args); bool IsAccessibilitySupportEnabled(); + Browser::LoginItemSettings GetLoginItemSettings(mate::Arguments* args); #if defined(USE_NSS_CERTS) void ImportCertificate(const base::DictionaryValue& options, const net::CompletionCallback& callback); diff --git a/atom/browser/browser.h b/atom/browser/browser.h index 400a48f5eb50..28103a99c704 100644 --- a/atom/browser/browser.h +++ b/atom/browser/browser.h @@ -98,9 +98,11 @@ class Browser : public WindowListObserver { bool restore_state = false; bool opened_at_login = false; bool opened_as_hidden = false; + base::string16 path; + std::vector args; }; - void SetLoginItemSettings(LoginItemSettings settings, mate::Arguments* args); - LoginItemSettings GetLoginItemSettings(mate::Arguments* args); + void SetLoginItemSettings(LoginItemSettings settings); + LoginItemSettings GetLoginItemSettings(LoginItemSettings options); #if defined(OS_MACOSX) // Hide the application. diff --git a/atom/browser/browser_linux.cc b/atom/browser/browser_linux.cc index 5e4ae8a42ecd..1adf992aec13 100644 --- a/atom/browser/browser_linux.cc +++ b/atom/browser/browser_linux.cc @@ -60,12 +60,11 @@ bool Browser::SetBadgeCount(int count) { } } -void Browser::SetLoginItemSettings(LoginItemSettings settings, - mate::Arguments* args) { +void Browser::SetLoginItemSettings(LoginItemSettings settings) { } Browser::LoginItemSettings Browser::GetLoginItemSettings( - mate::Arguments* args) { + LoginItemSettings options) { return LoginItemSettings(); } diff --git a/atom/browser/browser_mac.mm b/atom/browser/browser_mac.mm index 946a45ab7623..afb291bc3b33 100644 --- a/atom/browser/browser_mac.mm +++ b/atom/browser/browser_mac.mm @@ -152,7 +152,8 @@ bool Browser::ContinueUserActivity(const std::string& type, return prevent_default; } -Browser::LoginItemSettings Browser::GetLoginItemSettings(mate::Arguments* args) { +Browser::LoginItemSettings Browser::GetLoginItemSettings( + LoginItemSettings options) { LoginItemSettings settings; settings.open_at_login = base::mac::CheckLoginItemStatus( &settings.open_as_hidden); @@ -162,8 +163,7 @@ Browser::LoginItemSettings Browser::GetLoginItemSettings(mate::Arguments* args) return settings; } -void Browser::SetLoginItemSettings(LoginItemSettings settings, - mate::Arguments* args) { +void Browser::SetLoginItemSettings(LoginItemSettings settings) { if (settings.open_at_login) base::mac::AddToLoginItems(settings.open_as_hidden); else diff --git a/atom/browser/browser_win.cc b/atom/browser/browser_win.cc index 5daf40220e67..27a099b892ae 100644 --- a/atom/browser/browser_win.cc +++ b/atom/browser/browser_win.cc @@ -43,11 +43,11 @@ BOOL CALLBACK WindowsEnumerationHandler(HWND hwnd, LPARAM param) { return TRUE; } -bool ReadAppCommandLine(mate::Arguments* args, - base::string16* exe, +bool ReadAppCommandLine(base::string16* exe, + const std::vector& launch_args, bool includeOriginalArg) { // Executable Path - if (!args->GetNext(exe)) { + if (exe->empty()) { base::FilePath path; if (!PathService::Get(base::FILE_EXE, &path)) { LOG(ERROR) << "Error getting app exe path"; @@ -56,21 +56,20 @@ bool ReadAppCommandLine(mate::Arguments* args, *exe = path.value(); } - // Read in optional args arg - std::vector launch_args; - if (args->GetNext(&launch_args) && !launch_args.empty()) { + if (launch_args.empty()) { + base::string16 formatString = includeOriginalArg ? + L"\"%s\" \"%%1\"" : + L"\"%s\""; + *exe = base::StringPrintf(formatString.c_str(), exe->c_str()); + } else { base::string16 formatString = includeOriginalArg ? L"\"%s\" %s \"%%1\"" : L"\"%s\" %s"; *exe = base::StringPrintf(formatString.c_str(), exe->c_str(), base::JoinString(launch_args, L" ").c_str()); - } else { - base::string16 formatString = includeOriginalArg ? - L"\"%s\" \"%%1\"" : - L"\"%s\""; - *exe = base::StringPrintf(formatString.c_str(), exe->c_str()); } + return true; } @@ -164,7 +163,10 @@ bool Browser::RemoveAsDefaultProtocolClient(const std::string& protocol, return true; base::string16 exe; - if (!ReadAppCommandLine(args, &exe, true)) + std::vector launch_args; + args->GetNext(&exe); + args->GetNext(&launch_args); + if (!ReadAppCommandLine(&exe, launch_args, true)) return false; if (keyVal == exe) { @@ -198,7 +200,10 @@ bool Browser::SetAsDefaultProtocolClient(const std::string& protocol, return false; base::string16 exe; - if (!ReadAppCommandLine(args, &exe, true)) + std::vector launch_args; + args->GetNext(&exe); + args->GetNext(&launch_args); + if (!ReadAppCommandLine(&exe, launch_args, true)) return false; // Main Registry Key @@ -228,7 +233,10 @@ bool Browser::IsDefaultProtocolClient(const std::string& protocol, return false; base::string16 exe; - if (!ReadAppCommandLine(args, &exe, true)) + std::vector launch_args; + args->GetNext(&exe); + args->GetNext(&launch_args); + if (!ReadAppCommandLine(&exe, launch_args, true)) return false; // Main Registry Key @@ -261,14 +269,13 @@ bool Browser::SetBadgeCount(int count) { return false; } -void Browser::SetLoginItemSettings(LoginItemSettings settings, - mate::Arguments* args) { +void Browser::SetLoginItemSettings(LoginItemSettings settings) { 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::string16 exe; - if (!ReadAppCommandLine(args, &exe, false)) return; + base::string16 exe = settings.path; + if (!ReadAppCommandLine(&exe, settings.args, false)) return; key.WriteValue(GetAppUserModelID(), exe.c_str()); } else { @@ -277,15 +284,15 @@ void Browser::SetLoginItemSettings(LoginItemSettings settings, } Browser::LoginItemSettings Browser::GetLoginItemSettings( - mate::Arguments* args) { + LoginItemSettings options) { LoginItemSettings settings; base::string16 keyPath = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run"; base::win::RegKey key(HKEY_CURRENT_USER, keyPath.c_str(), KEY_ALL_ACCESS); base::string16 keyVal; if (!FAILED(key.ReadValue(GetAppUserModelID(), &keyVal))) { - base::string16 exe; - if (ReadAppCommandLine(args, &exe, false)) { + base::string16 exe = options.path; + if (ReadAppCommandLine(&exe, options.args, false)) { settings.open_at_login = keyVal == exe; } } diff --git a/docs/api/app.md b/docs/api/app.md index f88bc4c64759..f788a13cb62f 100644 --- a/docs/api/app.md +++ b/docs/api/app.md @@ -758,15 +758,16 @@ Returns `Integer` - The current value displayed in the counter badge. Returns `Boolean` - Whether the current desktop environment is Unity launcher. -### `app.getLoginItemSettings([path, args])` _macOS_ _Windows_ +### `app.getLoginItemSettings([options])` _macOS_ _Windows_ -* `path` String (optional) _Windows_ - The executable path to compare against. - Defaults to `process.execPath`. -* `args` String[] (optional) _Windows_ - The command-line arguments to compare - against. Defaults to an empty array. +* `options` Object (optional) + * `path` String (optional) _Windows_ - The executable path to compare against. + Defaults to `process.execPath`. + * `args` String[] (optional) _Windows_ - The command-line arguments to compare + against. Defaults to an empty array. -If you provided arguments to `app.setLoginItemSettings` you need to pass the -same arguments here for `openAtLogin` to be set correctly. +If you provided `path` and `argg` potions to `app.setLoginItemSettings` then you +need to pass the same arguments here for `openAtLogin` to be set correctly. Returns `Object`: @@ -783,8 +784,7 @@ Returns `Object`: app should restore the windows that were open the last time the app was closed. This setting is only supported on macOS. -**Note:** This API has no effect on -[MAS builds][mas-builds]. +**Note:** This API has no effect on [MAS builds][mas-builds]. ### `app.setLoginItemSettings(settings[, path, args])` _macOS_ _Windows_ @@ -796,15 +796,16 @@ Returns `Object`: `app.getLoginItemStatus().wasOpenedAsHidden` should be checked when the app is opened to know the current value. This setting is only supported on macOS. -* `path` String (optional) _Windows_ - The executable to launch at login. - Defaults to `process.execPath`. -* `args` String[] (optional) _Windows_ - The command-line arguments to pass to the - executable. Defaults to an empty array. Take care to wrap paths in quotes. + * `path` String (optional) _Windows_ - The executable to launch at login. + Defaults to `process.execPath`. + * `args` String[] (optional) _Windows_ - The command-line arguments to pass to + the executable. Defaults to an empty array. Take care to wrap paths in + quotes. Set the app's login item settings. To work with Electron's `autoUpdater` on Windows, which uses [Squirrel](Squirrel-Windows), -you'll want to set the launch path to Update.exe, and pass arguments that specify your +you'll want to set the launch path to Update.exe, and pass arguments that specify your application name. For example: ``` javascript @@ -818,8 +819,7 @@ app.setLoginItemSettings({openAtLogin: true}, updateExe, [ ]) ``` -**Note:** This API has no effect on -[MAS builds][mas-builds]. +**Note:** This API has no effect on [MAS builds][mas-builds]. ### `app.isAccessibilitySupportEnabled()` _macOS_ _Windows_ diff --git a/spec/api-app-spec.js b/spec/api-app-spec.js index 82210a20a6a3..53d8235f7b44 100644 --- a/spec/api-app-spec.js +++ b/spec/api-app-spec.js @@ -362,10 +362,10 @@ describe('app module', function () { '--process-start-args', `"--hidden"` ] - app.setLoginItemSettings({openAtLogin: true}, updateExe, processStartArgs) + app.setLoginItemSettings({openAtLogin: true, path: updateExe, args: processStartArgs}) - assert(!app.getLoginItemSettings().openAtLogin) - assert(app.getLoginItemSettings(updateExe, processStartArgs)) + assert.equal(app.getLoginItemSettings().openAtLogin, false) + assert.equal(app.getLoginItemSettings({path: updateExe, args: processStartArgs}).openAtLogin, true) }) })