blob: 1e59afac0b51f84b425d3734fa8e3a7f2da05d83 [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"
Eric Roman218c5ce2018-11-29 18:03:1314#include "content/public/browser/browser_thread.h"
15#include "content/public/browser/render_process_host.h"
juliatuttle95fe3cc52017-06-30 23:05:3116#include "content/public/browser/site_instance.h"
17#include "content/public/browser/storage_partition.h"
18#include "mojo/public/cpp/bindings/strong_binding.h"
juliatuttle667c0bb2017-07-06 15:17:1319#include "net/reporting/reporting_report.h"
juliatuttle95fe3cc52017-06-30 23:05:3120#include "net/reporting/reporting_service.h"
Douglas Creagerf6cb49f72018-07-19 20:14:5321#include "net/url_request/http_user_agent_settings.h"
juliatuttle95fe3cc52017-06-30 23:05:3122#include "net/url_request/url_request_context.h"
23#include "net/url_request/url_request_context_getter.h"
Eric Roman218c5ce2018-11-29 18:03:1324#include "services/network/public/mojom/network_context.mojom.h"
Julie Jeongeun Kim9912f2e2019-03-01 03:30:1825#include "third_party/blink/public/mojom/reporting/reporting.mojom.h"
juliatuttle95fe3cc52017-06-30 23:05:3126#include "url/gurl.h"
27
28namespace content {
29
30namespace {
31
Julia Tuttle6ae4dcb2017-09-08 21:41:2632class ReportingServiceProxyImpl : public blink::mojom::ReportingServiceProxy {
juliatuttle95fe3cc52017-06-30 23:05:3133 public:
Eric Roman218c5ce2018-11-29 18:03:1334 explicit ReportingServiceProxyImpl(int render_process_id)
35 : render_process_id_(render_process_id) {}
juliatuttle95fe3cc52017-06-30 23:05:3136
Julia Tuttle6ae4dcb2017-09-08 21:41:2637 // blink::mojom::ReportingServiceProxy:
38
39 void QueueInterventionReport(const GURL& url,
40 const std::string& message,
Paul Meyered85628b2018-06-14 00:33:0741 const base::Optional<std::string>& source_file,
Paul Meyer152136f2017-11-30 16:36:1742 int line_number,
43 int column_number) override {
Julia Tuttle6ae4dcb2017-09-08 21:41:2644 auto body = std::make_unique<base::DictionaryValue>();
45 body->SetString("message", message);
Paul Meyered85628b2018-06-14 00:33:0746 if (source_file)
47 body->SetString("sourceFile", *source_file);
48 if (line_number)
49 body->SetInteger("lineNumber", line_number);
50 if (column_number)
51 body->SetInteger("columnNumber", column_number);
Julia Tuttle6ae4dcb2017-09-08 21:41:2652 QueueReport(url, "default", "intervention", std::move(body));
53 }
54
55 void QueueDeprecationReport(const GURL& url,
Paul Meyere5df6cff2017-12-05 00:28:2656 const std::string& id,
Paul Meyered85628b2018-06-14 00:33:0757 base::Optional<base::Time> anticipatedRemoval,
Julia Tuttle6ae4dcb2017-09-08 21:41:2658 const std::string& message,
Paul Meyered85628b2018-06-14 00:33:0759 const base::Optional<std::string>& source_file,
Paul Meyer152136f2017-11-30 16:36:1760 int line_number,
61 int column_number) override {
Julia Tuttle6ae4dcb2017-09-08 21:41:2662 auto body = std::make_unique<base::DictionaryValue>();
Paul Meyere5df6cff2017-12-05 00:28:2663 body->SetString("id", id);
Paul Meyered85628b2018-06-14 00:33:0764 if (anticipatedRemoval)
65 body->SetDouble("anticipatedRemoval", anticipatedRemoval->ToDoubleT());
Julia Tuttle6ae4dcb2017-09-08 21:41:2666 body->SetString("message", message);
Paul Meyered85628b2018-06-14 00:33:0767 if (source_file)
68 body->SetString("sourceFile", *source_file);
69 if (line_number)
70 body->SetInteger("lineNumber", line_number);
71 if (column_number)
72 body->SetInteger("columnNumber", column_number);
Julia Tuttle6ae4dcb2017-09-08 21:41:2673 QueueReport(url, "default", "deprecation", std::move(body));
74 }
75
Andy Paicuacb9f5c2017-10-27 06:49:4076 void QueueCspViolationReport(const GURL& url,
77 const std::string& group,
78 const std::string& document_uri,
79 const std::string& referrer,
80 const std::string& violated_directive,
81 const std::string& effective_directive,
82 const std::string& original_policy,
83 const std::string& disposition,
84 const std::string& blocked_uri,
85 int line_number,
86 int column_number,
Paul Meyered85628b2018-06-14 00:33:0787 const base::Optional<std::string>& source_file,
Miyoung Shin4d4b2f92019-02-28 08:52:5888 uint16_t status_code,
Andy Paicuacb9f5c2017-10-27 06:49:4089 const std::string& script_sample) override {
90 auto body = std::make_unique<base::DictionaryValue>();
91 body->SetString("document-uri", document_uri);
92 body->SetString("referrer", referrer);
93 body->SetString("violated-directive", violated_directive);
94 body->SetString("effective-directive", effective_directive);
95 body->SetString("original-policy", original_policy);
96 body->SetString("disposition", disposition);
97 body->SetString("blocked-uri", blocked_uri);
98 if (line_number)
99 body->SetInteger("line-number", line_number);
100 if (column_number)
101 body->SetInteger("column-number", column_number);
Paul Meyered85628b2018-06-14 00:33:07102 if (source_file)
103 body->SetString("sourceFile", *source_file);
Andy Paicuacb9f5c2017-10-27 06:49:40104 if (status_code)
105 body->SetInteger("status-code", status_code);
106 body->SetString("script-sample", script_sample);
107 QueueReport(url, group, "csp", std::move(body));
108 }
109
Ian Clellandcda7f212018-08-13 14:04:36110 void QueueFeaturePolicyViolationReport(
111 const GURL& url,
Ian Clellandaa5ddc82018-11-22 15:55:59112 const std::string& policy_id,
Ian Clellandf482e772018-11-14 16:42:47113 const std::string& disposition,
Ian Clellandaa5ddc82018-11-22 15:55:59114 const base::Optional<std::string>& message,
Ian Clellandcda7f212018-08-13 14:04:36115 const base::Optional<std::string>& source_file,
116 int line_number,
117 int column_number) override {
118 auto body = std::make_unique<base::DictionaryValue>();
Ian Clellandaa5ddc82018-11-22 15:55:59119 body->SetString("policyId", policy_id);
Ian Clellandf482e772018-11-14 16:42:47120 body->SetString("disposition", disposition);
Ian Clellandaa5ddc82018-11-22 15:55:59121 if (message)
122 body->SetString("message", *message);
Ian Clellandcda7f212018-08-13 14:04:36123 if (source_file)
124 body->SetString("sourceFile", *source_file);
125 if (line_number)
126 body->SetInteger("lineNumber", line_number);
127 if (column_number)
128 body->SetInteger("columnNumber", column_number);
Ian Clellandaa5ddc82018-11-22 15:55:59129 QueueReport(url, "default", "feature-policy-violation", std::move(body));
Ian Clellandcda7f212018-08-13 14:04:36130 }
131
Julia Tuttle6ae4dcb2017-09-08 21:41:26132 private:
juliatuttle95fe3cc52017-06-30 23:05:31133 void QueueReport(const GURL& url,
134 const std::string& group,
135 const std::string& type,
Julia Tuttle6ae4dcb2017-09-08 21:41:26136 std::unique_ptr<base::Value> body) {
Eric Roman218c5ce2018-11-29 18:03:13137 auto* rph = RenderProcessHost::FromID(render_process_id_);
138 if (!rph)
juliatuttle95fe3cc52017-06-30 23:05:31139 return;
140
Eric Roman218c5ce2018-11-29 18:03:13141 rph->GetStoragePartition()->GetNetworkContext()->QueueReport(
142 type, group, url, /*user_agent=*/base::nullopt,
143 base::Value::FromUniquePtrValue(std::move(body)));
juliatuttle95fe3cc52017-06-30 23:05:31144 }
145
Eric Roman218c5ce2018-11-29 18:03:13146 int render_process_id_;
juliatuttle95fe3cc52017-06-30 23:05:31147};
148
juliatuttle95fe3cc52017-06-30 23:05:31149} // namespace
150
151// static
152void CreateReportingServiceProxy(
Eric Roman218c5ce2018-11-29 18:03:13153 int render_process_id,
Julia Tuttle6ae4dcb2017-09-08 21:41:26154 blink::mojom::ReportingServiceProxyRequest request) {
Eric Roman218c5ce2018-11-29 18:03:13155 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
156
157 mojo::MakeStrongBinding(
158 std::make_unique<ReportingServiceProxyImpl>(render_process_id),
159 std::move(request));
juliatuttle95fe3cc52017-06-30 23:05:31160}
161
162} // namespace content