Backed out changeset f594e6f00208 (bug 1940883) for causing crashes in bug 1941164.
[gecko.git] / toolkit / components / backgroundtasks / BackgroundTasksRunner.cpp
blobebd580ce7f5b5603541ca5a1a0f375d2c51f6845
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/. */
5 #include "mozilla/BackgroundTasksRunner.h"
7 #include "base/process_util.h"
8 #include "mozilla/StaticPrefs_datareporting.h"
9 #include "mozilla/StaticPrefs_telemetry.h"
10 #include "mozilla/StaticPrefs_toolkit.h"
11 #include "nsIFile.h"
13 #ifdef XP_WIN
14 # include "mozilla/AssembleCmdLine.h"
15 #endif
17 #include "mozilla/ResultVariant.h"
19 namespace mozilla {
21 NS_IMPL_ISUPPORTS(BackgroundTasksRunner, nsIBackgroundTasksRunner);
23 NS_IMETHODIMP BackgroundTasksRunner::RunInDetachedProcess(
24 const nsACString& aTaskName, const nsTArray<nsCString>& aArgs) {
25 nsCOMPtr<nsIFile> lf;
26 nsresult rv = XRE_GetBinaryPath(getter_AddRefs(lf));
27 NS_ENSURE_SUCCESS(rv, rv);
29 nsAutoCString exePath;
30 #if !defined(XP_WIN)
31 rv = lf->GetNativePath(exePath);
32 #else
33 rv = lf->GetNativeTarget(exePath);
34 #endif
35 NS_ENSURE_SUCCESS(rv, rv);
37 base::LaunchOptions options;
38 #ifdef XP_WIN
39 options.start_independent = true;
41 nsTArray<const char*> argv = {exePath.Data(), "--backgroundtask",
42 aTaskName.Data()};
43 for (const nsCString& str : aArgs) {
44 argv.AppendElement(str.get());
46 argv.AppendElement(nullptr);
48 wchar_t* assembledCmdLine = nullptr;
49 if (assembleCmdLine(argv.Elements(), &assembledCmdLine, CP_UTF8) == -1) {
50 return NS_ERROR_FAILURE;
53 if (base::LaunchApp(assembledCmdLine, options, nullptr).isErr()) {
54 return NS_ERROR_FAILURE;
56 #else
57 std::vector<std::string> argv = {exePath.Data(), "--backgroundtask",
58 aTaskName.Data()};
59 for (const nsCString& str : aArgs) {
60 argv.push_back(str.get());
63 if (base::LaunchApp(argv, std::move(options), nullptr).isErr()) {
64 return NS_ERROR_FAILURE;
66 #endif
68 return NS_OK;
71 NS_IMETHODIMP BackgroundTasksRunner::RemoveDirectoryInDetachedProcess(
72 const nsACString& aParentDirPath, const nsACString& aChildDirName,
73 const nsACString& aSecondsToWait, const nsACString& aOtherFoldersSuffix,
74 const nsACString& aMetricsId) {
75 nsTArray<nsCString> argv = {aParentDirPath + ""_ns, aChildDirName + ""_ns,
76 aSecondsToWait + ""_ns,
77 aOtherFoldersSuffix + ""_ns};
79 uint32_t testingSleepMs =
80 StaticPrefs::toolkit_background_tasks_remove_directory_testing_sleep_ms();
81 if (testingSleepMs > 0) {
82 argv.AppendElement("--test-sleep");
83 nsAutoCString sleep;
84 sleep.AppendInt(testingSleepMs);
85 argv.AppendElement(sleep);
88 bool telemetryEnabled =
89 StaticPrefs::datareporting_healthreport_uploadEnabled() &&
90 // Talos set this to not send telemetry but still enable the code path.
91 // But in this case we just disable it since this telemetry happens
92 // independently from the main process and thus shouldn't be relevant to
93 // performance tests.
94 StaticPrefs::telemetry_fog_test_localhost_port() != -1;
96 if (!aMetricsId.IsEmpty() && telemetryEnabled) {
97 argv.AppendElement("--metrics-id");
98 argv.AppendElement(aMetricsId);
101 #ifdef DEBUG
102 argv.AppendElement("--attach-console");
103 #endif
105 return RunInDetachedProcess("removeDirectory"_ns, argv);
108 } // namespace mozilla