Skip to content

Commit 03ff66c

Browse files
committed
Bug 1667276 - Part 3: Load a custom prefs file when running a background task. r=mossop,KrisWright
There are some complications here to handle unpackaged and packaged builds. In addition, there could be a difference between App prefs and GRE prefs. Since the underlying backgroundtasks code is built as part of Gecko (i.e., `toolkit/...` rather than `browser/...`) I have favoured GRE prefs. I think, however, that what is written will work for App-specific prefs, but I'm not concerned with that detail at this time. This also add tests for backgroundtask-specific prefs, which are structured as both xpcshell and mochitest-chrome tests because locally, the former tests unpackaged builds and the latter can accommodate testing packaged builds. We could use mochitest-chrome for both, but this has been pleasant to work with locally. Differential Revision: https://phabricator.services.mozilla.com/D97510
1 parent 9862045 commit 03ff66c

16 files changed

+238
-0
lines changed

.eslintignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,3 +220,6 @@ tools/tryselect/selectors/chooser/templates/chooser.html
220220

221221
# Ignore preprocessed *(P)refs.js files in update-packaging.
222222
tools/update-packaging/**/*refs.js
223+
224+
# Ignore backgroundtasks preferences files.
225+
toolkit/components/backgroundtasks/defaults

.prettierignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,3 +58,6 @@ devtools/client/debugger/src/test/mochitest/examples/ember/quickstart
5858

5959
# These are source mapped and the locations are asserted in the test case.
6060
devtools/client/webconsole/test/browser/test-mangled-function.*
61+
62+
# Ignore backgroundtasks preferences files.
63+
toolkit/components/backgroundtasks/defaults

browser/base/content/test/static/browser_all_files_referenced.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ if (AppConstants.platform == "macosx") {
5656
}
5757

5858
if (AppConstants.MOZ_BACKGROUNDTASKS) {
59+
// These preferences are active only when we're in background task mode.
60+
gExceptionPaths.push("resource://gre/defaults/backgroundtasks/");
5961
// `BackgroundTask_id.jsm` is loaded at runtime by `app --backgroundtask id ...`.
6062
gExceptionPaths.push("resource://gre/modules/backgroundtasks/");
6163
}

browser/installer/Makefile.in

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ ifdef MOZ_DEFAULT_BROWSER_AGENT
9898
DEFINES += -DMOZ_DEFAULT_BROWSER_AGENT=1
9999
endif
100100

101+
ifdef MOZ_BACKGROUNDTASKS
102+
DEFINES += -DMOZ_BACKGROUNDTASKS=1
103+
endif
104+
101105
ifeq (cocoa,$(MOZ_WIDGET_TOOLKIT))
102106
MOZ_PKG_MAC_DSSTORE=$(topsrcdir)/$(MOZ_BRANDING_DIRECTORY)/dsstore
103107
MOZ_PKG_MAC_BACKGROUND=$(topsrcdir)/$(MOZ_BRANDING_DIRECTORY)/background.png

browser/installer/package-manifest.in

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,12 @@
305305
; gre location for now.
306306
@RESPATH@/defaults/pref/channel-prefs.js
307307

308+
; Background tasks-specific preferences. These are in the GRE
309+
; location since they apply to all tasks at this time.
310+
#ifdef MOZ_BACKGROUNDTASKS
311+
@RESPATH@/defaults/backgroundtasks/backgroundtasks.js
312+
#endif
313+
308314
; [Layout Engine Resources]
309315
; Style Sheets, Graphics and other Resources used by the layout engine.
310316
@RESPATH@/res/EditorOverride.css

modules/libpref/Preferences.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@
8686
#include "plstr.h"
8787
#include "prlink.h"
8888
#include "xpcpublic.h"
89+
#ifdef MOZ_BACKGROUNDTASKS
90+
# include "mozilla/BackgroundTasks.h"
91+
#endif
8992

9093
#ifdef DEBUG
9194
# include <map>
@@ -4530,6 +4533,13 @@ nsresult Preferences::InitInitialObjects(bool aIsStartup) {
45304533
rv = pref_ReadDefaultPrefs(jarReader, "defaults/pref/*.js$");
45314534
NS_ENSURE_SUCCESS(rv, rv);
45324535

4536+
#ifdef MOZ_BACKGROUNDTASKS
4537+
if (BackgroundTasks::IsBackgroundTaskMode()) {
4538+
rv = pref_ReadDefaultPrefs(jarReader, "defaults/backgroundtasks/*.js$");
4539+
NS_ENSURE_SUCCESS(rv, rv);
4540+
}
4541+
#endif
4542+
45334543
#ifdef MOZ_WIDGET_ANDROID
45344544
// Load jar:$gre/omni.jar!/defaults/pref/$MOZ_ANDROID_CPU_ABI/*.js.
45354545
nsAutoCString path;
@@ -4609,6 +4619,25 @@ nsresult Preferences::InitInitialObjects(bool aIsStartup) {
46094619
NS_WARNING("Error parsing preferences.");
46104620
}
46114621
}
4622+
4623+
#ifdef MOZ_BACKGROUNDTASKS
4624+
if (BackgroundTasks::IsBackgroundTaskMode()) {
4625+
rv = appJarReader->FindInit("defaults/backgroundtasks/*.js$",
4626+
getter_Transfers(find));
4627+
NS_ENSURE_SUCCESS(rv, rv);
4628+
prefEntries.Clear();
4629+
while (NS_SUCCEEDED(find->FindNext(&entryName, &entryNameLen))) {
4630+
prefEntries.AppendElement(Substring(entryName, entryNameLen));
4631+
}
4632+
prefEntries.Sort();
4633+
for (uint32_t i = prefEntries.Length(); i--;) {
4634+
rv = pref_ReadPrefFromJar(appJarReader, prefEntries[i].get());
4635+
if (NS_FAILED(rv)) {
4636+
NS_WARNING("Error parsing preferences.");
4637+
}
4638+
}
4639+
}
4640+
#endif
46124641
}
46134642

