Fuchsia: Actually return the executable path from PathProviderFuchsia.

Bug: 746674
Change-Id: I4406148e5ba7c178f47b490ead9a8e76d9571457
Reviewed-on: https://chromium-review.googlesource.com/634045
Commit-Queue: Kevin Marshall <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Reviewed-by: Wez <[email protected]>
Cr-Commit-Position: refs/heads/master@{#497547}
diff --git a/base/base_paths_fuchsia.cc b/base/base_paths_fuchsia.cc
index b2f28ce..41883d9 100644
--- a/base/base_paths_fuchsia.cc
+++ b/base/base_paths_fuchsia.cc
@@ -4,28 +4,34 @@
 
 #include "base/base_paths.h"
 
+#include <stdlib.h>
+
+#include "base/command_line.h"
 #include "base/files/file_path.h"
 
 namespace base {
 
 bool PathProviderFuchsia(int key, FilePath* result) {
-  // TODO(fuchsia): There's no API to retrieve these on Fuchsia. The app name
-  // itself should be dynamic (i.e. not always "chrome") but other paths are
-  // correct as fixed paths like this. See https://crbug.com/726124.
   switch (key) {
-    case FILE_EXE:
     case FILE_MODULE:
-      // TODO(fuchsia): This is incorrect per
-      // https://fuchsia.googlesource.com/docs/+/master/namespaces.md, and
-      // should be /pkg/{bin,lib}/something. However, binaries are currently run
-      // by packing them into the system bootfs rather than running a "real"
-      // installer (which doesn't currently exist). Additionally, to the
-      // installer not existing, mmap() currently only works on bootfs file
-      // systems (like /system) but won't for files installed dynamically in
-      // other locations on other types of file systems. So, for now, we use
-      // /system/ as the location for everything.
-      *result = FilePath("/system/chrome");
+    // Not supported in debug or component builds. Fall back on using the EXE
+    // path for now.
+    // TODO(fuchsia): Get this value from an API. See crbug.com/726124
+    case FILE_EXE: {
+      // Use the binary name as specified on the command line.
+      // TODO(fuchsia): It would be nice to get the canonical executable path
+      // from a kernel API. See https://crbug.com/726124
+      char bin_dir[PATH_MAX + 1];
+      if (realpath(base::CommandLine::ForCurrentProcess()
+                       ->GetProgram()
+                       .AsUTF8Unsafe()
+                       .c_str(),
+                   bin_dir) == NULL) {
+        return false;
+      }
+      *result = FilePath(bin_dir);
       return true;
+    }
     case DIR_SOURCE_ROOT:
       // This is only used for tests, so we return the binary location for now.
       *result = FilePath("/system");