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.
5 #ifndef NET_PROXY_MOJO_PROXY_RESOLVER_V8_TRACING_BINDINGS_H_
6 #define NET_PROXY_MOJO_PROXY_RESOLVER_V8_TRACING_BINDINGS_H_
8 #include "base/memory/weak_ptr.h"
9 #include "base/single_thread_task_runner.h"
10 #include "base/thread_task_runner_handle.h"
11 #include "mojo/common/common_type_converters.h"
12 #include "net/dns/host_resolver_mojo.h"
13 #include "net/interfaces/proxy_resolver_service.mojom.h"
14 #include "net/proxy/proxy_resolver_v8_tracing.h"
18 // An implementation of ProxyResolverV8Tracing::Bindings that forwards requests
19 // onto a Client mojo interface. Alert() and OnError() may be called from any
20 // thread; when they are called from another thread, the calls are proxied to
21 // the origin task runner. GetHostResolver() and GetBoundNetLog() may only be
22 // called from the origin task runner.
23 template <typename Client
>
24 class MojoProxyResolverV8TracingBindings
25 : public ProxyResolverV8Tracing::Bindings
,
26 public HostResolverMojo::Impl
{
28 explicit MojoProxyResolverV8TracingBindings(base::WeakPtr
<Client
> client
)
29 : task_runner_(base::ThreadTaskRunnerHandle::Get()),
31 host_resolver_(this) {}
33 // ProxyResolverV8Tracing::Bindings overrides.
34 void Alert(const base::string16
& message
) override
{
35 if (!task_runner_
->BelongsToCurrentThread()) {
36 task_runner_
->PostTask(
38 base::Bind(&MojoProxyResolverV8TracingBindings::AlertOnTaskRunner
,
42 AlertOnTaskRunner(client_
, message
);
45 void OnError(int line_number
, const base::string16
& message
) override
{
46 if (!task_runner_
->BelongsToCurrentThread()) {
47 task_runner_
->PostTask(
49 base::Bind(&MojoProxyResolverV8TracingBindings::OnErrorOnTaskRunner
,
50 client_
, line_number
, message
));
53 OnErrorOnTaskRunner(client_
, line_number
, message
);
56 HostResolver
* GetHostResolver() override
{
57 DCHECK(task_runner_
->BelongsToCurrentThread());
58 return &host_resolver_
;
61 BoundNetLog
GetBoundNetLog() override
{ return BoundNetLog(); }
64 static void AlertOnTaskRunner(base::WeakPtr
<Client
> client
,
65 const base::string16
& message
) {
67 client
->Alert(mojo::String::From(message
));
70 static void OnErrorOnTaskRunner(base::WeakPtr
<Client
> client
,
72 const base::string16
& message
) {
74 client
->OnError(line_number
, mojo::String::From(message
));
77 // HostResolverMojo::Impl override.
78 void ResolveDns(interfaces::HostResolverRequestInfoPtr request_info
,
79 interfaces::HostResolverRequestClientPtr client
) {
80 DCHECK(task_runner_
->BelongsToCurrentThread());
82 client_
->ResolveDns(request_info
.Pass(), client
.Pass());
85 scoped_refptr
<base::SingleThreadTaskRunner
> task_runner_
;
86 base::WeakPtr
<Client
> client_
;
87 HostResolverMojo host_resolver_
;
92 #endif // NET_PROXY_MOJO_PROXY_RESOLVER_V8_TRACING_BINDINGS_H_