Skip to content

Commit

Permalink
fix: create userData on requestSingleInstanceLock() if needed (#33559
Browse files Browse the repository at this point in the history
…) (#33592)

* test: use custom userData folder for requestSingleInstanceLock()

* update test

* prefix test folder path

* fix: create userDataDir on requestSingleInstanceLock() if needed

* Trigger Build

Co-authored-by: Micha Hanselmann <micha.hanselmann@gmail.com>
  • Loading branch information
trop[bot] and deermichel committed Apr 4, 2022
1 parent c85bc3c commit 769088f
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
2 changes: 2 additions & 0 deletions shell/browser/api/electron_api_app.cc
Expand Up @@ -1148,6 +1148,8 @@ bool App::RequestSingleInstanceLock(gin::Arguments* args) {

base::FilePath user_dir;
base::PathService::Get(chrome::DIR_USER_DATA, &user_dir);
// The user_dir may not have been created yet.
base::CreateDirectoryAndGetError(user_dir, nullptr);

auto cb = base::BindRepeating(&App::OnSecondInstance, base::Unretained(this));
auto wrapped_cb = base::BindRepeating(NotificationCallbackWrapper, cb);
Expand Down
7 changes: 7 additions & 0 deletions spec-main/api-app-spec.ts
Expand Up @@ -228,6 +228,13 @@ describe('app module', () => {
expect(code1).to.equal(0);
});

it('returns true when setting non-existent user data folder', async function () {
const appPath = path.join(fixturesPath, 'api', 'singleton-userdata');
const instance = cp.spawn(process.execPath, [appPath]);
const [code] = await emittedOnce(instance, 'exit');
expect(code).to.equal(0);
});

async function testArgumentPassing (testArgs: SingleInstanceLockTestArgs) {
const appPath = path.join(fixturesPath, 'api', 'singleton-data');
const first = cp.spawn(process.execPath, [appPath, ...testArgs.args]);
Expand Down
12 changes: 12 additions & 0 deletions spec/fixtures/api/singleton-userdata/main.js
@@ -0,0 +1,12 @@
const { app } = require('electron');
const fs = require('fs');
const path = require('path');

// non-existent user data folder should not break requestSingleInstanceLock()
// ref: https://github.com/electron/electron/issues/33547
const userDataFolder = path.join(app.getPath('home'), 'electron-test-singleton-userdata');
fs.rmSync(userDataFolder, { force: true, recursive: true });
app.setPath('userData', userDataFolder);

const gotTheLock = app.requestSingleInstanceLock();
app.exit(gotTheLock ? 0 : 1);
4 changes: 4 additions & 0 deletions spec/fixtures/api/singleton-userdata/package.json
@@ -0,0 +1,4 @@
{
"name": "electron-test-singleton-userdata",
"main": "main.js"
}

0 comments on commit 769088f

Please sign in to comment.