Commit b9861bc6 authored by tzik's avatar tzik Committed by Commit Bot

Avoid touching ResourceRequestDetectorClient's ref count before it's fully constructed

The first reference to ResourceRequestDetectorClient instance used to be made by
base::Bind in its constructor, and posted to another thread. If the PostTask call
failed, or the posted task ran immediately after the other thread, the RRDC instance
may destroyed before the construction is completed.

Though this does not actually causes a crash, as no one use the result of
`new ResourceRequestDetectorClient`, this hits an assertion that being added as
a false positive case.

This CL adds ResourceRequestDetectorClient::Start, and makes the constructor
private to ensure no one touches the fragile way.

Also, this converts legacy base::Callback to base::OnceCallback, as a drive-by.

Bug: 866456
Change-Id: Ifb3d8136f03e2e233cf4b0b2b0ee070db6b3093d
Reviewed-on: https://chromium-review.googlesource.com/1149779Reviewed-by: default avatarJialiu Lin <jialiul@chromium.org>
Commit-Queue: Taiju Tsuiki <tzik@chromium.org>
Cr-Commit-Position: refs/heads/master@{#577961}
parent 026e21eb
...@@ -61,22 +61,28 @@ class ResourceRequestDetectorClient ...@@ -61,22 +61,28 @@ class ResourceRequestDetectorClient
using ResourceRequestIncidentMessage = using ResourceRequestIncidentMessage =
ClientIncidentReport::IncidentData::ResourceRequestIncident; ClientIncidentReport::IncidentData::ResourceRequestIncident;
typedef base::Callback<void(std::unique_ptr<ResourceRequestIncidentMessage>)> using OnResultCallback =
OnResultCallback; base::OnceCallback<void(std::unique_ptr<ResourceRequestIncidentMessage>)>;
ResourceRequestDetectorClient( static void Start(
const GURL& resource_url, const GURL& resource_url,
const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager, const scoped_refptr<SafeBrowsingDatabaseManager>& database_manager,
const OnResultCallback& callback) OnResultCallback callback) {
: database_manager_(database_manager) auto client = base::WrapRefCounted(new ResourceRequestDetectorClient(
, callback_(callback) { std::move(database_manager), std::move(callback)));
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::IO, FROM_HERE, content::BrowserThread::IO, FROM_HERE,
base::BindOnce(&ResourceRequestDetectorClient::StartCheck, this, base::BindOnce(&ResourceRequestDetectorClient::StartCheck, client,
resource_url)); resource_url));
} }
private: private:
ResourceRequestDetectorClient(
scoped_refptr<SafeBrowsingDatabaseManager> database_manager,
OnResultCallback callback)
: database_manager_(std::move(database_manager)),
callback_(std::move(callback)) {}
friend class base::RefCountedThreadSafe<ResourceRequestDetectorClient>; friend class base::RefCountedThreadSafe<ResourceRequestDetectorClient>;
~ResourceRequestDetectorClient() override {} ~ResourceRequestDetectorClient() override {}
...@@ -106,7 +112,7 @@ class ResourceRequestDetectorClient ...@@ -106,7 +112,7 @@ class ResourceRequestDetectorClient
incident_data->set_digest(threat_hash); incident_data->set_digest(threat_hash);
content::BrowserThread::PostTask( content::BrowserThread::PostTask(
content::BrowserThread::UI, FROM_HERE, content::BrowserThread::UI, FROM_HERE,
base::BindOnce(callback_, std::move(incident_data))); base::BindOnce(std::move(callback_), std::move(incident_data)));
} }
Release(); // Balanced in StartCheck. Release(); // Balanced in StartCheck.
} }
...@@ -152,11 +158,11 @@ void ResourceRequestDetector::ProcessResourceRequest( ...@@ -152,11 +158,11 @@ void ResourceRequestDetector::ProcessResourceRequest(
if (request->resource_type == content::RESOURCE_TYPE_SUB_FRAME || if (request->resource_type == content::RESOURCE_TYPE_SUB_FRAME ||
request->resource_type == content::RESOURCE_TYPE_SCRIPT || request->resource_type == content::RESOURCE_TYPE_SCRIPT ||
request->resource_type == content::RESOURCE_TYPE_OBJECT) { request->resource_type == content::RESOURCE_TYPE_OBJECT) {
new ResourceRequestDetectorClient( ResourceRequestDetectorClient::Start(
request->url, database_manager_, request->url, database_manager_,
base::Bind(&ResourceRequestDetector::ReportIncidentOnUIThread, base::BindOnce(&ResourceRequestDetector::ReportIncidentOnUIThread,
weak_ptr_factory_.GetWeakPtr(), request->render_process_id, weak_ptr_factory_.GetWeakPtr(),
request->render_frame_id)); request->render_process_id, request->render_frame_id));
} }
} }
......
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