proxy_resolver_v8.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2011 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. #ifndef SERVICES_PROXY_RESOLVER_PROXY_RESOLVER_V8_H_
  5. #define SERVICES_PROXY_RESOLVER_PROXY_RESOLVER_V8_H_
  6. #include <stddef.h>
  7. #include <memory>
  8. #include <string>
  9. #include "base/compiler_specific.h"
  10. #include "base/memory/ref_counted.h"
  11. #include "net/proxy_resolution/proxy_resolve_dns_operation.h"
  12. class GURL;
  13. namespace net {
  14. class ProxyInfo;
  15. class PacFileData;
  16. } // namespace net
  17. namespace proxy_resolver {
  18. // A synchronous ProxyResolver-like that uses V8 to evaluate PAC scripts.
  19. class ProxyResolverV8 {
  20. public:
  21. // Interface for the javascript bindings.
  22. class JSBindings {
  23. public:
  24. JSBindings() {}
  25. // Handler for "dnsResolve()", "dnsResolveEx()", "myIpAddress()",
  26. // "myIpAddressEx()". Returns true on success and fills |*output| with the
  27. // result. If |*terminate| is set to true, then the script execution will
  28. // be aborted. Note that termination may not happen right away.
  29. virtual bool ResolveDns(const std::string& host,
  30. net::ProxyResolveDnsOperation op,
  31. std::string* output,
  32. bool* terminate) = 0;
  33. // Handler for "alert(message)"
  34. virtual void Alert(const std::u16string& message) = 0;
  35. // Handler for when an error is encountered. |line_number| may be -1
  36. // if a line number is not applicable to this error.
  37. virtual void OnError(int line_number, const std::u16string& error) = 0;
  38. protected:
  39. virtual ~JSBindings() {}
  40. };
  41. // Constructs a ProxyResolverV8.
  42. static int Create(const scoped_refptr<net::PacFileData>& script_data,
  43. JSBindings* bindings,
  44. std::unique_ptr<ProxyResolverV8>* resolver);
  45. ProxyResolverV8(const ProxyResolverV8&) = delete;
  46. ProxyResolverV8& operator=(const ProxyResolverV8&) = delete;
  47. ~ProxyResolverV8();
  48. int GetProxyForURL(const GURL& url,
  49. net::ProxyInfo* results,
  50. JSBindings* bindings);
  51. // Get total/used heap memory usage of all v8 instances used by the proxy
  52. // resolver.
  53. static size_t GetTotalHeapSize();
  54. static size_t GetUsedHeapSize();
  55. private:
  56. // Context holds the Javascript state for the PAC script.
  57. class Context;
  58. explicit ProxyResolverV8(std::unique_ptr<Context> context);
  59. std::unique_ptr<Context> context_;
  60. };
  61. } // namespace proxy_resolver
  62. #endif // SERVICES_PROXY_RESOLVER_PROXY_RESOLVER_V8_H_