| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/net/reporting_service_proxy.h" | 5 #include "content/browser/net/reporting_service_proxy.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 #include <string> | 8 #include <string> |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "content/common/net/reporting.mojom.h" | 13 #include "content/common/net/reporting.mojom.h" |
| 14 #include "content/public/browser/browser_context.h" | 14 #include "content/public/browser/browser_context.h" |
| 15 #include "content/public/browser/site_instance.h" | 15 #include "content/public/browser/site_instance.h" |
| 16 #include "content/public/browser/storage_partition.h" | 16 #include "content/public/browser/storage_partition.h" |
| 17 #include "mojo/public/cpp/bindings/strong_binding.h" | 17 #include "mojo/public/cpp/bindings/strong_binding.h" |
| 18 #include "net/reporting/reporting_report.h" |
| 18 #include "net/reporting/reporting_service.h" | 19 #include "net/reporting/reporting_service.h" |
| 19 #include "net/url_request/url_request_context.h" | 20 #include "net/url_request/url_request_context.h" |
| 20 #include "net/url_request/url_request_context_getter.h" | 21 #include "net/url_request/url_request_context_getter.h" |
| 21 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 22 | 23 |
| 23 namespace content { | 24 namespace content { |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 | 27 |
| 27 std::string ReportTypeEnumToString(mojom::ReportingReportType type) { | 28 std::string ReportTypeEnumToString(mojom::ReportingReportType type) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 38 } | 39 } |
| 39 | 40 |
| 40 void QueueReportOnIOThread( | 41 void QueueReportOnIOThread( |
| 41 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | 42 scoped_refptr<net::URLRequestContextGetter> request_context_getter, |
| 42 const GURL& url, | 43 const GURL& url, |
| 43 const std::string& group, | 44 const std::string& group, |
| 44 const std::string& type, | 45 const std::string& type, |
| 45 std::unique_ptr<const base::Value> body) { | 46 std::unique_ptr<const base::Value> body) { |
| 46 net::URLRequestContext* request_context = | 47 net::URLRequestContext* request_context = |
| 47 request_context_getter->GetURLRequestContext(); | 48 request_context_getter->GetURLRequestContext(); |
| 48 if (!request_context) | 49 if (!request_context) { |
| 50 net::ReportingReport::RecordReportDiscardedForNoURLRequestContext(); |
| 49 return; | 51 return; |
| 52 } |
| 50 | 53 |
| 51 net::ReportingService* reporting_service = | 54 net::ReportingService* reporting_service = |
| 52 request_context->reporting_service(); | 55 request_context->reporting_service(); |
| 53 if (!reporting_service) | 56 if (!reporting_service) { |
| 57 net::ReportingReport::RecordReportDiscardedForNoReportingService(); |
| 54 return; | 58 return; |
| 59 } |
| 55 | 60 |
| 56 // TODO(juliatuttle): Get rid of this copy. | 61 // TODO(juliatuttle): Get rid of this copy. |
| 57 reporting_service->QueueReport(url, group, type, body->CreateDeepCopy()); | 62 reporting_service->QueueReport(url, group, type, body->CreateDeepCopy()); |
| 58 } | 63 } |
| 59 | 64 |
| 60 class ReportingServiceProxyImpl : public mojom::ReportingServiceProxy { | 65 class ReportingServiceProxyImpl : public mojom::ReportingServiceProxy { |
| 61 public: | 66 public: |
| 62 ReportingServiceProxyImpl( | 67 ReportingServiceProxyImpl( |
| 63 scoped_refptr<net::URLRequestContextGetter> request_context_getter) | 68 scoped_refptr<net::URLRequestContextGetter> request_context_getter) |
| 64 : request_context_getter_(std::move(request_context_getter)) {} | 69 : request_context_getter_(std::move(request_context_getter)) {} |
| 65 | 70 |
| 66 // mojom::ReportingServiceProxy: | 71 // mojom::ReportingServiceProxy: |
| 67 void QueueReport(const GURL& url, | 72 void QueueReport(const GURL& url, |
| 68 const std::string& group, | 73 const std::string& group, |
| 69 mojom::ReportingReportType type_enum, | 74 mojom::ReportingReportType type_enum, |
| 70 std::unique_ptr<base::Value> body) override { | 75 std::unique_ptr<base::Value> body) override { |
| 71 std::string type_string = ReportTypeEnumToString(type_enum); | 76 std::string type_string = ReportTypeEnumToString(type_enum); |
| 72 if (type_string.empty()) | 77 if (type_string.empty()) { |
| 78 net::ReportingReport::RecordReportDiscardedForInvalidTypeFromRenderer(); |
| 73 return; | 79 return; |
| 80 } |
| 74 | 81 |
| 75 // TODO(juliatuttle): Get rid of this intermediate step. | 82 // TODO(juliatuttle): Get rid of this intermediate step. |
| 76 const base::Value* raw_const_body = body.release(); | 83 const base::Value* raw_const_body = body.release(); |
| 77 | 84 |
| 78 request_context_getter_->GetNetworkTaskRunner()->PostTask( | 85 request_context_getter_->GetNetworkTaskRunner()->PostTask( |
| 79 FROM_HERE, | 86 FROM_HERE, |
| 80 base::BindOnce(&QueueReportOnIOThread, request_context_getter_, url, | 87 base::BindOnce(&QueueReportOnIOThread, request_context_getter_, url, |
| 81 group, type_string, base::WrapUnique(raw_const_body))); | 88 group, type_string, base::WrapUnique(raw_const_body))); |
| 82 } | 89 } |
| 83 | 90 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 97 BrowserContext::GetStoragePartition(browser_context, site_instance); | 104 BrowserContext::GetStoragePartition(browser_context, site_instance); |
| 98 // TODO(juliatuttle): Make sure making our own scoped_refptr is okay. | 105 // TODO(juliatuttle): Make sure making our own scoped_refptr is okay. |
| 99 scoped_refptr<net::URLRequestContextGetter> request_context_getter( | 106 scoped_refptr<net::URLRequestContextGetter> request_context_getter( |
| 100 storage_partition->GetURLRequestContext()); | 107 storage_partition->GetURLRequestContext()); |
| 101 mojo::MakeStrongBinding( | 108 mojo::MakeStrongBinding( |
| 102 base::MakeUnique<ReportingServiceProxyImpl>(request_context_getter), | 109 base::MakeUnique<ReportingServiceProxyImpl>(request_context_getter), |
| 103 std::move(request)); | 110 std::move(request)); |
| 104 } | 111 } |
| 105 | 112 |
| 106 } // namespace content | 113 } // namespace content |
| OLD | NEW |