Chromium Code Reviews| Index: content/browser/net/reporting_service_proxy.cc |
| diff --git a/content/browser/net/reporting_service_proxy.cc b/content/browser/net/reporting_service_proxy.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2e41335c11393e721213cd0c9043bcefc4bbe938 |
| --- /dev/null |
| +++ b/content/browser/net/reporting_service_proxy.cc |
| @@ -0,0 +1,106 @@ |
| +// Copyright 2017 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 "content/browser/net/reporting_service_proxy.h" |
| + |
| +#include <memory> |
| +#include <string> |
| +#include <utility> |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/values.h" |
| +#include "content/common/net/reporting.mojom.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/site_instance.h" |
| +#include "content/public/browser/storage_partition.h" |
| +#include "mojo/public/cpp/bindings/strong_binding.h" |
| +#include "net/reporting/reporting_service.h" |
| +#include "net/url_request/url_request_context.h" |
| +#include "net/url_request/url_request_context_getter.h" |
| +#include "url/gurl.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +std::string ReportTypeEnumToString(mojom::ReportingReportType type) { |
| + switch (type) { |
| + case mojom::ReportingReportType::CSP: |
| + return "csp"; |
| + case mojom::ReportingReportType::INTERVENTION: |
| + return "intervention"; |
| + case mojom::ReportingReportType::DEPRECATION: |
| + return "deprecation"; |
| + default: |
| + return ""; |
| + } |
| +} |
| + |
| +void QueueReportOnIOThread( |
| + scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| + const GURL& url, |
| + const std::string& group, |
| + const std::string& type, |
| + std::unique_ptr<const base::Value> body) { |
| + net::URLRequestContext* request_context = |
| + request_context_getter->GetURLRequestContext(); |
| + if (!request_context) |
| + return; |
| + |
| + net::ReportingService* reporting_service = |
| + request_context->reporting_service(); |
| + if (!reporting_service) |
| + return; |
| + |
| + // TODO(juliatuttle): Get rid of this copy. |
| + reporting_service->QueueReport(url, group, type, body->CreateDeepCopy()); |
| +} |
| + |
| +class ReportingServiceProxyImpl : public mojom::ReportingServiceProxy { |
| + public: |
| + ReportingServiceProxyImpl( |
| + scoped_refptr<net::URLRequestContextGetter> request_context_getter) |
| + : request_context_getter_(std::move(request_context_getter)) {} |
| + |
| + // mojom::ReportingServiceProxy: |
| + void QueueReport(const GURL& url, |
| + const std::string& group, |
| + mojom::ReportingReportType type_enum, |
| + std::unique_ptr<base::Value> body) override { |
| + std::string type_string = ReportTypeEnumToString(type_enum); |
|
dcheng
2017/05/24 00:23:12
Hmm... I didn't realize we were just going to turn
Julia Tuttle
2017/05/24 18:47:51
Oops! Will revert.
|
| + if (type_string.empty()) |
| + return; |
| + |
| + // TODO(juliatuttle): Get rid of this intermediate step. |
| + const base::Value* raw_const_body = body.release(); |
| + |
| + request_context_getter_->GetNetworkTaskRunner()->PostTask( |
| + FROM_HERE, |
| + base::BindOnce(&QueueReportOnIOThread, request_context_getter_, url, |
| + group, type_string, base::WrapUnique(raw_const_body))); |
| + } |
| + |
| + private: |
| + scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| +}; |
| + |
| +} // namespace |
| + |
| +// static |
| +void CreateReportingServiceProxy( |
| + SiteInstance* site_instance, |
| + const service_manager::BindSourceInfo& source_info, |
| + mojom::ReportingServiceProxyRequest request) { |
| + BrowserContext* browser_context = site_instance->GetBrowserContext(); |
| + StoragePartition* storage_partition = |
| + BrowserContext::GetStoragePartition(browser_context, site_instance); |
| + // TODO(juliatuttle): Make sure making our own scoped_refptr is okay. |
| + scoped_refptr<net::URLRequestContextGetter> request_context_getter( |
| + storage_partition->GetURLRequestContext()); |
| + mojo::MakeStrongBinding( |
| + base::MakeUnique<ReportingServiceProxyImpl>(request_context_getter), |
| + std::move(request)); |
| +} |
| + |
| +} // namespace content |