Reporting: Wire ReportingDelegate into ChromeNetworkDelegate

Add, implement, and call some methods in NetworkDelegate to check
whether sites are allowed to store reports and clients.

BUG=704259,676324

Review-Url: https://codereview.chromium.org/2891133003
Cr-Commit-Position: refs/heads/master@{#473917}
diff --git a/net/reporting/reporting_context.cc b/net/reporting/reporting_context.cc
index f8b3820..6d022be4 100644
--- a/net/reporting/reporting_context.cc
+++ b/net/reporting/reporting_context.cc
@@ -40,7 +40,8 @@
       : ReportingContext(policy,
                          base::MakeUnique<base::DefaultClock>(),
                          base::MakeUnique<base::DefaultTickClock>(),
-                         ReportingUploader::Create(request_context)) {}
+                         ReportingUploader::Create(request_context),
+                         ReportingDelegate::Create(request_context)) {}
 };
 
 }  // namespace
@@ -72,12 +73,13 @@
 ReportingContext::ReportingContext(const ReportingPolicy& policy,
                                    std::unique_ptr<base::Clock> clock,
                                    std::unique_ptr<base::TickClock> tick_clock,
-                                   std::unique_ptr<ReportingUploader> uploader)
+                                   std::unique_ptr<ReportingUploader> uploader,
+                                   std::unique_ptr<ReportingDelegate> delegate)
     : policy_(policy),
       clock_(std::move(clock)),
       tick_clock_(std::move(tick_clock)),
       uploader_(std::move(uploader)),
-      delegate_(ReportingDelegate::Create()),
+      delegate_(std::move(delegate)),
       cache_(base::MakeUnique<ReportingCache>(this)),
       endpoint_manager_(base::MakeUnique<ReportingEndpointManager>(this)),
       delivery_agent_(ReportingDeliveryAgent::Create(this)),
diff --git a/net/reporting/reporting_context.h b/net/reporting/reporting_context.h
index c3517da..4467cb0 100644
--- a/net/reporting/reporting_context.h
+++ b/net/reporting/reporting_context.h
@@ -72,7 +72,8 @@
   ReportingContext(const ReportingPolicy& policy,
                    std::unique_ptr<base::Clock> clock,
                    std::unique_ptr<base::TickClock> tick_clock,
-                   std::unique_ptr<ReportingUploader> uploader);
+                   std::unique_ptr<ReportingUploader> uploader,
+                   std::unique_ptr<ReportingDelegate> delegate);
 
  private:
   ReportingPolicy policy_;
diff --git a/net/reporting/reporting_delegate.cc b/net/reporting/reporting_delegate.cc
index 88ff913..25dfdf14 100644
--- a/net/reporting/reporting_delegate.cc
+++ b/net/reporting/reporting_delegate.cc
@@ -5,6 +5,8 @@
 #include "net/reporting/reporting_delegate.h"
 
 #include "base/memory/ptr_util.h"
+#include "net/base/network_delegate.h"
+#include "net/url_request/url_request_context.h"
 
 namespace net {
 
@@ -12,30 +14,49 @@
 
 class ReportingDelegateImpl : public ReportingDelegate {
  public:
-  ReportingDelegateImpl() {}
+  ReportingDelegateImpl(URLRequestContext* request_context)
+      : request_context_(request_context) {
+    DCHECK(request_context);
+  }
 
   ~ReportingDelegateImpl() override {}
 
-  bool CanQueueReport(const url::Origin& origin) const override { return true; }
+  bool CanQueueReport(const url::Origin& origin) const override {
+    return network_delegate() &&
+           network_delegate()->CanQueueReportingReport(origin);
+  }
 
-  bool CanSendReport(const url::Origin& origin) const override { return true; }
+  bool CanSendReport(const url::Origin& origin) const override {
+    return network_delegate() &&
+           network_delegate()->CanSendReportingReport(origin);
+  }
 
   bool CanSetClient(const url::Origin& origin,
                     const GURL& endpoint) const override {
-    return true;
+    return network_delegate() &&
+           network_delegate()->CanSetReportingClient(origin, endpoint);
   }
 
   bool CanUseClient(const url::Origin& origin,
                     const GURL& endpoint) const override {
-    return true;
+    return network_delegate() &&
+           network_delegate()->CanUseReportingClient(origin, endpoint);
   }
+
+ private:
+  const NetworkDelegate* network_delegate() const {
+    return request_context_->network_delegate();
+  }
+
+  URLRequestContext* request_context_;
 };
 
 }  // namespace
 
 // static
