mojo_proxy_resolver_v8_tracing_bindings.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2015 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_MOJO_PROXY_RESOLVER_V8_TRACING_BINDINGS_H_
  5. #define SERVICES_PROXY_RESOLVER_MOJO_PROXY_RESOLVER_V8_TRACING_BINDINGS_H_
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include "base/check.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "base/strings/utf_string_conversions.h"
  12. #include "base/threading/thread_checker.h"
  13. #include "net/base/address_family.h"
  14. #include "net/base/host_port_pair.h"
  15. #include "net/base/network_isolation_key.h"
  16. #include "net/log/net_log_with_source.h"
  17. #include "net/proxy_resolution/proxy_resolve_dns_operation.h"
  18. #include "services/proxy_resolver/host_resolver_mojo.h"
  19. #include "services/proxy_resolver/proxy_host_resolver.h"
  20. #include "services/proxy_resolver/proxy_resolver_v8_tracing.h"
  21. #include "services/proxy_resolver/public/mojom/proxy_resolver.mojom.h"
  22. namespace proxy_resolver {
  23. // An implementation of ProxyResolverV8Tracing::Bindings that forwards requests
  24. // onto a Client mojo interface. Alert() and OnError() may be called from any
  25. // thread; when they are called from another thread, the calls are proxied to
  26. // the origin task runner. GetHostResolver() and GetNetLogWithSource() may only
  27. // be called from the origin task runner.
  28. template <typename Client>
  29. class MojoProxyResolverV8TracingBindings
  30. : public ProxyResolverV8Tracing::Bindings,
  31. public HostResolverMojo::Impl {
  32. public:
  33. explicit MojoProxyResolverV8TracingBindings(Client* client)
  34. : client_(client), host_resolver_(this) {
  35. DCHECK(client_);
  36. }
  37. // ProxyResolverV8Tracing::Bindings overrides.
  38. void Alert(const std::u16string& message) override {
  39. DCHECK(thread_checker_.CalledOnValidThread());
  40. client_->Alert(base::UTF16ToUTF8(message));
  41. }
  42. void OnError(int line_number, const std::u16string& message) override {
  43. DCHECK(thread_checker_.CalledOnValidThread());
  44. client_->OnError(line_number, base::UTF16ToUTF8(message));
  45. }
  46. ProxyHostResolver* GetHostResolver() override {
  47. DCHECK(thread_checker_.CalledOnValidThread());
  48. return &host_resolver_;
  49. }
  50. net::NetLogWithSource GetNetLogWithSource() override {
  51. DCHECK(thread_checker_.CalledOnValidThread());
  52. return net::NetLogWithSource();
  53. }
  54. private:
  55. // HostResolverMojo::Impl override.
  56. void ResolveDns(
  57. const std::string& hostname,
  58. net::ProxyResolveDnsOperation operation,
  59. const net::NetworkIsolationKey& network_isolation_key,
  60. mojo::PendingRemote<mojom::HostResolverRequestClient> client) override {
  61. DCHECK(thread_checker_.CalledOnValidThread());
  62. client_->ResolveDns(hostname, operation, network_isolation_key,
  63. std::move(client));
  64. }
  65. base::ThreadChecker thread_checker_;
  66. const raw_ptr<Client> client_;
  67. HostResolverMojo host_resolver_;
  68. };
  69. } // namespace proxy_resolver
  70. #endif // SERVICES_PROXY_RESOLVER_MOJO_PROXY_RESOLVER_V8_TRACING_BINDINGS_H_