| 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/weak_ptr.h" |
| 8 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 9 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 10 #include "base/time.h" | 11 #include "base/time.h" |
| 11 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 13 #include "net/base/net_log.h" | 14 #include "net/base/net_log.h" |
| 14 #include "net/base/net_log_unittest.h" | 15 #include "net/base/net_log_unittest.h" |
| 15 #include "net/base/test_completion_callback.h" | 16 #include "net/base/test_completion_callback.h" |
| 16 #include "net/proxy/dhcp_proxy_script_fetcher.h" | 17 #include "net/proxy/dhcp_proxy_script_fetcher.h" |
| 17 #include "net/proxy/proxy_config.h" | 18 #include "net/proxy/proxy_config.h" |
| (...skipping 507 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 525 // it failed downloading, not that it failed parsing. | 526 // it failed downloading, not that it failed parsing. |
| 526 EXPECT_EQ(kFailedDownloading, | 527 EXPECT_EQ(kFailedDownloading, |
| 527 decider.Start(config, base::TimeDelta(), true, callback.callback())); | 528 decider.Start(config, base::TimeDelta(), true, callback.callback())); |
| 528 EXPECT_EQ(NULL, decider.script_data()); | 529 EXPECT_EQ(NULL, decider.script_data()); |
| 529 | 530 |
| 530 EXPECT_FALSE(decider.effective_config().has_pac_url()); | 531 EXPECT_FALSE(decider.effective_config().has_pac_url()); |
| 531 } | 532 } |
| 532 | 533 |
| 533 class AsyncFailDhcpFetcher | 534 class AsyncFailDhcpFetcher |
| 534 : public DhcpProxyScriptFetcher, | 535 : public DhcpProxyScriptFetcher, |
| 535 public base::RefCountedThreadSafe<AsyncFailDhcpFetcher> { | 536 public base::SupportsWeakPtr<AsyncFailDhcpFetcher> { |
| 536 public: | 537 public: |
| 537 AsyncFailDhcpFetcher() {} | 538 AsyncFailDhcpFetcher() {} |
| 539 ~AsyncFailDhcpFetcher() {} |
| 538 | 540 |
| 539 int Fetch(string16* utf16_text, const CompletionCallback& callback) OVERRIDE { | 541 int Fetch(string16* utf16_text, const CompletionCallback& callback) OVERRIDE { |
| 540 callback_ = callback; | 542 callback_ = callback; |
| 541 MessageLoop::current()->PostTask( | 543 MessageLoop::current()->PostTask( |
| 542 FROM_HERE, | 544 FROM_HERE, |
| 543 base::Bind(&AsyncFailDhcpFetcher::CallbackWithFailure, this)); | 545 base::Bind(&AsyncFailDhcpFetcher::CallbackWithFailure, AsWeakPtr())); |
| 544 return ERR_IO_PENDING; | 546 return ERR_IO_PENDING; |
| 545 } | 547 } |
| 546 | 548 |
| 547 void Cancel() OVERRIDE { | 549 void Cancel() OVERRIDE { |
| 548 callback_.Reset(); | 550 callback_.Reset(); |
| 549 } | 551 } |
| 550 | 552 |
| 551 const GURL& GetPacURL() const OVERRIDE { | 553 const GURL& GetPacURL() const OVERRIDE { |
| 552 return dummy_gurl_; | 554 return dummy_gurl_; |
| 553 } | 555 } |
| 554 | 556 |
| 555 void CallbackWithFailure() { | 557 void CallbackWithFailure() { |
| 556 if (!callback_.is_null()) | 558 if (!callback_.is_null()) |
| 557 callback_.Run(ERR_PAC_NOT_IN_DHCP); | 559 callback_.Run(ERR_PAC_NOT_IN_DHCP); |
| 558 } | 560 } |
| 559 | 561 |
| 560 private: | 562 private: |
| 561 friend class base::RefCountedThreadSafe<AsyncFailDhcpFetcher>; | |
| 562 ~AsyncFailDhcpFetcher() {} | |
| 563 | |
| 564 GURL dummy_gurl_; | 563 GURL dummy_gurl_; |
| 565 CompletionCallback callback_; | 564 CompletionCallback callback_; |
| 566 }; | 565 }; |
| 567 | 566 |
| 568 TEST(ProxyScriptDeciderTest, DhcpCancelledByDestructor) { | 567 TEST(ProxyScriptDeciderTest, DhcpCancelledByDestructor) { |
| 569 // This regression test would crash before | 568 // This regression test would crash before |
| 570 // http://codereview.chromium.org/7044058/ | 569 // http://codereview.chromium.org/7044058/ |
| 571 // Thus, we don't care much about actual results (hence no EXPECT or ASSERT | 570 // Thus, we don't care much about actual results (hence no EXPECT or ASSERT |
| 572 // macros below), just that it doesn't crash. | 571 // macros below), just that it doesn't crash. |
| 573 Rules rules; | 572 Rules rules; |
| 574 RuleBasedProxyScriptFetcher fetcher(&rules); | 573 RuleBasedProxyScriptFetcher fetcher(&rules); |
| 575 | 574 |
| 576 scoped_refptr<AsyncFailDhcpFetcher> dhcp_fetcher(new AsyncFailDhcpFetcher()); | 575 scoped_ptr<AsyncFailDhcpFetcher> dhcp_fetcher(new AsyncFailDhcpFetcher()); |
| 577 | 576 |
| 578 ProxyConfig config; | 577 ProxyConfig config; |
| 579 config.set_auto_detect(true); | 578 config.set_auto_detect(true); |
| 580 rules.AddFailDownloadRule("http://wpad/wpad.dat"); | 579 rules.AddFailDownloadRule("http://wpad/wpad.dat"); |
| 581 | 580 |
| 582 TestCompletionCallback callback; | 581 TestCompletionCallback callback; |
| 583 | 582 |
| 584 // Scope so ProxyScriptDecider gets destroyed early. | 583 // Scope so ProxyScriptDecider gets destroyed early. |
| 585 { | 584 { |
| 586 ProxyScriptDecider decider(&fetcher, dhcp_fetcher.get(), NULL); | 585 ProxyScriptDecider decider(&fetcher, dhcp_fetcher.get(), NULL); |
| 587 decider.Start(config, base::TimeDelta(), true, callback.callback()); | 586 decider.Start(config, base::TimeDelta(), true, callback.callback()); |
| 588 } | 587 } |
| 589 | 588 |
| 590 // Run the message loop to let the DHCP fetch complete and post the results | 589 // Run the message loop to let the DHCP fetch complete and post the results |
| 591 // back. Before the fix linked to above, this would try to invoke on | 590 // back. Before the fix linked to above, this would try to invoke on |
| 592 // the callback object provided by ProxyScriptDecider after it was | 591 // the callback object provided by ProxyScriptDecider after it was |
| 593 // no longer valid. | 592 // no longer valid. |
| 594 MessageLoop::current()->RunAllPending(); | 593 MessageLoop::current()->RunAllPending(); |
| 595 } | 594 } |
| 596 | 595 |
| 597 } // namespace | 596 } // namespace |
| 598 } // namespace net | 597 } // namespace net |
| OLD | NEW |