Commit 80f21a2a authored by Bettina's avatar Bettina Committed by Commit Bot

Pass in ClientPhishingRequest as a unique_ptr.

Previously, the ClientPhishingRequest was passed
as a raw pointer into SendClientPhishingRequest
and then converted back to a unique_ptr when
passed into StartClientPhishingRequest. When
using a raw pointer,it has to be made sure
to be deleted otherwise there is a memory leak.
To make this easier to maintain, it is switched
to a unique_ptr.

Bug: 1122226
Change-Id: Ia344ea26661eec202cd27224889ba398f8264835
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2379252Reviewed-by: default avatarDaniel Rubery <drubery@chromium.org>
Commit-Queue: Bettina Dea <bdea@chromium.org>
Cr-Commit-Position: refs/heads/master@{#807150}
parent 15385529
......@@ -567,8 +567,7 @@ void ClientSideDetectionHost::FeatureExtractionDone(
Profile::FromBrowserContext(web_contents()->GetBrowserContext());
// Send ping even if the browser feature extraction failed.
csd_service_->SendClientReportPhishingRequest(
request.release(), // The service takes ownership of the request object.
IsExtendedReportingEnabled(*profile->GetPrefs()),
std::move(request), IsExtendedReportingEnabled(*profile->GetPrefs()),
IsEnhancedProtectionEnabled(*profile->GetPrefs()), callback);
}
......
......@@ -29,15 +29,11 @@ class FakeClientSideDetectionService : public ClientSideDetectionService {
FakeClientSideDetectionService() : ClientSideDetectionService(nullptr) {}
void SendClientReportPhishingRequest(
ClientPhishingRequest* verdict,
std::unique_ptr<ClientPhishingRequest> verdict,
bool is_extended_reporting,
bool is_enhanced_protection,
const ClientReportPhishingRequestCallback& callback) override {
saved_request_ = *verdict;
// TODO(drubery): This can be removed if SendClientReportPhishingRequest
// takes a unique_ptr<ClientPhishingRequest>, while also providing better
// guarantees about memory safety.
delete verdict;
saved_callback_ = callback;
request_callback_.Run();
}
......
......@@ -154,7 +154,7 @@ void ClientSideDetectionService::OnPrefsUpdated() {
}
void ClientSideDetectionService::SendClientReportPhishingRequest(
ClientPhishingRequest* verdict,
std::unique_ptr<ClientPhishingRequest> verdict,
bool is_extended_reporting,
bool is_enhanced_reporting,
const ClientReportPhishingRequestCallback& callback) {
......@@ -163,7 +163,7 @@ void ClientSideDetectionService::SendClientReportPhishingRequest(
FROM_HERE,
base::BindOnce(
&ClientSideDetectionService::StartClientReportPhishingRequest,
weak_factory_.GetWeakPtr(), verdict, is_extended_reporting,
weak_factory_.GetWeakPtr(), std::move(verdict), is_extended_reporting,
is_enhanced_reporting, callback));
}
......@@ -213,12 +213,11 @@ void ClientSideDetectionService::SendModelToRenderers() {
}
void ClientSideDetectionService::StartClientReportPhishingRequest(
ClientPhishingRequest* verdict,
std::unique_ptr<ClientPhishingRequest> request,
bool is_extended_reporting,
bool is_enhanced_reporting,
const ClientReportPhishingRequestCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
std::unique_ptr<ClientPhishingRequest> request(verdict);
if (!enabled_) {
if (!callback.is_null())
......
......@@ -82,7 +82,7 @@ class ClientSideDetectionService : public KeyedService {
// SendClientReportPhishingRequest() was called. You may set |callback| to
// NULL if you don't care about the server verdict.
virtual void SendClientReportPhishingRequest(
ClientPhishingRequest* verdict,
std::unique_ptr<ClientPhishingRequest> verdict,
bool is_extended_reporting,
bool is_enhanced_protection,
const ClientReportPhishingRequestCallback& callback);
......@@ -167,7 +167,7 @@ class ClientSideDetectionService : public KeyedService {
// Starts sending the request to the client-side detection frontends.
// This method takes ownership of both pointers.
void StartClientReportPhishingRequest(
ClientPhishingRequest* verdict,
std::unique_ptr<ClientPhishingRequest> request,
bool is_extended_reporting,
bool is_enhanced_protection,
const ClientReportPhishingRequestCallback& callback);
......
......@@ -86,13 +86,15 @@ class ClientSideDetectionServiceTest : public testing::Test {
float score,
bool is_extended_reporting,
bool is_enhanced_reporting) {
ClientPhishingRequest* request = new ClientPhishingRequest();
std::unique_ptr<ClientPhishingRequest> request =
std::make_unique<ClientPhishingRequest>(ClientPhishingRequest());
request->set_url(phishing_url.spec());
request->set_client_score(score);
request->set_is_phishing(true); // client thinks the URL is phishing.
base::RunLoop run_loop;
csd_service_->SendClientReportPhishingRequest(
request, is_extended_reporting, is_enhanced_reporting,
std::move(request), is_extended_reporting, is_enhanced_reporting,
base::Bind(&ClientSideDetectionServiceTest::SendRequestDone,
base::Unretained(this), run_loop.QuitWhenIdleClosure()));
phishing_url_ = phishing_url;
......
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