[PGO] Change the output path for the child profile files on Android

Using an environment variable doesn't work because on Android processes
fork from the system zygote, we need to use a fixed location for the
profile files.

There's still some issues that impact the PGO profile build:
 - it's hard to intercept browser shutdown and to ensure that all profile
   files have been flushed to disk before terminating the child
   processes.
 - the browser process writes its profiling data in the wrong directory
   (this CL only impacts the child processes).

These 2 issues will be tackled in separate CLs.


Bug: 1227766
Change-Id: I5546163ff531d904340a0e95adf9d5f2d3d3b35a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3110569
Commit-Queue: Sébastien Marchand <[email protected]>
Reviewed-by: Avi Drissman <[email protected]>
Reviewed-by: Andrew Grieve <[email protected]>
Cr-Commit-Position: refs/heads/main@{#913992}
diff --git a/content/common/profiling_utils.cc b/content/common/profiling_utils.cc
index 57af9ce..fb1ec4b 100644
--- a/content/common/profiling_utils.cc
+++ b/content/common/profiling_utils.cc
@@ -31,22 +31,47 @@
 
 namespace content {
 
-base::File OpenProfilingFile() {
-  base::ScopedAllowBlockingForTesting allows_blocking;
-  std::unique_ptr<base::Environment> env(base::Environment::Create());
-  std::string prof_template;
+namespace {
+
+// Returns the path where the PGO profiles should be saved.
+// On Android this is always a static path, on other platforms it's either
+// the path specified by the LLVM_PROFILE_FILE environment variable or the
+// current path if it's not set.
+base::FilePath GetProfileFileDirectory() {
   base::FilePath path;
+
+  // Android differs from the other platforms because it's not possible to
+  // write in base::DIR_CURRENT and environment variables aren't well supported.
+#if defined(OS_ANDROID)
+  base::PathService::Get(base::DIR_TEMP, &path);
+  path = path.Append("pgo_profiles/");
+#else  // !defined(OS_ANDROID)
+  std::string prof_template;
+  std::unique_ptr<base::Environment> env(base::Environment::Create());
   if (env->GetVar("LLVM_PROFILE_FILE", &prof_template)) {
 #if defined(OS_WIN)
     path = base::FilePath(base::UTF8ToWide(prof_template)).DirName();
 #else
     path = base::FilePath(prof_template).DirName();
 #endif
+  }
+#endif
+
+  if (!path.empty()) {
     base::CreateDirectory(path);
   } else {
     base::PathService::Get(base::DIR_CURRENT, &path);
   }
 
+  return path;
+}
+
+}  // namespace
+
+base::File OpenProfilingFile() {
+  base::ScopedAllowBlockingForTesting allows_blocking;
+  base::FilePath path = GetProfileFileDirectory();
+
   // sajjadm@ and liaoyuke@ experimentally determined that a size 4 pool works
   // well for the coverage builder.
   // TODO(https://crbug.com/1059335): Check if this is an appropriate value for