| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/message_loop_proxy.h" | 9 #include "base/message_loop_proxy.h" |
| 9 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 10 #include "base/sys_string_conversions.h" | 11 #include "base/sys_string_conversions.h" |
| 11 #include "base/threading/worker_pool.h" | 12 #include "base/threading/worker_pool.h" |
| 12 #include "base/time.h" | 13 #include "base/time.h" |
| 13 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
| 14 #include "net/proxy/dhcpcsvc_init_win.h" | 15 #include "net/proxy/dhcpcsvc_init_win.h" |
| 15 #include "net/proxy/proxy_script_fetcher_impl.h" | 16 #include "net/proxy/proxy_script_fetcher_impl.h" |
| 16 #include "net/url_request/url_request_context.h" | 17 #include "net/url_request/url_request_context.h" |
| 17 | 18 |
| 18 #include <windows.h> | 19 #include <windows.h> |
| 19 #include <winsock2.h> | 20 #include <winsock2.h> |
| 20 #include <dhcpcsdk.h> | 21 #include <dhcpcsdk.h> |
| 21 #pragma comment(lib, "dhcpcsvc.lib") | 22 #pragma comment(lib, "dhcpcsvc.lib") |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 // Maximum amount of time to wait for response from the Win32 DHCP API. | 26 // Maximum amount of time to wait for response from the Win32 DHCP API. |
| 26 const int kTimeoutMs = 2000; | 27 const int kTimeoutMs = 2000; |
| 27 | 28 |
| 28 } // namespace | 29 } // namespace |
| 29 | 30 |
| 30 namespace net { | 31 namespace net { |
| 31 | 32 |
| 32 DhcpProxyScriptAdapterFetcher::DhcpProxyScriptAdapterFetcher( | 33 DhcpProxyScriptAdapterFetcher::DhcpProxyScriptAdapterFetcher( |
| 33 URLRequestContext* url_request_context) | 34 URLRequestContext* url_request_context) |
| 34 : state_(STATE_START), | 35 : state_(STATE_START), |
| 35 result_(ERR_IO_PENDING), | 36 result_(ERR_IO_PENDING), |
| 36 callback_(NULL), | |
| 37 ALLOW_THIS_IN_INITIALIZER_LIST( | |
| 38 script_fetcher_callback_( | |
| 39 this, &DhcpProxyScriptAdapterFetcher::OnFetcherDone)), | |
| 40 url_request_context_(url_request_context) { | 37 url_request_context_(url_request_context) { |
| 41 } | 38 } |
| 42 | 39 |
| 43 DhcpProxyScriptAdapterFetcher::~DhcpProxyScriptAdapterFetcher() { | 40 DhcpProxyScriptAdapterFetcher::~DhcpProxyScriptAdapterFetcher() { |
| 44 Cancel(); | 41 Cancel(); |
| 45 | 42 |
| 46 // The WeakPtr we passed to the worker thread may be destroyed on the | 43 // The WeakPtr we passed to the worker thread may be destroyed on the |
| 47 // worker thread. This detaches any outstanding WeakPtr state from | 44 // worker thread. This detaches any outstanding WeakPtr state from |
| 48 // the current thread. | 45 // the current thread. |
| 49 base::SupportsWeakPtr<DhcpProxyScriptAdapterFetcher>::DetachFromThread(); | 46 base::SupportsWeakPtr<DhcpProxyScriptAdapterFetcher>::DetachFromThread(); |
| 50 } | 47 } |
| 51 | 48 |
| 52 void DhcpProxyScriptAdapterFetcher::Fetch( | 49 void DhcpProxyScriptAdapterFetcher::Fetch( |
| 53 const std::string& adapter_name, OldCompletionCallback* callback) { | 50 const std::string& adapter_name, const CompletionCallback& callback) { |
| 54 DCHECK(CalledOnValidThread()); | 51 DCHECK(CalledOnValidThread()); |
| 55 DCHECK_EQ(state_, STATE_START); | 52 DCHECK_EQ(state_, STATE_START); |
| 56 result_ = ERR_IO_PENDING; | 53 result_ = ERR_IO_PENDING; |
| 57 pac_script_ = string16(); | 54 pac_script_ = string16(); |
| 58 state_ = STATE_WAIT_DHCP; | 55 state_ = STATE_WAIT_DHCP; |
| 59 callback_ = callback; | 56 callback_ = callback; |
| 60 | 57 |
| 61 wait_timer_.Start(FROM_HERE, ImplGetTimeout(), | 58 wait_timer_.Start(FROM_HERE, ImplGetTimeout(), |
| 62 this, &DhcpProxyScriptAdapterFetcher::OnTimeout); | 59 this, &DhcpProxyScriptAdapterFetcher::OnTimeout); |
| 63 scoped_refptr<DhcpQuery> dhcp_query(ImplCreateDhcpQuery()); | 60 scoped_refptr<DhcpQuery> dhcp_query(ImplCreateDhcpQuery()); |
| 64 base::WorkerPool::PostTaskAndReply( | 61 base::WorkerPool::PostTaskAndReply( |
| 65 FROM_HERE, | 62 FROM_HERE, |
| 66 base::Bind( | 63 base::Bind( |
| 67 &DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter, | 64 &DhcpProxyScriptAdapterFetcher::DhcpQuery::GetPacURLForAdapter, |
| 68 dhcp_query.get(), | 65 dhcp_query.get(), |
| 69 adapter_name), | 66 adapter_name), |
| 70 base::Bind( | 67 base::Bind( |
| 71 &DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone, | 68 &DhcpProxyScriptAdapterFetcher::OnDhcpQueryDone, |
| 72 AsWeakPtr(), | 69 AsWeakPtr(), |
| 73 dhcp_query), | 70 dhcp_query), |
| 74 true); | 71 true); |
| 75 } | 72 } |
| 76 | 73 |
| 77 void DhcpProxyScriptAdapterFetcher::Cancel() { | 74 void DhcpProxyScriptAdapterFetcher::Cancel() { |
| 78 DCHECK(CalledOnValidThread()); | 75 DCHECK(CalledOnValidThread()); |
| 79 callback_ = NULL; | 76 callback_.Reset(); |
| 80 wait_timer_.Stop(); | 77 wait_timer_.Stop(); |
| 81 script_fetcher_.reset(); | 78 script_fetcher_.reset(); |
| 82 | 79 |
| 83 switch (state_) { | 80 switch (state_) { |
| 84 case STATE_WAIT_DHCP: | 81 case STATE_WAIT_DHCP: |
| 85 // Nothing to do here, we let the worker thread run to completion, | 82 // Nothing to do here, we let the worker thread run to completion, |
| 86 // the task it posts back when it completes will check the state. | 83 // the task it posts back when it completes will check the state. |
| 87 break; | 84 break; |
| 88 case STATE_WAIT_URL: | 85 case STATE_WAIT_URL: |
| 89 break; | 86 break; |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 154 | 151 |
| 155 wait_timer_.Stop(); | 152 wait_timer_.Stop(); |
| 156 | 153 |
| 157 pac_url_ = GURL(dhcp_query->url()); | 154 pac_url_ = GURL(dhcp_query->url()); |
| 158 if (pac_url_.is_empty() || !pac_url_.is_valid()) { | 155 if (pac_url_.is_empty() || !pac_url_.is_valid()) { |
| 159 result_ = ERR_PAC_NOT_IN_DHCP; | 156 result_ = ERR_PAC_NOT_IN_DHCP; |
| 160 TransitionToFinish(); | 157 TransitionToFinish(); |
| 161 } else { | 158 } else { |
| 162 state_ = STATE_WAIT_URL; | 159 state_ = STATE_WAIT_URL; |
| 163 script_fetcher_.reset(ImplCreateScriptFetcher()); | 160 script_fetcher_.reset(ImplCreateScriptFetcher()); |
| 164 script_fetcher_->Fetch(pac_url_, &pac_script_, &script_fetcher_callback_); | 161 script_fetcher_->Fetch( |
| 162 pac_url_, &pac_script_, |
| 163 base::Bind(&DhcpProxyScriptAdapterFetcher::OnFetcherDone, |
| 164 base::Unretained(this))); |
| 165 } | 165 } |
| 166 } | 166 } |
| 167 | 167 |
| 168 void DhcpProxyScriptAdapterFetcher::OnTimeout() { | 168 void DhcpProxyScriptAdapterFetcher::OnTimeout() { |
| 169 DCHECK_EQ(state_, STATE_WAIT_DHCP); | 169 DCHECK_EQ(state_, STATE_WAIT_DHCP); |
| 170 result_ = ERR_TIMED_OUT; | 170 result_ = ERR_TIMED_OUT; |
| 171 TransitionToFinish(); | 171 TransitionToFinish(); |
| 172 } | 172 } |
| 173 | 173 |
| 174 void DhcpProxyScriptAdapterFetcher::OnFetcherDone(int result) { | 174 void DhcpProxyScriptAdapterFetcher::OnFetcherDone(int result) { |
| 175 DCHECK(CalledOnValidThread()); | 175 DCHECK(CalledOnValidThread()); |
| 176 DCHECK(state_ == STATE_WAIT_URL || state_ == STATE_CANCEL); | 176 DCHECK(state_ == STATE_WAIT_URL || state_ == STATE_CANCEL); |
| 177 if (state_ == STATE_CANCEL) | 177 if (state_ == STATE_CANCEL) |
| 178 return; | 178 return; |
| 179 | 179 |
| 180 // At this point, pac_script_ has already been written to. | 180 // At this point, pac_script_ has already been written to. |
| 181 script_fetcher_.reset(); | 181 script_fetcher_.reset(); |
| 182 result_ = result; | 182 result_ = result; |
| 183 TransitionToFinish(); | 183 TransitionToFinish(); |
| 184 } | 184 } |
| 185 | 185 |
| 186 void DhcpProxyScriptAdapterFetcher::TransitionToFinish() { | 186 void DhcpProxyScriptAdapterFetcher::TransitionToFinish() { |
| 187 DCHECK(state_ == STATE_WAIT_DHCP || state_ == STATE_WAIT_URL); | 187 DCHECK(state_ == STATE_WAIT_DHCP || state_ == STATE_WAIT_URL); |
| 188 state_ = STATE_FINISH; | 188 state_ = STATE_FINISH; |
| 189 OldCompletionCallback* callback = callback_; | 189 CompletionCallback callback = callback_; |
| 190 callback_ = NULL; | 190 callback_.Reset(); |
| 191 | 191 |
| 192 // Be careful not to touch any member state after this, as the client | 192 // Be careful not to touch any member state after this, as the client |
| 193 // may delete us during this callback. | 193 // may delete us during this callback. |
| 194 callback->Run(result_); | 194 callback.Run(result_); |
| 195 } | 195 } |
| 196 | 196 |
| 197 DhcpProxyScriptAdapterFetcher::State | 197 DhcpProxyScriptAdapterFetcher::State |
| 198 DhcpProxyScriptAdapterFetcher::state() const { | 198 DhcpProxyScriptAdapterFetcher::state() const { |
| 199 return state_; | 199 return state_; |
| 200 } | 200 } |
| 201 | 201 |
| 202 ProxyScriptFetcher* DhcpProxyScriptAdapterFetcher::ImplCreateScriptFetcher() { | 202 ProxyScriptFetcher* DhcpProxyScriptAdapterFetcher::ImplCreateScriptFetcher() { |
| 203 return new ProxyScriptFetcherImpl(url_request_context_); | 203 return new ProxyScriptFetcherImpl(url_request_context_); |
| 204 } | 204 } |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 // back a buffer with embedded NULLs, something is broken anyway. | 282 // back a buffer with embedded NULLs, something is broken anyway. |
| 283 return std::string( | 283 return std::string( |
| 284 std::string(reinterpret_cast<const char *>(wpad_params.Data), | 284 std::string(reinterpret_cast<const char *>(wpad_params.Data), |
| 285 wpad_params.nBytesData).c_str()); | 285 wpad_params.nBytesData).c_str()); |
| 286 } | 286 } |
| 287 | 287 |
| 288 return ""; | 288 return ""; |
| 289 } | 289 } |
| 290 | 290 |
| 291 } // namespace net | 291 } // namespace net |
| OLD | NEW |