| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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/single_threaded_proxy_resolver.h" | 5 #include "net/proxy/single_threaded_proxy_resolver.h" |
| 6 | 6 |
| 7 #include "base/thread.h" | 7 #include "base/thread.h" |
| 8 #include "net/base/load_log.h" | 8 #include "net/base/load_log.h" |
| 9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
| 10 #include "net/proxy/proxy_info.h" | 10 #include "net/proxy/proxy_info.h" |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 url_(url), | 118 url_(url), |
| 119 is_started_(false), | 119 is_started_(false), |
| 120 origin_loop_(MessageLoop::current()) { | 120 origin_loop_(MessageLoop::current()) { |
| 121 DCHECK(callback); | 121 DCHECK(callback); |
| 122 } | 122 } |
| 123 | 123 |
| 124 // Start the resolve proxy request on the worker thread. | 124 // Start the resolve proxy request on the worker thread. |
| 125 void Start() { | 125 void Start() { |
| 126 is_started_ = true; | 126 is_started_ = true; |
| 127 | 127 |
| 128 size_t load_log_bound = load_log_ ? load_log_->max_num_entries() : 0; |
| 129 |
| 128 coordinator_->thread()->message_loop()->PostTask( | 130 coordinator_->thread()->message_loop()->PostTask( |
| 129 FROM_HERE, NewRunnableMethod(this, &Job::DoQuery, | 131 FROM_HERE, NewRunnableMethod(this, &Job::DoQuery, |
| 130 coordinator_->resolver_.get())); | 132 coordinator_->resolver_.get(), |
| 133 load_log_bound)); |
| 131 } | 134 } |
| 132 | 135 |
| 133 bool is_started() const { return is_started_; } | 136 bool is_started() const { return is_started_; } |
| 134 | 137 |
| 135 void Cancel() { | 138 void Cancel() { |
| 136 // Clear these to inform QueryComplete that it should not try to | 139 // Clear these to inform QueryComplete that it should not try to |
| 137 // access them. | 140 // access them. |
| 138 coordinator_ = NULL; | 141 coordinator_ = NULL; |
| 139 callback_ = NULL; | 142 callback_ = NULL; |
| 140 results_ = NULL; | 143 results_ = NULL; |
| 141 } | 144 } |
| 142 | 145 |
| 143 // Returns true if Cancel() has been called. | 146 // Returns true if Cancel() has been called. |
| 144 bool was_cancelled() const { return callback_ == NULL; } | 147 bool was_cancelled() const { return callback_ == NULL; } |
| 145 | 148 |
| 146 private: | 149 private: |
| 147 friend class base::RefCountedThreadSafe<SingleThreadedProxyResolver::Job>; | 150 friend class base::RefCountedThreadSafe<SingleThreadedProxyResolver::Job>; |
| 148 | 151 |
| 149 ~Job() {} | 152 ~Job() {} |
| 150 | 153 |
| 151 // Runs on the worker thread. | 154 // Runs on the worker thread. |
| 152 void DoQuery(ProxyResolver* resolver) { | 155 void DoQuery(ProxyResolver* resolver, size_t load_log_bound) { |
| 153 LoadLog* worker_log = new LoadLog; | 156 LoadLog* worker_log = NULL; |
| 154 worker_log->AddRef(); // Balanced in QueryComplete. | 157 if (load_log_bound > 0) { |
| 158 worker_log = new LoadLog(load_log_bound); |
| 159 worker_log->AddRef(); // Balanced in QueryComplete. |
| 160 } |
| 155 | 161 |
| 156 int rv = resolver->GetProxyForURL(url_, &results_buf_, NULL, NULL, | 162 int rv = resolver->GetProxyForURL(url_, &results_buf_, NULL, NULL, |
| 157 worker_log); | 163 worker_log); |
| 158 DCHECK_NE(rv, ERR_IO_PENDING); | 164 DCHECK_NE(rv, ERR_IO_PENDING); |
| 159 | 165 |
| 160 origin_loop_->PostTask(FROM_HERE, | 166 origin_loop_->PostTask(FROM_HERE, |
| 161 NewRunnableMethod(this, &Job::QueryComplete, rv, worker_log)); | 167 NewRunnableMethod(this, &Job::QueryComplete, rv, worker_log)); |
| 162 } | 168 } |
| 163 | 169 |
| 164 // Runs the completion callback on the origin thread. | 170 // Runs the completion callback on the origin thread. |
| 165 void QueryComplete(int result_code, LoadLog* worker_log) { | 171 void QueryComplete(int result_code, LoadLog* worker_log) { |
| 166 // Merge the load log that was generated on the worker thread, into the | 172 // Merge the load log that was generated on the worker thread, into the |
| 167 // main log. | 173 // main log. |
| 168 if (load_log_) | 174 if (worker_log) { |
| 169 load_log_->Append(worker_log); | 175 if (load_log_) |
| 170 worker_log->Release(); | 176 load_log_->Append(worker_log); |
| 177 worker_log->Release(); |
| 178 } |
| 171 | 179 |
| 172 // The Job may have been cancelled after it was started. | 180 // The Job may have been cancelled after it was started. |
| 173 if (!was_cancelled()) { | 181 if (!was_cancelled()) { |
| 174 if (result_code >= OK) { // Note: unit-tests use values > 0. | 182 if (result_code >= OK) { // Note: unit-tests use values > 0. |
| 175 results_->Use(results_buf_); | 183 results_->Use(results_buf_); |
| 176 } | 184 } |
| 177 callback_->Run(result_code); | 185 callback_->Run(result_code); |
| 178 | 186 |
| 179 // We check for cancellation once again, in case the callback deleted | 187 // We check for cancellation once again, in case the callback deleted |
| 180 // the owning ProxyService (whose destructor will in turn cancel us). | 188 // the owning ProxyService (whose destructor will in turn cancel us). |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 ProcessPendingJobs(); | 329 ProcessPendingJobs(); |
| 322 } | 330 } |
| 323 | 331 |
| 324 void SingleThreadedProxyResolver::RemoveOutstandingSetPacScriptTask( | 332 void SingleThreadedProxyResolver::RemoveOutstandingSetPacScriptTask( |
| 325 SetPacScriptTask* task) { | 333 SetPacScriptTask* task) { |
| 326 DCHECK_EQ(outstanding_set_pac_script_task_.get(), task); | 334 DCHECK_EQ(outstanding_set_pac_script_task_.get(), task); |
| 327 outstanding_set_pac_script_task_ = NULL; | 335 outstanding_set_pac_script_task_ = NULL; |
| 328 } | 336 } |
| 329 | 337 |
| 330 } // namespace net | 338 } // namespace net |
| OLD | NEW |