diff --git a/netwerk/cache2/CacheFileIOManager.cpp b/netwerk/cache2/CacheFileIOManager.cpp index 0c0ab48fadea..11ac484114be 100644 --- a/netwerk/cache2/CacheFileIOManager.cpp +++ b/netwerk/cache2/CacheFileIOManager.cpp @@ -4088,7 +4088,7 @@ nsresult CacheFileIOManager::DispatchPurgeTask( NS_ENSURE_SUCCESS(rv, rv); const char* const argv[] = {exePath.get(), "--backgroundtask", - "purgeHTTPCache", path.get(), + "removeDirectory", path.get(), aCacheDirName.get(), aSecondsToWait.get(), aPurgeExtension.get(), nullptr}; if (NS_WARN_IF(PR_FAILURE == PR_CreateProcessDetached(exePath.get(), diff --git a/netwerk/cache2/moz.build b/netwerk/cache2/moz.build index 9d999597e3d4..697f4e398345 100644 --- a/netwerk/cache2/moz.build +++ b/netwerk/cache2/moz.build @@ -63,8 +63,3 @@ LOCAL_INCLUDES += [ ] FINAL_LIBRARY = "xul" - -if CONFIG["MOZ_BACKGROUNDTASKS"]: - EXTRA_JS_MODULES.backgroundtasks += [ - "BackgroundTask_purgeHTTPCache.sys.mjs", - ] diff --git a/netwerk/test/unit/xpcshell.ini b/netwerk/test/unit/xpcshell.ini index 0be6a8732ff2..bf75fe22c6a4 100644 --- a/netwerk/test/unit/xpcshell.ini +++ b/netwerk/test/unit/xpcshell.ini @@ -346,7 +346,7 @@ skip-if = os != "win" [test_trr_ttl.js] [test_synthesized_response.js] [test_udp_multicast.js] -skip-if = +skip-if = win10_2004 # Bug 1742311 [test_redirect_history.js] [test_reply_without_content_type.js] @@ -453,7 +453,7 @@ run-sequentially = http3server skip-if = os == 'android' run-sequentially = http3server [test_http3_early_hint_listener.js] -skip-if = +skip-if = os == 'android' os == 'linux' # Bug 1773916 run-sequentially = http3server @@ -631,8 +631,6 @@ skip-if = run-sequentially = node server exceptions dont replay well [test_http_408_retry.js] [test_brotli_decoding.js] -[test_backgroundtask_purgeHTTPCache.js] -skip-if = os == "android" # MultiInstanceLock doesn't work on Android [test_http2-proxy-failing.js] run-sequentially = node server exceptions dont replay well [test_tls13_disabled.js] diff --git a/netwerk/cache2/BackgroundTask_purgeHTTPCache.sys.mjs b/toolkit/components/backgroundtasks/BackgroundTask_removeDirectory.sys.mjs similarity index 82% rename from netwerk/cache2/BackgroundTask_purgeHTTPCache.sys.mjs rename to toolkit/components/backgroundtasks/BackgroundTask_removeDirectory.sys.mjs index 7b28643a2bf5..bc19005c7a3d 100644 --- a/netwerk/cache2/BackgroundTask_purgeHTTPCache.sys.mjs +++ b/toolkit/components/backgroundtasks/BackgroundTask_removeDirectory.sys.mjs @@ -23,18 +23,18 @@ function tryRemoveDir(aFile) { const FILE_CHECK_ITERATION_TIMEOUT_MS = 1000; -async function deleteCacheDirectory( - profileDirPath, - cacheDirName, +async function deleteChildDirectory( + parentDirPath, + childDirName, secondsToWait ) { - if (!cacheDirName || !cacheDirName.length) { + if (!childDirName || !childDirName.length) { return; } let targetFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); - targetFile.initWithPath(profileDirPath); - targetFile.append(cacheDirName); + targetFile.initWithPath(parentDirPath); + targetFile.append(childDirName); // We create the lock before the file is actually there so this task // is the first one to acquire the lock. Otherwise a different task @@ -47,7 +47,7 @@ async function deleteCacheDirectory( let wasFirst = false; let locked = false; try { - dirLock.lock(cacheDirName); + dirLock.lock(childDirName); locked = true; wasFirst = !dirLock.isOtherInstanceRunning(); } catch (e) { @@ -106,16 +106,13 @@ async function deleteCacheDirectory( } } -async function cleanupOtherCacheDirectories( - profileDirPath, - otherFoldersSuffix -) { +async function cleanupOtherDirectories(parentDirPath, otherFoldersSuffix) { if (!otherFoldersSuffix || !otherFoldersSuffix.length) { return; } let targetFile = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsIFile); - targetFile.initWithPath(profileDirPath); + targetFile.initWithPath(parentDirPath); let entries = targetFile.directoryEntries; while (entries.hasMoreElements()) { @@ -169,22 +166,22 @@ async function cleanupOtherCacheDirectories( } // Usage: -// purgeHTTPCache profileDirPath cacheDirName secondsToWait [otherFoldersSuffix] +// removeDirectory parentDirPath childDirName secondsToWait [otherFoldersSuffix] // arg0 arg1 arg2 arg3 -// profileDirPath - The path to the "profile directory" containing the moved cache dir -// cacheDirName - The "leaf name" of the moved cache directory +// parentDirPath - The path to the parent directory that includes the target directory +// childDirName - The "leaf name" of the moved cache directory // If empty, the background task will only purge folders that have the "otherFoldersSuffix". // secondsToWait - String representing the number of seconds to wait for the cacheDir to be moved -// otherFoldersSuffix - [optional] The suffix of moved purged cache directories +// otherFoldersSuffix - [optional] The suffix of directories that should be removed // When not empty, this task will also attempt to remove all directories in -// the profile dir that end with this suffix +// the parent dir that end with this suffix export async function runBackgroundTask(commandLine) { if (commandLine.length < 3) { throw new Error("Insufficient arguments"); } - let profileDirPath = commandLine.getArgument(0); - let cacheDirName = commandLine.getArgument(1); + const parentDirPath = commandLine.getArgument(0); + const childDirName = commandLine.getArgument(1); let secondsToWait = parseInt(commandLine.getArgument(2)); if (isNaN(secondsToWait)) { secondsToWait = 10; @@ -194,15 +191,10 @@ export async function runBackgroundTask(commandLine) { otherFoldersSuffix = commandLine.getArgument(3); } - console.error( - profileDirPath, - cacheDirName, - secondsToWait, - otherFoldersSuffix - ); + console.error(parentDirPath, childDirName, secondsToWait, otherFoldersSuffix); - await deleteCacheDirectory(profileDirPath, cacheDirName, secondsToWait); - await cleanupOtherCacheDirectories(profileDirPath, otherFoldersSuffix); + await deleteChildDirectory(parentDirPath, childDirName, secondsToWait); + await cleanupOtherDirectories(parentDirPath, otherFoldersSuffix); // TODO: event telemetry with timings, and how often we have left over cache folders from previous runs. diff --git a/toolkit/components/backgroundtasks/moz.build b/toolkit/components/backgroundtasks/moz.build index e34e21c00ed3..4f54da8ec333 100644 --- a/toolkit/components/backgroundtasks/moz.build +++ b/toolkit/components/backgroundtasks/moz.build @@ -47,6 +47,7 @@ EXTRA_JS_MODULES.backgroundtasks += [ "BackgroundTask_exception.sys.mjs", "BackgroundTask_failure.sys.mjs", "BackgroundTask_message.sys.mjs", + "BackgroundTask_removeDirectory.sys.mjs", "BackgroundTask_success.sys.mjs", ] diff --git a/netwerk/test/unit/test_backgroundtask_purgeHTTPCache.js b/toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtask_removeDirectory.js similarity index 88% rename from netwerk/test/unit/test_backgroundtask_purgeHTTPCache.js rename to toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtask_removeDirectory.js index 04e0d9334dac..0bcd76444088 100644 --- a/netwerk/test/unit/test_backgroundtask_purgeHTTPCache.js +++ b/toolkit/components/backgroundtasks/tests/xpcshell/test_backgroundtask_removeDirectory.js @@ -5,14 +5,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ "use strict"; -const { BackgroundTasksTestUtils } = ChromeUtils.import( - "resource://testing-common/BackgroundTasksTestUtils.jsm" -); -BackgroundTasksTestUtils.init(this); -const do_backgroundtask = BackgroundTasksTestUtils.do_backgroundtask.bind( - BackgroundTasksTestUtils -); - // This test exercises functionality and also ensures the exit codes, // which are a public API, do not change over time. const { EXIT_CODE } = ChromeUtils.import( @@ -35,7 +27,7 @@ add_task(async function test_simple() { extraDir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744); equal(extraDir.exists(), true); - let exitCode = await do_backgroundtask("purgeHTTPCache", { + let exitCode = await do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, LEAF_NAME, "10", ".abc"], }); equal(exitCode, EXIT_CODE.SUCCESS); @@ -51,7 +43,7 @@ add_task(async function test_no_extension() { dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744); equal(dir.exists(), true); - let exitCode = await do_backgroundtask("purgeHTTPCache", { + let exitCode = await do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, LEAF_NAME, "10"], }); equal(exitCode, EXIT_CODE.SUCCESS); @@ -64,7 +56,7 @@ add_task(async function test_createAfter() { let dir = do_get_profile(); dir.append(LEAF_NAME); - let task = do_backgroundtask("purgeHTTPCache", { + let task = do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, LEAF_NAME, "10", ""], }); @@ -82,7 +74,7 @@ add_task(async function test_folderNameWithSpaces() { let leafNameWithSpaces = `${LEAF_NAME} space`; dir.append(leafNameWithSpaces); - let task = do_backgroundtask("purgeHTTPCache", { + let task = do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, leafNameWithSpaces, "10", ""], }); @@ -99,7 +91,7 @@ add_task(async function test_missing_folder() { let dir = do_get_profile(); dir.append(LEAF_NAME); - let exitCode = await do_backgroundtask("purgeHTTPCache", { + let exitCode = await do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, LEAF_NAME, "1", ""], }); @@ -124,7 +116,7 @@ add_task( dir.moveTo(null, "newName"); // This will fail because of the readonly file. - let exitCode = await do_backgroundtask("purgeHTTPCache", { + let exitCode = await do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, "newName", "1", ""], }); @@ -152,7 +144,7 @@ add_task(async function test_purgeFile() { file.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o644); equal(file.exists(), true); - let exitCode = await do_backgroundtask("purgeHTTPCache", { + let exitCode = await do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, LEAF_NAME, "2", ""], }); equal(exitCode, EXIT_CODE.EXCEPTION); @@ -171,7 +163,7 @@ add_task(async function test_purgeFile() { dir2.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744); equal(dir2.exists(), true); - exitCode = await do_backgroundtask("purgeHTTPCache", { + exitCode = await do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, LEAF_NAME, "2", ".abc"], }); @@ -191,13 +183,13 @@ add_task(async function test_two_tasks() { let tasks = []; tasks.push( - do_backgroundtask("purgeHTTPCache", { + do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, dir1.leafName, "5", ".abc"], }) ); dir1.create(Ci.nsIFile.DIRECTORY_TYPE, 0o744); tasks.push( - do_backgroundtask("purgeHTTPCache", { + do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, dir2.leafName, "5", ".abc"], }) ); @@ -224,7 +216,7 @@ add_task(async function test_aLotOfTasks() { dir.append(`leaf${i}.abc`); tasks.push( - do_backgroundtask("purgeHTTPCache", { + do_backgroundtask("removeDirectory", { extraArgs: [do_get_profile().path, dir.leafName, "5", ".abc"], extraEnv: { MOZ_LOG: "BackgroundTasks:5" }, }) diff --git a/toolkit/components/backgroundtasks/tests/xpcshell/xpcshell.ini b/toolkit/components/backgroundtasks/tests/xpcshell/xpcshell.ini index 6c93fa804a02..eda014e68d1d 100644 --- a/toolkit/components/backgroundtasks/tests/xpcshell/xpcshell.ini +++ b/toolkit/components/backgroundtasks/tests/xpcshell/xpcshell.ini @@ -23,6 +23,8 @@ run-sequentially = Uses global profile directory `DefProfRt` [test_backgroundtask_policies.js] [test_backgroundtask_profile_is_slim.js] [test_backgroundtask_profile_service_configuration.js] +[test_backgroundtask_removeDirectory.js] +skip-if = os == "android" # MultiInstanceLock doesn't work on Android [test_backgroundtask_shouldprocessupdates.js] [test_backgroundtask_simultaneous_instances.js] [test_backgroundtask_specific_pref.js]