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" |
Miyoung Shin | d3cb464 | 2019-08-31 01:24:22 | [diff] [blame] | 18 | #include "mojo/public/cpp/bindings/self_owned_receiver.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" |
| 21 | #include "net/url_request/url_request_context.h" |
| 22 | #include "net/url_request/url_request_context_getter.h" |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 23 | #include "services/network/public/mojom/network_context.mojom.h" |
Julie Jeongeun Kim | 9912f2e | 2019-03-01 03:30:18 | [diff] [blame] | 24 | #include "third_party/blink/public/mojom/reporting/reporting.mojom.h" |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 25 | #include "url/gurl.h" |
| 26 | |
| 27 | namespace content { |
| 28 | |
| 29 | namespace { |
| 30 | |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 31 | class ReportingServiceProxyImpl : public blink::mojom::ReportingServiceProxy { |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 32 | public: |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 33 | explicit ReportingServiceProxyImpl(int render_process_id) |
| 34 | : render_process_id_(render_process_id) {} |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 35 | |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 36 | // blink::mojom::ReportingServiceProxy: |
| 37 | |
| 38 | void QueueInterventionReport(const GURL& url, |
Paul Meyer | 3687257 | 2019-05-08 17:24:27 | [diff] [blame] | 39 | const std::string& id, |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 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>(); |
Paul Meyer | 3687257 | 2019-05-08 17:24:27 | [diff] [blame] | 45 | body->SetString("id", id); |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 46 | body->SetString("message", message); |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 47 | if (source_file) |
| 48 | body->SetString("sourceFile", *source_file); |
| 49 | if (line_number) |
| 50 | body->SetInteger("lineNumber", line_number); |
| 51 | if (column_number) |
| 52 | body->SetInteger("columnNumber", column_number); |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 53 | QueueReport(url, "default", "intervention", std::move(body)); |
| 54 | } |
| 55 | |
| 56 | void QueueDeprecationReport(const GURL& url, |
Paul Meyer | e5df6cff | 2017-12-05 00:28:26 | [diff] [blame] | 57 | const std::string& id, |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 58 | base::Optional<base::Time> anticipatedRemoval, |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 59 | const std::string& message, |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 60 | const base::Optional<std::string>& source_file, |
Paul Meyer | 152136f | 2017-11-30 16:36:17 | [diff] [blame] | 61 | int line_number, |
| 62 | int column_number) override { |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 63 | auto body = std::make_unique<base::DictionaryValue>(); |
Paul Meyer | e5df6cff | 2017-12-05 00:28:26 | [diff] [blame] | 64 | body->SetString("id", id); |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 65 | if (anticipatedRemoval) |
| 66 | body->SetDouble("anticipatedRemoval", anticipatedRemoval->ToDoubleT()); |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 67 | body->SetString("message", message); |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 68 | if (source_file) |
| 69 | body->SetString("sourceFile", *source_file); |
| 70 | if (line_number) |
| 71 | body->SetInteger("lineNumber", line_number); |
| 72 | if (column_number) |
| 73 | body->SetInteger("columnNumber", column_number); |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 74 | QueueReport(url, "default", "deprecation", std::move(body)); |
| 75 | } |
| 76 | |
Andy Paicu | acb9f5c | 2017-10-27 06:49:40 | [diff] [blame] | 77 | void QueueCspViolationReport(const GURL& url, |
| 78 | const std::string& group, |
Paul Meyer | 079b845 | 2019-05-08 19:32:28 | [diff] [blame] | 79 | const std::string& document_url, |
| 80 | const base::Optional<std::string>& referrer, |
| 81 | const base::Optional<std::string>& blocked_url, |
Andy Paicu | acb9f5c | 2017-10-27 06:49:40 | [diff] [blame] | 82 | const std::string& effective_directive, |
| 83 | const std::string& original_policy, |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 84 | const base::Optional<std::string>& source_file, |
Paul Meyer | 079b845 | 2019-05-08 19:32:28 | [diff] [blame] | 85 | const base::Optional<std::string>& script_sample, |
| 86 | const std::string& disposition, |
Miyoung Shin | 4d4b2f9 | 2019-02-28 08:52:58 | [diff] [blame] | 87 | uint16_t status_code, |
Paul Meyer | 079b845 | 2019-05-08 19:32:28 | [diff] [blame] | 88 | int line_number, |
| 89 | int column_number) override { |
Andy Paicu | acb9f5c | 2017-10-27 06:49:40 | [diff] [blame] | 90 | auto body = std::make_unique<base::DictionaryValue>(); |
Paul Meyer | 079b845 | 2019-05-08 19:32:28 | [diff] [blame] | 91 | body->SetString("documentURL", document_url); |
| 92 | if (referrer) |
| 93 | body->SetString("referrer", *referrer); |
| 94 | if (blocked_url) |
| 95 | body->SetString("blockedURL", *blocked_url); |
| 96 | body->SetString("effectiveDirective", effective_directive); |
| 97 | body->SetString("originalPolicy", original_policy); |
Paul Meyer | ed85628b | 2018-06-14 00:33:07 | [diff] [blame] | 98 | if (source_file) |
| 99 | body->SetString("sourceFile", *source_file); |
Paul Meyer | 079b845 | 2019-05-08 19:32:28 | [diff] [blame] | 100 | if (script_sample) |
| 101 | body->SetString("sample", *script_sample); |
| 102 | body->SetString("disposition", disposition); |
| 103 | body->SetInteger("statusCode", status_code); |
| 104 | if (line_number) |
| 105 | body->SetInteger("lineNumber", line_number); |
| 106 | if (column_number) |
| 107 | body->SetInteger("columnNumber", column_number); |
| 108 | QueueReport(url, group, "csp-violation", std::move(body)); |
Andy Paicu | acb9f5c | 2017-10-27 06:49:40 | [diff] [blame] | 109 | } |
| 110 | |
Ian Clelland | cda7f21 | 2018-08-13 14:04:36 | [diff] [blame] | 111 | void QueueFeaturePolicyViolationReport( |
| 112 | const GURL& url, |
Ian Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 113 | const std::string& policy_id, |
Ian Clelland | f482e77 | 2018-11-14 16:42:47 | [diff] [blame] | 114 | const std::string& disposition, |
Ian Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 115 | const base::Optional<std::string>& message, |
Ian Clelland | cda7f21 | 2018-08-13 14:04:36 | [diff] [blame] | 116 | const base::Optional<std::string>& source_file, |
| 117 | int line_number, |
| 118 | int column_number) override { |
| 119 | auto body = std::make_unique<base::DictionaryValue>(); |
Ian Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 120 | body->SetString("policyId", policy_id); |
Ian Clelland | f482e77 | 2018-11-14 16:42:47 | [diff] [blame] | 121 | body->SetString("disposition", disposition); |
Ian Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 122 | if (message) |
| 123 | body->SetString("message", *message); |
Ian Clelland | cda7f21 | 2018-08-13 14:04:36 | [diff] [blame] | 124 | if (source_file) |
| 125 | body->SetString("sourceFile", *source_file); |
| 126 | if (line_number) |
| 127 | body->SetInteger("lineNumber", line_number); |
| 128 | if (column_number) |
| 129 | body->SetInteger("columnNumber", column_number); |
Ian Clelland | aa5ddc8 | 2018-11-22 15:55:59 | [diff] [blame] | 130 | QueueReport(url, "default", "feature-policy-violation", std::move(body)); |
Ian Clelland | cda7f21 | 2018-08-13 14:04:36 | [diff] [blame] | 131 | } |
| 132 | |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 133 | private: |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 134 | void QueueReport(const GURL& url, |
| 135 | const std::string& group, |
| 136 | const std::string& type, |
Julia Tuttle | 6ae4dcb | 2017-09-08 21:41:26 | [diff] [blame] | 137 | std::unique_ptr<base::Value> body) { |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 138 | auto* rph = RenderProcessHost::FromID(render_process_id_); |
| 139 | if (!rph) |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 140 | return; |
| 141 | |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 142 | rph->GetStoragePartition()->GetNetworkContext()->QueueReport( |
| 143 | type, group, url, /*user_agent=*/base::nullopt, |
| 144 | base::Value::FromUniquePtrValue(std::move(body))); |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 145 | } |
| 146 | |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 147 | int render_process_id_; |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 148 | }; |
| 149 | |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 150 | } // namespace |
| 151 | |
| 152 | // static |
| 153 | void CreateReportingServiceProxy( |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 154 | int render_process_id, |
Miyoung Shin | d3cb464 | 2019-08-31 01:24:22 | [diff] [blame] | 155 | mojo::PendingReceiver<blink::mojom::ReportingServiceProxy> receiver) { |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 156 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 157 | |
Miyoung Shin | d3cb464 | 2019-08-31 01:24:22 | [diff] [blame] | 158 | mojo::MakeSelfOwnedReceiver( |
Eric Roman | 218c5ce | 2018-11-29 18:03:13 | [diff] [blame] | 159 | std::make_unique<ReportingServiceProxyImpl>(render_process_id), |
Miyoung Shin | d3cb464 | 2019-08-31 01:24:22 | [diff] [blame] | 160 | std::move(receiver)); |
juliatuttle | 95fe3cc5 | 2017-06-30 23:05:31 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | } // namespace content |