-std::unique_ptr<ReportingDelegate> ReportingDelegate::Create() {
-  return base::MakeUnique<ReportingDelegateImpl>();
+std::unique_ptr<ReportingDelegate> ReportingDelegate::Create(
+    URLRequestContext* request_context) {
+  return base::MakeUnique<ReportingDelegateImpl>(request_context);
 }
 
 ReportingDelegate::~ReportingDelegate() {}
diff --git a/net/reporting/reporting_delegate.h b/net/reporting/reporting_delegate.h
index 7acf4c76..d2052f6e 100644
--- a/net/reporting/reporting_delegate.h
+++ b/net/reporting/reporting_delegate.h
@@ -18,6 +18,8 @@
 
 namespace net {
 
+class URLRequestContext;
+
 class NET_EXPORT ReportingDelegate {
  public:
   virtual ~ReportingDelegate();
@@ -38,7 +40,8 @@
   virtual bool CanUseClient(const url::Origin& origin,
                             const GURL& endpoint) const = 0;
 
-  static std::unique_ptr<ReportingDelegate> Create();
+  static std::unique_ptr<ReportingDelegate> Create(
+      URLRequestContext* request_context);
 };
 
 }  // namespace net
diff --git a/net/reporting/reporting_test_util.cc b/net/reporting/reporting_test_util.cc
index 2a7d1e0c..0b8237fb 100644
--- a/net/reporting/reporting_test_util.cc
+++ b/net/reporting/reporting_test_util.cc
@@ -17,6 +17,7 @@
 #include "net/reporting/reporting_cache.h"
 #include "net/reporting/reporting_client.h"
 #include "net/reporting/reporting_context.h"
+#include "net/reporting/reporting_delegate.h"
 #include "net/reporting/reporting_delivery_agent.h"
 #include "net/reporting/reporting_garbage_collector.h"
 #include "net/reporting/reporting_persister.h"
@@ -103,11 +104,34 @@
       url, json, callback, base::Bind(&ErasePendingUpload, &pending_uploads_)));
 }
 
+TestReportingDelegate::TestReportingDelegate() {}
+
+TestReportingDelegate::~TestReportingDelegate() {}
+
+bool TestReportingDelegate::CanQueueReport(const url::Origin& origin) const {
+  return true;
+}
+
+bool TestReportingDelegate::CanSendReport(const url::Origin& origin) const {
+  return true;
+}
+
+bool TestReportingDelegate::CanSetClient(const url::Origin& origin,
+                                         const GURL& endpoint) const {
+  return true;
+}
+
+bool TestReportingDelegate::CanUseClient(const url::Origin& origin,
+                                         const GURL& endpoint) const {
+  return true;
+}
+
 TestReportingContext::TestReportingContext(const ReportingPolicy& policy)
     : ReportingContext(policy,
                        base::MakeUnique<base::SimpleTestClock>(),
                        base::MakeUnique<base::SimpleTestTickClock>(),
-                       base::MakeUnique<TestReportingUploader>()),
+                       base::MakeUnique<TestReportingUploader>(),
+                       base::MakeUnique<TestReportingDelegate>()),
       delivery_timer_(new base::MockTimer(/* retain_user_task= */ false,
                                           /* is_repeating= */ false)),
       garbage_collection_timer_(
diff --git a/net/reporting/reporting_test_util.h b/net/reporting/reporting_test_util.h
index 6e64870..9343278 100644
--- a/net/reporting/reporting_test_util.h
+++ b/net/reporting/reporting_test_util.h
@@ -11,6 +11,7 @@
 
 #include "base/macros.h"
 #include "net/reporting/reporting_context.h"
+#include "net/reporting/reporting_delegate.h"
 #include "net/reporting/reporting_uploader.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
@@ -76,6 +77,28 @@
   DISALLOW_COPY_AND_ASSIGN(TestReportingUploader);
 };
 
+class TestReportingDelegate : public ReportingDelegate {
+ public:
+  TestReportingDelegate();
+
+  // ReportingDelegate implementation:
+
+  ~TestReportingDelegate() override;
+
+  bool CanQueueReport(const url::Origin& origin) const override;
+
+  bool CanSendReport(const url::Origin& origin) const override;
+
+  bool CanSetClient(const url::Origin& origin,
+                    const GURL& endpoint) const override;
+
+  bool CanUseClient(const url::Origin& origin,
+                    const GURL& endpoint) const override;
+
+ private:
+  DISALLOW_COPY_AND_ASSIGN(TestReportingDelegate);
+};
+
 // A test implementation of ReportingContext that uses test versions of
 // Clock, TickClock, Timer, and ReportingUploader.
 class TestReportingContext : public ReportingContext {
@@ -96,6 +119,9 @@
   TestReportingUploader* test_uploader() {
     return reinterpret_cast<TestReportingUploader*>(uploader());
   }
+  TestReportingDelegate* test_delegate() {
+    return reinterpret_cast<TestReportingDelegate*>(delegate());
+  }
 
  private:
   // Owned by the Persister and GarbageCollector, respectively, but referenced