Browse Source

Add path and args to LoginItemSettings struct

Kevin Sawicki 8 years ago
parent
commit
e2501a80e4

+ 9 - 3
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<Browser::LoginItemSettings> {
 
     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)

+ 2 - 0
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);

+ 4 - 2
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<base::string16> 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.

+ 2 - 3
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();
 }
 

+ 3 - 3
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

+ 28 - 21
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<base::string16>& 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<base::string16> 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<base::string16> 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<base::string16> 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<base::string16> 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;
     }
   }

+ 16 - 16
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_
 

+ 3 - 3
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)
     })
   })