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