Skip to content

Commit

Permalink
Bug 1775132 - Part 1: Make profile service select background task pro…
Browse files Browse the repository at this point in the history
…file directory. r=mossop

This allows the profile service to handle `XRE_PROFILE_PATH`,
`--profile`, etc as normal before choosing a "default" background task
profile directory.

Differential Revision: https://phabricator.services.mozilla.com/D149918
  • Loading branch information
ncalexan committed Jul 13, 2022
1 parent 1cfd1ee commit 6e9d6bf
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 20 deletions.
9 changes: 8 additions & 1 deletion python/mozbuild/mozbuild/mach_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -1809,7 +1809,14 @@ def _run_desktop(
no_profile_option_given = all(
p not in params for p in ["-profile", "--profile", "-P"]
)
if no_profile_option_given and not noprofile:
no_backgroundtask_mode_option_given = all(
p not in params for p in ["-backgroundtask", "--backgroundtask"]
)
if (
no_profile_option_given
and no_backgroundtask_mode_option_given
and not noprofile
):
prefs = {
"browser.aboutConfig.showWarning": False,
"browser.shell.checkDefaultBrowser": False,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
* vim: sw=4 ts=4 sts=4 et
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

setupProfileService();

async function doOne(extraArgs = [], extraEnv = {}) {
let sentinel = Services.uuid.generateUUID().toString();
sentinel = sentinel.substring(1, sentinel.length - 1);

let stdoutLines = [];
let exitCode = await do_backgroundtask("unique_profile", {
extraArgs: [sentinel, ...extraArgs],
extraEnv,
onStdoutLine: line => stdoutLines.push(line),
});
Assert.equal(0, exitCode);

let infos = [];
let profiles = [];
for (let line of stdoutLines) {
if (line.includes(sentinel)) {
let info = JSON.parse(line.split(sentinel)[1]);
infos.push(info);
profiles.push(info[1]);
}
}

Assert.equal(
1,
profiles.length,
`Found 1 profile: ${JSON.stringify(profiles)}`
);

return profiles[0];
}

// Run a background task which displays its profile directory, and verify that
// the "normal profile configuration" mechanisms override the background
// task-specific default.
add_task(async function test_backgroundtask_profile_service_configuration() {
let profileService = Cc["@mozilla.org/toolkit/profile-service;1"].getService(
Ci.nsIToolkitProfileService
);

let profile = profileService.createUniqueProfile(
do_get_profile(),
"test_locked_profile"
);
let path = profile.rootDir.path;

Assert.ok(
profile.rootDir.exists(),
`Temporary profile directory does exists: ${profile.rootDir}`
);

let profileDir = await doOne(["--profile", path]);
Assert.equal(profileDir, path);

profileDir = await doOne([], { XRE_PROFILE_PATH: path });
Assert.equal(profileDir, path);
});
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ support-files =
[test_backgroundtask_locked_profile.js]
[test_backgroundtask_policies.js]
[test_backgroundtask_profile_is_slim.js]
[test_backgroundtask_profile_service_configuration.js]
[test_backgroundtask_shouldprocessupdates.js]
[test_backgroundtask_simultaneous_instances.js]
[test_backgroundtask_specific_pref.js]
Expand Down
3 changes: 3 additions & 0 deletions toolkit/profile/moz.build
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ if CONFIG["OS_ARCH"] == "WINNT":
if CONFIG["OS_TARGET"] == "Android":
UNIFIED_SOURCES += ["ProfileUnlockerAndroid.cpp"]

if CONFIG["MOZ_BACKGROUNDTASKS"]:
DEFINES["MOZ_BACKGROUNDTASKS"] = True

UNIFIED_SOURCES += ["nsToolkitProfileService.cpp"]

LOCAL_INCLUDES += [
Expand Down
34 changes: 34 additions & 0 deletions toolkit/profile/nsToolkitProfileService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
#include "nsProxyRelease.h"
#include "prinrval.h"
#include "prthread.h"
#ifdef MOZ_BACKGROUNDTASKS
# include "mozilla/BackgroundTasks.h"
#endif

using namespace mozilla;

Expand Down Expand Up @@ -1475,6 +1478,37 @@ nsresult nsToolkitProfileService::SelectStartupProfile(
return NS_ERROR_SHOW_PROFILE_MANAGER;
}

#ifdef MOZ_BACKGROUNDTASKS
if (BackgroundTasks::IsBackgroundTaskMode()) {
nsString installHash;
rv = gDirServiceProvider->GetInstallHash(installHash);
NS_ENSURE_SUCCESS(rv, rv);

nsCOMPtr<nsIFile> file;
nsresult rv = BackgroundTasks::CreateEphemeralProfileDirectory(
NS_LossyConvertUTF16toASCII(installHash), getter_AddRefs(file));
if (NS_WARN_IF(NS_FAILED(rv))) {
// In background task mode, NS_ERROR_UNEXPECTED is handled specially to
// exit with a non-zero exit code.
return NS_ERROR_UNEXPECTED;
}

// We don't expect a matching profile, but just in case.
GetProfileByDir(file, nullptr, getter_AddRefs(mCurrent));

// Background task mode does not enable legacy telemetry, so this is for
// completeness only.
mStartupReason = u"backgroundtask"_ns;

nsCOMPtr<nsIFile> localDir = file;
file.forget(aRootDir);
localDir.forget(aLocalDir);

NS_IF_ADDREF(*aProfile = mCurrent);
return NS_OK;
}
#endif

if (mIsFirstRun && mUseDedicatedProfile &&
!mInstallSection.Equals(mLegacyInstallSection)) {
// The default profile could be assigned to a hash generated from an
Expand Down
19 changes: 0 additions & 19 deletions toolkit/xre/nsAppRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4873,25 +4873,6 @@ int XREMain::XRE_mainStartup(bool* aExitFlag) {
return 0;
}

#ifdef MOZ_BACKGROUNDTASKS
if (BackgroundTasks::IsBackgroundTaskMode()) {
// Allow tests to specify profile path via the environment.
if (!EnvHasValue("XRE_PROFILE_PATH")) {
nsString installHash;
mDirProvider.GetInstallHash(installHash);

nsCOMPtr<nsIFile> file;
nsresult rv = BackgroundTasks::CreateEphemeralProfileDirectory(
NS_LossyConvertUTF16toASCII(installHash), getter_AddRefs(file));
if (NS_WARN_IF(NS_FAILED(rv))) {
return 1;
}

SaveFileToEnv("XRE_PROFILE_PATH", file);
}
}
#endif

mProfileSvc = NS_GetToolkitProfileService();
if (!mProfileSvc) {
// We failed to choose or create profile - notify user and quit
Expand Down

0 comments on commit 6e9d6bf

Please sign in to comment.