Reland "Refactor crashpad_handler binary on macOS"

This is a reland of c5a20e34006837ab54037ea75e9a7a00bf1275e3. Trying to
rename chrome_crashpad_handler to crashpad_handler in the final output
led to contortions in the build, instead invoke the handler using the new
name.

Original change's description:
> Refactor crashpad_handler binary on macOS
>
> In order to define custom UserStreamDataSources, we need a
> chromium-built crashpad_handler binary. This change refactors the
> crashpad_handler executable to be built within chromium as
> chrome_crashpad_handler and moved into it's final destination under the
> original 'crashpad_handler' name.
>
> Bug: 912286
> Change-Id: I0f52028ae4c149823b30747589e2dd94225694c6
> Reviewed-on: https://chromium-review.googlesource.com/c/1449330
> Reviewed-by: Lei Zhang <thestig@chromium.org>
> Reviewed-by: Peter Beverloo <peter@chromium.org>
> Reviewed-by: Pavel Feldman <pfeldman@chromium.org>
> Reviewed-by: Mark Mentovai <mark@chromium.org>
> Commit-Queue: Vlad Tsyrklevich <vtsyrklevich@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#629394}

TBR=peter@chromium.org,pfeldman@chromium.org

Bug: 912286
Change-Id: Idabc3f3bf547afe0c63ce0910641f83ec0106304
Reviewed-on: https://chromium-review.googlesource.com/c/1455724
Reviewed-by: Lei Zhang <thestig@chromium.org>
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Vlad Tsyrklevich <vtsyrklevich@chromium.org>
Cr-Commit-Position: refs/heads/master@{#629713}
diff --git a/chrome/BUILD.gn b/chrome/BUILD.gn
index 51a34b51..05a9bfe 100644
--- a/chrome/BUILD.gn
+++ b/chrome/BUILD.gn
@@ -844,7 +844,7 @@
 
   bundle_data("chrome_framework_helpers") {
     sources = [
-      "$root_out_dir/crashpad_handler",
+      "$root_out_dir/chrome_crashpad_handler",
     ]
 
     outputs = [
@@ -852,7 +852,7 @@
     ]
 
     public_deps = [
-      "//third_party/crashpad/crashpad/handler:crashpad_handler",
+      "//components/crash/content/app:chrome_crashpad_handler",
     ]
 
     if (using_sanitizer) {
@@ -1334,7 +1334,7 @@
       "$root_out_dir/AlertNotificationService.xpc/Contents/MacOS/AlertNotificationService",
       "$root_out_dir/$chrome_helper_name.app/Contents/MacOS/$chrome_helper_name",
       "$root_out_dir/$chrome_product_full_name.app/Contents/MacOS/$chrome_product_full_name",
-      "$root_out_dir/crashpad_handler",
+      "$root_out_dir/chrome_crashpad_handler",
       "$root_out_dir/libswiftshader_libEGL.dylib",
       "$root_out_dir/libswiftshader_libGLESv2.dylib",
     ]
@@ -1365,8 +1365,8 @@
         ":chrome_framework",
         ":chrome_helper_app",
         "//chrome/browser/ui/cocoa/notifications:alert_notification_xpc_service",
+        "//components/crash/content/app:chrome_crashpad_handler",
         "//third_party/breakpad:dump_syms",
-        "//third_party/crashpad/crashpad/handler:crashpad_handler",
         "//third_party/swiftshader/src/OpenGL/libEGL:swiftshader_libEGL",
         "//third_party/swiftshader/src/OpenGL/libGLESv2:swiftshader_libGLESv2",
       ]
@@ -1384,7 +1384,7 @@
         "$root_out_dir/$chrome_framework_name.dSYM",
         "$root_out_dir/$chrome_helper_name.dSYM",
         "$root_out_dir/$chrome_product_full_name.dSYM",
-        "$root_out_dir/crashpad_handler.dSYM",
+        "$root_out_dir/chrome_crashpad_handler.dSYM",
         "$root_out_dir/libswiftshader_libEGL.dylib.dSYM",
         "$root_out_dir/libswiftshader_libGLESv2.dylib.dSYM",
       ]
@@ -1405,7 +1405,7 @@
         ":chrome_framework",
         ":chrome_helper_app",
         "//chrome/browser/ui/cocoa/notifications:alert_notification_xpc_service",
