Fix issue with longpress drag selection motion
[chromium-blink-merge.git] / net / proxy / mojo_proxy_resolver_factory_impl.cc
bloba913ce25ead573d9d0a1e2a1f8320d2a7028f64f
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/mojo_proxy_resolver_factory_impl.h"
7 #include <string>
9 #include "base/stl_util.h"
10 #include "net/base/net_errors.h"
11 #include "net/proxy/mojo_proxy_resolver_impl.h"
12 #include "net/proxy/mojo_proxy_resolver_v8_tracing_bindings.h"
13 #include "net/proxy/proxy_resolver_factory.h"
14 #include "net/proxy/proxy_resolver_v8_tracing.h"
16 namespace net {
17 namespace {
19 // A class to manage the lifetime of a MojoProxyResolverImpl. An instance will
20 // remain while the message pipe for the mojo connection remains open.
21 class MojoProxyResolverHolder {
22 public:
23 MojoProxyResolverHolder(
24 scoped_ptr<ProxyResolverV8Tracing> proxy_resolver_impl,
25 mojo::InterfaceRequest<interfaces::ProxyResolver> request);
27 private:
28 // Mojo error handler.
29 void OnConnectionError();
31 MojoProxyResolverImpl mojo_proxy_resolver_;
32 mojo::Binding<interfaces::ProxyResolver> binding_;
34 DISALLOW_COPY_AND_ASSIGN(MojoProxyResolverHolder);
37 MojoProxyResolverHolder::MojoProxyResolverHolder(
38 scoped_ptr<ProxyResolverV8Tracing> proxy_resolver_impl,
39 mojo::InterfaceRequest<interfaces::ProxyResolver> request)
40 : mojo_proxy_resolver_(proxy_resolver_impl.Pass()),
41 binding_(&mojo_proxy_resolver_, request.Pass()) {
42 binding_.set_connection_error_handler(base::Bind(
43 &MojoProxyResolverHolder::OnConnectionError, base::Unretained(this)));
46 void MojoProxyResolverHolder::OnConnectionError() {
47 delete this;
50 } // namespace
52 class MojoProxyResolverFactoryImpl::Job {
53 public:
54 Job(MojoProxyResolverFactoryImpl* parent,
55 const scoped_refptr<ProxyResolverScriptData>& pac_script,
56 ProxyResolverV8TracingFactory* proxy_resolver_factory,
57 mojo::InterfaceRequest<interfaces::ProxyResolver> request,
58 interfaces::ProxyResolverFactoryRequestClientPtr client);
59 ~Job();
61 private:
62 // Mojo error handler.
63 void OnConnectionError();
65 void OnProxyResolverCreated(int error);
67 MojoProxyResolverFactoryImpl* const parent_;
68 scoped_ptr<ProxyResolverV8Tracing> proxy_resolver_impl_;
69 mojo::InterfaceRequest<interfaces::ProxyResolver> proxy_request_;
70 ProxyResolverV8TracingFactory* factory_;
71 scoped_ptr<net::ProxyResolverFactory::Request> request_;
72 interfaces::ProxyResolverFactoryRequestClientPtr client_ptr_;
74 base::WeakPtrFactory<interfaces::ProxyResolverFactoryRequestClient>
75 weak_factory_;
77 DISALLOW_COPY_AND_ASSIGN(Job);
80 MojoProxyResolverFactoryImpl::Job::Job(
81 MojoProxyResolverFactoryImpl* factory,
82 const scoped_refptr<ProxyResolverScriptData>& pac_script,
83 ProxyResolverV8TracingFactory* proxy_resolver_factory,
84 mojo::InterfaceRequest<interfaces::ProxyResolver> request,
85 interfaces::ProxyResolverFactoryRequestClientPtr client)
86 : parent_(factory),
87 proxy_request_(request.Pass()),
88 factory_(proxy_resolver_factory),
89 client_ptr_(client.Pass()),
90 weak_factory_(client_ptr_.get()) {
91 client_ptr_.set_connection_error_handler(
92 base::Bind(&MojoProxyResolverFactoryImpl::Job::OnConnectionError,
93 base::Unretained(this)));
94 factory_->CreateProxyResolverV8Tracing(
95 pac_script, make_scoped_ptr(new MojoProxyResolverV8TracingBindings<
96 interfaces::ProxyResolverFactoryRequestClient>(
97 weak_factory_.GetWeakPtr())),
98 &proxy_resolver_impl_,
99 base::Bind(&MojoProxyResolverFactoryImpl::Job::OnProxyResolverCreated,
100 base::Unretained(this)),
101 &request_);
104 MojoProxyResolverFactoryImpl::Job::~Job() = default;
106 void MojoProxyResolverFactoryImpl::Job::OnConnectionError() {
107 client_ptr_->ReportResult(ERR_PAC_SCRIPT_TERMINATED);
108 parent_->RemoveJob(this);
111 void MojoProxyResolverFactoryImpl::Job::OnProxyResolverCreated(int error) {
112 if (error == OK) {
113 // The MojoProxyResolverHolder will delete itself if |proxy_request_|
114 // encounters a connection error.
115 new MojoProxyResolverHolder(proxy_resolver_impl_.Pass(),
116 proxy_request_.Pass());
118 client_ptr_->ReportResult(error);
119 parent_->RemoveJob(this);
122 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl(
123 scoped_ptr<ProxyResolverV8TracingFactory> proxy_resolver_factory,
124 mojo::InterfaceRequest<interfaces::ProxyResolverFactory> request)
125 : proxy_resolver_impl_factory_(proxy_resolver_factory.Pass()),
126 binding_(this, request.Pass()) {
129 MojoProxyResolverFactoryImpl::MojoProxyResolverFactoryImpl(
130 mojo::InterfaceRequest<interfaces::ProxyResolverFactory> request)
131 : MojoProxyResolverFactoryImpl(ProxyResolverV8TracingFactory::Create(),
132 request.Pass()) {
135 MojoProxyResolverFactoryImpl::~MojoProxyResolverFactoryImpl() {
136 STLDeleteElements(&jobs_);
139 void MojoProxyResolverFactoryImpl::CreateResolver(
140 const mojo::String& pac_script,
141 mojo::InterfaceRequest<interfaces::ProxyResolver> request,
142 interfaces::ProxyResolverFactoryRequestClientPtr client) {
143 // The Job will call RemoveJob on |this| when either the create request
144 // finishes or |request| or |client| encounters a connection error.
145 jobs_.insert(new Job(
146 this, ProxyResolverScriptData::FromUTF8(pac_script.To<std::string>()),
147 proxy_resolver_impl_factory_.get(), request.Pass(), client.Pass()));
150 void MojoProxyResolverFactoryImpl::RemoveJob(Job* job) {
151 size_t erased = jobs_.erase(job);
152 DCHECK_EQ(1u, erased);
153 delete job;
156 } // namespace net