OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/proxy/proxy_resolver_factory.h" | 5 #include "net/proxy/proxy_resolver_factory.h" |
6 | 6 |
7 #include "net/base/net_errors.h" | 7 #include "net/base/net_errors.h" |
8 #include "net/proxy/proxy_resolver.h" | 8 #include "net/proxy/proxy_resolver.h" |
9 | 9 |
10 namespace net { | 10 namespace net { |
11 | 11 |
12 class LegacyProxyResolverFactory::Job : public ProxyResolverFactory::Request { | |
13 public: | |
14 Job(LegacyProxyResolverFactory* factory, | |
15 const scoped_refptr<ProxyResolverScriptData>& pac_script, | |
16 scoped_ptr<ProxyResolver> resolver, | |
17 scoped_ptr<ProxyResolver>* resolver_out, | |
18 const net::CompletionCallback& callback); | |
19 ~Job() override; | |
20 int Start(); | |
21 | |
22 void FactoryDestroyed(); | |
23 | |
24 private: | |
25 void OnSetPacScriptDone(int error); | |
26 | |
27 LegacyProxyResolverFactory* factory_; | |
28 const scoped_refptr<ProxyResolverScriptData> pac_script_; | |
29 scoped_ptr<ProxyResolver> resolver_; | |
30 scoped_ptr<ProxyResolver>* resolver_out_; | |
31 const net::CompletionCallback callback_; | |
32 }; | |
33 | |
34 LegacyProxyResolverFactory::Job::Job( | |
35 LegacyProxyResolverFactory* factory, | |
36 const scoped_refptr<ProxyResolverScriptData>& pac_script, | |
37 scoped_ptr<ProxyResolver> resolver, | |
38 scoped_ptr<ProxyResolver>* resolver_out, | |
39 const net::CompletionCallback& callback) | |
40 : factory_(factory), | |
41 pac_script_(pac_script), | |
42 resolver_(resolver.Pass()), | |
43 resolver_out_(resolver_out), | |
44 callback_(callback) { | |
45 } | |
46 | |
47 LegacyProxyResolverFactory::Job::~Job() { | |
48 if (factory_) { | |
49 resolver_->CancelSetPacScript(); | |
50 factory_->RemoveJob(this); | |
51 } | |
52 } | |
53 | |
54 int LegacyProxyResolverFactory::Job::Start() { | |
55 int error = resolver_->SetPacScript( | |
56 pac_script_, | |
57 base::Bind(&Job::OnSetPacScriptDone, base::Unretained(this))); | |
58 if (error != ERR_IO_PENDING) { | |
59 factory_ = nullptr; | |
60 if (error == OK) | |
61 *resolver_out_ = resolver_.Pass(); | |
62 return error; | |
63 } | |
64 | |
65 return ERR_IO_PENDING; | |
66 } | |
67 | |
68 void LegacyProxyResolverFactory::Job::FactoryDestroyed() { | |
69 factory_ = nullptr; | |
70 resolver_->CancelSetPacScript(); | |
71 } | |
72 | |
73 void LegacyProxyResolverFactory::Job::OnSetPacScriptDone(int error) { | |
74 factory_->RemoveJob(this); | |
75 if (error == OK) | |
76 *resolver_out_ = resolver_.Pass(); | |
77 | |
78 CompletionCallback callback = callback_; | |
79 factory_ = nullptr; | |
80 if (!callback.is_null()) | |
81 callback.Run(error); | |
82 } | |
83 | |
84 ProxyResolverFactory::ProxyResolverFactory(bool expects_pac_bytes) | 12 ProxyResolverFactory::ProxyResolverFactory(bool expects_pac_bytes) |
85 : expects_pac_bytes_(expects_pac_bytes) { | 13 : expects_pac_bytes_(expects_pac_bytes) { |
86 } | 14 } |
87 | 15 |
88 ProxyResolverFactory::~ProxyResolverFactory() { | 16 ProxyResolverFactory::~ProxyResolverFactory() { |
89 } | 17 } |
90 | 18 |
91 LegacyProxyResolverFactory::LegacyProxyResolverFactory(bool expects_pac_bytes) | |
92 : ProxyResolverFactory(expects_pac_bytes) { | |
93 } | |
94 | |
95 LegacyProxyResolverFactory::~LegacyProxyResolverFactory() { | |
96 for (auto job : jobs_) { | |
97 job->FactoryDestroyed(); | |
98 } | |
99 } | |
100 | |
101 int LegacyProxyResolverFactory::CreateProxyResolver( | |
102 const scoped_refptr<ProxyResolverScriptData>& pac_script, | |
103 scoped_ptr<ProxyResolver>* resolver, | |
104 const net::CompletionCallback& callback, | |
105 scoped_ptr<ProxyResolverFactory::Request>* request) { | |
106 scoped_ptr<Job> job( | |
107 new Job(this, pac_script, CreateProxyResolver(), resolver, callback)); | |
108 int error = job->Start(); | |
109 if (error != ERR_IO_PENDING) | |
110 return error; | |
111 | |
112 jobs_.insert(job.get()); | |
113 *request = job.Pass(); | |
114 return ERR_IO_PENDING; | |
115 } | |
116 | |
117 void LegacyProxyResolverFactory::RemoveJob(Job* job) { | |
118 size_t erased = jobs_.erase(job); | |
119 DCHECK_EQ(1u, erased); | |
120 } | |
121 | |
122 } // namespace net | 19 } // namespace net |
OLD | NEW |