Merge pull request #6444 from electron/windows-login-items

Implement login item API on Windows
This commit is contained in:
Kevin Sawicki 2016-07-12 10:26:02 -07:00 committed by GitHub
commit d842604d16
6 changed files with 58 additions and 21 deletions

View file

@ -72,7 +72,6 @@ struct Converter<Browser::UserTask> {
}; };
#endif #endif
#if defined(OS_MACOSX)
template<> template<>
struct Converter<Browser::LoginItemSettings> { struct Converter<Browser::LoginItemSettings> {
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val,
@ -97,8 +96,6 @@ struct Converter<Browser::LoginItemSettings> {
return dict.GetHandle(); return dict.GetHandle();
} }
}; };
#endif
} // namespace mate } // namespace mate
@ -559,6 +556,10 @@ void App::BuildPrototype(
base::Bind(&Browser::RemoveAsDefaultProtocolClient, browser)) base::Bind(&Browser::RemoveAsDefaultProtocolClient, browser))
.SetMethod("setBadgeCount", base::Bind(&Browser::SetBadgeCount, browser)) .SetMethod("setBadgeCount", base::Bind(&Browser::SetBadgeCount, browser))
.SetMethod("getBadgeCount", base::Bind(&Browser::GetBadgeCount, 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) #if defined(OS_MACOSX)
.SetMethod("hide", base::Bind(&Browser::Hide, browser)) .SetMethod("hide", base::Bind(&Browser::Hide, browser))
.SetMethod("show", base::Bind(&Browser::Show, browser)) .SetMethod("show", base::Bind(&Browser::Show, browser))
@ -566,10 +567,6 @@ void App::BuildPrototype(
base::Bind(&Browser::SetUserActivity, browser)) base::Bind(&Browser::SetUserActivity, browser))
.SetMethod("getCurrentActivityType", .SetMethod("getCurrentActivityType",
base::Bind(&Browser::GetCurrentActivityType, browser)) base::Bind(&Browser::GetCurrentActivityType, browser))
.SetMethod("getLoginItemSettings",
base::Bind(&Browser::GetLoginItemSettings, browser))
.SetMethod("setLoginItemSettings",
base::Bind(&Browser::SetLoginItemSettings, browser))
#endif #endif
#if defined(OS_WIN) #if defined(OS_WIN)
.SetMethod("setUserTasks", base::Bind(&Browser::SetUserTasks, browser)) .SetMethod("setUserTasks", base::Bind(&Browser::SetUserTasks, browser))

View file

@ -88,7 +88,7 @@ class Browser : public WindowListObserver {
bool SetBadgeCount(int count); bool SetBadgeCount(int count);
int GetBadgeCount(); int GetBadgeCount();
#if defined(OS_MACOSX) // Set/Get the login item settings of the app
struct LoginItemSettings { struct LoginItemSettings {
bool open_at_login = false; bool open_at_login = false;
bool open_as_hidden = false; bool open_as_hidden = false;
@ -96,7 +96,10 @@ class Browser : public WindowListObserver {
bool opened_at_login = false; bool opened_at_login = false;
bool opened_as_hidden = false; bool opened_as_hidden = false;
}; };
void SetLoginItemSettings(LoginItemSettings settings);
LoginItemSettings GetLoginItemSettings();
#if defined(OS_MACOSX)
// Hide the application. // Hide the application.
void Hide(); void Hide();
@ -139,12 +142,6 @@ class Browser : public WindowListObserver {
// Set docks' icon. // Set docks' icon.
void DockSetIcon(const gfx::Image& image); 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) #endif // defined(OS_MACOSX)
#if defined(OS_WIN) #if defined(OS_WIN)

View file

@ -57,6 +57,13 @@ bool Browser::SetBadgeCount(int count) {
} }
} }
void Browser::SetLoginItemSettings(LoginItemSettings settings) {
}
Browser::LoginItemSettings Browser::GetLoginItemSettings() {
return LoginItemSettings();
}
std::string Browser::GetExecutableFileVersion() const { std::string Browser::GetExecutableFileVersion() const {
return brightray::GetApplicationVersion(); return brightray::GetApplicationVersion();
} }

View file

@ -273,6 +273,39 @@ bool Browser::SetBadgeCount(int count) {
return false; return false;
} }
void Browser::SetLoginItemSettings(LoginItemSettings settings) {
std::wstring 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());
key.WriteValue(GetAppUserModelID(), exePath.c_str());
}
} else {
key.DeleteValue(GetAppUserModelID());
}
}
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;
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;
}
}
return settings;
}
PCWSTR Browser::GetAppUserModelID() { PCWSTR Browser::GetAppUserModelID() {
if (app_user_model_id_.empty()) { if (app_user_model_id_.empty()) {
SetAppUserModelID(base::ReplaceStringPlaceholders( SetAppUserModelID(base::ReplaceStringPlaceholders(

View file

@ -611,22 +611,24 @@ Returns the current value displayed in the counter badge.
Returns whether current desktop environment is Unity launcher. 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. Return an Object with the login item settings of the app.
* `openAtLogin` Boolean - `true` if the app is set to open at login. * `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. * `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 * `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 * `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. 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 * `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 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 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 * `settings` Object
* `openAtLogin` Boolean - `true` to open the app at login, `false` to remove * `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 * `openAsHidden` Boolean - `true` to open the app as hidden. Defaults to
`false`. The user can edit this setting from the System Preferences so `false`. The user can edit this setting from the System Preferences so
`app.getLoginItemStatus().wasOpenedAsHidden` should be checked when the app `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. Set the app's login item settings.

View file

@ -300,7 +300,7 @@ describe('app module', function () {
}) })
describe('app.get/setLoginItemSettings API', function () { describe('app.get/setLoginItemSettings API', function () {
if (process.platform !== 'darwin') return if (process.platform === 'linux') return
beforeEach(function () { beforeEach(function () {
app.setLoginItemSettings({openAtLogin: false}) app.setLoginItemSettings({openAtLogin: false})
@ -323,7 +323,7 @@ describe('app module', function () {
app.setLoginItemSettings({openAtLogin: true, openAsHidden: true}) app.setLoginItemSettings({openAtLogin: true, openAsHidden: true})
assert.deepEqual(app.getLoginItemSettings(), { assert.deepEqual(app.getLoginItemSettings(), {
openAtLogin: true, openAtLogin: true,
openAsHidden: true, openAsHidden: process.platform === 'darwin', // Only available on macOS
wasOpenedAtLogin: false, wasOpenedAtLogin: false,
wasOpenedAsHidden: false, wasOpenedAsHidden: false,
restoreState: false restoreState: false