| 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 "net/proxy/mock_proxy_script_fetcher.h" | 5 #include "net/proxy/mock_proxy_script_fetcher.h" |
| 6 | 6 |
| 7 #include "base/callback_helpers.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 9 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| 10 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
| 12 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 13 | 14 |
| 14 namespace net { | 15 namespace net { |
| 15 | 16 |
| 16 MockProxyScriptFetcher::MockProxyScriptFetcher() | 17 MockProxyScriptFetcher::MockProxyScriptFetcher() |
| 17 : pending_request_text_(NULL), | 18 : pending_request_text_(NULL), |
| 18 waiting_for_fetch_(false) { | 19 waiting_for_fetch_(false), |
| 19 } | 20 is_shutdown_(false) {} |
| 20 | 21 |
| 21 MockProxyScriptFetcher::~MockProxyScriptFetcher() {} | 22 MockProxyScriptFetcher::~MockProxyScriptFetcher() {} |
| 22 | 23 |
| 23 // ProxyScriptFetcher implementation. | 24 // ProxyScriptFetcher implementation. |
| 24 int MockProxyScriptFetcher::Fetch(const GURL& url, base::string16* text, | 25 int MockProxyScriptFetcher::Fetch(const GURL& url, base::string16* text, |
| 25 const CompletionCallback& callback) { | 26 const CompletionCallback& callback) { |
| 26 DCHECK(!has_pending_request()); | 27 DCHECK(!has_pending_request()); |
| 27 | 28 |
| 29 if (waiting_for_fetch_) |
| 30 base::MessageLoop::current()->QuitWhenIdle(); |
| 31 |
| 32 if (is_shutdown_) |
| 33 return ERR_CONTEXT_SHUT_DOWN; |
| 34 |
| 28 // Save the caller's information, and have them wait. | 35 // Save the caller's information, and have them wait. |
| 29 pending_request_url_ = url; | 36 pending_request_url_ = url; |
| 30 pending_request_callback_ = callback; | 37 pending_request_callback_ = callback; |
| 31 pending_request_text_ = text; | 38 pending_request_text_ = text; |
| 32 | 39 |
| 33 if (waiting_for_fetch_) | |
| 34 base::MessageLoop::current()->QuitWhenIdle(); | |
| 35 | |
| 36 return ERR_IO_PENDING; | 40 return ERR_IO_PENDING; |
| 37 } | 41 } |
| 38 | 42 |
| 39 void MockProxyScriptFetcher::NotifyFetchCompletion( | 43 void MockProxyScriptFetcher::NotifyFetchCompletion( |
| 40 int result, const std::string& ascii_text) { | 44 int result, const std::string& ascii_text) { |
| 41 DCHECK(has_pending_request()); | 45 DCHECK(has_pending_request()); |
| 42 *pending_request_text_ = base::ASCIIToUTF16(ascii_text); | 46 *pending_request_text_ = base::ASCIIToUTF16(ascii_text); |
| 43 CompletionCallback callback = pending_request_callback_; | 47 CompletionCallback callback = pending_request_callback_; |
| 44 pending_request_callback_.Reset(); | 48 pending_request_callback_.Reset(); |
| 45 callback.Run(result); | 49 callback.Run(result); |
| 46 } | 50 } |
| 47 | 51 |
| 48 void MockProxyScriptFetcher::Cancel() { | 52 void MockProxyScriptFetcher::Cancel() { |
| 53 pending_request_callback_.Reset(); |
| 54 } |
| 55 |
| 56 void MockProxyScriptFetcher::OnShutdown() { |
| 57 is_shutdown_ = true; |
| 58 if (pending_request_callback_) { |
| 59 base::ResetAndReturn(&pending_request_callback_).Run(ERR_CONTEXT_SHUT_DOWN); |
| 60 } |
| 49 } | 61 } |
| 50 | 62 |
| 51 URLRequestContext* MockProxyScriptFetcher::GetRequestContext() const { | 63 URLRequestContext* MockProxyScriptFetcher::GetRequestContext() const { |
| 52 return NULL; | 64 return NULL; |
| 53 } | 65 } |
| 54 | 66 |
| 55 const GURL& MockProxyScriptFetcher::pending_request_url() const { | 67 const GURL& MockProxyScriptFetcher::pending_request_url() const { |
| 56 return pending_request_url_; | 68 return pending_request_url_; |
| 57 } | 69 } |
| 58 | 70 |
| 59 bool MockProxyScriptFetcher::has_pending_request() const { | 71 bool MockProxyScriptFetcher::has_pending_request() const { |
| 60 return !pending_request_callback_.is_null(); | 72 return !pending_request_callback_.is_null(); |
| 61 } | 73 } |
| 62 | 74 |
| 63 void MockProxyScriptFetcher::WaitUntilFetch() { | 75 void MockProxyScriptFetcher::WaitUntilFetch() { |
| 64 DCHECK(!has_pending_request()); | 76 DCHECK(!has_pending_request()); |
| 65 waiting_for_fetch_ = true; | 77 waiting_for_fetch_ = true; |
| 66 base::RunLoop().Run(); | 78 base::RunLoop().Run(); |
| 67 waiting_for_fetch_ = false; | 79 waiting_for_fetch_ = false; |
| 68 } | 80 } |
| 69 | 81 |
| 70 } // namespace net | 82 } // namespace net |
| OLD | NEW |