Implement login helper to manage login item in Mac App Store build

This commit is contained in:
Ted Kim 2017-10-17 16:28:29 +09:00
parent 306b627090
commit 4119da607f
9 changed files with 123 additions and 17 deletions

View file

@ -6,6 +6,7 @@
#import <Carbon/Carbon.h>
#import <Cocoa/Cocoa.h>
#import <ServiceManagement/ServiceManagement.h>
#include "base/callback.h"
#include "base/files/file_path.h"
@ -98,6 +99,10 @@ std::string OpenURL(NSURL* ns_url, bool activate) {
return "";
}
NSString* GetLoginHelperBundleIdentifier() {
return [[[NSBundle mainBundle] bundleIdentifier] stringByAppendingString:@".loginhelper"];
}
} // namespace
namespace platform_util {
@ -177,4 +182,26 @@ void Beep() {
NSBeep();
}
bool GetLoginItemEnabled() {
BOOL enabled = NO;
// SMJobCopyDictionary does not work in sandbox (see rdar://13626319)
CFArrayRef jobs = SMCopyAllJobDictionaries(kSMDomainUserLaunchd);
NSArray* jobs_ = CFBridgingRelease(jobs);
NSString* identifier = GetLoginHelperBundleIdentifier();
if (jobs_ && [jobs_ count] > 0) {
for (NSDictionary* job in jobs_) {
if ([identifier isEqualToString:[job objectForKey:@"Label"]]) {
enabled = [[job objectForKey:@"OnDemand"] boolValue];
break;
}
}
}
return enabled;
}
void SetLoginItemEnabled(bool enabled) {
NSString* identifier = GetLoginHelperBundleIdentifier();
SMLoginItemSetEnabled((__bridge CFStringRef) identifier, enabled);
}
} // namespace platform_util