QUIC - wait for disk cache to load server config if the server is among
[chromium-blink-merge.git] / net / proxy / proxy_resolver_v8_tracing_wrapper.cc
blob01dde66487203f2e344d78b01385eba2f13cc122
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"
7 #include <string>
9 #include "base/bind.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"
15 namespace net {
16 namespace {
18 // Returns event parameters for a PAC error message (line number + message).
19 scoped_ptr<base::Value> NetLogErrorCallback(
20 int line_number,
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);
26 return dict.Pass();
29 class BindingsImpl : public ProxyResolverV8Tracing::Bindings {
30 public:
31 BindingsImpl(ProxyResolverErrorObserver* error_observer,
32 HostResolver* host_resolver,
33 NetLog* net_log,
34 const BoundNetLog& bound_net_log)
35 : error_observer_(error_observer),
36 host_resolver_(host_resolver),
37 net_log_(net_log),
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));
53 if (error_observer_)
54 error_observer_->OnPACScriptError(line_number, message);
57 HostResolver* GetHostResolver() override { return host_resolver_; }
59 BoundNetLog GetBoundNetLog() override { return bound_net_log_; }
61 private:
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.
68 if (net_log_)
69 net_log_->AddGlobalEntry(type, parameters_callback);
72 ProxyResolverErrorObserver* error_observer_;
73 HostResolver* host_resolver_;
74 NetLog* net_log_;
75 BoundNetLog bound_net_log_;
78 class ProxyResolverV8TracingWrapper : public ProxyResolver {
79 public:
80 ProxyResolverV8TracingWrapper(
81 scoped_ptr<ProxyResolverV8Tracing> resolver_impl,
82 NetLog* net_log,
83 HostResolver* host_resolver,
84 scoped_ptr<ProxyResolverErrorObserver> error_observer);
86 int GetProxyForURL(const GURL& url,
87 ProxyInfo* results,
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;
96 private:
97 scoped_ptr<ProxyResolverV8Tracing> resolver_impl_;
98 NetLog* net_log_;
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,
107 NetLog* net_log,
108 HostResolver* host_resolver,
109 scoped_ptr<ProxyResolverErrorObserver> error_observer)
110 : resolver_impl_(resolver_impl.Pass()),
111 net_log_(net_log),
112 host_resolver_(host_resolver),
113 error_observer_(error_observer.Pass()) {
116 int ProxyResolverV8TracingWrapper::GetProxyForURL(
117 const GURL& url,
118 ProxyInfo* results,
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);
138 } // namespace
140 ProxyResolverFactoryV8TracingWrapper::ProxyResolverFactoryV8TracingWrapper(
141 HostResolver* host_resolver,
142 NetLog* net_log,
143 const base::Callback<scoped_ptr<ProxyResolverErrorObserver>()>&
144 error_observer_factory)
145 : ProxyResolverFactory(true),
146 factory_impl_(ProxyResolverV8TracingFactory::Create()),
147 host_resolver_(host_resolver),
148 net_log_(net_log),
149 error_observer_factory_(error_observer_factory) {
152 ProxyResolverFactoryV8TracingWrapper::~ProxyResolverFactoryV8TracingWrapper() =
153 default;
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(
169 pac_script,
170 make_scoped_ptr(new BindingsImpl(error_observer_local, host_resolver_,
171 net_log_, BoundNetLog())),
172 v8_resolver_local,
173 base::Bind(&ProxyResolverFactoryV8TracingWrapper::OnProxyResolverCreated,
174 base::Unretained(this), base::Passed(&v8_resolver), resolver,
175 callback, base::Passed(&error_observer)),
176 request);
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,
185 int error) {
186 if (error == OK) {
187 resolver->reset(new ProxyResolverV8TracingWrapper(
188 v8_resolver->Pass(), net_log_, host_resolver_, error_observer.Pass()));
190 callback.Run(error);
193 } // namespace net