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 class ReportingServiceProxyImpl : public mojom::ReportingServiceProxy { | |
| 28 public: | |
| 29 ReportingServiceProxyImpl( | |
| 30 scoped_refptr<net::URLRequestContextGetter> request_context_getter) | |
| 31 : request_context_getter_(std::move(request_context_getter)) {} | |
| 32 | |
| 33 // mojom::ReportingServiceProxy: | |
| 34 void QueueReport(const GURL& url, | |
| 35 const std::string& group, | |
| 36 const std::string& type, | |
| 37 std::unique_ptr<base::Value> body) override { | |
| 38 // TODO(juliatuttle): Get rid of this intermediate step. | |
| 39 std::unique_ptr<const base::Value> const_body = | |
| 40 base::WrapUnique(body.release()); | |
| 41 | |
| 42 net::URLRequestContext* request_context = | |
| 43 request_context_getter_->GetURLRequestContext(); | |
| 44 if (!request_context) | |
| 45 return; | |
| 46 | |
| 47 net::ReportingService* reporting_service = | |
| 48 request_context->reporting_service(); | |
| 49 if (!reporting_service) | |
| 50 return; | |
| 51 | |
| 52 // TODO(juliatuttle): Get rid of this copy. | |
| 53 reporting_service->QueueReport(url, group, type, std::move(const_body)); | |
| 54 } | |
| 55 | |
| 56 private: | |
| 57 scoped_refptr<net::URLRequestContextGetter> request_context_getter_; | |
| 58 }; | |
| 59 | |
| 60 void CreateReportingServiceProxyOnNetworkTaskRunner( | |
| 61 mojom::ReportingServiceProxyRequest request, | |
| 62 scoped_refptr<net::URLRequestContextGetter> request_context_getter) { | |
| 63 mojo::MakeStrongBinding(base::MakeUnique<ReportingServiceProxyImpl>( | |
| 64 std::move(request_context_getter)), | |
| 65 std::move(request)); | |
| 66 } | |
| 67 | |
| 68 } // namespace | |
| 69 | |
| 70 // static | |
| 71 void CreateReportingServiceProxy( | |
| 72 StoragePartition* storage_partition, | |
| 73 const service_manager::BindSourceInfo& source_info, | |
| 74 mojom::ReportingServiceProxyRequest request) { | |
| 75 // TODO(juliatuttle): Make sure making our own scoped_refptr is okay. | |
|
jam
2017/06/30 01:43:40
yep this is safe, this is the pattern that it's us
| |
| 76 scoped_refptr<net::URLRequestContextGetter> request_context_getter( | |
| 77 storage_partition->GetURLRequestContext()); | |
| 78 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner( | |
| 79 request_context_getter->GetNetworkTaskRunner()); | |
| 80 network_task_runner->PostTask( | |
| 81 FROM_HERE, | |
| 82 base::BindOnce(&CreateReportingServiceProxyOnNetworkTaskRunner, | |
| 83 std::move(request), std::move(request_context_getter))); | |
| 84 } | |
| 85 | |
| 86 } // namespace content | |
| OLD | NEW |