refactor: simplify RemoveFromLoginItems (#19179)

This commit is contained in:
Jeremy Apthorp 2019-07-15 14:36:23 -07:00 committed by Shelley Vohr
parent 50f4977048
commit 4439249617

View file

@ -222,13 +222,14 @@ Browser::LoginItemSettings Browser::GetLoginItemSettings(
return settings; return settings;
} }
// copied from GetLoginItemForApp in base/mac/mac_util.mm void RemoveFromLoginItems() {
LSSharedFileListItemRef GetLoginItemForApp() { // logic to find the login item copied from GetLoginItemForApp in
// base/mac/mac_util.mm
base::ScopedCFTypeRef<LSSharedFileListRef> login_items( base::ScopedCFTypeRef<LSSharedFileListRef> login_items(
LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL)); LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL));
if (!login_items.get()) { if (!login_items.get()) {
LOG(ERROR) << "Couldn't get a Login Items list."; LOG(ERROR) << "Couldn't get a Login Items list.";
return NULL; return;
} }
base::scoped_nsobject<NSArray> login_items_array( base::scoped_nsobject<NSArray> login_items_array(
base::mac::CFToNSCast(LSSharedFileListCopySnapshot(login_items, NULL))); base::mac::CFToNSCast(LSSharedFileListCopySnapshot(login_items, NULL)));
@ -242,43 +243,9 @@ LSSharedFileListItemRef GetLoginItemForApp() {
if (!error && item_url_ref) { if (!error && item_url_ref) {
base::ScopedCFTypeRef<CFURLRef> item_url(item_url_ref); base::ScopedCFTypeRef<CFURLRef> item_url(item_url_ref);
if (CFEqual(item_url, url)) { if (CFEqual(item_url, url)) {
CFRetain(item); LSSharedFileListItemRemove(login_items, item);
return item;
}
}
}
return NULL;
}
void RemoveFromLoginItems() {
base::ScopedCFTypeRef<LSSharedFileListRef> list(
LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL));
if (!list) {
LOG(ERROR) << "Unable to access shared file list";
return; return;
} }
if (GetLoginItemForApp() != NULL) {
base::scoped_nsobject<NSArray> login_items_array(
base::mac::CFToNSCast(LSSharedFileListCopySnapshot(list, NULL)));
if (!login_items_array) {
LOG(ERROR) << "No items in list of auto-loaded apps";
return;
}
for (NSUInteger i = 0; i < [login_items_array count]; ++i) {
LSSharedFileListItemRef item =
reinterpret_cast<LSSharedFileListItemRef>(login_items_array[i]);
base::ScopedCFTypeRef<CFErrorRef> error;
CFURLRef url_ref =
LSSharedFileListItemCopyResolvedURL(item, 0, error.InitializeInto());
if (!error && url_ref) {
base::ScopedCFTypeRef<CFURLRef> url(url_ref);
if ([[base::mac::CFToNSCast(url.get()) path]
hasPrefix:[[NSBundle mainBundle] bundlePath]])
LSSharedFileListItemRemove(list, item);
}
} }
} }
} }