feat: update app.{set|get}LoginItemSettings(settings)
(#37244)
* feat: update app.{set|get}LoginItemSettings(settings) * test: fixup and add tests * docs: add type link * chore: name -> serviceName
This commit is contained in:
parent
6d0d350e13
commit
f7b1c75c72
7 changed files with 321 additions and 89 deletions
|
@ -8,6 +8,8 @@
|
|||
#include <string>
|
||||
#include <utility>
|
||||
|
||||
#import <ServiceManagement/ServiceManagement.h>
|
||||
|
||||
#include "base/apple/bridging.h"
|
||||
#include "base/apple/bundle_locations.h"
|
||||
#include "base/apple/scoped_cftyperef.h"
|
||||
|
@ -19,6 +21,7 @@
|
|||
#include "chrome/browser/browser_process.h"
|
||||
#include "net/base/mac/url_conversions.h"
|
||||
#include "shell/browser/badging/badge_manager.h"
|
||||
#include "shell/browser/javascript_environment.h"
|
||||
#include "shell/browser/mac/dict_util.h"
|
||||
#include "shell/browser/mac/electron_application.h"
|
||||
#include "shell/browser/mac/electron_application_delegate.h"
|
||||
|
@ -85,6 +88,15 @@ bool CheckLoginItemStatus(bool* is_hidden) {
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
Browser::LoginItemSettings GetLoginItemSettingsDeprecated() {
|
||||
Browser::LoginItemSettings settings;
|
||||
settings.open_at_login = CheckLoginItemStatus(&settings.open_as_hidden);
|
||||
settings.restore_state = base::mac::WasLaunchedAsLoginItemRestoreState();
|
||||
settings.opened_at_login = base::mac::WasLaunchedAsLoginOrResumeItem();
|
||||
settings.opened_as_hidden = base::mac::WasLaunchedAsHiddenLoginItem();
|
||||
return settings;
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace
|
||||
|
@ -367,28 +379,71 @@ void Browser::ApplyForcedRTL() {
|
|||
Browser::LoginItemSettings Browser::GetLoginItemSettings(
|
||||
const LoginItemSettings& options) {
|
||||
LoginItemSettings settings;
|
||||
if (options.type != "mainAppService" && options.service_name.empty()) {
|
||||
gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
|
||||
.ThrowTypeError("'name' is required when type is not mainAppService");
|
||||
return settings;
|
||||
}
|
||||
|
||||
#if IS_MAS_BUILD()
|
||||
settings.open_at_login = platform_util::GetLoginItemEnabled();
|
||||
const std::string status =
|
||||
platform_util::GetLoginItemEnabled(options.type, options.service_name);
|
||||
settings.open_at_login =
|
||||
status == "enabled" || status == "enabled-deprecated";
|
||||
if (@available(macOS 13, *))
|
||||
settings.status = status;
|
||||
#else
|
||||
settings.open_at_login = CheckLoginItemStatus(&settings.open_as_hidden);
|
||||
settings.restore_state = base::mac::WasLaunchedAsLoginItemRestoreState();
|
||||
settings.opened_at_login = base::mac::WasLaunchedAsLoginOrResumeItem();
|
||||
settings.opened_as_hidden = base::mac::WasLaunchedAsHiddenLoginItem();
|
||||
// If the app was previously set as a LoginItem with the deprecated API,
|
||||
// we should report its LoginItemSettings via the old API.
|
||||
LoginItemSettings settings_deprecated = GetLoginItemSettingsDeprecated();
|
||||
if (@available(macOS 13, *)) {
|
||||
const std::string status =
|
||||
platform_util::GetLoginItemEnabled(options.type, options.service_name);
|
||||
if (status == "enabled-deprecated") {
|
||||
settings = settings_deprecated;
|
||||
} else {
|
||||
settings.open_at_login = status == "enabled";
|
||||
settings.status = status;
|
||||
}
|
||||
} else {
|
||||
settings = settings_deprecated;
|
||||
}
|
||||
#endif
|
||||
return settings;
|
||||
}
|
||||
|
||||
void Browser::SetLoginItemSettings(LoginItemSettings settings) {
|
||||
#if IS_MAS_BUILD()
|
||||
if (!platform_util::SetLoginItemEnabled(settings.open_at_login)) {
|
||||
LOG(ERROR) << "Unable to set login item enabled on sandboxed app.";
|
||||
if (settings.type != "mainAppService" && settings.service_name.empty()) {
|
||||
gin_helper::ErrorThrower(JavascriptEnvironment::GetIsolate())
|
||||
.ThrowTypeError("'name' is required when type is not mainAppService");
|
||||
return;
|
||||
}
|
||||
#if IS_MAS_BUILD()
|
||||
platform_util::SetLoginItemEnabled(settings.type, settings.service_name,
|
||||
settings.open_at_login);
|
||||
#else
|
||||
if (settings.open_at_login) {
|
||||
base::mac::AddToLoginItems(base::apple::MainBundlePath(),
|
||||
settings.open_as_hidden);
|
||||
const base::FilePath bundle_path = base::apple::MainBundlePath();
|
||||
if (@available(macOS 13, *)) {
|
||||
// If the app was previously set as a LoginItem with the old API, remove it
|
||||
// as a LoginItem via the old API before re-enabling with the new API.
|
||||
const std::string status =
|
||||
platform_util::GetLoginItemEnabled("mainAppService", "");
|
||||
if (status == "enabled-deprecated") {
|
||||
base::mac::RemoveFromLoginItems(bundle_path);
|
||||
if (settings.open_at_login) {
|
||||
platform_util::SetLoginItemEnabled(settings.type, settings.service_name,
|
||||
settings.open_at_login);
|
||||
}
|
||||
} else {
|
||||
platform_util::SetLoginItemEnabled(settings.type, settings.service_name,
|
||||
settings.open_at_login);
|
||||
}
|
||||
} else {
|
||||
base::mac::RemoveFromLoginItems(base::apple::MainBundlePath());
|
||||
if (settings.open_at_login) {
|
||||
base::mac::AddToLoginItems(bundle_path, settings.open_as_hidden);
|
||||
} else {
|
||||
base::mac::RemoveFromLoginItems(bundle_path);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue