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 3 // found in the LICENSE file. 4 5 #include "chrome/browser/net/url_request_mock_util.h" 6 7 #include <string> 8 9 #include "base/path_service.h" 10 #include "base/threading/thread_restrictions.h" 11 #include "chrome/browser/google/google_util.h" 12 #include "chrome/common/chrome_paths.h" 13 #include "content/public/browser/browser_thread.h" 14 #include "content/test/net/url_request_failed_job.h" 15 #include "content/test/net/url_request_mock_http_job.h" 16 #include "content/test/net/url_request_slow_download_job.h" 17 #include "net/url_request/url_request_filter.h" 18 19 using content::BrowserThread; 20 21 namespace chrome_browser_net { 22 23 void SetUrlRequestMocksEnabled(bool enabled) { 24 // Since this involves changing the net::URLRequest ProtocolFactory, we need 25 // to run on the IO thread. 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); 27 28 if (enabled) { 29 // We have to look around for our helper files, but we only use 30 // this from tests, so allow these IO operations to happen 31 // anywhere. 32 base::ThreadRestrictions::ScopedAllowIO allow_io; 33 34 net::URLRequestFilter::GetInstance()->ClearHandlers(); 35 36 content::URLRequestFailedJob::AddUrlHandler(); 37 content::URLRequestSlowDownloadJob::AddUrlHandler(); 38 39 base::FilePath root_http; 40 PathService::Get(chrome::DIR_TEST_DATA, &root_http); 41 content::URLRequestMockHTTPJob::AddUrlHandler(root_http); 42 content::URLRequestMockHTTPJob::AddHostnameToFileHandler( 43 google_util::LinkDoctorBaseURL().host(), 44 root_http.AppendASCII("mock-link-doctor.html")); 45 } else { 46 // Revert to the default handlers. 47 net::URLRequestFilter::GetInstance()->ClearHandlers(); 48 } 49 } 50 51 } // namespace chrome_browser_net 52