| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 <set> | 5 #include <set> |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/path_service.h" | 10 #include "base/path_service.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 #include "chrome/test/base/ui_test_utils.h" | 28 #include "chrome/test/base/ui_test_utils.h" |
| 29 #include "content/public/browser/browser_thread.h" | 29 #include "content/public/browser/browser_thread.h" |
| 30 #include "content/public/browser/web_contents.h" | 30 #include "content/public/browser/web_contents.h" |
| 31 #include "content/public/test/browser_test_utils.h" | 31 #include "content/public/test/browser_test_utils.h" |
| 32 #include "content/public/test/test_navigation_observer.h" | 32 #include "content/public/test/test_navigation_observer.h" |
| 33 #include "content/test/net/url_request_failed_job.h" | 33 #include "content/test/net/url_request_failed_job.h" |
| 34 #include "content/test/net/url_request_mock_http_job.h" | 34 #include "content/test/net/url_request_mock_http_job.h" |
| 35 #include "net/base/net_errors.h" | 35 #include "net/base/net_errors.h" |
| 36 #include "net/dns/dns_test_util.h" | 36 #include "net/dns/dns_test_util.h" |
| 37 #include "net/url_request/url_request_filter.h" | 37 #include "net/url_request/url_request_filter.h" |
| 38 #include "net/url_request/url_request_interceptor.h" |
| 38 #include "net/url_request/url_request_job.h" | 39 #include "net/url_request/url_request_job.h" |
| 39 #include "net/url_request/url_request_job_factory.h" | |
| 40 | 40 |
| 41 using base::Bind; | 41 using base::Bind; |
| 42 using base::Callback; | 42 using base::Callback; |
| 43 using base::Closure; | 43 using base::Closure; |
| 44 using base::ConstRef; | 44 using base::ConstRef; |
| 45 using base::FilePath; | 45 using base::FilePath; |
| 46 using base::MessageLoop; | 46 using base::MessageLoop; |
| 47 using base::Unretained; | 47 using base::Unretained; |
| 48 using chrome_common_net::DnsProbeStatus; | 48 using chrome_common_net::DnsProbeStatus; |
| 49 using content::BrowserThread; | 49 using content::BrowserThread; |
| 50 using content::URLRequestFailedJob; | 50 using content::URLRequestFailedJob; |
| 51 using content::URLRequestMockHTTPJob; | 51 using content::URLRequestMockHTTPJob; |
| 52 using content::WebContents; | 52 using content::WebContents; |
| 53 using google_util::LinkDoctorBaseURL; | 53 using google_util::LinkDoctorBaseURL; |
| 54 using net::MockDnsClientRule; | 54 using net::MockDnsClientRule; |
| 55 using net::NetworkDelegate; | 55 using net::NetworkDelegate; |
| 56 using net::URLRequest; | 56 using net::URLRequest; |
| 57 using net::URLRequestFilter; | 57 using net::URLRequestFilter; |
| 58 using net::URLRequestInterceptor; |
| 58 using net::URLRequestJob; | 59 using net::URLRequestJob; |
| 59 using net::URLRequestJobFactory; | |
| 60 using ui_test_utils::NavigateToURL; | 60 using ui_test_utils::NavigateToURL; |
| 61 using ui_test_utils::NavigateToURLBlockUntilNavigationsComplete; | 61 using ui_test_utils::NavigateToURLBlockUntilNavigationsComplete; |
| 62 | 62 |
| 63 namespace chrome_browser_net { | 63 namespace chrome_browser_net { |
| 64 | 64 |
| 65 namespace { | 65 namespace { |
| 66 | 66 |
| 67 // Postable function to run a Closure on the UI thread. Since | 67 // Postable function to run a Closure on the UI thread. Since |
| 68 // BrowserThread::PostTask returns a bool, it can't directly be posted to | 68 // BrowserThread::PostTask returns a bool, it can't directly be posted to |
| 69 // another thread. | 69 // another thread. |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 virtual ~DelayableURLRequestMockHTTPJob() { | 208 virtual ~DelayableURLRequestMockHTTPJob() { |
| 209 if (should_delay_) | 209 if (should_delay_) |
| 210 destruction_callback_.Run(this); | 210 destruction_callback_.Run(this); |
| 211 } | 211 } |
| 212 | 212 |
| 213 bool should_delay_; | 213 bool should_delay_; |
| 214 bool start_delayed_; | 214 bool start_delayed_; |
| 215 const DestructionCallback destruction_callback_; | 215 const DestructionCallback destruction_callback_; |
| 216 }; | 216 }; |
| 217 | 217 |
| 218 // ProtocolHandler for navigation correction requests. Can cause requests to | 218 // Interceptor for navigation correction requests. Can cause requests to |
| 219 // fail with an error, and/or delay a request until a test allows to continue. | 219 // fail with an error, and/or delay a request until a test allows to continue. |
| 220 // Also can run a callback when a delayed request is cancelled. | 220 // Also can run a callback when a delayed request is cancelled. |
| 221 class BreakableCorrectionProtocolHandler | 221 class BreakableCorrectionInterceptor : public URLRequestInterceptor { |
| 222 : public URLRequestJobFactory::ProtocolHandler { | |
| 223 public: | 222 public: |
| 224 explicit BreakableCorrectionProtocolHandler( | 223 explicit BreakableCorrectionInterceptor( |
| 225 const FilePath& mock_corrections_file_path) | 224 const FilePath& mock_corrections_file_path) |
| 226 : mock_corrections_file_path_(mock_corrections_file_path), | 225 : mock_corrections_file_path_(mock_corrections_file_path), |
| 227 net_error_(net::OK), | 226 net_error_(net::OK), |
| 228 delay_requests_(false), | 227 delay_requests_(false), |
| 229 on_request_destroyed_callback_( | 228 on_request_destroyed_callback_( |
| 230 base::Bind(&BreakableCorrectionProtocolHandler::OnRequestDestroyed, | 229 base::Bind(&BreakableCorrectionInterceptor::OnRequestDestroyed, |
| 231 base::Unretained(this))) { | 230 base::Unretained(this))) { |
| 232 } | 231 } |
| 233 | 232 |
| 234 virtual ~BreakableCorrectionProtocolHandler() { | 233 virtual ~BreakableCorrectionInterceptor() { |
| 235 // All delayed requests should have been resumed or cancelled by this point. | 234 // All delayed requests should have been resumed or cancelled by this point. |
| 236 EXPECT_TRUE(delayed_requests_.empty()); | 235 EXPECT_TRUE(delayed_requests_.empty()); |
| 237 } | 236 } |
| 238 | 237 |
| 239 virtual URLRequestJob* MaybeCreateJob( | 238 virtual URLRequestJob* MaybeInterceptRequest( |
| 240 URLRequest* request, | 239 URLRequest* request, |
| 241 NetworkDelegate* network_delegate) const OVERRIDE { | 240 NetworkDelegate* network_delegate) const OVERRIDE { |
| 242 if (net_error_ != net::OK) { | 241 if (net_error_ != net::OK) { |
| 243 DelayableURLRequestFailedJob* job = | 242 DelayableURLRequestFailedJob* job = |
| 244 new DelayableURLRequestFailedJob( | 243 new DelayableURLRequestFailedJob( |
| 245 request, network_delegate, net_error_, delay_requests_, | 244 request, network_delegate, net_error_, delay_requests_, |
| 246 on_request_destroyed_callback_); | 245 on_request_destroyed_callback_); |
| 247 if (delay_requests_) | 246 if (delay_requests_) |
| 248 delayed_requests_.insert(job); | 247 delayed_requests_.insert(job); |
| 249 return job; | 248 return job; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 MockDnsClientRule::Result public_good_result); | 320 MockDnsClientRule::Result public_good_result); |
| 322 void SetCorrectionServiceNetError(int net_error); | 321 void SetCorrectionServiceNetError(int net_error); |
| 323 void SetCorrectionServiceDelayRequests(bool delay_requests); | 322 void SetCorrectionServiceDelayRequests(bool delay_requests); |
| 324 void SetRequestDestructionCallback(const base::Closure& callback); | 323 void SetRequestDestructionCallback(const base::Closure& callback); |
| 325 void StartDelayedProbes(int expected_delayed_probe_count); | 324 void StartDelayedProbes(int expected_delayed_probe_count); |
| 326 | 325 |
| 327 private: | 326 private: |
| 328 IOThread* io_thread_; | 327 IOThread* io_thread_; |
| 329 DnsProbeService* original_dns_probe_service_; | 328 DnsProbeService* original_dns_probe_service_; |
| 330 DelayingDnsProbeService* delaying_dns_probe_service_; | 329 DelayingDnsProbeService* delaying_dns_probe_service_; |
| 331 BreakableCorrectionProtocolHandler* protocol_handler_; | 330 BreakableCorrectionInterceptor* interceptor_; |
| 332 FilePath mock_corrections_file_path_; | 331 FilePath mock_corrections_file_path_; |
| 333 }; | 332 }; |
| 334 | 333 |
| 335 DnsProbeBrowserTestIOThreadHelper::DnsProbeBrowserTestIOThreadHelper() | 334 DnsProbeBrowserTestIOThreadHelper::DnsProbeBrowserTestIOThreadHelper() |
| 336 : io_thread_(NULL), | 335 : io_thread_(NULL), |
| 337 original_dns_probe_service_(NULL), | 336 original_dns_probe_service_(NULL), |
| 338 delaying_dns_probe_service_(NULL), | 337 delaying_dns_probe_service_(NULL), |
| 339 protocol_handler_(NULL), | 338 interceptor_(NULL), |
| 340 mock_corrections_file_path_(GetMockLinkDoctorFilePath()) {} | 339 mock_corrections_file_path_(GetMockLinkDoctorFilePath()) {} |
| 341 | 340 |
| 342 void DnsProbeBrowserTestIOThreadHelper::SetUpOnIOThread(IOThread* io_thread) { | 341 void DnsProbeBrowserTestIOThreadHelper::SetUpOnIOThread(IOThread* io_thread) { |
| 343 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 342 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 344 CHECK(io_thread); | 343 CHECK(io_thread); |
| 345 CHECK(!io_thread_); | 344 CHECK(!io_thread_); |
| 346 CHECK(!original_dns_probe_service_); | 345 CHECK(!original_dns_probe_service_); |
| 347 CHECK(!delaying_dns_probe_service_); | 346 CHECK(!delaying_dns_probe_service_); |
| 348 CHECK(!protocol_handler_); | 347 CHECK(!interceptor_); |
| 349 | 348 |
| 350 io_thread_ = io_thread; | 349 io_thread_ = io_thread; |
| 351 | 350 |
| 352 delaying_dns_probe_service_ = new DelayingDnsProbeService(); | 351 delaying_dns_probe_service_ = new DelayingDnsProbeService(); |
| 353 | 352 |
| 354 IOThread::Globals* globals = io_thread_->globals(); | 353 IOThread::Globals* globals = io_thread_->globals(); |
| 355 original_dns_probe_service_ = globals->dns_probe_service.release(); | 354 original_dns_probe_service_ = globals->dns_probe_service.release(); |
| 356 globals->dns_probe_service.reset(delaying_dns_probe_service_); | 355 globals->dns_probe_service.reset(delaying_dns_probe_service_); |
| 357 | 356 |
| 358 URLRequestFailedJob::AddUrlHandler(); | 357 URLRequestFailedJob::AddUrlHandler(); |
| 359 | 358 |
| 360 scoped_ptr<URLRequestJobFactory::ProtocolHandler> protocol_handler( | 359 interceptor_ = |
| 361 new BreakableCorrectionProtocolHandler(mock_corrections_file_path_)); | 360 new BreakableCorrectionInterceptor(mock_corrections_file_path_); |
| 362 protocol_handler_ = | 361 URLRequestFilter::GetInstance()->AddUrlInterceptor( |
| 363 static_cast<BreakableCorrectionProtocolHandler*>(protocol_handler.get()); | 362 LinkDoctorBaseURL(), scoped_ptr<URLRequestInterceptor>(interceptor_)); |
| 364 URLRequestFilter::GetInstance()->AddUrlProtocolHandler( | |
| 365 LinkDoctorBaseURL(), protocol_handler.Pass()); | |
| 366 } | 363 } |
| 367 | 364 |
| 368 void DnsProbeBrowserTestIOThreadHelper::CleanUpOnIOThreadAndDeleteHelper() { | 365 void DnsProbeBrowserTestIOThreadHelper::CleanUpOnIOThreadAndDeleteHelper() { |
| 369 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 366 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 370 | 367 |
| 371 URLRequestFilter::GetInstance()->ClearHandlers(); | 368 URLRequestFilter::GetInstance()->ClearHandlers(); |
| 372 | 369 |
| 373 IOThread::Globals* globals = io_thread_->globals(); | 370 IOThread::Globals* globals = io_thread_->globals(); |
| 374 scoped_ptr<DnsProbeService> delaying_dns_probe_service( | 371 scoped_ptr<DnsProbeService> delaying_dns_probe_service( |
| 375 globals->dns_probe_service.release()); | 372 globals->dns_probe_service.release()); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 389 service->SetSystemClientForTesting( | 386 service->SetSystemClientForTesting( |
| 390 CreateMockDnsClientForProbes(system_result)); | 387 CreateMockDnsClientForProbes(system_result)); |
| 391 service->SetPublicClientForTesting( | 388 service->SetPublicClientForTesting( |
| 392 CreateMockDnsClientForProbes(public_result)); | 389 CreateMockDnsClientForProbes(public_result)); |
| 393 } | 390 } |
| 394 | 391 |
| 395 void DnsProbeBrowserTestIOThreadHelper::SetCorrectionServiceNetError( | 392 void DnsProbeBrowserTestIOThreadHelper::SetCorrectionServiceNetError( |
| 396 int net_error) { | 393 int net_error) { |
| 397 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 394 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 398 | 395 |
| 399 protocol_handler_->set_net_error(net_error); | 396 interceptor_->set_net_error(net_error); |
| 400 } | 397 } |
| 401 | 398 |
| 402 void DnsProbeBrowserTestIOThreadHelper::SetCorrectionServiceDelayRequests( | 399 void DnsProbeBrowserTestIOThreadHelper::SetCorrectionServiceDelayRequests( |
| 403 bool delay_requests) { | 400 bool delay_requests) { |
| 404 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 401 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 405 | 402 |
| 406 protocol_handler_->SetDelayRequests(delay_requests); | 403 interceptor_->SetDelayRequests(delay_requests); |
| 407 } | 404 } |
| 408 | 405 |
| 409 void DnsProbeBrowserTestIOThreadHelper::SetRequestDestructionCallback( | 406 void DnsProbeBrowserTestIOThreadHelper::SetRequestDestructionCallback( |
| 410 const base::Closure& callback) { | 407 const base::Closure& callback) { |
| 411 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 408 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 412 | 409 |
| 413 protocol_handler_->SetRequestDestructionCallback(callback); | 410 interceptor_->SetRequestDestructionCallback(callback); |
| 414 } | 411 } |
| 415 | 412 |
| 416 void DnsProbeBrowserTestIOThreadHelper::StartDelayedProbes( | 413 void DnsProbeBrowserTestIOThreadHelper::StartDelayedProbes( |
| 417 int expected_delayed_probe_count) { | 414 int expected_delayed_probe_count) { |
| 418 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 415 CHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 419 | 416 |
| 420 CHECK(delaying_dns_probe_service_); | 417 CHECK(delaying_dns_probe_service_); |
| 421 | 418 |
| 422 int actual_delayed_probe_count = | 419 int actual_delayed_probe_count = |
| 423 delaying_dns_probe_service_->delayed_probe_count(); | 420 delaying_dns_probe_service_->delayed_probe_count(); |
| (...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 989 | 986 |
| 990 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_INCONCLUSIVE, | 987 EXPECT_EQ(chrome_common_net::DNS_PROBE_FINISHED_INCONCLUSIVE, |
| 991 WaitForSentStatus()); | 988 WaitForSentStatus()); |
| 992 EXPECT_EQ(0, pending_status_count()); | 989 EXPECT_EQ(0, pending_status_count()); |
| 993 ExpectDisplayingLocalErrorPage("ERR_NAME_NOT_RESOLVED"); | 990 ExpectDisplayingLocalErrorPage("ERR_NAME_NOT_RESOLVED"); |
| 994 } | 991 } |
| 995 | 992 |
| 996 } // namespace | 993 } // namespace |
| 997 | 994 |
| 998 } // namespace chrome_browser_net | 995 } // namespace chrome_browser_net |
| OLD | NEW |