Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(179)

Unified Diff: net/proxy/proxy_resolver_factory_mojo.cc

Issue 1439053002: Change ProxyResolver::GetProxyForURL() to take a scoped_ptr<Request>* rather than a RequestHandle* (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ToT Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: net/proxy/proxy_resolver_factory_mojo.cc
diff --git a/net/proxy/proxy_resolver_factory_mojo.cc b/net/proxy/proxy_resolver_factory_mojo.cc
index 4203eea39cdc06ec09a5ecb7b4d2cabedb1dd5d2..51f511c5eccb33d4b97225c23863075f5eaec648 100644
--- a/net/proxy/proxy_resolver_factory_mojo.cc
+++ b/net/proxy/proxy_resolver_factory_mojo.cc
@@ -118,13 +118,14 @@ class ProxyResolverMojo : public ProxyResolver {
int GetProxyForURL(const GURL& url,
ProxyInfo* results,
const net::CompletionCallback& callback,
- RequestHandle* request,
+ scoped_ptr<Request>* request,
const BoundNetLog& net_log) override;
- void CancelRequest(RequestHandle request) override;
- LoadState GetLoadState(RequestHandle request) const override;
private:
class Job;
+ class RequestImpl;
+
+ base::ThreadChecker thread_checker_;
// Mojo error handler.
void OnConnectionError();
@@ -142,22 +143,34 @@ class ProxyResolverMojo : public ProxyResolver {
std::set<Job*> pending_jobs_;
- base::ThreadChecker thread_checker_;
scoped_ptr<base::ScopedClosureRunner> on_delete_callback_runner_;
DISALLOW_COPY_AND_ASSIGN(ProxyResolverMojo);
};
+class ProxyResolverMojo::RequestImpl : public ProxyResolver::Request {
+ public:
+ RequestImpl(base::WeakPtr<Job> job);
+
+ ~RequestImpl() override;
+
+ LoadState GetLoadState() override;
+
+ private:
+ base::WeakPtr<Job> job_;
+};
+
class ProxyResolverMojo::Job
- : public ClientMixin<interfaces::ProxyResolverRequestClient> {
+ : public ClientMixin<interfaces::ProxyResolverRequestClient>,
+ public base::SupportsWeakPtr<Job>,
+ public base::RefCounted<Job> {
public:
Job(ProxyResolverMojo* resolver,
const GURL& url,
ProxyInfo* results,
const CompletionCallback& callback,
const BoundNetLog& net_log);
- ~Job() override;
// Cancels the job and prevents the callback from being run.
void Cancel();
@@ -165,7 +178,14 @@ class ProxyResolverMojo::Job
// Returns the LoadState of this job.
LoadState GetLoadState();
+ ProxyResolverMojo* GetResolver();
+
+ // Delete seld owned refpointer
+ void Remove();
+
private:
+ ~Job() override;
+ friend class base::RefCounted<Job>;
// Mojo error handler.
void OnConnectionError();
@@ -181,8 +201,27 @@ class ProxyResolverMojo::Job
base::ThreadChecker thread_checker_;
mojo::Binding<interfaces::ProxyResolverRequestClient> binding_;
+
+ // The job holds a reference to itself to ensure that it remains alive until
+ // either completion or cancellation. This is the only owner.
+ scoped_refptr<Job> owned_self_reference_ = this;
};
+ProxyResolverMojo::RequestImpl::RequestImpl(base::WeakPtr<Job> job)
+ : job_(job) {}
+
+ProxyResolverMojo::RequestImpl::~RequestImpl() {
+ if (job_) {
+ job_->Cancel();
+ job_->GetResolver()->RemoveJob(job_.get());
+ }
+}
+
+LoadState ProxyResolverMojo::RequestImpl::GetLoadState() {
+ CHECK_EQ(1u, job_->GetResolver()->pending_jobs_.count(job_.get()));
+ return job_->GetLoadState();
+}
+
ProxyResolverMojo::Job::Job(ProxyResolverMojo* resolver,
const GURL& url,
ProxyInfo* results,
@@ -223,6 +262,14 @@ LoadState ProxyResolverMojo::Job::GetLoadState() {
: LOAD_STATE_RESOLVING_PROXY_FOR_URL;
}
+ProxyResolverMojo* ProxyResolverMojo::Job::GetResolver() {
+ return resolver_;
+};
+
+void ProxyResolverMojo::Job::Remove() {
+ owned_self_reference_ = NULL;
+}
+
void ProxyResolverMojo::Job::OnConnectionError() {
DCHECK(thread_checker_.CalledOnValidThread());
DVLOG(1) << "ProxyResolverMojo::Job::OnConnectionError";
@@ -279,40 +326,29 @@ void ProxyResolverMojo::RemoveJob(Job* job) {
DCHECK(thread_checker_.CalledOnValidThread());
size_t num_erased = pending_jobs_.erase(job);
DCHECK(num_erased);
- delete job;
+ job->Remove();
}
int ProxyResolverMojo::GetProxyForURL(const GURL& url,
ProxyInfo* results,
const CompletionCallback& callback,
- RequestHandle* request,
+ scoped_ptr<Request>* request,
const BoundNetLog& net_log) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!mojo_proxy_resolver_ptr_)
return ERR_PAC_SCRIPT_TERMINATED;
- Job* job = new Job(this, url, results, callback, net_log);
- bool inserted = pending_jobs_.insert(job).second;
+ // Job owns itselfs.
+ scoped_refptr<Job> job = new Job(this, url, results, callback, net_log);
+ bool inserted = pending_jobs_.insert(job.get()).second;
DCHECK(inserted);
- *request = job;
+ request->reset(new RequestImpl(job->AsWeakPtr()));
return ERR_IO_PENDING;
}
-void ProxyResolverMojo::CancelRequest(RequestHandle request) {
- DCHECK(thread_checker_.CalledOnValidThread());
- Job* job = static_cast<Job*>(request);
- DCHECK(job);
- job->Cancel();
- RemoveJob(job);
-}
-LoadState ProxyResolverMojo::GetLoadState(RequestHandle request) const {
- Job* job = static_cast<Job*>(request);
- CHECK_EQ(1u, pending_jobs_.count(job));
- return job->GetLoadState();
-}
} // namespace

Powered by Google App Engine
This is Rietveld 408576698