This source file includes following definitions.
- outstanding_job
- resolver
- thread_number
- was_cancelled_
- set_executor
- executor
- Cancel
- was_cancelled
- type
- has_user_callback
- WaitingForThread
- FinishedWaitingForThread
- OnJobCompleted
- RunUserCallback
- script_data_
- Run
- SetPacScriptJob
- RequestComplete
- was_waiting_for_thread_
- net_log
- WaitingForThread
- FinishedWaitingForThread
- Run
- GetProxyForURLJob
- QueryComplete
- RecordPerformanceMetrics
- resolver_
- StartJob
- OnJobCompleted
- Destroy
- max_num_threads_
- GetProxyForURL
- CancelRequest
- GetLoadState
- CancelSetPacScript
- SetPacScript
- CheckNoOutstandingUserRequests
- ReleaseAllExecutors
- FindIdleExecutor
- AddNewExecutor
- OnExecutorReady
#include "net/proxy/multi_threaded_proxy_resolver.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/message_loop/message_loop_proxy.h"
#include "base/metrics/histogram.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/threading/thread.h"
#include "base/threading/thread_restrictions.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"
#include "net/proxy/proxy_info.h"
namespace net {
class MultiThreadedProxyResolver::Executor
: public base::RefCountedThreadSafe<MultiThreadedProxyResolver::Executor > {
public:
Executor(MultiThreadedProxyResolver* coordinator,
ProxyResolver* resolver,
int thread_number);
void StartJob(Job* job);
void OnJobCompleted(Job* job);
void Destroy();
Job* outstanding_job() const { return outstanding_job_.get(); }
ProxyResolver* resolver() { return resolver_.get(); }
int thread_number() const { return thread_number_; }
private:
friend class base::RefCountedThreadSafe<Executor>;
~Executor();
MultiThreadedProxyResolver* coordinator_;
const int thread_number_;
scoped_refptr<Job> outstanding_job_;
scoped_ptr<ProxyResolver> resolver_;
scoped_ptr<base::Thread> thread_;
};
class MultiThreadedProxyResolver::Job
: public base::RefCountedThreadSafe<MultiThreadedProxyResolver::Job> {
public:
enum Type {
TYPE_GET_PROXY_FOR_URL,
TYPE_SET_PAC_SCRIPT,
TYPE_SET_PAC_SCRIPT_INTERNAL,
};
Job(Type type, const CompletionCallback& callback)
: type_(type),
callback_(callback),
executor_(NULL),
was_cancelled_(false) {
}
void set_executor(Executor* executor) {
executor_ = executor;
}
Executor* executor() {
return executor_;
}
void Cancel() {
was_cancelled_ = true;
}
bool was_cancelled() const { return was_cancelled_; }
Type type() const { return type_; }
bool has_user_callback() const { return !callback_.is_null(); }
virtual void WaitingForThread() {}
virtual void FinishedWaitingForThread() {}
virtual void Run(scoped_refptr<base::MessageLoopProxy> origin_loop) = 0;
protected:
void OnJobCompleted() {
if (executor_)
executor_->OnJobCompleted(this);
}
void RunUserCallback(int result) {
DCHECK(has_user_callback());
CompletionCallback callback = callback_;
callback_.Reset();
callback.Run(result);
}
friend class base::RefCountedThreadSafe<MultiThreadedProxyResolver::Job>;
virtual ~Job() {}
private:
const Type type_;
CompletionCallback callback_;
Executor* executor_;
bool was_cancelled_;
};
class MultiThreadedProxyResolver::SetPacScriptJob
: public MultiThreadedProxyResolver::Job {
public:
SetPacScriptJob(const scoped_refptr<ProxyResolverScriptData>& script_data,
const CompletionCallback& callback)
: Job(!callback.is_null() ? TYPE_SET_PAC_SCRIPT :
TYPE_SET_PAC_SCRIPT_INTERNAL,
callback),
script_data_(script_data) {
}
virtual void Run(scoped_refptr<base::MessageLoopProxy> origin_loop) OVERRIDE {
ProxyResolver* resolver = executor()->resolver();
int rv = resolver->SetPacScript(script_data_, CompletionCallback());
DCHECK_NE(rv, ERR_IO_PENDING);
origin_loop->PostTask(
FROM_HERE,
base::Bind(&SetPacScriptJob::RequestComplete, this, rv));
}
protected:
virtual ~SetPacScriptJob() {}
private:
void RequestComplete(int result_code) {
if (!was_cancelled() && has_user_callback()) {
RunUserCallback(result_code);
}
OnJobCompleted();
}
const scoped_refptr<ProxyResolverScriptData> script_data_;
};
class MultiThreadedProxyResolver::GetProxyForURLJob
: public MultiThreadedProxyResolver::Job {
public:
GetProxyForURLJob(const GURL& url,
ProxyInfo* results,
const CompletionCallback& callback,
const BoundNetLog& net_log)
: Job(TYPE_GET_PROXY_FOR_URL, callback),
results_(results),
net_log_(net_log),
url_(url),
was_waiting_for_thread_(false) {
DCHECK(!callback.is_null());
start_time_ = base::TimeTicks::Now();
}
BoundNetLog* net_log() { return &net_log_; }
virtual void WaitingForThread() OVERRIDE {
was_waiting_for_thread_ = true;
net_log_.BeginEvent(NetLog::TYPE_WAITING_FOR_PROXY_RESOLVER_THREAD);
}
virtual void FinishedWaitingForThread() OVERRIDE {
DCHECK(executor());
submitted_to_thread_time_ = base::TimeTicks::Now();
if (was_waiting_for_thread_) {
net_log_.EndEvent(NetLog::TYPE_WAITING_FOR_PROXY_RESOLVER_THREAD);
}
net_log_.AddEvent(
NetLog::TYPE_SUBMITTED_TO_RESOLVER_THREAD,
NetLog::IntegerCallback("thread_number", executor()->thread_number()));
}
virtual void Run(scoped_refptr<base::MessageLoopProxy> origin_loop) OVERRIDE {
ProxyResolver* resolver = executor()->resolver();
int rv = resolver->GetProxyForURL(
url_, &results_buf_, CompletionCallback(), NULL, net_log_);
DCHECK_NE(rv, ERR_IO_PENDING);
origin_loop->PostTask(
FROM_HERE,
base::Bind(&GetProxyForURLJob::QueryComplete, this, rv));
}
protected:
virtual ~GetProxyForURLJob() {}
private:
void QueryComplete(int result_code) {
if (!was_cancelled()) {
RecordPerformanceMetrics();
if (result_code >= OK) {
results_->Use(results_buf_);
}
RunUserCallback(result_code);
}
OnJobCompleted();
}
void RecordPerformanceMetrics() {
DCHECK(!was_cancelled());
base::TimeTicks now = base::TimeTicks::Now();
UMA_HISTOGRAM_MEDIUM_TIMES("Net.MTPR_GetProxyForUrl_Time",
now - start_time_);
UMA_HISTOGRAM_MEDIUM_TIMES("Net.MTPR_GetProxyForUrl_Thread_Wait_Time",
submitted_to_thread_time_ - start_time_);
}
ProxyInfo* results_;
BoundNetLog net_log_;
const GURL url_;
ProxyInfo results_buf_;
base::TimeTicks start_time_;
base::TimeTicks submitted_to_thread_time_;
bool was_waiting_for_thread_;
};
MultiThreadedProxyResolver::Executor::Executor(
MultiThreadedProxyResolver* coordinator,
ProxyResolver* resolver,
int thread_number)
: coordinator_(coordinator),
thread_number_(thread_number),
resolver_(resolver) {
DCHECK(coordinator);
DCHECK(resolver);
std::string thread_name =
base::StringPrintf("PAC thread #%d", thread_number);
thread_.reset(new base::Thread(thread_name.c_str()));
CHECK(thread_->Start());
}
void MultiThreadedProxyResolver::Executor::StartJob(Job* job) {
DCHECK(!outstanding_job_.get());
outstanding_job_ = job;
job->set_executor(this);
job->FinishedWaitingForThread();
thread_->message_loop()->PostTask(
FROM_HERE,
base::Bind(&Job::Run, job, base::MessageLoopProxy::current()));
}
void MultiThreadedProxyResolver::Executor::OnJobCompleted(Job* job) {
DCHECK_EQ(job, outstanding_job_.get());
outstanding_job_ = NULL;
coordinator_->OnExecutorReady(this);
}
void MultiThreadedProxyResolver::Executor::Destroy() {
DCHECK(coordinator_);
{
base::ThreadRestrictions::ScopedAllowIO allow_io;
thread_.reset();
}
if (outstanding_job_.get()) {
outstanding_job_->Cancel();
outstanding_job_->set_executor(NULL);
}
resolver_.reset();
coordinator_ = NULL;
outstanding_job_ = NULL;
}
MultiThreadedProxyResolver::Executor::~Executor() {
DCHECK(!coordinator_) << "Destroy() was not called";
DCHECK(!thread_.get());
DCHECK(!resolver_.get());
DCHECK(!outstanding_job_.get());
}
MultiThreadedProxyResolver::MultiThreadedProxyResolver(
ProxyResolverFactory* resolver_factory,
size_t max_num_threads)
: ProxyResolver(resolver_factory->resolvers_expect_pac_bytes()),
resolver_factory_(resolver_factory),
max_num_threads_(max_num_threads) {
DCHECK_GE(max_num_threads, 1u);
}
MultiThreadedProxyResolver::~MultiThreadedProxyResolver() {
pending_jobs_.clear();
ReleaseAllExecutors();
}
int MultiThreadedProxyResolver::GetProxyForURL(
const GURL& url, ProxyInfo* results, const CompletionCallback& callback,
RequestHandle* request, const BoundNetLog& net_log) {
DCHECK(CalledOnValidThread());
DCHECK(!callback.is_null());
DCHECK(current_script_data_.get())
<< "Resolver is un-initialized. Must call SetPacScript() first!";
scoped_refptr<GetProxyForURLJob> job(
new GetProxyForURLJob(url, results, callback, net_log));
if (request)
*request = reinterpret_cast<RequestHandle>(job.get());
Executor* executor = FindIdleExecutor();
if (executor) {
DCHECK_EQ(0u, pending_jobs_.size());
executor->StartJob(job.get());
return ERR_IO_PENDING;
}
job->WaitingForThread();
pending_jobs_.push_back(job);
if (executors_.size() < max_num_threads_) {
executor = AddNewExecutor();
executor->StartJob(
new SetPacScriptJob(current_script_data_, CompletionCallback()));
}
return ERR_IO_PENDING;
}
void MultiThreadedProxyResolver::CancelRequest(RequestHandle req) {
DCHECK(CalledOnValidThread());
DCHECK(req);
Job* job = reinterpret_cast<Job*>(req);
DCHECK_EQ(Job::TYPE_GET_PROXY_FOR_URL, job->type());
if (job->executor()) {
job->Cancel();
} else {
PendingJobsQueue::iterator it =
std::find(pending_jobs_.begin(), pending_jobs_.end(), job);
DCHECK(it != pending_jobs_.end());
pending_jobs_.erase(it);
}
}
LoadState MultiThreadedProxyResolver::GetLoadState(RequestHandle req) const {
DCHECK(CalledOnValidThread());
DCHECK(req);
return LOAD_STATE_RESOLVING_PROXY_FOR_URL;
}
void MultiThreadedProxyResolver::CancelSetPacScript() {
DCHECK(CalledOnValidThread());
DCHECK_EQ(0u, pending_jobs_.size());
DCHECK_EQ(1u, executors_.size());
DCHECK_EQ(Job::TYPE_SET_PAC_SCRIPT,
executors_[0]->outstanding_job()->type());
current_script_data_ = NULL;
ReleaseAllExecutors();
}
int MultiThreadedProxyResolver::SetPacScript(
const scoped_refptr<ProxyResolverScriptData>& script_data,
const CompletionCallback&callback) {
DCHECK(CalledOnValidThread());
DCHECK(!callback.is_null());
current_script_data_ = script_data;
CheckNoOutstandingUserRequests();
ReleaseAllExecutors();
Executor* executor = AddNewExecutor();
executor->StartJob(new SetPacScriptJob(script_data, callback));
return ERR_IO_PENDING;
}
void MultiThreadedProxyResolver::CheckNoOutstandingUserRequests() const {
DCHECK(CalledOnValidThread());
CHECK_EQ(0u, pending_jobs_.size());
for (ExecutorList::const_iterator it = executors_.begin();
it != executors_.end(); ++it) {
const Executor* executor = it->get();
Job* job = executor->outstanding_job();
CHECK(!job || job->was_cancelled() || !job->has_user_callback());
}
}
void MultiThreadedProxyResolver::ReleaseAllExecutors() {
DCHECK(CalledOnValidThread());
for (ExecutorList::iterator it = executors_.begin();
it != executors_.end(); ++it) {
Executor* executor = it->get();
executor->Destroy();
}
executors_.clear();
}
MultiThreadedProxyResolver::Executor*
MultiThreadedProxyResolver::FindIdleExecutor() {
DCHECK(CalledOnValidThread());
for (ExecutorList::iterator it = executors_.begin();
it != executors_.end(); ++it) {
Executor* executor = it->get();
if (!executor->outstanding_job())
return executor;
}
return NULL;
}
MultiThreadedProxyResolver::Executor*
MultiThreadedProxyResolver::AddNewExecutor() {
DCHECK(CalledOnValidThread());
DCHECK_LT(executors_.size(), max_num_threads_);
int thread_number = executors_.size();
ProxyResolver* resolver = resolver_factory_->CreateProxyResolver();
Executor* executor = new Executor(
this, resolver, thread_number);
executors_.push_back(make_scoped_refptr(executor));
return executor;
}
void MultiThreadedProxyResolver::OnExecutorReady(Executor* executor) {
DCHECK(CalledOnValidThread());
if (pending_jobs_.empty())
return;
scoped_refptr<Job> job = pending_jobs_.front();
pending_jobs_.pop_front();
executor->StartJob(job.get());
}
}