Files
firefox/toolkit/components/backgroundtasks/BackgroundTasksRunner.cpp
T
Nika Layzell 5f0f9dd22a Bug 2020817 - Part 4: Annotate & clean up nsACString callers of BeginReading/Data, r=xpcom-reviewers,media-playback-reviewers,webrtc-reviewers,cookie-reviewers,profiler-reviewers,win-reviewers,dom-storage-reviewers,permissions-reviewers,toolkit-telemetry-reviewers-rotation,layout-reviewers,dom-worker-reviewers,gfx-reviewers,webgpu-reviewers,necko-reviewers,dom-core,smaug,handyman,emilio,pehrsons,kershaw,timhuang,edenchuang,padenot,bradwerth
This is a large number of largely mechanical changes. When possible, the code
was called to directly call `.get()` instead, performing the assertions
manually. In many places PromiseFlatCString() calls were added to dynamically
copy the strings into null terminated buffers if the string was not null
terminated.

Differential Revision: https://phabricator.services.mozilla.com/D286158
2026-03-25 08:23:04 +00:00

108 lines
3.3 KiB
C++

/* 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/. */
#include "mozilla/BackgroundTasksRunner.h"
#include "base/process_util.h"
#include "mozilla/StaticPrefs_datareporting.h"
#include "mozilla/StaticPrefs_telemetry.h"
#include "mozilla/StaticPrefs_toolkit.h"
#include "nsIFile.h"
#ifdef XP_WIN
# include "mozilla/AssembleCmdLine.h"
#endif
namespace mozilla {
NS_IMPL_ISUPPORTS(BackgroundTasksRunner, nsIBackgroundTasksRunner);
NS_IMETHODIMP BackgroundTasksRunner::RunInDetachedProcess(
const nsACString& aTaskName, const nsTArray<nsCString>& aArgs) {
nsCOMPtr<nsIFile> lf;
nsresult rv = XRE_GetBinaryPath(getter_AddRefs(lf));
NS_ENSURE_SUCCESS(rv, rv);
nsAutoCString exePath;
#if !defined(XP_WIN)
rv = lf->GetNativePath(exePath);
#else
rv = lf->GetNativeTarget(exePath);
#endif
NS_ENSURE_SUCCESS(rv, rv);
base::LaunchOptions options;
nsPromiseFlatCString flatTaskName(aTaskName);
#ifdef XP_WIN
options.start_independent = true;
nsTArray<const char*> argv = {exePath.get(), "--backgroundtask",
flatTaskName.get()};
for (const nsCString& str : aArgs) {
argv.AppendElement(str.get());
}
argv.AppendElement(nullptr);
wchar_t* assembledCmdLine = nullptr;
if (assembleCmdLine(argv.Elements(), &assembledCmdLine, CP_UTF8) == -1) {
return NS_ERROR_FAILURE;
}
if (base::LaunchApp(assembledCmdLine, options, nullptr).isErr()) {
return NS_ERROR_FAILURE;
}
#else
std::vector<std::string> argv = {exePath.get(), "--backgroundtask",
flatTaskName.get()};
for (const nsCString& str : aArgs) {
argv.push_back(str.get());
}
if (base::LaunchApp(argv, std::move(options), nullptr).isErr()) {
return NS_ERROR_FAILURE;
}
#endif
return NS_OK;
}
NS_IMETHODIMP BackgroundTasksRunner::RemoveDirectoryInDetachedProcess(
const nsACString& aParentDirPath, const nsACString& aChildDirName,
const nsACString& aSecondsToWait, const nsACString& aOtherFoldersSuffix,
const nsACString& aMetricsId) {
nsTArray<nsCString> argv = {aParentDirPath + ""_ns, aChildDirName + ""_ns,
aSecondsToWait + ""_ns,
aOtherFoldersSuffix + ""_ns};
uint32_t testingSleepMs =
StaticPrefs::toolkit_background_tasks_remove_directory_testing_sleep_ms();
if (testingSleepMs > 0) {
argv.AppendElement("--test-sleep");
nsAutoCString sleep;
sleep.AppendInt(testingSleepMs);
argv.AppendElement(sleep);
}
bool telemetryEnabled =
StaticPrefs::datareporting_healthreport_uploadEnabled() &&
// Talos set this to not send telemetry but still enable the code path.
// But in this case we just disable it since this telemetry happens
// independently from the main process and thus shouldn't be relevant to
// performance tests.
StaticPrefs::telemetry_fog_test_localhost_port() != -1;
if (!aMetricsId.IsEmpty() && telemetryEnabled) {
argv.AppendElement("--metrics-id");
argv.AppendElement(aMetricsId);
}
#ifdef DEBUG
argv.AppendElement("--attach-console");
#endif
return RunInDetachedProcess("removeDirectory"_ns, argv);
}
} // namespace mozilla