From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Jeremy Apthorp <jeremya@chromium.org>
Date: Thu, 30 Apr 2020 10:08:06 -0700
Subject: crash: allow setting more options

This allows the client of //components/crash to set upload url,
rate-limiting, compression and global annotations.

This should be upstreamed.

diff --git a/components/crash/core/app/breakpad_linux.cc b/components/crash/core/app/breakpad_linux.cc
index 192b0a7f137f311abb6a991f997e11f5eba52256..364af690879d79d25d118d5b2fdbaabe21a1c52d 100644
--- a/components/crash/core/app/breakpad_linux.cc
+++ b/components/crash/core/app/breakpad_linux.cc
@@ -103,9 +103,18 @@ namespace {
 // while we do have functions to deal with uint64_t's.
 uint64_t g_crash_loop_before_time = 0;
 #else
-const char kUploadURL[] = "https://clients2.google.com/cr/report";
+const char kDefaultUploadURL[] = "https://clients2.google.com/cr/report";
+char* g_upload_url = nullptr;
 #endif
 
+void SetUploadURL(const std::string& url) {
+  const size_t url_len = url.size() + 1;
+  DCHECK(!g_upload_url);
+  g_upload_url = new char[url_len];
+  strncpy(g_upload_url, url.c_str(), url_len);
+}
+
+bool g_is_node = false;
 bool g_is_crash_reporter_enabled = false;
 uint64_t g_process_start_time = 0;
 pid_t g_pid = 0;
@@ -1398,13 +1407,15 @@ void ExecUploadProcessOrTerminate(const BreakpadInfo& info,
   char* status_fd_path =
       StringFromPrefixAndUint("/dev/fd/", upload_status_fd, allocator);
 
+  const char* upload_url = g_upload_url ? g_upload_url : kDefaultUploadURL;
+
   static const char kWgetBinary[] = "/usr/bin/wget";
   const char* args[] = {
     kWgetBinary,
     header_content_encoding,
     header_content_type,
     post_file,
-    kUploadURL,
+    upload_url,
     "--timeout=10",  // Set a timeout so we don't hang forever.
     "--tries=1",     // Don't retry if the upload fails.
     "-O",  // Output reply to the file descriptor path.
@@ -2037,6 +2048,10 @@ void InitCrashReporter(const std::string& process_type) {
 #endif
       process_type.empty();
 
+  std::string upload_url;
+  if (GetCrashReporterClient()->GetUploadUrl(&upload_url))
+    SetUploadURL(upload_url);
+
   if (is_browser_process) {
     bool enable_breakpad = GetCrashReporterClient()->GetCollectStatsConsent() ||
                            GetCrashReporterClient()->IsRunningUnattended();
diff --git a/components/crash/core/app/crash_reporter_client.cc b/components/crash/core/app/crash_reporter_client.cc
index e778f68af30f8c071d1360d06b553cc957160c5b..b7f6ca60ef9c2367989d39e1268bf9f9a517951c 100644
--- a/components/crash/core/app/crash_reporter_client.cc
+++ b/components/crash/core/app/crash_reporter_client.cc
@@ -148,6 +148,17 @@ bool CrashReporterClient::ReportingIsEnforcedByPolicy(bool* breakpad_enabled) {
   return false;
 }
 
+bool CrashReporterClient::GetShouldRateLimit() {
+  return true;
+}
+
+bool CrashReporterClient::GetShouldCompressUploads() {
+  return true;
+}
+
+void CrashReporterClient::GetProcessSimpleAnnotations(std::map<std::string, std::string>* annotations) {
+}
+
 #if defined(OS_ANDROID)
 unsigned int CrashReporterClient::GetCrashDumpPercentage() {
   return 100;
@@ -196,6 +207,10 @@ void CrashReporterClient::GetSanitizationInformation(
 }
 #endif
 
+bool CrashReporterClient::GetUploadUrl(std::string* url) {
+  return false;
+}
+
 bool CrashReporterClient::ShouldMonitorCrashHandlerExpensively() {
   return false;
 }
diff --git a/components/crash/core/app/crash_reporter_client.h b/components/crash/core/app/crash_reporter_client.h
index 9cc78fc2584061d26fd1e83b3ebf5ada0a12c138..aa63d7b95c37e4a143283450798b8bd500dc17a1 100644
--- a/components/crash/core/app/crash_reporter_client.h
+++ b/components/crash/core/app/crash_reporter_client.h
@@ -5,6 +5,7 @@
 #ifndef COMPONENTS_CRASH_CORE_APP_CRASH_REPORTER_CLIENT_H_
 #define COMPONENTS_CRASH_CORE_APP_CRASH_REPORTER_CLIENT_H_
 
+#include <map>
 #include <string>
 
 #include "base/strings/string16.h"
@@ -153,6 +154,19 @@ class CrashReporterClient {
   // that case, |breakpad_enabled| is set to the value enforced by policies.
   virtual bool ReportingIsEnforcedByPolicy(bool* breakpad_enabled);
 
+  // Returns true if crash uploads should be rate limited. If false, no
+  // throttling will be applied for uploads.
+  virtual bool GetShouldRateLimit();
+
+  // Returns true if crash uploads should be compressed with gzip. If false,
+  // reports will be uploaded uncompressed.
+  virtual bool GetShouldCompressUploads();
+
+  // Allows the client to add or edit global annotations passed to the crashpad
+  // handler.
+  virtual void GetProcessSimpleAnnotations(
+      std::map<std::string, std::string>* annotations);
+
 #if defined(OS_ANDROID)
   // Used by WebView to sample crashes without generating the unwanted dumps. If
   // the returned value is less than 100, crash dumping will be sampled to that
@@ -194,6 +208,9 @@ class CrashReporterClient {
       bool* sanitize_stacks);
 #endif
 
+  // Override the default upload url. Returns true if overridden.
+  virtual bool GetUploadUrl(std::string* url);
+
   // This method should return true to configure a crash reporter capable of
   // monitoring itself for its own crashes to do so, even if self-monitoring
   // would be expensive. "Expensive" self-monitoring dedicates an additional
diff --git a/components/crash/core/app/crashpad_mac.mm b/components/crash/core/app/crashpad_mac.mm
index b579521d55860823722df2ee849f6b1628b3c950..f4f71e5174cf8fb706a2f8385252ba877d1a03a7 100644
--- a/components/crash/core/app/crashpad_mac.mm
+++ b/components/crash/core/app/crashpad_mac.mm
@@ -67,6 +67,8 @@ std::map<std::string, std::string> GetProcessSimpleAnnotations() {
     }  // @autoreleasepool
     return process_annotations;
   }();
+  CrashReporterClient* crash_reporter_client = GetCrashReporterClient();
+  crash_reporter_client->GetProcessSimpleAnnotations(&annotations);
   return annotations;
 }
 
@@ -140,9 +142,17 @@ base::FilePath PlatformCrashpadInitialization(
 #else
       std::string url;
 #endif
+      crash_reporter_client->GetUploadUrl(&url);
 
       std::vector<std::string> arguments;
 
+      if (!crash_reporter_client->GetShouldRateLimit()) {
+        arguments.push_back("--no-rate-limit");
+      }
+      if (!crash_reporter_client->GetShouldCompressUploads()) {
+        arguments.push_back("--no-upload-gzip");
+      }
+
       if (crash_reporter_client->ShouldMonitorCrashHandlerExpensively()) {
         arguments.push_back("--monitor-self");
       }
diff --git a/components/crash/core/app/crashpad_win.cc b/components/crash/core/app/crashpad_win.cc
index 669f5bea844d75f0e5c34b58994f4cfb8e856af0..8c1fb8fb8f2e02466b51ef08de25b056f8b2052d 100644
--- a/components/crash/core/app/crashpad_win.cc
+++ b/components/crash/core/app/crashpad_win.cc
@@ -84,12 +84,14 @@ base::FilePath PlatformCrashpadInitialization(
 
     std::map<std::string, std::string> process_annotations;
     GetPlatformCrashpadAnnotations(&process_annotations);
+    crash_reporter_client->GetProcessSimpleAnnotations(&process_annotations);
 
 #if BUILDFLAG(GOOGLE_CHROME_BRANDING)
     std::string url = "https://clients2.google.com/cr/report";
 #else
     std::string url;
 #endif
+    crash_reporter_client->GetUploadUrl(&url);
 
     // Allow the crash server to be overridden for testing. If the variable
     // isn't present in the environment then the default URL will remain.
@@ -126,6 +128,13 @@ base::FilePath PlatformCrashpadInitialization(
 
     std::vector<std::string> arguments(start_arguments);
 
+    if (!crash_reporter_client->GetShouldRateLimit()) {
+      arguments.push_back("--no-rate-limit");
+    }
+    if (!crash_reporter_client->GetShouldCompressUploads()) {
+      arguments.push_back("--no-upload-gzip");
+    }
+
     if (crash_reporter_client->ShouldMonitorCrashHandlerExpensively()) {
       arguments.push_back("--monitor-self");
       for (const std::string& start_argument : start_arguments) {
