Commit 1170eb93 authored by juliatuttle's avatar juliatuttle Committed by Commit bot

Reporting: Implement browsing data remover.

Reporting is a spec for delivering out-of-band reports from various
other parts of the browser. See http://wicg.github.io/reporting/ for
the spec, or https://goo.gl/pygX5I for details of the planned
implementation in Chromium.

The browsing data remover removes reports and/or endpoints that match
an (optional) origin filter, or all reports and/or endpoints. It will
be called from Chromium's browsing_data_remover, potentially among
other places.

BUG=704259

Review-Url: https://codereview.chromium.org/2752523005
Cr-Commit-Position: refs/heads/master@{#463016}
parent 7f9c1f75
......@@ -1397,6 +1397,8 @@ component("net") {
"quic/quartc/quartc_stream.h",
"quic/quartc/quartc_stream_interface.h",
"quic/quartc/quartc_task_runner_interface.h",
"reporting/reporting_browsing_data_remover.cc",
"reporting/reporting_browsing_data_remover.h",
"reporting/reporting_cache.cc",
"reporting/reporting_cache.h",
"reporting/reporting_client.cc",
......@@ -4634,6 +4636,7 @@ test("net_unittests") {
"quic/test_tools/simulator/switch.h",
"quic/test_tools/simulator/traffic_policer.cc",
"quic/test_tools/simulator/traffic_policer.h",
"reporting/reporting_browsing_data_remover_unittest.cc",
"reporting/reporting_cache_unittest.cc",
"reporting/reporting_delivery_agent_unittest.cc",
"reporting/reporting_endpoint_manager_unittest.cc",
......
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/reporting/reporting_browsing_data_remover.h"
#include <vector>
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_context.h"
#include "net/reporting/reporting_report.h"
namespace net {
// static
void ReportingBrowsingDataRemover::RemoveBrowsingData(
ReportingContext* context,
int data_type_mask,
base::Callback<bool(const GURL&)> origin_filter) {
ReportingCache* cache = context->cache();
bool remove_reports = (data_type_mask & DATA_TYPE_REPORTS) != 0;
bool remove_clients = (data_type_mask & DATA_TYPE_CLIENTS) != 0;
if (origin_filter.is_null()) {
if (remove_reports)
cache->RemoveAllReports();
if (remove_clients)
cache->RemoveAllClients();
return;
}
if (remove_reports) {
std::vector<const ReportingReport*> all_reports;
cache->GetReports(&all_reports);
std::vector<const ReportingReport*> reports_to_remove;
for (const ReportingReport* report : all_reports) {
if (origin_filter.Run(report->url))
reports_to_remove.push_back(report);
}
cache->RemoveReports(reports_to_remove);
}
if (remove_clients) {
std::vector<const ReportingClient*> all_clients;
cache->GetClients(&all_clients);
std::vector<const ReportingClient*> clients_to_remove;
for (const ReportingClient* client : all_clients) {
if (origin_filter.Run(client->origin.GetURL()))
clients_to_remove.push_back(client);
}
cache->RemoveClients(clients_to_remove);
}
}
} // namespace net
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_REPORTING_REPORTING_BROWSING_DATA_REMOVER_H_
#define NET_REPORTING_REPORTING_BROWSING_DATA_REMOVER_H_
#include "base/callback.h"
#include "base/macros.h"
#include "net/base/net_export.h"
#include "url/gurl.h"
namespace net {
class ReportingContext;
// Clears browsing data (reports and clients) from the Reporting system.
class NET_EXPORT ReportingBrowsingDataRemover {
public:
enum DataType {
DATA_TYPE_REPORTS = 0x1,
DATA_TYPE_CLIENTS = 0x2,
};
static void RemoveBrowsingData(
ReportingContext* context,
int data_type_mask,
base::Callback<bool(const GURL&)> origin_filter);
private:
DISALLOW_IMPLICIT_CONSTRUCTORS(ReportingBrowsingDataRemover);
};
} // namespace net
#endif // NET_REPORTING_REPORTING_BROWSING_DATA_REMOVER_H_
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/reporting/reporting_browsing_data_remover.h"
#include <string>
#include "base/bind.h"
#include "base/memory/ptr_util.h"
#include "base/test/simple_test_tick_clock.h"
#include "net/reporting/reporting_cache.h"
#include "net/reporting/reporting_client.h"
#include "net/reporting/reporting_report.h"
#include "net/reporting/reporting_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
class ReportingBrowsingDataRemoverTest : public ReportingTestBase {
protected:
void RemoveBrowsingData(bool remove_reports,
bool remove_clients,
std::string host) {
int data_type_mask = 0;
if (remove_reports)
data_type_mask |= ReportingBrowsingDataRemover::DATA_TYPE_REPORTS;
if (remove_clients)
data_type_mask |= ReportingBrowsingDataRemover::DATA_TYPE_CLIENTS;
base::Callback<bool(const GURL&)> origin_filter;
if (!host.empty()) {
origin_filter =
base::Bind(&ReportingBrowsingDataRemoverTest::HostIs, host);
}
ReportingBrowsingDataRemover::RemoveBrowsingData(context(), data_type_mask,
origin_filter);
}
static bool HostIs(std::string host, const GURL& url) {
return url.host() == host;
}
size_t report_count() {
std::vector<const ReportingReport*> reports;
cache()->GetReports(&reports);
return reports.size();
}
size_t client_count() {
std::vector<const ReportingClient*> clients;
cache()->GetClients(&clients);
return clients.size();
}
const GURL kUrl1_ = GURL("https://origin1/path");
const GURL kUrl2_ = GURL("https://origin2/path");
const url::Origin kOrigin1_ = url::Origin(kUrl1_);
const url::Origin kOrigin2_ = url::Origin(kUrl2_);
const GURL kEndpoint_ = GURL("https://endpoint/");
const std::string kGroup_ = "group";
const std::string kType_ = "default";
};
TEST_F(ReportingBrowsingDataRemoverTest, RemoveNothing) {
cache()->AddReport(kUrl1_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->AddReport(kUrl2_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->SetClient(kOrigin1_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
cache()->SetClient(kOrigin2_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
RemoveBrowsingData(/* remove_reports= */ false, /* remove_clients= */ false,
/* host= */ "");
EXPECT_EQ(2u, report_count());
EXPECT_EQ(2u, client_count());
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveAllReports) {
cache()->AddReport(kUrl1_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->AddReport(kUrl2_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->SetClient(kOrigin1_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
cache()->SetClient(kOrigin2_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
RemoveBrowsingData(/* remove_reports= */ true, /* remove_clients= */ false,
/* host= */ "");
EXPECT_EQ(0u, report_count());
EXPECT_EQ(2u, client_count());
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveAllClients) {
cache()->AddReport(kUrl1_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->AddReport(kUrl2_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->SetClient(kOrigin1_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
cache()->SetClient(kOrigin2_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
RemoveBrowsingData(/* remove_reports= */ false, /* remove_clients= */ true,
/* host= */ "");
EXPECT_EQ(2u, report_count());
EXPECT_EQ(0u, client_count());
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveAllReportsAndClients) {
cache()->AddReport(kUrl1_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->AddReport(kUrl2_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->SetClient(kOrigin1_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
cache()->SetClient(kOrigin2_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
RemoveBrowsingData(/* remove_reports= */ true, /* remove_clients= */ true,
/* host= */ "");
EXPECT_EQ(0u, report_count());
EXPECT_EQ(0u, client_count());
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveSomeReports) {
cache()->AddReport(kUrl1_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->AddReport(kUrl2_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->SetClient(kOrigin1_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
cache()->SetClient(kOrigin2_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
RemoveBrowsingData(/* remove_reports= */ true, /* remove_clients= */ false,
/* host= */ kUrl1_.host());
EXPECT_EQ(2u, client_count());
std::vector<const ReportingReport*> reports;
cache()->GetReports(&reports);
ASSERT_EQ(1u, reports.size());
EXPECT_EQ(kUrl2_, reports[0]->url);
}
TEST_F(ReportingBrowsingDataRemoverTest, RemoveSomeClients) {
cache()->AddReport(kUrl1_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->AddReport(kUrl2_, kGroup_, kType_,
base::MakeUnique<base::DictionaryValue>(),
tick_clock()->NowTicks(), 0);
cache()->SetClient(kOrigin1_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
cache()->SetClient(kOrigin2_, kEndpoint_,
ReportingClient::Subdomains::EXCLUDE, kGroup_,
tick_clock()->NowTicks() + base::TimeDelta::FromDays(7));
RemoveBrowsingData(/* remove_reports= */ false, /* remove_clients= */ true,
/* host= */ kUrl1_.host());
EXPECT_EQ(2u, report_count());
EXPECT_FALSE(FindClientInCache(cache(), kOrigin1_, kEndpoint_) != nullptr);
EXPECT_TRUE(FindClientInCache(cache(), kOrigin2_, kEndpoint_) != nullptr);
}
} // namespace
} // namespace net
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment