blob: a0acd86d20558bf2661d586c2e659179e6555cfd [file] [log] [blame]
juliatuttle95fe3cc52017-06-30 23:05:311// 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/public/browser/browser_context.h"
14#include "content/public/browser/site_instance.h"
15#include "content/public/browser/storage_partition.h"
16#include "mojo/public/cpp/bindings/strong_binding.h"
juliatuttle667c0bb2017-07-06 15:17:1317#include "net/reporting/reporting_report.h"
juliatuttle95fe3cc52017-06-30 23:05:3118#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 "third_party/WebKit/public/platform/reporting.mojom.h"
22#include "url/gurl.h"
23
24namespace content {
25
26namespace {
27
Julia Tuttle6ae4dcb2017-09-08 21:41:2628class ReportingServiceProxyImpl : public blink::mojom::ReportingServiceProxy {
juliatuttle95fe3cc52017-06-30 23:05:3129 public:
30 ReportingServiceProxyImpl(
31 scoped_refptr<net::URLRequestContextGetter> request_context_getter)
32 : request_context_getter_(std::move(request_context_getter)) {}
33
Julia Tuttle6ae4dcb2017-09-08 21:41:2634 // blink::mojom::ReportingServiceProxy:
35
36 void QueueInterventionReport(const GURL& url,
37 const std::string& message,
38 const std::string& source_file,
39 int line_number) override {
40 auto body = std::make_unique<base::DictionaryValue>();
41 body->SetString("message", message);
42 body->SetString("sourceFile", source_file);
43 body->SetInteger("lineNumber", line_number);
44 QueueReport(url, "default", "intervention", std::move(body));
45 }
46
47 void QueueDeprecationReport(const GURL& url,
48 const std::string& message,
49 const std::string& source_file,
50 int line_number) override {
51 auto body = std::make_unique<base::DictionaryValue>();
52 body->SetString("message", message);
53 body->SetString("sourceFile", source_file);
54 body->SetInteger("lineNumber", line_number);
55 QueueReport(url, "default", "deprecation", std::move(body));
56 }
57
Andy Paicuacb9f5c2017-10-27 06:49:4058 void QueueCspViolationReport(const GURL& url,
59 const std::string& group,
60 const std::string& document_uri,
61 const std::string& referrer,
62 const std::string& violated_directive,
63 const std::string& effective_directive,
64 const std::string& original_policy,
65 const std::string& disposition,
66 const std::string& blocked_uri,
67 int line_number,
68 int column_number,
69 const std::string& source_file,
70 int status_code,
71 const std::string& script_sample) override {
72 auto body = std::make_unique<base::DictionaryValue>();
73 body->SetString("document-uri", document_uri);
74 body->SetString("referrer", referrer);
75 body->SetString("violated-directive", violated_directive);
76 body->SetString("effective-directive", effective_directive);
77 body->SetString("original-policy", original_policy);
78 body->SetString("disposition", disposition);
79 body->SetString("blocked-uri", blocked_uri);
80 if (line_number)
81 body->SetInteger("line-number", line_number);
82 if (column_number)
83 body->SetInteger("column-number", column_number);
84 body->SetString("source-file", source_file);
85 if (status_code)
86 body->SetInteger("status-code", status_code);
87 body->SetString("script-sample", script_sample);
88 QueueReport(url, group, "csp", std::move(body));
89 }
90
Julia Tuttle6ae4dcb2017-09-08 21:41:2691 private:
juliatuttle95fe3cc52017-06-30 23:05:3192 void QueueReport(const GURL& url,
93 const std::string& group,
94 const std::string& type,
Julia Tuttle6ae4dcb2017-09-08 21:41:2695 std::unique_ptr<base::Value> body) {
juliatuttle95fe3cc52017-06-30 23:05:3196 net::URLRequestContext* request_context =
97 request_context_getter_->GetURLRequestContext();
juliatuttle667c0bb2017-07-06 15:17:1398 if (!request_context) {
99 net::ReportingReport::RecordReportDiscardedForNoURLRequestContext();
juliatuttle95fe3cc52017-06-30 23:05:31100 return;
juliatuttle667c0bb2017-07-06 15:17:13101 }
juliatuttle95fe3cc52017-06-30 23:05:31102
103 net::ReportingService* reporting_service =
104 request_context->reporting_service();
juliatuttle667c0bb2017-07-06 15:17:13105 if (!reporting_service) {
106 net::ReportingReport::RecordReportDiscardedForNoReportingService();
juliatuttle95fe3cc52017-06-30 23:05:31107 return;
juliatuttle667c0bb2017-07-06 15:17:13108 }
juliatuttle95fe3cc52017-06-30 23:05:31109
Jeremy Romand4b4d3d2017-09-01 19:12:58110 reporting_service->QueueReport(url, group, type, std::move(body));
juliatuttle95fe3cc52017-06-30 23:05:31111 }
112
juliatuttle95fe3cc52017-06-30 23:05:31113 scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
114};
115
116void CreateReportingServiceProxyOnNetworkTaskRunner(
Julia Tuttle6ae4dcb2017-09-08 21:41:26117 blink::mojom::ReportingServiceProxyRequest request,
juliatuttle95fe3cc52017-06-30 23:05:31118 scoped_refptr<net::URLRequestContextGetter> request_context_getter) {
Julia Tuttle6ae4dcb2017-09-08 21:41:26119 mojo::MakeStrongBinding(std::make_unique<ReportingServiceProxyImpl>(
juliatuttle95fe3cc52017-06-30 23:05:31120 std::move(request_context_getter)),
121 std::move(request));
122}
123
124} // namespace
125
126// static
127void CreateReportingServiceProxy(
128 StoragePartition* storage_partition,
Julia Tuttle6ae4dcb2017-09-08 21:41:26129 blink::mojom::ReportingServiceProxyRequest request) {
juliatuttle95fe3cc52017-06-30 23:05:31130 scoped_refptr<net::URLRequestContextGetter> request_context_getter(
131 storage_partition->GetURLRequestContext());
132 scoped_refptr<base::SingleThreadTaskRunner> network_task_runner(
133 request_context_getter->GetNetworkTaskRunner());
134 network_task_runner->PostTask(
135 FROM_HERE,
136 base::BindOnce(&CreateReportingServiceProxyOnNetworkTaskRunner,
137 std::move(request), std::move(request_context_getter)));
138}
139
140} // namespace content