Reporting: Prevent report deleting while checking permissions

We now mark the reports as pending before checking whether we're allowed
to deliver them to the report collectors.  Since the permission check
might happen on a different thread, we need to ensure that the garbage
collector doesn't delete the reports on us while we're performing the
permissions check.

Bug: 818273, 818582
Change-Id: Iba92a58468f439589b56f1b2169c289e3473c71c
Reviewed-on: https://chromium-review.googlesource.com/948983
Reviewed-by: Julia Tuttle <[email protected]>
Commit-Queue: Douglas Creager <[email protected]>
Cr-Commit-Position: refs/heads/master@{#540960}
diff --git a/net/reporting/reporting_test_util.cc b/net/reporting/reporting_test_util.cc
index 94b7e46..e998b58 100644
--- a/net/reporting/reporting_test_util.cc
+++ b/net/reporting/reporting_test_util.cc
@@ -124,11 +124,27 @@
 void TestReportingDelegate::CanSendReports(
     std::set<url::Origin> origins,
     base::OnceCallback<void(std::set<url::Origin>)> result_callback) const {
+  if (pause_permissions_check_) {
+    saved_origins_ = std::move(origins);
+    permissions_check_callback_ = std::move(result_callback);
+    return;
+  }
+
   if (disallow_report_uploads_)
     origins.clear();
   std::move(result_callback).Run(std::move(origins));
 }
 
+bool TestReportingDelegate::PermissionsCheckPaused() const {
+  return !permissions_check_callback_.is_null();
+}
+
+void TestReportingDelegate::ResumePermissionsCheck() {
+  if (disallow_report_uploads_)
+    saved_origins_.clear();
+  std::move(permissions_check_callback_).Run(std::move(saved_origins_));
+}
+
 bool TestReportingDelegate::CanSetClient(const url::Origin& origin,
                                          const GURL& endpoint) const {
   return true;