1// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_PROXY_PROXY_RESOLVER_H_
6#define NET_PROXY_PROXY_RESOLVER_H_
7
8#include <string>
9
10#include "base/logging.h"
11#include "googleurl/src/gurl.h"
12#include "net/base/completion_callback.h"
13
14namespace net {
15
16class LoadLog;
17class ProxyInfo;
18
19// Interface for "proxy resolvers". A ProxyResolver fills in a list of proxies
20// to use for a particular URL. Generally the backend for a ProxyResolver is
21// a PAC script, but it doesn't need to be. ProxyResolver can service multiple
22// requests at a time.
23class ProxyResolver {
24 public:
25  // Opaque pointer type, to return a handle to cancel outstanding requests.
26  typedef void* RequestHandle;
27
28  // See |expects_pac_bytes()| for the meaning of |expects_pac_bytes|.
29  explicit ProxyResolver(bool expects_pac_bytes)
30      : expects_pac_bytes_(expects_pac_bytes) {}
31
32  virtual ~ProxyResolver() {}
33
34  // Gets a list of proxy servers to use for |url|. If the request will
35  // complete asynchronously returns ERR_IO_PENDING and notifies the result
36  // by running |callback|.  If the result code is OK then
37  // the request was successful and |results| contains the proxy
38  // resolution information.  In the case of asynchronous completion
39  // |*request| is written to, and can be passed to CancelRequest().
40  virtual int GetProxyForURL(const GURL& url,
41                             ProxyInfo* results,
42                             CompletionCallback* callback,
43                             RequestHandle* request,
44                             LoadLog* load_log) = 0;
45
46  // Cancels |request|.
47  virtual void CancelRequest(RequestHandle request) = 0;
48
49  // The PAC script backend can be specified to the ProxyResolver either via
50  // URL, or via the javascript text itself.  If |expects_pac_bytes| is true,
51  // then PAC scripts should be specified using SetPacScriptByData(). Otherwise
52  // they should be specified using SetPacScriptByUrl().
53  bool expects_pac_bytes() const { return expects_pac_bytes_; }
54
55  // Sets the PAC script backend to use for this proxy resolver (by URL).
56  int SetPacScriptByUrl(const GURL& url, CompletionCallback* callback) {
57    DCHECK(!expects_pac_bytes());
58    return SetPacScript(url, std::string(), callback);
59  }
60
61  // Sets the PAC script backend to use for this proxy resolver (by contents).
62  int SetPacScriptByData(const std::string& bytes_utf8,
63                         CompletionCallback* callback) {
64    DCHECK(expects_pac_bytes());
65    return SetPacScript(GURL(), bytes_utf8, callback);
66  }
67
68  // TODO(eroman): Make this =0.
69  virtual void CancelSetPacScript() {
70    NOTREACHED();
71  }
72
73  // Frees any unneeded memory held by the resolver, e.g. garbage in the JS
74  // engine.  Most subclasses don't need to do anything, so we provide a default
75  // no-op implementation.
76  virtual void PurgeMemory() {}
77
78 private:
79  // Called to set the PAC script backend to use. If |pac_url| is invalid,
80  // this is a request to use WPAD (auto detect). |bytes_utf8| may be empty if
81  // the fetch failed, or if the fetch returned no content.
82  // Returns ERR_IO_PENDING in the case of asynchronous completion, and notifies
83  // the result through |callback|.
84  virtual int SetPacScript(const GURL& pac_url,
85                           const std::string& bytes_utf8,
86                           CompletionCallback* callback) = 0;
87
88  const bool expects_pac_bytes_;
89
90  DISALLOW_COPY_AND_ASSIGN(ProxyResolver);
91};
92
93}  // namespace net
94
95#endif  // NET_PROXY_PROXY_RESOLVER_H_
96