-        "//third_party/crashpad/crashpad/handler:crashpad_handler",
+        "//components/crash/content/app:chrome_crashpad_handler",
         "//third_party/swiftshader/src/OpenGL/libEGL:swiftshader_libEGL",
         "//third_party/swiftshader/src/OpenGL/libGLESv2:swiftshader_libGLESv2",
       ]
diff --git a/components/crash/content/app/BUILD.gn b/components/crash/content/app/BUILD.gn
index 5a13a519..30ba4f9 100644
--- a/components/crash/content/app/BUILD.gn
+++ b/components/crash/content/app/BUILD.gn
@@ -189,6 +189,41 @@
   }
 }
 
+if (is_mac) {
+  # We build a chromium-specific crashpad_handler executable so that we can
+  # define custom UserStreamDataSources.
+  executable("chrome_crashpad_handler") {
+    sources = [
+      "chrome_crashpad_handler.cc",
+    ]
+
+    deps = [
+      "//third_party/crashpad/crashpad/handler:handler",
+    ]
+
+    if (is_component_build) {
+      ldflags = [
+        # The handler is in
+        # Chromium.app/Contents/Versions/X/Chromium Framework.framework/Versions/A/Helpers/
+        # so set rpath up to the base.
+        "-rpath",
+        "@loader_path/../../../../../../../..",
+
+        # The handler is also in
+        # Content Shell.app/Contents/Frameworks/Content Shell Framework.framework/Versions/C/Helpers/
+        # so set the rpath for that too.
+        "-rpath",
+        "@loader_path/../../../../../../..",
+
+        # The handler can also be executed in an unbundled framework at
+        # Chromium Framework.framework/Versions/A/Helpers/
+        "-rpath",
+        "@loader_path/../../../..",
+      ]
+    }
+  }
+}
+
 # This source set provides the functionality required for tests, which on Windows
 # link the export thunks directly into the test binary.
 source_set("test_support") {
diff --git a/components/crash/content/app/chrome_crashpad_handler.cc b/components/crash/content/app/chrome_crashpad_handler.cc
new file mode 100644
index 0000000..ac121e2
--- /dev/null
+++ b/components/crash/content/app/chrome_crashpad_handler.cc
@@ -0,0 +1,9 @@
+// Copyright 2019 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "third_party/crashpad/crashpad/handler/handler_main.h"
+
+int main(int argc, char* argv[]) {
+  return crashpad::HandlerMain(argc, argv, nullptr);
+}
diff --git a/components/crash/content/app/crashpad_mac.mm b/components/crash/content/app/crashpad_mac.mm
index f079174..a76d264 100644
--- a/components/crash/content/app/crashpad_mac.mm
+++ b/components/crash/content/app/crashpad_mac.mm
@@ -124,7 +124,8 @@
     @autoreleasepool {
       base::FilePath framework_bundle_path = base::mac::FrameworkBundlePath();
       base::FilePath handler_path =
-          framework_bundle_path.Append("Helpers").Append("crashpad_handler");
+          framework_bundle_path.Append("Helpers").Append(
+              "chrome_crashpad_handler");
 
       // Is there a way to recover if this fails?
       CrashReporterClient* crash_reporter_client = GetCrashReporterClient();
diff --git a/content/shell/BUILD.gn b/content/shell/BUILD.gn
index 62224e0..329b06e 100644
--- a/content/shell/BUILD.gn
+++ b/content/shell/BUILD.gn
@@ -658,13 +658,13 @@
   bundle_data("content_shell_framework_helpers") {
     testonly = true
     sources = [
-      "$root_out_dir/crashpad_handler",
+      "$root_out_dir/chrome_crashpad_handler",
     ]
     outputs = [
       "{{bundle_contents_dir}}/Helpers/{{source_file_part}}",
     ]
     public_deps = [
-      "//third_party/crashpad/crashpad/handler:crashpad_handler",
+      "//components/crash/content/app:chrome_crashpad_handler",
     ]
   }
 
diff --git a/headless/BUILD.gn b/headless/BUILD.gn
index e09816c..5440be35 100644
--- a/headless/BUILD.gn
+++ b/headless/BUILD.gn
@@ -603,12 +603,12 @@
 if (is_mac) {
   copy("mac_helpers") {
     sources = [
-      "$root_out_dir/crashpad_handler",
+      "$root_out_dir/chrome_crashpad_handler",
     ]
 
     deps = [
       "//base",
-      "//third_party/crashpad/crashpad/handler:crashpad_handler",
+      "//components/crash/content/app:chrome_crashpad_handler",
     ]
 
     if (is_component_build) {