Skip to content

Commit 3ee697b

Browse files
trop[bot]MarshallOfSound
authored andcommitted
fix: natively implement LoginItem functions (backport: 4-0-x) (electron#15127)
* fix: natively implement some LoginItem functions * move url_ref inside loop
1 parent 6e30d85 commit 3ee697b

File tree

3 files changed

+84
-12
lines changed

3 files changed

+84
-12
lines changed

atom/browser/browser_mac.mm

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,14 +217,74 @@
217217
return settings;
218218
}
219219

220+
// copied from GetLoginItemForApp in base/mac/mac_util.mm
221+
LSSharedFileListItemRef GetLoginItemForApp() {
222+
base::ScopedCFTypeRef<LSSharedFileListRef> login_items(
223+
LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL));
224+
if (!login_items.get()) {
225+
LOG(ERROR) << "Couldn't get a Login Items list.";
226+
return NULL;
227+
}
228+
base::scoped_nsobject<NSArray> login_items_array(
229+
base::mac::CFToNSCast(LSSharedFileListCopySnapshot(login_items, NULL)));
230+
NSURL* url = [NSURL fileURLWithPath:[base::mac::MainBundle() bundlePath]];
231+
for (NSUInteger i = 0; i < [login_items_array count]; ++i) {
232+
LSSharedFileListItemRef item =
233+
reinterpret_cast<LSSharedFileListItemRef>(login_items_array[i]);
234+
CFURLRef item_url_ref = NULL;
235+
if (LSSharedFileListItemResolve(item, 0, &item_url_ref, NULL) == noErr &&
236+
item_url_ref) {
237+
base::ScopedCFTypeRef<CFURLRef> item_url(item_url_ref);
238+
if (CFEqual(item_url, url)) {
239+
CFRetain(item);
240+
return item;
241+
}
242+
}
243+
}
244+
return NULL;
245+
}
246+
247+
void RemoveFromLoginItems() {
248+
base::ScopedCFTypeRef<LSSharedFileListRef> list(
249+
LSSharedFileListCreate(NULL, kLSSharedFileListSessionLoginItems, NULL));
250+
if (!list) {
251+
LOG(ERROR) << "Unable to access shared file list";
252+
return;
253+
}
254+
255+
if (GetLoginItemForApp() != NULL) {
256+
base::scoped_nsobject<NSArray> login_items_array(
257+
base::mac::CFToNSCast(LSSharedFileListCopySnapshot(list, NULL)));
258+
259+
if (!login_items_array) {
260+
LOG(ERROR) << "No items in list of auto-loaded apps";
261+
return;
262+
}
263+
264+
for (NSUInteger i = 0; i < [login_items_array count]; ++i) {
265+
LSSharedFileListItemRef item =
266+
reinterpret_cast<LSSharedFileListItemRef>(login_items_array[i]);
267+
CFURLRef url_ref = NULL;
268+
if (LSSharedFileListItemResolve(item, 0, &url_ref, NULL) == noErr &&
269+
item) {
270+
base::ScopedCFTypeRef<CFURLRef> url(url_ref);
271+
if ([[base::mac::CFToNSCast(url.get()) path]
272+
hasPrefix:[[NSBundle mainBundle] bundlePath]])
273+
LSSharedFileListItemRemove(list, item);
274+
}
275+
}
276+
}
277+
}
278+
220279
void Browser::SetLoginItemSettings(LoginItemSettings settings) {
221280
#if defined(MAS_BUILD)
222281
platform_util::SetLoginItemEnabled(settings.open_at_login);
223282
#else
224283
if (settings.open_at_login)
225284
base::mac::AddToLoginItems(settings.open_as_hidden);
226-
else
227-
base::mac::RemoveFromLoginItems();
285+
else {
286+
RemoveFromLoginItems();
287+
}
228288
#endif
229289
}
230290

spec/api-app-spec.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,21 @@ describe('app module', () => {
466466
}, delay)
467467
})
468468

469+
it('correctly sets and unsets the LoginItem as hidden', function () {
470+
if (process.platform !== 'darwin' || process.mas) this.skip()
471+
472+
expect(app.getLoginItemSettings().openAtLogin).to.be.false()
473+
expect(app.getLoginItemSettings().openAsHidden).to.be.false()
474+
475+
app.setLoginItemSettings({ openAtLogin: true, openAsHidden: true })
476+
expect(app.getLoginItemSettings().openAtLogin).to.be.true()
477+
expect(app.getLoginItemSettings().openAsHidden).to.be.true()
478+
479+
app.setLoginItemSettings({ openAtLogin: true, openAsHidden: false })
480+
expect(app.getLoginItemSettings().openAtLogin).to.be.true()
481+
expect(app.getLoginItemSettings().openAsHidden).to.be.false()
482+
})
483+
469484
it('allows you to pass a custom executable and arguments', function () {
470485
if (process.platform !== 'win32') {
471486
// FIXME(alexeykuzmin): Skip the test.

vsts.yml

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,8 @@ jobs:
208208
# Run this job only if we are supposed to run the tests,
209209
# and we have an Electron built either in the previous job or in a different job, defined by a user.
210210
condition: and(eq(variables['RUN_TESTS'], '1'), or(succeeded(), ne(variables['Custom.UseArtifacts.BuildId'], '')))
211-
# TODO(alexeykuzmin): Run on Microsoft-hosted agents
212-
# once https://github.com/electron/electron/issues/15041 is fixed.
213-
# pool:
214-
# vmImage: 'macOS-10.13'
211+
pool:
212+
vmImage: 'macOS-10.13'
215213
timeoutInMinutes: 20
216214
variables:
217215
CI: true
@@ -269,12 +267,11 @@ jobs:
269267
destinationFolder: src/out/Default/gen
270268
timeoutInMinutes: 1
271269

272-
# TODO(alexeykuzmin): Install Node when tests are run on Microsoft-hosted agents.
273-
# - task: NodeTool@0
274-
# displayName: Install Node.js 10.x
275-
# inputs:
276-
# versionSpec: '10.x'
277-
# timeoutInMinutes: 2 # Should take less than a minute.
270+
- task: NodeTool@0
271+
displayName: Install Node.js 10.x
272+
inputs:
273+
versionSpec: '10.x'
274+
timeoutInMinutes: 2 # Should take less than a minute.
278275

279276
- bash: |
280277
cd src/electron

0 commit comments

Comments
 (0)