juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 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/public/browser/browser_context.h" |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 14 | #include "content/public/browser/browser_thread.h" |
| 15 | #include "content/public/browser/render_process_host.h" |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 16 | #include "content/public/browser/site_instance.h" |
| 17 | #include "content/public/browser/storage_partition.h" |
| 18 | #include "mojo/public/cpp/bindings/strong_binding.h" |
juliatuttle | 667c0bb | 2017-07-06 15:17:13 | [diff] [blame] | 19 | #include "net/reporting/reporting_report.h" |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 20 | #include "net/reporting/reporting_service.h" |
Douglas Creager | f6cb49f7 | 2018-07-19 20:14:53 | [diff] [blame] | 21 | #include "net/url_request/http_user_agent_settings.h" |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 22 | #include "net/url_request/url_request_context.h" |
| 23 | #include "net/url_request/url_request_context_getter.h" |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 24 | #include "services/network/public/mojom/network_context.mojom.h" |
Julie Jeongeun Kim | 9912f2e | 2019-03-01 03:30:18 | [diff] [blame^] | 25 | #include "third_party/blink/public/mojom/reporting/reporting.mojom.h" |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 26 | #include "url/gurl.h" |
| 27 | |
| 28 | namespace content { |
| 29 | |
| 30 | namespace { |
| 31 | |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 32 | class ReportingServiceProxyImpl : public blink::mojom::ReportingServiceProxy { |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 33 | public: |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 34 | explicit ReportingServiceProxyImpl(int render_process_id) |
| 35 | : render_process_id_(render_process_id) {} |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 36 | |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 37 | // blink::mojom::ReportingServiceProxy: |
| 38 | |
| 39 | void QueueInterventionReport(const GURL& url, |
| 40 | const std::string& message, |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 41 | const base::Optional<std::string>& source_file, |
Paul Meyer | 152136f | 2017-11-30 16:36:17 | [diff] [blame] | 42 | int line_number, |
| 43 | int column_number) override { |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 44 | auto body = std::make_unique<base::DictionaryValue>(); |
| 45 | body->SetString("message", message); |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 46 | 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 Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 52 | QueueReport(url, "default", "intervention", std::move(body)); |
| 53 | } |
| 54 | |
| 55 | void QueueDeprecationReport(const GURL& url, |
Paul Meyer | e5df6cff | 2017-12-05 00:28:26 | [diff] [blame] | 56 | const std::string& id, |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 57 | base::Optional<base::Time> anticipatedRemoval, |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 58 | const std::string& message, |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 59 | const base::Optional<std::string>& source_file, |
Paul Meyer | 152136f | 2017-11-30 16:36:17 | [diff] [blame] | 60 | int line_number, |
| 61 | int column_number) override { |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 62 | auto body = std::make_unique<base::DictionaryValue>(); |
Paul Meyer | e5df6cff | 2017-12-05 00:28:26 | [diff] [blame] | 63 | body->SetString("id", id); |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 64 | if (anticipatedRemoval) |
| 65 | body->SetDouble("anticipatedRemoval", anticipatedRemoval->ToDoubleT()); |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 66 | body->SetString("message", message); |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 67 | 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 Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 73 | QueueReport(url, "default", "deprecation", std::move(body)); |
| 74 | } |
| 75 | |
Andy Paicu | acb9f5c | 2017-10-27 06:49:40 | [diff] [blame] | 76 | 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 Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 87 | const base::Optional<std::string>& source_file, |
Miyoung Shin | 4d4b2f9 | 2019-02-28 08:52:58 | [diff] [blame] | 88 | uint16_t status_code, |
Andy Paicu | acb9f5c | 2017-10-27 06:49:40 | [diff] [blame] | 89 | 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 Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 102 | if (source_file) |
| 103 | body->SetString("sourceFile", *source_file); |
Andy Paicu | acb9f5c | 2017-10-27 06:49:40 | [diff] [blame] | 104 | 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 Clelland | cda7f21 | 2018-08-13 14:04:36 | [diff] [blame] | 110 | void QueueFeaturePolicyViolationReport( |
| 111 | const GURL& url, |
Ian Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 112 | const std::string& policy_id, |
Ian Clelland | f482e77 | 2018-11-14 16:42:47 | [diff] [blame] | 113 | const std::string& disposition, |
Ian Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 114 | const base::Optional<std::string>& message, |
Ian Clelland | cda7f21 | 2018-08-13 14:04:36 | [diff] [blame] | 115 | 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 Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 119 | body->SetString("policyId", policy_id); |
Ian Clelland | f482e77 | 2018-11-14 16:42:47 | [diff] [blame] | 120 | body->SetString("disposition", disposition); |
Ian Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 121 | if (message) |
| 122 | body->SetString("message", *message); |
Ian Clelland | cda7f21 | 2018-08-13 14:04:36 | [diff] [blame] | 123 | 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 Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 129 | QueueReport(url, "default", "feature-policy-violation", std::move(body)); |
Ian Clelland | cda7f21 | 2018-08-13 14:04:36 | [diff] [blame] | 130 | } |
| 131 | |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 132 | private: |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 133 | void QueueReport(const GURL& url, |
| 134 | const std::string& group, |
| 135 | const std::string& type, |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 136 | std::unique_ptr<base::Value> body) { |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 137 | auto* rph = RenderProcessHost::FromID(render_process_id_); |
| 138 | if (!rph) |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 139 | return; |
| 140 | |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 141 | rph->GetStoragePartition()->GetNetworkContext()->QueueReport( |
| 142 | type, group, url, /*user_agent=*/base::nullopt, |
| 143 | base::Value::FromUniquePtrValue(std::move(body))); |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 144 | } |
| 145 | |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 146 | int render_process_id_; |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 147 | }; |
| 148 | |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 149 | } // namespace |
| 150 | |
| 151 | // static |
| 152 | void CreateReportingServiceProxy( |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 153 | int render_process_id, |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 154 | blink::mojom::ReportingServiceProxyRequest request) { |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 155 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 156 | |
| 157 | mojo::MakeStrongBinding( |
| 158 | std::make_unique<ReportingServiceProxyImpl>(render_process_id), |
| 159 | std::move(request)); |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | } // namespace content |