OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/multi_threaded_proxy_resolver.h" | 5 #include "net/proxy/multi_threaded_proxy_resolver.h" |
6 | 6 |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/stl_util.h" | 8 #include "base/stl_util.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 18 matching lines...) Expand all Loading... |
29 // conjunction with MultiThreadedProxyResolver. | 29 // conjunction with MultiThreadedProxyResolver. |
30 // - returns a single-item proxy list with the query's host. | 30 // - returns a single-item proxy list with the query's host. |
31 class MockProxyResolver : public ProxyResolver { | 31 class MockProxyResolver : public ProxyResolver { |
32 public: | 32 public: |
33 MockProxyResolver() | 33 MockProxyResolver() |
34 : ProxyResolver(true /*expects_pac_bytes*/), | 34 : ProxyResolver(true /*expects_pac_bytes*/), |
35 wrong_loop_(base::MessageLoop::current()), | 35 wrong_loop_(base::MessageLoop::current()), |
36 request_count_(0) {} | 36 request_count_(0) {} |
37 | 37 |
38 // ProxyResolver implementation. | 38 // ProxyResolver implementation. |
39 virtual int GetProxyForURL(const GURL& query_url, | 39 int GetProxyForURL(const GURL& query_url, |
40 ProxyInfo* results, | 40 ProxyInfo* results, |
41 const CompletionCallback& callback, | 41 const CompletionCallback& callback, |
42 RequestHandle* request, | 42 RequestHandle* request, |
43 const BoundNetLog& net_log) override { | 43 const BoundNetLog& net_log) override { |
44 if (resolve_latency_ != base::TimeDelta()) | 44 if (resolve_latency_ != base::TimeDelta()) |
45 base::PlatformThread::Sleep(resolve_latency_); | 45 base::PlatformThread::Sleep(resolve_latency_); |
46 | 46 |
47 CheckIsOnWorkerThread(); | 47 CheckIsOnWorkerThread(); |
48 | 48 |
49 EXPECT_TRUE(callback.is_null()); | 49 EXPECT_TRUE(callback.is_null()); |
50 EXPECT_TRUE(request == NULL); | 50 EXPECT_TRUE(request == NULL); |
51 | 51 |
52 // Write something into |net_log| (doesn't really have any meaning.) | 52 // Write something into |net_log| (doesn't really have any meaning.) |
53 net_log.BeginEvent(NetLog::TYPE_PAC_JAVASCRIPT_ALERT); | 53 net_log.BeginEvent(NetLog::TYPE_PAC_JAVASCRIPT_ALERT); |
54 | 54 |
55 results->UseNamedProxy(query_url.host()); | 55 results->UseNamedProxy(query_url.host()); |
56 | 56 |
57 // Return a success code which represents the request's order. | 57 // Return a success code which represents the request's order. |
58 return request_count_++; | 58 return request_count_++; |
59 } | 59 } |
60 | 60 |
61 virtual void CancelRequest(RequestHandle request) override { | 61 void CancelRequest(RequestHandle request) override { NOTREACHED(); } |
62 NOTREACHED(); | |
63 } | |
64 | 62 |
65 virtual LoadState GetLoadState(RequestHandle request) const override { | 63 LoadState GetLoadState(RequestHandle request) const override { |
66 NOTREACHED(); | 64 NOTREACHED(); |
67 return LOAD_STATE_IDLE; | 65 return LOAD_STATE_IDLE; |
68 } | 66 } |
69 | 67 |
70 virtual void CancelSetPacScript() override { | 68 void CancelSetPacScript() override { NOTREACHED(); } |
71 NOTREACHED(); | |
72 } | |
73 | 69 |
74 virtual int SetPacScript( | 70 int SetPacScript(const scoped_refptr<ProxyResolverScriptData>& script_data, |
75 const scoped_refptr<ProxyResolverScriptData>& script_data, | 71 const CompletionCallback& callback) override { |
76 const CompletionCallback& callback) override { | |
77 CheckIsOnWorkerThread(); | 72 CheckIsOnWorkerThread(); |
78 last_script_data_ = script_data; | 73 last_script_data_ = script_data; |
79 return OK; | 74 return OK; |
80 } | 75 } |
81 | 76 |
82 int request_count() const { return request_count_; } | 77 int request_count() const { return request_count_; } |
83 | 78 |
84 const ProxyResolverScriptData* last_script_data() const { | 79 const ProxyResolverScriptData* last_script_data() const { |
85 return last_script_data_.get(); | 80 return last_script_data_.get(); |
86 } | 81 } |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 void Unblock() { | 121 void Unblock() { |
127 should_block_ = false; | 122 should_block_ = false; |
128 blocked_.Reset(); | 123 blocked_.Reset(); |
129 unblocked_.Signal(); | 124 unblocked_.Signal(); |
130 } | 125 } |
131 | 126 |
132 void WaitUntilBlocked() { | 127 void WaitUntilBlocked() { |
133 blocked_.Wait(); | 128 blocked_.Wait(); |
134 } | 129 } |
135 | 130 |
136 virtual int GetProxyForURL(const GURL& query_url, | 131 int GetProxyForURL(const GURL& query_url, |
137 ProxyInfo* results, | 132 ProxyInfo* results, |
138 const CompletionCallback& callback, | 133 const CompletionCallback& callback, |
139 RequestHandle* request, | 134 RequestHandle* request, |
140 const BoundNetLog& net_log) override { | 135 const BoundNetLog& net_log) override { |
141 if (should_block_) { | 136 if (should_block_) { |
142 blocked_.Signal(); | 137 blocked_.Signal(); |
143 unblocked_.Wait(); | 138 unblocked_.Wait(); |
144 } | 139 } |
145 | 140 |
146 return MockProxyResolver::GetProxyForURL( | 141 return MockProxyResolver::GetProxyForURL( |
147 query_url, results, callback, request, net_log); | 142 query_url, results, callback, request, net_log); |
148 } | 143 } |
149 | 144 |
150 private: | 145 private: |
151 bool should_block_; | 146 bool should_block_; |
152 base::WaitableEvent unblocked_; | 147 base::WaitableEvent unblocked_; |
153 base::WaitableEvent blocked_; | 148 base::WaitableEvent blocked_; |
154 }; | 149 }; |
155 | 150 |
156 // ForwardingProxyResolver forwards all requests to |impl|. | 151 // ForwardingProxyResolver forwards all requests to |impl|. |
157 class ForwardingProxyResolver : public ProxyResolver { | 152 class ForwardingProxyResolver : public ProxyResolver { |
158 public: | 153 public: |
159 explicit ForwardingProxyResolver(ProxyResolver* impl) | 154 explicit ForwardingProxyResolver(ProxyResolver* impl) |
160 : ProxyResolver(impl->expects_pac_bytes()), | 155 : ProxyResolver(impl->expects_pac_bytes()), |
161 impl_(impl) {} | 156 impl_(impl) {} |
162 | 157 |
163 virtual int GetProxyForURL(const GURL& query_url, | 158 int GetProxyForURL(const GURL& query_url, |
164 ProxyInfo* results, | 159 ProxyInfo* results, |
165 const CompletionCallback& callback, | 160 const CompletionCallback& callback, |
166 RequestHandle* request, | 161 RequestHandle* request, |
167 const BoundNetLog& net_log) override { | 162 const BoundNetLog& net_log) override { |
168 return impl_->GetProxyForURL( | 163 return impl_->GetProxyForURL( |
169 query_url, results, callback, request, net_log); | 164 query_url, results, callback, request, net_log); |
170 } | 165 } |
171 | 166 |
172 virtual void CancelRequest(RequestHandle request) override { | 167 void CancelRequest(RequestHandle request) override { |
173 impl_->CancelRequest(request); | 168 impl_->CancelRequest(request); |
174 } | 169 } |
175 | 170 |
176 virtual LoadState GetLoadState(RequestHandle request) const override { | 171 LoadState GetLoadState(RequestHandle request) const override { |
177 NOTREACHED(); | 172 NOTREACHED(); |
178 return LOAD_STATE_IDLE; | 173 return LOAD_STATE_IDLE; |
179 } | 174 } |
180 | 175 |
181 virtual void CancelSetPacScript() override { | 176 void CancelSetPacScript() override { impl_->CancelSetPacScript(); } |
182 impl_->CancelSetPacScript(); | |
183 } | |
184 | 177 |
185 virtual int SetPacScript( | 178 int SetPacScript(const scoped_refptr<ProxyResolverScriptData>& script_data, |
186 const scoped_refptr<ProxyResolverScriptData>& script_data, | 179 const CompletionCallback& callback) override { |
187 const CompletionCallback& callback) override { | |
188 return impl_->SetPacScript(script_data, callback); | 180 return impl_->SetPacScript(script_data, callback); |
189 } | 181 } |
190 | 182 |
191 private: | 183 private: |
192 ProxyResolver* impl_; | 184 ProxyResolver* impl_; |
193 }; | 185 }; |
194 | 186 |
195 // This factory returns ProxyResolvers that forward all requests to | 187 // This factory returns ProxyResolvers that forward all requests to |
196 // |resolver|. | 188 // |resolver|. |
197 class ForwardingProxyResolverFactory : public ProxyResolverFactory { | 189 class ForwardingProxyResolverFactory : public ProxyResolverFactory { |
198 public: | 190 public: |
199 explicit ForwardingProxyResolverFactory(ProxyResolver* resolver) | 191 explicit ForwardingProxyResolverFactory(ProxyResolver* resolver) |
200 : ProxyResolverFactory(resolver->expects_pac_bytes()), | 192 : ProxyResolverFactory(resolver->expects_pac_bytes()), |
201 resolver_(resolver) {} | 193 resolver_(resolver) {} |
202 | 194 |
203 virtual ProxyResolver* CreateProxyResolver() override { | 195 ProxyResolver* CreateProxyResolver() override { |
204 return new ForwardingProxyResolver(resolver_); | 196 return new ForwardingProxyResolver(resolver_); |
205 } | 197 } |
206 | 198 |
207 private: | 199 private: |
208 ProxyResolver* resolver_; | 200 ProxyResolver* resolver_; |
209 }; | 201 }; |
210 | 202 |
211 // This factory returns new instances of BlockableProxyResolver. | 203 // This factory returns new instances of BlockableProxyResolver. |
212 class BlockableProxyResolverFactory : public ProxyResolverFactory { | 204 class BlockableProxyResolverFactory : public ProxyResolverFactory { |
213 public: | 205 public: |
214 BlockableProxyResolverFactory() : ProxyResolverFactory(true) {} | 206 BlockableProxyResolverFactory() : ProxyResolverFactory(true) {} |
215 | 207 |
216 virtual ~BlockableProxyResolverFactory() { | 208 ~BlockableProxyResolverFactory() override { STLDeleteElements(&resolvers_); } |
217 STLDeleteElements(&resolvers_); | |
218 } | |
219 | 209 |
220 virtual ProxyResolver* CreateProxyResolver() override { | 210 ProxyResolver* CreateProxyResolver() override { |
221 BlockableProxyResolver* resolver = new BlockableProxyResolver; | 211 BlockableProxyResolver* resolver = new BlockableProxyResolver; |
222 resolvers_.push_back(resolver); | 212 resolvers_.push_back(resolver); |
223 return new ForwardingProxyResolver(resolver); | 213 return new ForwardingProxyResolver(resolver); |
224 } | 214 } |
225 | 215 |
226 std::vector<BlockableProxyResolver*> resolvers() { | 216 std::vector<BlockableProxyResolver*> resolvers() { |
227 return resolvers_; | 217 return resolvers_; |
228 } | 218 } |
229 | 219 |
230 private: | 220 private: |
(...skipping 523 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
754 // All in all, the first thread should have seen just 1 request. And the | 744 // All in all, the first thread should have seen just 1 request. And the |
755 // second thread 3 requests. | 745 // second thread 3 requests. |
756 ASSERT_EQ(2u, factory->resolvers().size()); | 746 ASSERT_EQ(2u, factory->resolvers().size()); |
757 EXPECT_EQ(1, factory->resolvers()[0]->request_count()); | 747 EXPECT_EQ(1, factory->resolvers()[0]->request_count()); |
758 EXPECT_EQ(3, factory->resolvers()[1]->request_count()); | 748 EXPECT_EQ(3, factory->resolvers()[1]->request_count()); |
759 } | 749 } |
760 | 750 |
761 } // namespace | 751 } // namespace |
762 | 752 |
763 } // namespace net | 753 } // namespace net |
OLD | NEW |