Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/net/reporting_service_proxy.h" | |
| 6 | |
| 7 #include <memory> | |
| 8 #include <string> | |
| 9 #include <utility> | |
| 10 | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/values.h" | |
| 13 #include "content/common/net/reporting.mojom.h" | |
| 14 #include "content/public/browser/browser_context.h" | |
| 15 #include "content/public/browser/site_instance.h" | |
| 16 #include "content/public/browser/storage_partition.h" | |
| 17 #include "mojo/public/cpp/bindings/strong_binding.h" | |
| 18 #include "net/reporting/reporting_service.h" | |
| 19 #include "net/url_request/url_request_context.h" | |
| 20 #include "net/url_request/url_request_context_getter.h" | |
| 21 #include "url/gurl.h" | |
| 22 | |
| 23 namespace content { | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 std::string ReportTypeEnumToString(mojom::ReportingReportType type) { | |
| 28 switch (type) { | |
| 29 case mojom::ReportingReportType::CSP: | |
| 30 return "csp"; | |
| 31 case mojom::ReportingReportType::INTERVENTION: | |
| 32 return "intervention"; | |
| 33 case mojom::ReportingReportType::DEPRECATION: | |
| 34 return "deprecation"; | |
| 35 default: | |
| 36 return ""; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 void QueueReportOnIOThread( | |
| 41 scoped_refptr<net::URLRequestContextGetter> request_context_getter, | |
| 42 const GURL& url, | |
| 43 const std::string& group, | |
| 44 const std::string& type, | |
| 45 std::unique_ptr<const base::Value> body) { | |
| 46 net::URLRequestContext* request_context = | |
| 47 request_context_getter->GetURLRequestContext(); | |
| 48 if (!request_context) | |
| 49 return; | |
| 50 | |
| 51 net::ReportingService* reporting_service = | |
| 52 request_context->reporting_service(); | |
| 53 if (!reporting_service) | |
| 54 return; | |
| 55 | |
| 56 // TODO(juliatuttle): Get rid of this copy. | |
| 57 reporting_service->QueueReport(url, group, type, body->CreateDeepCopy()); | |
| 58 } | |
| 59 | |
| 60 class ReportingServiceProxyImpl : public mojom::ReportingServiceProxy { | |
| 61 public: | |
| 62 ReportingServiceProxyImpl( | |
| 63 scoped_refptr<net::URLRequestContextGetter> request_context_getter) | |
| 64 : request_context_getter_(std::move(request_context_getter)) {} | |
| 65 | |
| 66 // mojom::ReportingServiceProxy: | |
| 67 void QueueReport(const GURL& url, | |
| 68 const std::string& group, | |
| 69 mojom::ReportingReportType type_enum, | |
| 70 std::unique_ptr<base::Value> body) override { | |
| 71 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.
| |
| 72 if (type_string.empty()) | |
| 73 return; | |
| 74 | |
| 75 // TODO(juliatuttle): Get rid of this intermediate step. | |
| 76 const base::Value* raw_const_body = body.release(); | |
| 77 | |
| 78 request_context_getter_->GetNetworkTaskRunner()->PostTask( | |
| 79 FROM_HERE, | |
| 80 base::BindOnce(&QueueReportOnIOThread, request_context_getter_, url, | |
| 81 group, type_string, base::WrapUnique(raw_const_body))); | |
| 82 } | |
| 83 | |
| 84 private: | |
| 85 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 86 }; | |
| 87 | |
| 88 } // namespace | |
| 89 | |
| 90 // static | |
| 91 void CreateReportingServiceProxy( | |
| 92 SiteInstance* site_instance, | |
| 93 const service_manager::BindSourceInfo& source_info, | |
| 94 mojom::ReportingServiceProxyRequest request) { | |
| 95 BrowserContext* browser_context = site_instance->GetBrowserContext(); | |
| 96 StoragePartition* storage_partition = | |
| 97 BrowserContext::GetStoragePartition(browser_context, site_instance); | |
| 98 // TODO(juliatuttle): Make sure making our own scoped_refptr is okay. | |
| 99 scoped_refptr<net::URLRequestContextGetter> request_context_getter( | |
| 100 storage_partition->GetURLRequestContext()); | |
| 101 mojo::MakeStrongBinding( | |
| 102 base::MakeUnique<ReportingServiceProxyImpl>(request_context_getter), | |
| 103 std::move(request)); | |
| 104 } | |
| 105 | |
| 106 } // namespace content | |
| OLD | NEW |