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"
14 # include "mozilla/AssembleCmdLine.h"
17 #include "mozilla/ResultVariant.h"
21 NS_IMPL_ISUPPORTS(BackgroundTasksRunner
, nsIBackgroundTasksRunner
);
23 NS_IMETHODIMP
BackgroundTasksRunner::RunInDetachedProcess(
24 const nsACString
& aTaskName
, const nsTArray
<nsCString
>& aArgs
) {
26 nsresult rv
= XRE_GetBinaryPath(getter_AddRefs(lf
));
27 NS_ENSURE_SUCCESS(rv
, rv
);
29 nsAutoCString exePath
;
31 rv
= lf
->GetNativePath(exePath
);
33 rv
= lf
->GetNativeTarget(exePath
);
35 NS_ENSURE_SUCCESS(rv
, rv
);
37 base::LaunchOptions options
;
39 options
.start_independent
= true;
41 nsTArray
<const char*> argv
= {exePath
.Data(), "--backgroundtask",
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
;
57 std::vector
<std::string
> argv
= {exePath
.Data(), "--backgroundtask",
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
;
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");
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
94 StaticPrefs::telemetry_fog_test_localhost_port() != -1;
96 if (!aMetricsId
.IsEmpty() && telemetryEnabled
) {
97 argv
.AppendElement("--metrics-id");
98 argv
.AppendElement(aMetricsId
);
102 argv
.AppendElement("--attach-console");
105 return RunInDetachedProcess("removeDirectory"_ns
, argv
);
108 } // namespace mozilla