| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/dhcp_proxy_script_fetcher_win.h" | 5 #include "net/proxy/dhcp_proxy_script_fetcher_win.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/perftimer.h" | 10 #include "base/perftimer.h" |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 | 153 |
| 154 // For RealFetchWithDeferredCancel, below. | 154 // For RealFetchWithDeferredCancel, below. |
| 155 class DelayingDhcpProxyScriptAdapterFetcher | 155 class DelayingDhcpProxyScriptAdapterFetcher |
| 156 : public DhcpProxyScriptAdapterFetcher { | 156 : public DhcpProxyScriptAdapterFetcher { |
| 157 public: | 157 public: |
| 158 explicit DelayingDhcpProxyScriptAdapterFetcher( | 158 explicit DelayingDhcpProxyScriptAdapterFetcher( |
| 159 URLRequestContext* url_request_context) | 159 URLRequestContext* url_request_context) |
| 160 : DhcpProxyScriptAdapterFetcher(url_request_context) { | 160 : DhcpProxyScriptAdapterFetcher(url_request_context) { |
| 161 } | 161 } |
| 162 | 162 |
| 163 class DelayingWorkerThread : public WorkerThread { | 163 class DelayingDhcpQuery : public DhcpQuery { |
| 164 public: | 164 public: |
| 165 explicit DelayingWorkerThread( | 165 explicit DelayingDhcpQuery() |
| 166 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner) | 166 : DhcpQuery() { |
| 167 : WorkerThread(owner) { | |
| 168 } | 167 } |
| 169 | 168 |
| 170 std::string ImplGetPacURLFromDhcp( | 169 std::string ImplGetPacURLFromDhcp( |
| 171 const std::string& adapter_name) OVERRIDE { | 170 const std::string& adapter_name) OVERRIDE { |
| 172 base::PlatformThread::Sleep(20); | 171 base::PlatformThread::Sleep(20); |
| 173 return WorkerThread::ImplGetPacURLFromDhcp(adapter_name); | 172 return DhcpQuery::ImplGetPacURLFromDhcp(adapter_name); |
| 174 } | 173 } |
| 175 }; | 174 }; |
| 176 | 175 |
| 177 WorkerThread* ImplCreateWorkerThread( | 176 DhcpQuery* ImplCreateDhcpQuery() OVERRIDE { |
| 178 const base::WeakPtr<DhcpProxyScriptAdapterFetcher>& owner) OVERRIDE { | 177 return new DelayingDhcpQuery(); |
| 179 return new DelayingWorkerThread(owner); | |
| 180 } | 178 } |
| 181 }; | 179 }; |
| 182 | 180 |
| 183 // For RealFetchWithDeferredCancel, below. | 181 // For RealFetchWithDeferredCancel, below. |
| 184 class DelayingDhcpProxyScriptFetcherWin | 182 class DelayingDhcpProxyScriptFetcherWin |
| 185 : public DhcpProxyScriptFetcherWin { | 183 : public DhcpProxyScriptFetcherWin { |
| 186 public: | 184 public: |
| 187 explicit DelayingDhcpProxyScriptFetcherWin( | 185 explicit DelayingDhcpProxyScriptFetcherWin( |
| 188 URLRequestContext* context) | 186 URLRequestContext* context) |
| 189 : DhcpProxyScriptFetcherWin(context) { | 187 : DhcpProxyScriptFetcherWin(context) { |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 261 bool did_finish_; | 259 bool did_finish_; |
| 262 int result_; | 260 int result_; |
| 263 string16 pac_script_; | 261 string16 pac_script_; |
| 264 int fetch_delay_ms_; | 262 int fetch_delay_ms_; |
| 265 OldCompletionCallback* client_callback_; | 263 OldCompletionCallback* client_callback_; |
| 266 base::OneShotTimer<DummyDhcpProxyScriptAdapterFetcher> timer_; | 264 base::OneShotTimer<DummyDhcpProxyScriptAdapterFetcher> timer_; |
| 267 }; | 265 }; |
| 268 | 266 |
| 269 class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { | 267 class MockDhcpProxyScriptFetcherWin : public DhcpProxyScriptFetcherWin { |
| 270 public: | 268 public: |
| 271 class MockWorkerThread : public WorkerThread { | 269 class MockAdapterQuery : public AdapterQuery { |
| 272 public: | 270 public: |
| 273 MockWorkerThread() : worker_finished_event_(true, false) { | 271 MockAdapterQuery() { |
| 274 } | 272 } |
| 275 | 273 |
| 276 virtual ~MockWorkerThread() { | 274 virtual ~MockAdapterQuery() { |
| 277 } | |
| 278 | |
| 279 void Init(const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) { | |
| 280 WorkerThread::Init(owner); | |
| 281 } | 275 } |
| 282 | 276 |
| 283 virtual bool ImplGetCandidateAdapterNames( | 277 virtual bool ImplGetCandidateAdapterNames( |
| 284 std::set<std::string>* adapter_names) OVERRIDE { | 278 std::set<std::string>* adapter_names) OVERRIDE { |
| 285 adapter_names->insert( | 279 adapter_names->insert( |
| 286 mock_adapter_names_.begin(), mock_adapter_names_.end()); | 280 mock_adapter_names_.begin(), mock_adapter_names_.end()); |
| 287 return true; | 281 return true; |
| 288 } | 282 } |
| 289 | 283 |
| 290 virtual void OnThreadDone() OVERRIDE { | |
| 291 WorkerThread::OnThreadDone(); | |
| 292 worker_finished_event_.Signal(); | |
| 293 } | |
| 294 | |
| 295 std::vector<std::string> mock_adapter_names_; | 284 std::vector<std::string> mock_adapter_names_; |
| 296 base::WaitableEvent worker_finished_event_; | |
| 297 }; | 285 }; |
| 298 | 286 |
| 299 MockDhcpProxyScriptFetcherWin() | 287 MockDhcpProxyScriptFetcherWin() |
| 300 : DhcpProxyScriptFetcherWin(new TestURLRequestContext()), | 288 : DhcpProxyScriptFetcherWin(new TestURLRequestContext()), |
| 301 num_fetchers_created_(0) { | 289 num_fetchers_created_(0), |
| 290 worker_finished_event_(true, false) { |
| 302 ResetTestState(); | 291 ResetTestState(); |
| 303 } | 292 } |
| 304 | 293 |
| 305 virtual ~MockDhcpProxyScriptFetcherWin() { | 294 virtual ~MockDhcpProxyScriptFetcherWin() { |
| 306 ResetTestState(); | 295 ResetTestState(); |
| 307 } | 296 } |
| 308 | 297 |
| 309 // Adds a fetcher object to the queue of fetchers used by | 298 // Adds a fetcher object to the queue of fetchers used by |
| 310 // |ImplCreateAdapterFetcher()|, and its name to the list of adapters | 299 // |ImplCreateAdapterFetcher()|, and its name to the list of adapters |
| 311 // returned by ImplGetCandidateAdapterNames. | 300 // returned by ImplGetCandidateAdapterNames. |
| 312 void PushBackAdapter(const std::string& adapter_name, | 301 void PushBackAdapter(const std::string& adapter_name, |
| 313 DhcpProxyScriptAdapterFetcher* fetcher) { | 302 DhcpProxyScriptAdapterFetcher* fetcher) { |
| 314 worker_thread_->mock_adapter_names_.push_back(adapter_name); | 303 adapter_query_->mock_adapter_names_.push_back(adapter_name); |
| 315 adapter_fetchers_.push_back(fetcher); | 304 adapter_fetchers_.push_back(fetcher); |
| 316 } | 305 } |
| 317 | 306 |
| 318 void ConfigureAndPushBackAdapter(const std::string& adapter_name, | 307 void ConfigureAndPushBackAdapter(const std::string& adapter_name, |
| 319 bool did_finish, | 308 bool did_finish, |
| 320 int result, | 309 int result, |
| 321 string16 pac_script, | 310 string16 pac_script, |
| 322 int fetch_delay_ms) { | 311 int fetch_delay_ms) { |
| 323 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( | 312 scoped_ptr<DummyDhcpProxyScriptAdapterFetcher> adapter_fetcher( |
| 324 new DummyDhcpProxyScriptAdapterFetcher()); | 313 new DummyDhcpProxyScriptAdapterFetcher()); |
| 325 adapter_fetcher->Configure(did_finish, result, pac_script, fetch_delay_ms); | 314 adapter_fetcher->Configure(did_finish, result, pac_script, fetch_delay_ms); |
| 326 PushBackAdapter(adapter_name, adapter_fetcher.release()); | 315 PushBackAdapter(adapter_name, adapter_fetcher.release()); |
| 327 } | 316 } |
| 328 | 317 |
| 329 DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher() OVERRIDE { | 318 DhcpProxyScriptAdapterFetcher* ImplCreateAdapterFetcher() OVERRIDE { |
| 330 ++num_fetchers_created_; | 319 ++num_fetchers_created_; |
| 331 return adapter_fetchers_[next_adapter_fetcher_index_++]; | 320 return adapter_fetchers_[next_adapter_fetcher_index_++]; |
| 332 } | 321 } |
| 333 | 322 |
| 334 virtual WorkerThread* ImplCreateWorkerThread( | 323 virtual AdapterQuery* ImplCreateAdapterQuery() OVERRIDE { |
| 335 const base::WeakPtr<DhcpProxyScriptFetcherWin>& owner) OVERRIDE { | 324 DCHECK(adapter_query_); |
| 336 DCHECK(worker_thread_); | 325 return adapter_query_.get(); |
| 337 worker_thread_->Init(owner); | |
| 338 return worker_thread_.get(); | |
| 339 } | 326 } |
| 340 | 327 |
| 341 int ImplGetMaxWaitMs() OVERRIDE { | 328 int ImplGetMaxWaitMs() OVERRIDE { |
| 342 return max_wait_ms_; | 329 return max_wait_ms_; |
| 343 } | 330 } |
| 344 | 331 |
| 332 void ImplOnGetCandidateAdapterNamesDone() OVERRIDE { |
| 333 worker_finished_event_.Signal(); |
| 334 } |
| 335 |
| 345 void ResetTestState() { | 336 void ResetTestState() { |
| 346 // Delete any adapter fetcher objects we didn't hand out. | 337 // Delete any adapter fetcher objects we didn't hand out. |
| 347 std::vector<DhcpProxyScriptAdapterFetcher*>::const_iterator it | 338 std::vector<DhcpProxyScriptAdapterFetcher*>::const_iterator it |
| 348 = adapter_fetchers_.begin(); | 339 = adapter_fetchers_.begin(); |
| 349 for (; it != adapter_fetchers_.end(); ++it) { | 340 for (; it != adapter_fetchers_.end(); ++it) { |
| 350 if (num_fetchers_created_-- <= 0) { | 341 if (num_fetchers_created_-- <= 0) { |
| 351 delete (*it); | 342 delete (*it); |
| 352 } | 343 } |
| 353 } | 344 } |
| 354 | 345 |
| 355 next_adapter_fetcher_index_ = 0; | 346 next_adapter_fetcher_index_ = 0; |
| 356 num_fetchers_created_ = 0; | 347 num_fetchers_created_ = 0; |
| 357 adapter_fetchers_.clear(); | 348 adapter_fetchers_.clear(); |
| 358 worker_thread_ = new MockWorkerThread(); | 349 adapter_query_ = new MockAdapterQuery(); |
| 359 max_wait_ms_ = TestTimeouts::tiny_timeout_ms(); | 350 max_wait_ms_ = TestTimeouts::tiny_timeout_ms(); |
| 360 } | 351 } |
| 361 | 352 |
| 362 bool HasPendingFetchers() { | 353 bool HasPendingFetchers() { |
| 363 return num_pending_fetchers() > 0; | 354 return num_pending_fetchers() > 0; |
| 364 } | 355 } |
| 365 | 356 |
| 366 int next_adapter_fetcher_index_; | 357 int next_adapter_fetcher_index_; |
| 367 | 358 |
| 368 // Ownership gets transferred to the implementation class via | 359 // Ownership gets transferred to the implementation class via |
| 369 // ImplCreateAdapterFetcher, but any objects not handed out are | 360 // ImplCreateAdapterFetcher, but any objects not handed out are |
| 370 // deleted on destruction. | 361 // deleted on destruction. |
| 371 std::vector<DhcpProxyScriptAdapterFetcher*> adapter_fetchers_; | 362 std::vector<DhcpProxyScriptAdapterFetcher*> adapter_fetchers_; |
| 372 | 363 |
| 373 scoped_refptr<MockWorkerThread> worker_thread_; | 364 scoped_refptr<MockAdapterQuery> adapter_query_; |
| 374 | 365 |
| 375 int max_wait_ms_; | 366 int max_wait_ms_; |
| 376 int num_fetchers_created_; | 367 int num_fetchers_created_; |
| 368 base::WaitableEvent worker_finished_event_; |
| 377 }; | 369 }; |
| 378 | 370 |
| 379 class FetcherClient { | 371 class FetcherClient { |
| 380 public: | 372 public: |
| 381 FetcherClient() | 373 FetcherClient() |
| 382 : finished_(false), | 374 : finished_(false), |
| 383 result_(ERR_UNEXPECTED), | 375 result_(ERR_UNEXPECTED), |
| 384 ALLOW_THIS_IN_INITIALIZER_LIST( | 376 ALLOW_THIS_IN_INITIALIZER_LIST( |
| 385 completion_callback_(this, &FetcherClient::OnCompletion)) { | 377 completion_callback_(this, &FetcherClient::OnCompletion)) { |
| 386 } | 378 } |
| 387 | 379 |
| 388 void RunTest() { | 380 void RunTest() { |
| 389 int result = fetcher_.Fetch(&pac_text_, &completion_callback_); | 381 int result = fetcher_.Fetch(&pac_text_, &completion_callback_); |
| 390 ASSERT_EQ(ERR_IO_PENDING, result); | 382 ASSERT_EQ(ERR_IO_PENDING, result); |
| 391 } | 383 } |
| 392 | 384 |
| 393 void RunMessageLoopUntilComplete() { | 385 void RunMessageLoopUntilComplete() { |
| 394 while (!finished_) { | 386 while (!finished_) { |
| 395 MessageLoop::current()->RunAllPending(); | 387 MessageLoop::current()->RunAllPending(); |
| 396 } | 388 } |
| 397 MessageLoop::current()->RunAllPending(); | 389 MessageLoop::current()->RunAllPending(); |
| 398 } | 390 } |
| 399 | 391 |
| 400 void RunMessageLoopUntilWorkerDone() { | 392 void RunMessageLoopUntilWorkerDone() { |
| 401 DCHECK(fetcher_.worker_thread_.get()); | 393 DCHECK(fetcher_.adapter_query_.get()); |
| 402 while (!fetcher_.worker_thread_->worker_finished_event_.TimedWait( | 394 while (!fetcher_.worker_finished_event_.TimedWait( |
| 403 base::TimeDelta::FromMilliseconds(10))) { | 395 base::TimeDelta::FromMilliseconds(10))) { |
| 404 MessageLoop::current()->RunAllPending(); | 396 MessageLoop::current()->RunAllPending(); |
| 405 } | 397 } |
| 406 } | 398 } |
| 407 | 399 |
| 408 void OnCompletion(int result) { | 400 void OnCompletion(int result) { |
| 409 finished_ = true; | 401 finished_ = true; |
| 410 result_ = result; | 402 result_ = result; |
| 411 } | 403 } |
| 412 | 404 |
| (...skipping 215 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 628 } | 620 } |
| 629 | 621 |
| 630 // Re-do the first test to make sure the last test that was run did | 622 // Re-do the first test to make sure the last test that was run did |
| 631 // not leave things in a bad state. | 623 // not leave things in a bad state. |
| 632 (*test_functions.begin())(&client); | 624 (*test_functions.begin())(&client); |
| 633 } | 625 } |
| 634 | 626 |
| 635 } // namespace | 627 } // namespace |
| 636 | 628 |
| 637 } // namespace net | 629 } // namespace net |
| OLD | NEW |