blob: 7a4cc6b3e10c713d799da4e7eb01cf2af989d99c [file] [log] [blame]
// Copyright (c) 2012 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 <memory>
#include <vector>
#include "base/macros.h"
#include "net/base/completion_once_callback.h"
#include "net/base/net_errors.h"
#include "net/proxy_resolution/proxy_resolver.h"
#include "net/proxy_resolution/proxy_resolver_factory.h"
#include "url/gurl.h"
namespace net {
// Asynchronous mock proxy resolver. All requests complete asynchronously,
// user must call Job::CompleteNow() on a pending request to signal it.
class MockAsyncProxyResolver : public ProxyResolver {
class Job {
Job(MockAsyncProxyResolver* resolver,
const GURL& url,
ProxyInfo* results,
CompletionOnceCallback callback);
const GURL& url() const { return url_; }
ProxyInfo* results() const { return results_; }
MockAsyncProxyResolver* Resolver() const { return resolver_; };
void CompleteNow(int rv);
MockAsyncProxyResolver* resolver_;
const GURL url_;
ProxyInfo* results_;
CompletionOnceCallback callback_;
class RequestImpl : public ProxyResolver::Request {
explicit RequestImpl(std::unique_ptr<Job> job);
~RequestImpl() override;
LoadState GetLoadState() override;
std::unique_ptr<Job> job_;
~MockAsyncProxyResolver() override;
// ProxyResolver implementation.
int GetProxyForURL(const GURL& url,
ProxyInfo* results,
CompletionOnceCallback callback,
std::unique_ptr<Request>* request,
const NetLogWithSource& /*net_log*/) override;
const std::vector<Job*>& pending_jobs() const { return pending_jobs_; }
const std::vector<std::unique_ptr<Job>>& cancelled_jobs() const {
return cancelled_jobs_;
void AddCancelledJob(std::unique_ptr<Job> job);
void RemovePendingJob(Job* job);
std::vector<Job*> pending_jobs_;
std::vector<std::unique_ptr<Job>> cancelled_jobs_;
// Asynchronous mock proxy resolver factory . All requests complete
// asynchronously; the user must call Request::CompleteNow() on a pending
// request to signal it.
class MockAsyncProxyResolverFactory : public ProxyResolverFactory {
class Request;
using RequestsList = std::vector<scoped_refptr<Request>>;
explicit MockAsyncProxyResolverFactory(bool resolvers_expect_pac_bytes);
~MockAsyncProxyResolverFactory() override;
int CreateProxyResolver(
const scoped_refptr<PacFileData>& pac_script,
std::unique_ptr<ProxyResolver>* resolver,
CompletionOnceCallback callback,
std::unique_ptr<ProxyResolverFactory::Request>* request) override;
const RequestsList& pending_requests() const { return pending_requests_; }
const RequestsList& cancelled_requests() const { return cancelled_requests_; }
void RemovePendingRequest(Request* request);
class Job;
RequestsList pending_requests_;
RequestsList cancelled_requests_;
class MockAsyncProxyResolverFactory::Request
: public base::RefCounted<Request> {
Request(MockAsyncProxyResolverFactory* factory,
const scoped_refptr<PacFileData>& script_data,
std::unique_ptr<ProxyResolver>* resolver,
CompletionOnceCallback callback);
const scoped_refptr<PacFileData>& script_data() const { return script_data_; }
// Completes this request. A ForwardingProxyResolver that forwards to
// |resolver| will be returned to the requester. |resolver| must not be
// null and must remain as long as the resolver returned by this request
// remains in use.
void CompleteNowWithForwarder(int rv, ProxyResolver* resolver);
void CompleteNow(int rv, std::unique_ptr<ProxyResolver> resolver);
friend class base::RefCounted<Request>;
friend class MockAsyncProxyResolverFactory;
friend class MockAsyncProxyResolverFactory::Job;
void FactoryDestroyed();
MockAsyncProxyResolverFactory* factory_;
const scoped_refptr<PacFileData> script_data_;
std::unique_ptr<ProxyResolver>* resolver_;
CompletionOnceCallback callback_;
// ForwardingProxyResolver forwards all requests to |impl|. |impl| must remain
// so long as this remains in use.
class ForwardingProxyResolver : public ProxyResolver {
explicit ForwardingProxyResolver(ProxyResolver* impl);
// ProxyResolver overrides.
int GetProxyForURL(const GURL& query_url,
ProxyInfo* results,
CompletionOnceCallback callback,
std::unique_ptr<Request>* request,
const NetLogWithSource& net_log) override;
ProxyResolver* impl_;
} // namespace net