Commit 218c5ce5 authored by Eric Roman's avatar Eric Roman Committed by Commit Bot

Use NetworkContext to queue reports.

Bug: 903948
Change-Id: Ida8e955b212f26c9938a43cb196d9f90aac3404b
Reviewed-on: https://chromium-review.googlesource.com/c/1351098
Commit-Queue: John Abd-El-Malek <jam@chromium.org>
Reviewed-by: default avatarJohn Abd-El-Malek <jam@chromium.org>
Cr-Commit-Position: refs/heads/master@{#612271}
parent 30b56844
...@@ -11,6 +11,8 @@ ...@@ -11,6 +11,8 @@
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "base/values.h" #include "base/values.h"
#include "content/public/browser/browser_context.h" #include "content/public/browser/browser_context.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/site_instance.h" #include "content/public/browser/site_instance.h"
#include "content/public/browser/storage_partition.h" #include "content/public/browser/storage_partition.h"
#include "mojo/public/cpp/bindings/strong_binding.h" #include "mojo/public/cpp/bindings/strong_binding.h"
...@@ -19,6 +21,7 @@ ...@@ -19,6 +21,7 @@
#include "net/url_request/http_user_agent_settings.h" #include "net/url_request/http_user_agent_settings.h"
#include "net/url_request/url_request_context.h" #include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h" #include "net/url_request/url_request_context_getter.h"
#include "services/network/public/mojom/network_context.mojom.h"
#include "third_party/blink/public/platform/reporting.mojom.h" #include "third_party/blink/public/platform/reporting.mojom.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -28,9 +31,8 @@ namespace { ...@@ -28,9 +31,8 @@ namespace {
class ReportingServiceProxyImpl : public blink::mojom::ReportingServiceProxy { class ReportingServiceProxyImpl : public blink::mojom::ReportingServiceProxy {
public: public:
ReportingServiceProxyImpl( explicit ReportingServiceProxyImpl(int render_process_id)
scoped_refptr<net::URLRequestContextGetter> request_context_getter) : render_process_id_(render_process_id) {}
: request_context_getter_(std::move(request_context_getter)) {}
// blink::mojom::ReportingServiceProxy: // blink::mojom::ReportingServiceProxy:
...@@ -132,55 +134,29 @@ class ReportingServiceProxyImpl : public blink::mojom::ReportingServiceProxy { ...@@ -132,55 +134,29 @@ class ReportingServiceProxyImpl : public blink::mojom::ReportingServiceProxy {
const std::string& group, const std::string& group,
const std::string& type, const std::string& type,
std::unique_ptr<base::Value> body) { std::unique_ptr<base::Value> body) {
net::URLRequestContext* request_context = auto* rph = RenderProcessHost::FromID(render_process_id_);
request_context_getter_->GetURLRequestContext(); if (!rph)
if (!request_context) {
net::ReportingReport::RecordReportDiscardedForNoURLRequestContext();
return; return;
}
net::ReportingService* reporting_service = rph->GetStoragePartition()->GetNetworkContext()->QueueReport(
request_context->reporting_service(); type, group, url, /*user_agent=*/base::nullopt,
if (!reporting_service) { base::Value::FromUniquePtrValue(std::move(body)));
net::ReportingReport::RecordReportDiscardedForNoReportingService();
return;
}
// Depth is only non-zero for NEL reports, and those can't come from the
// renderer.
std::string user_agent;
if (request_context->http_user_agent_settings() != nullptr)
user_agent = request_context->http_user_agent_settings()->GetUserAgent();
reporting_service->QueueReport(url, user_agent, group, type,
std::move(body),
/* depth= */ 0);
} }
scoped_refptr<net::URLRequestContextGetter> request_context_getter_; int render_process_id_;
}; };
void CreateReportingServiceProxyOnNetworkTaskRunner(
blink::mojom::ReportingServiceProxyRequest request,
scoped_refptr<net::URLRequestContextGetter> request_context_getter) {
mojo::MakeStrongBinding(std::make_unique<ReportingServiceProxyImpl>(
std::move(request_context_getter)),
std::move(request));
}
} // namespace } // namespace
// static // static
void CreateReportingServiceProxy( void CreateReportingServiceProxy(
StoragePartition* storage_partition, int render_process_id,
blink::mojom::ReportingServiceProxyRequest request) { blink::mojom::ReportingServiceProxyRequest request) {
scoped_refptr<net::URLRequestContextGetter> request_context_getter( DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
storage_partition->GetURLRequestContext());
scoped_refptr<base::SingleThreadTaskRunner> network_task_runner( mojo::MakeStrongBinding(
request_context_getter->GetNetworkTaskRunner()); std::make_unique<ReportingServiceProxyImpl>(render_process_id),
network_task_runner->PostTask( std::move(request));
FROM_HERE,
base::BindOnce(&CreateReportingServiceProxyOnNetworkTaskRunner,
std::move(request), std::move(request_context_getter)));
} }
} // namespace content } // namespace content
...@@ -9,10 +9,10 @@ ...@@ -9,10 +9,10 @@
namespace content { namespace content {
class StoragePartition; // Binds a mojom::ReportingServiceProxy to |request| that queues reports using
// |render_process_id|'s NetworkContext. This must be called on the UI thread.
void CreateReportingServiceProxy( void CreateReportingServiceProxy(
StoragePartition* storage_partition, int render_process_id,
blink::mojom::ReportingServiceProxyRequest request); blink::mojom::ReportingServiceProxyRequest request);
} // namespace content } // namespace content
......
...@@ -2223,8 +2223,9 @@ void RenderProcessHostImpl::RegisterMojoInterfaces() { ...@@ -2223,8 +2223,9 @@ void RenderProcessHostImpl::RegisterMojoInterfaces() {
storage_partition_impl_->GetGeneratedCodeCacheContext()))); storage_partition_impl_->GetGeneratedCodeCacheContext())));
#if BUILDFLAG(ENABLE_REPORTING) #if BUILDFLAG(ENABLE_REPORTING)
registry->AddInterface( AddUIThreadInterface(
base::Bind(&CreateReportingServiceProxy, storage_partition_impl_)); registry.get(),
base::BindRepeating(&CreateReportingServiceProxy, GetID()));
#endif // BUILDFLAG(ENABLE_REPORTING) #endif // BUILDFLAG(ENABLE_REPORTING)
registry->AddInterface(base::BindRepeating( registry->AddInterface(base::BindRepeating(
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment