blob: 7963744a44bd866f3260b4f912ce741700b08763 [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"
Miyoung Shind3cb4642019-08-31 01:24:2218#include "mojo/public/cpp/bindings/self_owned_receiver.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"
21#include "net/url_request/url_request_context.h"
22#include "net/url_request/url_request_context_getter.h"
Eric Roman218c5ce2018-11-29 18:03:1323#include "services/network/public/mojom/network_context.mojom.h"
Julie Jeongeun Kim9912f2e2019-03-01 03:30:1824#include "third_party/blink/public/mojom/reporting/reporting.mojom.h"
juliatuttle95fe3cc52017-06-30 23:05:3125#include "url/gurl.h"
26
27namespace content {
28
29namespace {
30
Julia Tuttle6ae4dcb2017-09-08 21:41:2631class ReportingServiceProxyImpl : public blink::mojom::ReportingServiceProxy {
juliatuttle95fe3cc52017-06-30 23:05:3132 public:
Eric Roman218c5ce2018-11-29 18:03:1333 explicit ReportingServiceProxyImpl(int render_process_id)
34 : render_process_id_(render_process_id) {}
juliatuttle95fe3cc52017-06-30 23:05:3135
Julia Tuttle6ae4dcb2017-09-08 21:41:2636 // blink::mojom::ReportingServiceProxy:
37
38 void QueueInterventionReport(const GURL& url,
Paul Meyer36872572019-05-08 17:24:2739 const std::string& id,
Julia Tuttle6ae4dcb2017-09-08 21:41:2640 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>();
Paul Meyer36872572019-05-08 17:24:2745 body->SetString("id", id);
Julia Tuttle6ae4dcb2017-09-08 21:41:2646 body->SetString("message", message);
Paul Meyered85628b2018-06-14 00:33:0747 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 Tuttle6ae4dcb2017-09-08 21:41:2653 QueueReport(url, "default", "intervention", std::move(body));
54 }
55
56 void QueueDeprecationReport(const GURL& url,
Paul Meyere5df6cff2017-12-05 00:28:2657 const std::string& id,
Paul Meyered85628b2018-06-14 00:33:0758 base::Optional<base::Time> anticipatedRemoval,
Julia Tuttle6ae4dcb2017-09-08 21:41:2659 const std::string& message,
Paul Meyered85628b2018-06-14 00:33:0760 const base::Optional<std::string>& source_file,
Paul Meyer152136f2017-11-30 16:36:1761 int line_number,
62 int column_number) override {
Julia Tuttle6ae4dcb2017-09-08 21:41:2663 auto body = std::make_unique<base::DictionaryValue>();
Paul Meyere5df6cff2017-12-05 00:28:2664 body->SetString("id", id);
Paul Meyered85628b2018-06-14 00:33:0765 if (anticipatedRemoval)
66 body->SetDouble("anticipatedRemoval", anticipatedRemoval->ToDoubleT());
Julia Tuttle6ae4dcb2017-09-08 21:41:2667 body->SetString("message", message);
Paul Meyered85628b2018-06-14 00:33:0768 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 Tuttle6ae4dcb2017-09-08 21:41:2674 QueueReport(url, "default", "deprecation", std::move(body));
75 }
76
Andy Paicuacb9f5c2017-10-27 06:49:4077 void QueueCspViolationReport(const GURL& url,
78 const std::string& group,
Paul Meyer079b8452019-05-08 19:32:2879 const std::string& document_url,
80 const base::Optional<std::string>& referrer,
81 const base::Optional<std::string>& blocked_url,
Andy Paicuacb9f5c2017-10-27 06:49:4082 const std::string& effective_directive,
83 const std::string& original_policy,
Paul Meyered85628b2018-06-14 00:33:0784 const base::Optional<std::string>& source_file,
Paul Meyer079b8452019-05-08 19:32:2885 const base::Optional<std::string>& script_sample,
86 const std::string& disposition,
Miyoung Shin4d4b2f92019-02-28 08:52:5887 uint16_t status_code,
Paul Meyer079b8452019-05-08 19:32:2888 int line_number,
89 int column_number) override {
Andy Paicuacb9f5c2017-10-27 06:49:4090 auto body = std::make_unique<base::DictionaryValue>();
Paul Meyer079b8452019-05-08 19:32:2891 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 Meyered85628b2018-06-14 00:33:0798 if (source_file)
99 body->SetString("sourceFile", *source_file);
Paul Meyer079b8452019-05-08 19:32:28100 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 Paicuacb9f5c2017-10-27 06:49:40109 }
110
Ian Clellandcda7f212018-08-13 14:04:36111 void QueueFeaturePolicyViolationReport(
112 const GURL& url,
Ian Clellandaa5ddc82018-11-22 15:55:59113 const std::string& policy_id,
Ian Clellandf482e772018-11-14 16:42:47114 const std::string& disposition,
Ian Clellandaa5ddc82018-11-22 15:55:59115 const base::Optional<std::string>& message,
Ian Clellandcda7f212018-08-13 14:04:36116 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 Clellandaa5ddc82018-11-22 15:55:59120 body->SetString("policyId", policy_id);
Ian Clellandf482e772018-11-14 16:42:47121 body->SetString("disposition", disposition);
Ian Clellandaa5ddc82018-11-22 15:55:59122 if (message)
123 body->SetString("message", *message);
Ian Clellandcda7f212018-08-13 14:04:36124 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 Clellandaa5ddc82018-11-22 15:55:59130 QueueReport(url, "default", "feature-policy-violation", std::move(body));
Ian Clellandcda7f212018-08-13 14:04:36131 }
132
Julia Tuttle6ae4dcb2017-09-08 21:41:26133 private:
juliatuttle95fe3cc52017-06-30 23:05:31134 void QueueReport(const GURL& url,
135 const std::string& group,
136 const std::string& type,
Julia Tuttle6ae4dcb2017-09-08 21:41:26137 std::unique_ptr<base::Value> body) {
Eric Roman218c5ce2018-11-29 18:03:13138 auto* rph = RenderProcessHost::FromID(render_process_id_);
139 if (!rph)
juliatuttle95fe3cc52017-06-30 23:05:31140 return;
141
Eric Roman218c5ce2018-11-29 18:03:13142 rph->GetStoragePartition()->GetNetworkContext()->QueueReport(
143 type, group, url, /*user_agent=*/base::nullopt,
144 base::Value::FromUniquePtrValue(std::move(body)));
juliatuttle95fe3cc52017-06-30 23:05:31145 }
146
Eric Roman218c5ce2018-11-29 18:03:13147 int render_process_id_;
juliatuttle95fe3cc52017-06-30 23:05:31148};
149
juliatuttle95fe3cc52017-06-30 23:05:31150} // namespace
151
152// static
153void CreateReportingServiceProxy(
Eric Roman218c5ce2018-11-29 18:03:13154 int render_process_id,
Miyoung Shind3cb4642019-08-31 01:24:22155 mojo::PendingReceiver<blink::mojom::ReportingServiceProxy> receiver) {
Eric Roman218c5ce2018-11-29 18:03:13156 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
157
Miyoung Shind3cb4642019-08-31 01:24:22158 mojo::MakeSelfOwnedReceiver(
Eric Roman218c5ce2018-11-29 18:03:13159 std::make_unique<ReportingServiceProxyImpl>(render_process_id),
Miyoung Shind3cb4642019-08-31 01:24:22160 std::move(receiver));
juliatuttle95fe3cc52017-06-30 23:05:31161}
162
163} // namespace content