1 // Copyright 2012 The Chromium Authors 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 "net/proxy_resolution/mock_pac_file_fetcher.h" 6 7 #include <string> 8 #include <utility> 9 10 #include "base/check.h" 11 #include "base/run_loop.h" 12 #include "base/strings/utf_string_conversions.h" 13 #include "net/base/net_errors.h" 14 15 namespace net { 16 17 MockPacFileFetcher::MockPacFileFetcher() = default; 18 19 MockPacFileFetcher::~MockPacFileFetcher() = default; 20 21 // PacFileFetcher implementation. Fetch(const GURL & url,std::u16string * text,CompletionOnceCallback callback,const NetworkTrafficAnnotationTag traffic_annotation)22int MockPacFileFetcher::Fetch( 23 const GURL& url, 24 std::u16string* text, 25 CompletionOnceCallback callback, 26 const NetworkTrafficAnnotationTag traffic_annotation) { 27 DCHECK(!has_pending_request()); 28 29 if (on_fetch_complete_) 30 std::move(on_fetch_complete_).Run(); 31 32 if (is_shutdown_) 33 return ERR_CONTEXT_SHUT_DOWN; 34 35 // Save the caller's information, and have them wait. 36 pending_request_url_ = url; 37 pending_request_callback_ = std::move(callback); 38 pending_request_text_ = text; 39 40 return ERR_IO_PENDING; 41 } 42 NotifyFetchCompletion(int result,const std::string & ascii_text)43void MockPacFileFetcher::NotifyFetchCompletion(int result, 44 const std::string& ascii_text) { 45 DCHECK(has_pending_request()); 46 *pending_request_text_ = base::ASCIIToUTF16(ascii_text); 47 std::move(pending_request_callback_).Run(result); 48 } 49 Cancel()50void MockPacFileFetcher::Cancel() { 51 pending_request_callback_.Reset(); 52 } 53 OnShutdown()54void MockPacFileFetcher::OnShutdown() { 55 is_shutdown_ = true; 56 if (pending_request_callback_) { 57 std::move(pending_request_callback_).Run(ERR_CONTEXT_SHUT_DOWN); 58 } 59 } 60 GetRequestContext() const61URLRequestContext* MockPacFileFetcher::GetRequestContext() const { 62 return nullptr; 63 } 64 pending_request_url() const65const GURL& MockPacFileFetcher::pending_request_url() const { 66 return pending_request_url_; 67 } 68 has_pending_request() const69bool MockPacFileFetcher::has_pending_request() const { 70 return !pending_request_callback_.is_null(); 71 } 72 WaitUntilFetch()73void MockPacFileFetcher::WaitUntilFetch() { 74 DCHECK(!has_pending_request()); 75 base::RunLoop run_loop; 76 on_fetch_complete_ = run_loop.QuitClosure(); 77 run_loop.Run(); 78 } 79 80 } // namespace net 81