46144643
nsCOMPtr<nsIProperties> dirSvc(

modules/libpref/moz.build

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,3 +167,6 @@ else:
167167
FINAL_TARGET_PP_FILES += [
168168
"greprefs.js",
169169
]
170+
171+
if CONFIG["MOZ_BACKGROUNDTASKS"]:
172+
DEFINES["MOZ_BACKGROUNDTASKS"] = True
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
pref("browser.dom.window.dump.enabled", true);
6+
pref("devtools.console.stdout.chrome", true);
7+
8+
pref("network.process.enabled", false);
9+
10+
pref("toolkit.telemetry.archive.enabled", false);
11+
pref("toolkit.telemetry.firstShutdownPing.enabled", false);
12+
pref("toolkit.telemetry.healthping.enabled", false);
13+
pref("toolkit.telemetry.newProfilePing.enabled", false);
14+
pref("toolkit.telemetry.eventping.enabled", false);
15+
pref("toolkit.telemetry.ecosystemtelemetry.enabled", false);
16+
pref("toolkit.telemetry.prioping.enabled", false);
17+
pref("datareporting.policy.dataSubmissionEnabled", false);
18+
pref("datareporting.healthreport.uploadEnabled", false);
19+
20+
pref("browser.cache.offline.enable", false);
21+
pref("browser.cache.offline.storage.enable", false);
22+
pref("browser.cache.disk.enable", false);
23+
pref("permissions.memory_only", true);
24+
25+
// For testing only: used to test that backgroundtask-specific prefs are
26+
// processed. This just needs to be an unusual integer in the range 0..127.
27+
pref("test.backgroundtask_specific_pref.exitCode", 79);

toolkit/components/backgroundtasks/moz.build

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,13 @@ EXTRA_JS_MODULES.backgroundtasks += [
3737
"BackgroundTask_success.jsm",
3838
]
3939

40+
BROWSER_CHROME_MANIFESTS += ["tests/browser/browser.ini"]
4041
XPCSHELL_TESTS_MANIFESTS += ["tests/xpcshell/xpcshell.ini"]
42+
43+
TESTING_JS_MODULES.backgroundtasks += [
44+
"tests/BackgroundTask_backgroundtask_specific_pref.jsm",
45+
]
46+
47+
FINAL_TARGET_FILES.defaults.backgroundtasks += [
48+
"defaults/backgroundtasks.js",
49+
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2+
* This Source Code Form is subject to the terms of the Mozilla Public
3+
* License, v. 2.0. If a copy of the MPL was not distributed with this
4+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5+
6+
var EXPORTED_SYMBOLS = ["runBackgroundTask"];
7+
8+
const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
9+
10+
async function runBackgroundTask(commandLine) {
11+
let pref = commandLine.length
12+
? commandLine.getArgument(0)
13+
: "test.backgroundtask_specific_pref.exitCode";
14+
15+
// 0, 1, 2, 3 are all meaningful exit codes already.
16+
let exitCode = Services.prefs.getIntPref(pref, 4);
17+
18+
console.error(
19+
`runBackgroundTask: backgroundtask_specific_pref read pref '${pref}', exiting with exitCode ${exitCode}`
20+
);
21+
22+
return exitCode;
23+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# This Source Code Form is subject to the terms of the Mozilla Public
2+
# License, v. 2.0. If a copy of the MPL was not distributed with this
3+
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
[DEFAULT]
6+
skip-if = toolkit == 'android'
7+
head = head.js
8+
9+
[browser_backgroundtask_specific_pref.js]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2+
* vim: sw=4 ts=4 sts=4 et
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6+
7+
"use strict";
8+
9+
add_task(async function test_backgroundtask_specific_pref() {
10+
// First, verify this pref isn't set in Gecko itself.
11+
Assert.equal(
12+
-1,
13+
Services.prefs.getIntPref("test.backgroundtask_specific_pref.exitCode", -1)
14+
);
15+
16+
// Second, verify that this pref is set in background tasks.
17+
// mochitest-chrome tests locally test both unpackaged and packaged
18+
// builds (with `--appname=dist`).
19+
let exitCode = await do_backgroundtask("backgroundtask_specific_pref", {
20+
extraArgs: ["test.backgroundtask_specific_pref.exitCode"],
21+
});
22+
Assert.equal(79, exitCode);
23+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2+
* vim: sw=4 ts=4 sts=4 et
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6+
7+
"use strict";
8+
9+
async function do_backgroundtask(
10+
task,
11+
options = { extraArgs: [], extraEnv: {} }
12+
) {
13+
options = Object.assign({}, options);
14+
options.extraArgs = options.extraArgs || [];
15+
options.extraEnv = options.extraEnv || {};
16+
17+
let command = Services.dirsvc.get("XREExeF", Ci.nsIFile).path;
18+
let args = ["--backgroundtask", task];
19+
args.push(...options.extraArgs);
20+
21+
// Ensure `resource://testing-common` gets mapped.
22+
let protocolHandler = Services.io
23+
.getProtocolHandler("resource")
24+
.QueryInterface(Ci.nsIResProtocolHandler);
25+
26+
let uri = protocolHandler.getSubstitution("testing-common");
27+
Assert.ok(uri, "resource://testing-common is not substituted");
28+
29+
// The equivalent of _TESTING_MODULES_DIR in xpcshell.
30+
options.extraEnv.XPCSHELL_TESTING_MODULES_URI = uri.spec;
31+
32+
// Now we can actually invoke the process.
33+
info(
34+
`launching child process ${command} with args: ${args} and extra environment: ${JSON.stringify(
35+
options.extraEnv
36+
)}`
37+
);
38+
39+
const { Subprocess } = ChromeUtils.import(
40+
"resource://gre/modules/Subprocess.jsm"
41+
);
42+
let proc = await Subprocess.call({
43+
command,
44+
arguments: args,
45+
environment: options.extraEnv,
46+
environmentAppend: true,
47+
stderr: "stdout",
48+
}).then(p => {
49+
p.stdin.close();
50+
const dumpPipe = async pipe => {
51+
let data = await pipe.readString();
52+
while (data) {
53+
for (let line of data.split(/\r\n|\r|\n/).slice(0, -1)) {
54+
dump("> " + line + "\n");
55+
}
56+
data = await pipe.readString();
57+
}
58+
};
59+
dumpPipe(p.stdout);
60+
61+
return p;
62+
});
63+
64+
let { exitCode } = await proc.wait();
65+
return exitCode;
66+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
2+
* vim: sw=4 ts=4 sts=4 et
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6+
7+
add_task(async function test_backgroundtask_specific_pref() {
8+
// First, verify this pref isn't set in Gecko itself.
9+
Assert.equal(
10+
-1,
11+
Services.prefs.getIntPref("test.backgroundtask_specific_pref.exitCode", -1)
12+
);
13+
14+
// Second, verify that this pref is set in background tasks.
15+
// xpcshell tests locally test unpackaged builds.
16+
let exitCode = await do_backgroundtask("backgroundtask_specific_pref", {
17+
extraArgs: ["test.backgroundtask_specific_pref.exitCode"],
18+
});
19+
Assert.equal(79, exitCode);
20+
});

toolkit/components/backgroundtasks/tests/xpcshell/xpcshell.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ support-files =
88
CatBackgroundTaskRegistrationComponents.manifest
99

1010
[test_backgroundtask_exitcodes.js]
11+
[test_backgroundtask_specific_pref.js]
1112
[test_manifest_with_backgroundtask.js]
1213
[test_manifest_without_backgroundtask.js]

toolkit/xre/nsXREDirProvider.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -853,6 +853,10 @@ static nsresult DeleteDirIfExists(nsIFile* dir) {
853853

854854
static const char* const kAppendPrefDir[] = {"defaults", "preferences",
855855
nullptr};
856+
#ifdef MOZ_BACKGROUNDTASKS
857+
static const char* const kAppendBackgroundTasksPrefDir[] = {
858+
"defaults", "backgroundtasks", nullptr};
859+
#endif
856860

857861
nsresult nsXREDirProvider::GetFilesInternal(const char* aProperty,
858862
nsISimpleEnumerator** aResult) {
@@ -863,6 +867,12 @@ nsresult nsXREDirProvider::GetFilesInternal(const char* aProperty,
863867
nsCOMArray<nsIFile> directories;
864868

865869
LoadDirIntoArray(mXULAppDir, kAppendPrefDir, directories);
870+
#ifdef MOZ_BACKGROUNDTASKS
871+
if (mozilla::BackgroundTasks::IsBackgroundTaskMode()) {
872+
LoadDirIntoArray(mGREDir, kAppendBackgroundTasksPrefDir, directories);
873+
LoadDirIntoArray(mXULAppDir, kAppendBackgroundTasksPrefDir, directories);
874+
}
875+
#endif
866876

867877
rv = NS_NewArrayEnumerator(aResult, directories, NS_GET_IID(nsIFile));
868878
} else if (!strcmp(aProperty, NS_APP_CHROME_DIR_LIST)) {

0 commit comments

Comments
 (0)