| 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/dhcp_proxy_script_adapter_fetcher_win.h" | 5 #include "net/proxy/dhcp_proxy_script_adapter_fetcher_win.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/location.h" | 9 #include "base/location.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 24 matching lines...) Expand all Loading... |
| 35 URLRequestContext* url_request_context, | 35 URLRequestContext* url_request_context, |
| 36 scoped_refptr<base::TaskRunner> task_runner) | 36 scoped_refptr<base::TaskRunner> task_runner) |
| 37 : task_runner_(task_runner), | 37 : task_runner_(task_runner), |
| 38 state_(STATE_START), | 38 state_(STATE_START), |
| 39 result_(ERR_IO_PENDING), | 39 result_(ERR_IO_PENDING), |
| 40 url_request_context_(url_request_context) { | 40 url_request_context_(url_request_context) { |
| 41 DCHECK(url_request_context_); | 41 DCHECK(url_request_context_); |
| 42 } | 42 } |
| 43 | 43 |
| 44 DhcpProxyScriptAdapterFetcher::~DhcpProxyScriptAdapterFetcher() { | 44 DhcpProxyScriptAdapterFetcher::~DhcpProxyScriptAdapterFetcher() { |
| 45 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 45 Cancel(); | 46 Cancel(); |
| 46 } | 47 } |
| 47 | 48 |
| 48 void DhcpProxyScriptAdapterFetcher::Fetch( | 49 void DhcpProxyScriptAdapterFetcher::Fetch( |
| 49 const std::string& adapter_name, const CompletionCallback& callback) { | 50 const std::string& adapter_name, const CompletionCallback& callback) { |
| 50 DCHECK(CalledOnValidThread()); | 51 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 51 DCHECK_EQ(state_, STATE_START); | 52 DCHECK_EQ(state_, STATE_START); |
| 52 result_ = ERR_IO_PENDING; | 53 result_ = ERR_IO_PENDING; |
| 53 pac_script_ = base::string16(); | 54 pac_script_ = base::string16(); |
| 54 state_ = STATE_WAIT_DHCP; | 55 state_ = STATE_WAIT_DHCP; |
| 55 callback_ = callback; | 56 callback_ = callback; |
| 56 | 57 |
| 57 wait_timer_.Start(FROM_HERE, ImplGetTimeout(), | 58 wait_timer_.Start(FROM_HERE, ImplGetTimeout(), |
| 58 this, &DhcpProxyScriptAdapterFetcher::OnTimeout); | 59 this, &DhcpProxyScriptAdapterFetcher::OnTimeout); |
| 59 scoped_refptr<DhcpQuery> dhcp_query(ImplCreateDhcpQuery()); | 60 scoped_refptr<DhcpQuery> dhcp_query(ImplCreateDhcpQuery()); |
| 60 task_runner_->PostTaskAndReply( | 61 task_runner_->PostTaskAndReply( |
| 61 FROM_HERE, | 62 FROM_HERE, |
| 62 base::Bind( | 63 base::Bind( |
| 63 &DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter, | 64 &DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter, |
| 64 dhcp_query.get(), | 65 dhcp_query.get(), |
| 65 adapter_name), | 66 adapter_name), |
| 66 base::Bind( | 67 base::Bind( |
| 67 &DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone, | 68 &DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone, |
| 68 AsWeakPtr(), | 69 AsWeakPtr(), |
| 69 dhcp_query)); | 70 dhcp_query)); |
| 70 } | 71 } |
| 71 | 72 |
| 72 void DhcpProxyScriptAdapterFetcher::Cancel() { | 73 void DhcpProxyScriptAdapterFetcher::Cancel() { |
| 73 DCHECK(CalledOnValidThread()); | 74 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 74 callback_.Reset(); | 75 callback_.Reset(); |
| 75 wait_timer_.Stop(); | 76 wait_timer_.Stop(); |
| 76 script_fetcher_.reset(); | 77 script_fetcher_.reset(); |
| 77 | 78 |
| 78 switch (state_) { | 79 switch (state_) { |
| 79 case STATE_WAIT_DHCP: | 80 case STATE_WAIT_DHCP: |
| 80 // Nothing to do here, we let the worker thread run to completion, | 81 // Nothing to do here, we let the worker thread run to completion, |
| 81 // the task it posts back when it completes will check the state. | 82 // the task it posts back when it completes will check the state. |
| 82 break; | 83 break; |
| 83 case STATE_WAIT_URL: | 84 case STATE_WAIT_URL: |
| 84 break; | 85 break; |
| 85 case STATE_START: | 86 case STATE_START: |
| 86 case STATE_FINISH: | 87 case STATE_FINISH: |
| 87 case STATE_CANCEL: | 88 case STATE_CANCEL: |
| 88 break; | 89 break; |
| 89 } | 90 } |
| 90 | 91 |
| 91 if (state_ != STATE_FINISH) { | 92 if (state_ != STATE_FINISH) { |
| 92 result_ = ERR_ABORTED; | 93 result_ = ERR_ABORTED; |
| 93 state_ = STATE_CANCEL; | 94 state_ = STATE_CANCEL; |
| 94 } | 95 } |
| 95 } | 96 } |
| 96 | 97 |
| 97 bool DhcpProxyScriptAdapterFetcher::DidFinish() const { | 98 bool DhcpProxyScriptAdapterFetcher::DidFinish() const { |
| 98 DCHECK(CalledOnValidThread()); | 99 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 99 return state_ == STATE_FINISH; | 100 return state_ == STATE_FINISH; |
| 100 } | 101 } |
| 101 | 102 |
| 102 int DhcpProxyScriptAdapterFetcher::GetResult() const { | 103 int DhcpProxyScriptAdapterFetcher::GetResult() const { |
| 103 DCHECK(CalledOnValidThread()); | 104 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 104 return result_; | 105 return result_; |
| 105 } | 106 } |
| 106 | 107 |
| 107 base::string16 DhcpProxyScriptAdapterFetcher::GetPacScript() const { | 108 base::string16 DhcpProxyScriptAdapterFetcher::GetPacScript() const { |
| 108 DCHECK(CalledOnValidThread()); | 109 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 109 return pac_script_; | 110 return pac_script_; |
| 110 } | 111 } |
| 111 | 112 |
| 112 GURL DhcpProxyScriptAdapterFetcher::GetPacURL() const { | 113 GURL DhcpProxyScriptAdapterFetcher::GetPacURL() const { |
| 113 DCHECK(CalledOnValidThread()); | 114 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 114 return pac_url_; | 115 return pac_url_; |
| 115 } | 116 } |
| 116 | 117 |
| 117 DhcpProxyScriptAdapterFetcher::DhcpQuery::DhcpQuery() { | 118 DhcpProxyScriptAdapterFetcher::DhcpQuery::DhcpQuery() { |
| 118 } | 119 } |
| 119 | 120 |
| 120 void DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter( | 121 void DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter( |
| 121 const std::string& adapter_name) { | 122 const std::string& adapter_name) { |
| 122 url_ = ImplGetPacURLFromDhcp(adapter_name); | 123 url_ = ImplGetPacURLFromDhcp(adapter_name); |
| 123 } | 124 } |
| 124 | 125 |
| 125 const std::string& DhcpProxyScriptAdapterFetcher::DhcpQuery::url() const { | 126 const std::string& DhcpProxyScriptAdapterFetcher::DhcpQuery::url() const { |
| 126 return url_; | 127 return url_; |
| 127 } | 128 } |
| 128 | 129 |
| 129 std::string | 130 std::string |
| 130 DhcpProxyScriptAdapterFetcher::DhcpQuery::ImplGetPacURLFromDhcp( | 131 DhcpProxyScriptAdapterFetcher::DhcpQuery::ImplGetPacURLFromDhcp( |
| 131 const std::string& adapter_name) { | 132 const std::string& adapter_name) { |
| 132 return DhcpProxyScriptAdapterFetcher::GetPacURLFromDhcp(adapter_name); | 133 return DhcpProxyScriptAdapterFetcher::GetPacURLFromDhcp(adapter_name); |
| 133 } | 134 } |
| 134 | 135 |
| 135 DhcpProxyScriptAdapterFetcher::DhcpQuery::~DhcpQuery() { | 136 DhcpProxyScriptAdapterFetcher::DhcpQuery::~DhcpQuery() { |
| 136 } | 137 } |
| 137 | 138 |
| 138 void DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone( | 139 void DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone( |
| 139 scoped_refptr<DhcpQuery> dhcp_query) { | 140 scoped_refptr<DhcpQuery> dhcp_query) { |
| 140 DCHECK(CalledOnValidThread()); | 141 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 141 // Because we can't cancel the call to the Win32 API, we can expect | 142 // Because we can't cancel the call to the Win32 API, we can expect |
| 142 // it to finish while we are in a few different states. The expected | 143 // it to finish while we are in a few different states. The expected |
| 143 // one is WAIT_DHCP, but it could be in CANCEL if Cancel() was called, | 144 // one is WAIT_DHCP, but it could be in CANCEL if Cancel() was called, |
| 144 // or FINISH if timeout occurred. | 145 // or FINISH if timeout occurred. |
| 145 DCHECK(state_ == STATE_WAIT_DHCP || state_ == STATE_CANCEL || | 146 DCHECK(state_ == STATE_WAIT_DHCP || state_ == STATE_CANCEL || |
| 146 state_ == STATE_FINISH); | 147 state_ == STATE_FINISH); |
| 147 if (state_ != STATE_WAIT_DHCP) | 148 if (state_ != STATE_WAIT_DHCP) |
| 148 return; | 149 return; |
| 149 | 150 |
| 150 wait_timer_.Stop(); | 151 wait_timer_.Stop(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 163 } | 164 } |
| 164 } | 165 } |
| 165 | 166 |
| 166 void DhcpProxyScriptAdapterFetcher::OnTimeout() { | 167 void DhcpProxyScriptAdapterFetcher::OnTimeout() { |
| 167 DCHECK_EQ(state_, STATE_WAIT_DHCP); | 168 DCHECK_EQ(state_, STATE_WAIT_DHCP); |
| 168 result_ = ERR_TIMED_OUT; | 169 result_ = ERR_TIMED_OUT; |
| 169 TransitionToFinish(); | 170 TransitionToFinish(); |
| 170 } | 171 } |
| 171 | 172 |
| 172 void DhcpProxyScriptAdapterFetcher::OnFetcherDone(int result) { | 173 void DhcpProxyScriptAdapterFetcher::OnFetcherDone(int result) { |
| 173 DCHECK(CalledOnValidThread()); | 174 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_); |
| 174 DCHECK(state_ == STATE_WAIT_URL || state_ == STATE_CANCEL); | 175 DCHECK(state_ == STATE_WAIT_URL || state_ == STATE_CANCEL); |
| 175 if (state_ == STATE_CANCEL) | 176 if (state_ == STATE_CANCEL) |
| 176 return; | 177 return; |
| 177 | 178 |
| 178 // At this point, pac_script_ has already been written to. | 179 // At this point, pac_script_ has already been written to. |
| 179 script_fetcher_.reset(); | 180 script_fetcher_.reset(); |
| 180 result_ = result; | 181 result_ = result; |
| 181 TransitionToFinish(); | 182 TransitionToFinish(); |
| 182 } | 183 } |
| 183 | 184 |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 283 // first null in case of embedded NULLs; this is the outer | 284 // first null in case of embedded NULLs; this is the outer |
| 284 // constructor that takes the result of c_str() on the inner. If | 285 // constructor that takes the result of c_str() on the inner. If |
| 285 // the server is giving us back a buffer with embedded NULLs, | 286 // the server is giving us back a buffer with embedded NULLs, |
| 286 // something is broken anyway. Finally, trim trailing whitespace. | 287 // something is broken anyway. Finally, trim trailing whitespace. |
| 287 std::string result(std::string(data, count_bytes).c_str()); | 288 std::string result(std::string(data, count_bytes).c_str()); |
| 288 base::TrimWhitespaceASCII(result, base::TRIM_TRAILING, &result); | 289 base::TrimWhitespaceASCII(result, base::TRIM_TRAILING, &result); |
| 289 return result; | 290 return result; |
| 290 } | 291 } |
| 291 | 292 |
| 292 } // namespace net | 293 } // namespace net |
| OLD | NEW |