refactor: prevent RemoveFromLoginItems() from mounting volumes from login items (#34068)

This commit is contained in:
Shelley Vohr 2022-05-06 02:46:36 +02:00 committed by GitHub
parent d8a7219d07
commit a401360057
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -318,26 +318,32 @@ Browser::LoginItemSettings Browser::GetLoginItemSettings(
return settings; return settings;
} }
// Some logic here copied from GetLoginItemForApp in base/mac/mac_util.mm
void RemoveFromLoginItems() { void RemoveFromLoginItems() {
#pragma clang diagnostic push // https://crbug.com/1154377 #pragma clang diagnostic push // https://crbug.com/1154377
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
// 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; 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)));
NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]]; NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]];
for (NSUInteger i = 0; i < [login_items_array count]; ++i) { for (id login_item in login_items_array.get()) {
LSSharedFileListItemRef item = LSSharedFileListItemRef item =
reinterpret_cast<LSSharedFileListItemRef>(login_items_array[i]); reinterpret_cast<LSSharedFileListItemRef>(login_item);
// kLSSharedFileListDoNotMountVolumes is used so that we don't trigger
// mounting when it's not expected by a user. Just listing the login
// items should not cause any side-effects.
base::ScopedCFTypeRef<CFErrorRef> error; base::ScopedCFTypeRef<CFErrorRef> error;
CFURLRef item_url_ref = base::ScopedCFTypeRef<CFURLRef> item_url_ref(
LSSharedFileListItemCopyResolvedURL(item, 0, error.InitializeInto()); LSSharedFileListItemCopyResolvedURL(
item, kLSSharedFileListDoNotMountVolumes, error.InitializeInto()));
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)) {