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

File tree

16 files changed

+238
-0
lines changed

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+
}

0 commit comments

Comments
 (0)