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 #include "net/proxy/proxy_resolver_v8_tracing_wrapper.h"
10 #include "base/values.h"
11 #include "net/base/net_errors.h"
12 #include "net/log/net_log.h"
13 #include "net/proxy/proxy_resolver_error_observer.h"
18 // Returns event parameters for a PAC error message (line number + message).
19 scoped_ptr
<base::Value
> NetLogErrorCallback(
21 const base::string16
* message
,
22 NetLogCaptureMode
/* capture_mode */) {
23 scoped_ptr
<base::DictionaryValue
> dict(new base::DictionaryValue());
24 dict
->SetInteger("line_number", line_number
);
25 dict
->SetString("message", *message
);
29 class BindingsImpl
: public ProxyResolverV8Tracing::Bindings
{
31 BindingsImpl(ProxyResolverErrorObserver
* error_observer
,
32 HostResolver
* host_resolver
,
34 const BoundNetLog
& bound_net_log
)
35 : error_observer_(error_observer
),
36 host_resolver_(host_resolver
),
38 bound_net_log_(bound_net_log
) {}
40 // ProxyResolverV8Tracing::Bindings overrides.
41 void Alert(const base::string16
& message
) override
{
42 // Send to the NetLog.
43 LogEventToCurrentRequestAndGlobally(
44 NetLog::TYPE_PAC_JAVASCRIPT_ALERT
,
45 NetLog::StringCallback("message", &message
));
48 void OnError(int line_number
, const base::string16
& message
) override
{
49 // Send the error to the NetLog.
50 LogEventToCurrentRequestAndGlobally(
51 NetLog::TYPE_PAC_JAVASCRIPT_ERROR
,
52 base::Bind(&NetLogErrorCallback
, line_number
, &message
));
54 error_observer_
->OnPACScriptError(line_number
, message
);
57 HostResolver
* GetHostResolver() override
{ return host_resolver_
; }
59 BoundNetLog
GetBoundNetLog() override
{ return bound_net_log_
; }
62 void LogEventToCurrentRequestAndGlobally(
63 NetLog::EventType type
,
64 const NetLog::ParametersCallback
& parameters_callback
) {
65 bound_net_log_
.AddEvent(type
, parameters_callback
);
67 // Emit to the global NetLog event stream.
69 net_log_
->AddGlobalEntry(type
, parameters_callback
);
72 ProxyResolverErrorObserver
* error_observer_
;
73 HostResolver
* host_resolver_
;
75 BoundNetLog bound_net_log_
;
78 class ProxyResolverV8TracingWrapper
: public ProxyResolver
{
80 ProxyResolverV8TracingWrapper(
81 scoped_ptr
<ProxyResolverV8Tracing
> resolver_impl
,
83 HostResolver
* host_resolver
,
84 scoped_ptr
<ProxyResolverErrorObserver
> error_observer
);
86 int GetProxyForURL(const GURL
& url
,
88 const CompletionCallback
& callback
,
89 RequestHandle
* request
,
90 const BoundNetLog
& net_log
) override
;
92 void CancelRequest(RequestHandle request
) override
;
94 LoadState
GetLoadState(RequestHandle request
) const override
;
97 scoped_ptr
<ProxyResolverV8Tracing
> resolver_impl_
;
99 HostResolver
* host_resolver_
;
100 scoped_ptr
<ProxyResolverErrorObserver
> error_observer_
;
102 DISALLOW_COPY_AND_ASSIGN(ProxyResolverV8TracingWrapper
);
105 ProxyResolverV8TracingWrapper::ProxyResolverV8TracingWrapper(
106 scoped_ptr
<ProxyResolverV8Tracing
> resolver_impl
,
108 HostResolver
* host_resolver
,
109 scoped_ptr
<ProxyResolverErrorObserver
> error_observer
)
110 : resolver_impl_(resolver_impl
.Pass()),
112 host_resolver_(host_resolver
),
113 error_observer_(error_observer
.Pass()) {
116 int ProxyResolverV8TracingWrapper::GetProxyForURL(
119 const CompletionCallback
& callback
,
120 RequestHandle
* request
,
121 const BoundNetLog
& net_log
) {
122 resolver_impl_
->GetProxyForURL(
123 url
, results
, callback
, request
,
124 make_scoped_ptr(new BindingsImpl(error_observer_
.get(), host_resolver_
,
125 net_log_
, net_log
)));
126 return ERR_IO_PENDING
;
129 void ProxyResolverV8TracingWrapper::CancelRequest(RequestHandle request
) {
130 resolver_impl_
->CancelRequest(request
);
133 LoadState
ProxyResolverV8TracingWrapper::GetLoadState(
134 RequestHandle request
) const {
135 return resolver_impl_
->GetLoadState(request
);
140 ProxyResolverFactoryV8TracingWrapper::ProxyResolverFactoryV8TracingWrapper(
141 HostResolver
* host_resolver
,
143 const base::Callback
<scoped_ptr
<ProxyResolverErrorObserver
>()>&
144 error_observer_factory
)
145 : ProxyResolverFactory(true),
146 factory_impl_(ProxyResolverV8TracingFactory::Create()),
147 host_resolver_(host_resolver
),
149 error_observer_factory_(error_observer_factory
) {
152 ProxyResolverFactoryV8TracingWrapper::~ProxyResolverFactoryV8TracingWrapper() =
155 int ProxyResolverFactoryV8TracingWrapper::CreateProxyResolver(
156 const scoped_refptr
<ProxyResolverScriptData
>& pac_script
,
157 scoped_ptr
<ProxyResolver
>* resolver
,
158 const CompletionCallback
& callback
,
159 scoped_ptr
<Request
>* request
) {
160 scoped_ptr
<scoped_ptr
<ProxyResolverV8Tracing
>> v8_resolver(
161 new scoped_ptr
<ProxyResolverV8Tracing
>);
162 scoped_ptr
<ProxyResolverErrorObserver
> error_observer
=
163 error_observer_factory_
.Run();
164 // Note: Argument evaluation order is unspecified, so make copies before
165 // passing |v8_resolver| and |error_observer|.
166 scoped_ptr
<ProxyResolverV8Tracing
>* v8_resolver_local
= v8_resolver
.get();
167 ProxyResolverErrorObserver
* error_observer_local
= error_observer
.get();
168 factory_impl_
->CreateProxyResolverV8Tracing(
170 make_scoped_ptr(new BindingsImpl(error_observer_local
, host_resolver_
,
171 net_log_
, BoundNetLog())),
173 base::Bind(&ProxyResolverFactoryV8TracingWrapper::OnProxyResolverCreated
,
174 base::Unretained(this), base::Passed(&v8_resolver
), resolver
,
175 callback
, base::Passed(&error_observer
)),
177 return ERR_IO_PENDING
;
180 void ProxyResolverFactoryV8TracingWrapper::OnProxyResolverCreated(
181 scoped_ptr
<scoped_ptr
<ProxyResolverV8Tracing
>> v8_resolver
,
182 scoped_ptr
<ProxyResolver
>* resolver
,
183 const CompletionCallback
& callback
,
184 scoped_ptr
<ProxyResolverErrorObserver
> error_observer
,
187 resolver
->reset(new ProxyResolverV8TracingWrapper(
188 v8_resolver
->Pass(), net_log_
, host_resolver_
, error_observer
.Pass()));