| 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/init_proxy_resolver.h" | 5 #include "net/proxy/init_proxy_resolver.h" |
| 6 | 6 |
| 7 #include "base/compiler_specific.h" | 7 #include "base/compiler_specific.h" |
| 8 #include "base/format_macros.h" | 8 #include "base/format_macros.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "net/base/load_log.h" | 11 #include "net/base/net_log.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/proxy/proxy_config.h" | 13 #include "net/proxy/proxy_config.h" |
| 14 #include "net/proxy/proxy_resolver.h" | 14 #include "net/proxy/proxy_resolver.h" |
| 15 #include "net/proxy/proxy_script_fetcher.h" | 15 #include "net/proxy/proxy_script_fetcher.h" |
| 16 | 16 |
| 17 namespace net { | 17 namespace net { |
| 18 | 18 |
| 19 InitProxyResolver::InitProxyResolver(ProxyResolver* resolver, | 19 InitProxyResolver::InitProxyResolver(ProxyResolver* resolver, |
| 20 ProxyScriptFetcher* proxy_script_fetcher) | 20 ProxyScriptFetcher* proxy_script_fetcher) |
| 21 : resolver_(resolver), | 21 : resolver_(resolver), |
| 22 proxy_script_fetcher_(proxy_script_fetcher), | 22 proxy_script_fetcher_(proxy_script_fetcher), |
| 23 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( | 23 ALLOW_THIS_IN_INITIALIZER_LIST(io_callback_( |
| 24 this, &InitProxyResolver::OnIOCompletion)), | 24 this, &InitProxyResolver::OnIOCompletion)), |
| 25 user_callback_(NULL), | 25 user_callback_(NULL), |
| 26 current_pac_url_index_(0u), | 26 current_pac_url_index_(0u), |
| 27 next_state_(STATE_NONE) { | 27 next_state_(STATE_NONE) { |
| 28 } | 28 } |
| 29 | 29 |
| 30 InitProxyResolver::~InitProxyResolver() { | 30 InitProxyResolver::~InitProxyResolver() { |
| 31 if (next_state_ != STATE_NONE) | 31 if (next_state_ != STATE_NONE) |
| 32 Cancel(); | 32 Cancel(); |
| 33 } | 33 } |
| 34 | 34 |
| 35 int InitProxyResolver::Init(const ProxyConfig& config, | 35 int InitProxyResolver::Init(const ProxyConfig& config, |
| 36 CompletionCallback* callback, | 36 CompletionCallback* callback, |
| 37 LoadLog* load_log) { | 37 const BoundNetLog& net_log) { |
| 38 DCHECK_EQ(STATE_NONE, next_state_); | 38 DCHECK_EQ(STATE_NONE, next_state_); |
| 39 DCHECK(callback); | 39 DCHECK(callback); |
| 40 DCHECK(config.MayRequirePACResolver()); | 40 DCHECK(config.MayRequirePACResolver()); |
| 41 DCHECK(!load_log_); | 41 DCHECK(!net_log_.net_log()); |
| 42 | 42 |
| 43 load_log_ = load_log; | 43 net_log_ = net_log; |
| 44 | 44 |
| 45 LoadLog::BeginEvent(load_log_, LoadLog::TYPE_INIT_PROXY_RESOLVER); | 45 net_log_.BeginEvent(NetLog::TYPE_INIT_PROXY_RESOLVER); |
| 46 | 46 |
| 47 pac_urls_ = BuildPacUrlsFallbackList(config); | 47 pac_urls_ = BuildPacUrlsFallbackList(config); |
| 48 DCHECK(!pac_urls_.empty()); | 48 DCHECK(!pac_urls_.empty()); |
| 49 | 49 |
| 50 next_state_ = GetStartState(); | 50 next_state_ = GetStartState(); |
| 51 | 51 |
| 52 int rv = DoLoop(OK); | 52 int rv = DoLoop(OK); |
| 53 if (rv == ERR_IO_PENDING) | 53 if (rv == ERR_IO_PENDING) |
| 54 user_callback_ = callback; | 54 user_callback_ = callback; |
| 55 else | 55 else |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 | 115 |
| 116 void InitProxyResolver::DoCallback(int result) { | 116 void InitProxyResolver::DoCallback(int result) { |
| 117 DCHECK_NE(ERR_IO_PENDING, result); | 117 DCHECK_NE(ERR_IO_PENDING, result); |
| 118 DCHECK(user_callback_); | 118 DCHECK(user_callback_); |
| 119 user_callback_->Run(result); | 119 user_callback_->Run(result); |
| 120 } | 120 } |
| 121 | 121 |
| 122 int InitProxyResolver::DoFetchPacScript() { | 122 int InitProxyResolver::DoFetchPacScript() { |
| 123 DCHECK(resolver_->expects_pac_bytes()); | 123 DCHECK(resolver_->expects_pac_bytes()); |
| 124 | 124 |
| 125 LoadLog::BeginEvent(load_log_, | 125 net_log_.BeginEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT); |
| 126 LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT); | |
| 127 | 126 |
| 128 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; | 127 next_state_ = STATE_FETCH_PAC_SCRIPT_COMPLETE; |
| 129 | 128 |
| 130 const GURL& pac_url = current_pac_url(); | 129 const GURL& pac_url = current_pac_url(); |
| 131 | 130 |
| 132 LoadLog::AddString(load_log_, pac_url.spec()); | 131 net_log_.AddString(pac_url.spec()); |
| 133 | 132 |
| 134 if (!proxy_script_fetcher_) { | 133 if (!proxy_script_fetcher_) { |
| 135 LoadLog::AddStringLiteral(load_log_, | 134 net_log_.AddStringLiteral( |
| 136 "Can't download PAC script, because no fetcher was specified"); | 135 "Can't download PAC script, because no fetcher was specified"); |
| 137 return ERR_UNEXPECTED; | 136 return ERR_UNEXPECTED; |
| 138 } | 137 } |
| 139 | 138 |
| 140 return proxy_script_fetcher_->Fetch(pac_url, &pac_bytes_, &io_callback_); | 139 return proxy_script_fetcher_->Fetch(pac_url, &pac_bytes_, &io_callback_); |
| 141 } | 140 } |
| 142 | 141 |
| 143 int InitProxyResolver::DoFetchPacScriptComplete(int result) { | 142 int InitProxyResolver::DoFetchPacScriptComplete(int result) { |
| 144 DCHECK(resolver_->expects_pac_bytes()); | 143 DCHECK(resolver_->expects_pac_bytes()); |
| 145 | 144 |
| 146 LoadLog::AddString(load_log_, | 145 net_log_.AddString(StringPrintf( |
| 147 StringPrintf( | |
| 148 "Completed fetch with result %s. Received %" PRIuS " bytes", | 146 "Completed fetch with result %s. Received %" PRIuS " bytes", |
| 149 ErrorToString(result), | 147 ErrorToString(result), |
| 150 pac_bytes_.size())); | 148 pac_bytes_.size())); |
| 151 | 149 |
| 152 LoadLog::EndEvent(load_log_, | 150 net_log_.EndEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT); |
| 153 LoadLog::TYPE_INIT_PROXY_RESOLVER_FETCH_PAC_SCRIPT); | |
| 154 | 151 |
| 155 if (result != OK) | 152 if (result != OK) |
| 156 return TryToFallbackPacUrl(result); | 153 return TryToFallbackPacUrl(result); |
| 157 | 154 |
| 158 next_state_ = STATE_SET_PAC_SCRIPT; | 155 next_state_ = STATE_SET_PAC_SCRIPT; |
| 159 return result; | 156 return result; |
| 160 } | 157 } |
| 161 | 158 |
| 162 int InitProxyResolver::DoSetPacScript() { | 159 int InitProxyResolver::DoSetPacScript() { |
| 163 LoadLog::BeginEvent(load_log_, | 160 net_log_.BeginEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT); |
| 164 LoadLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT); | |
| 165 | 161 |
| 166 const GURL& pac_url = current_pac_url(); | 162 const GURL& pac_url = current_pac_url(); |
| 167 | 163 |
| 168 next_state_ = STATE_SET_PAC_SCRIPT_COMPLETE; | 164 next_state_ = STATE_SET_PAC_SCRIPT_COMPLETE; |
| 169 | 165 |
| 170 return resolver_->expects_pac_bytes() ? | 166 return resolver_->expects_pac_bytes() ? |
| 171 resolver_->SetPacScriptByData(pac_bytes_, &io_callback_) : | 167 resolver_->SetPacScriptByData(pac_bytes_, &io_callback_) : |
| 172 resolver_->SetPacScriptByUrl(pac_url, &io_callback_); | 168 resolver_->SetPacScriptByUrl(pac_url, &io_callback_); |
| 173 } | 169 } |
| 174 | 170 |
| 175 int InitProxyResolver::DoSetPacScriptComplete(int result) { | 171 int InitProxyResolver::DoSetPacScriptComplete(int result) { |
| 176 if (result != OK) { | 172 if (result != OK) { |
| 177 LoadLog::AddString(load_log_, | 173 net_log_.AddString( |
| 178 StringPrintf("Failed initializing the PAC script with error: %s", | 174 StringPrintf("Failed initializing the PAC script with error: %s", |
| 179 ErrorToString(result))); | 175 ErrorToString(result))); |
| 180 LoadLog::EndEvent(load_log_, | 176 net_log_.EndEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT); |
| 181 LoadLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT); | |
| 182 return TryToFallbackPacUrl(result); | 177 return TryToFallbackPacUrl(result); |
| 183 } | 178 } |
| 184 | 179 |
| 185 LoadLog::AddStringLiteral(load_log_, "Successfully initialized PAC script."); | 180 net_log_.AddStringLiteral("Successfully initialized PAC script."); |
| 186 | 181 |
| 187 LoadLog::EndEvent(load_log_, | 182 net_log_.EndEvent(NetLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT); |
| 188 LoadLog::TYPE_INIT_PROXY_RESOLVER_SET_PAC_SCRIPT); | |
| 189 return result; | 183 return result; |
| 190 } | 184 } |
| 191 | 185 |
| 192 int InitProxyResolver::TryToFallbackPacUrl(int error) { | 186 int InitProxyResolver::TryToFallbackPacUrl(int error) { |
| 193 DCHECK_LT(error, 0); | 187 DCHECK_LT(error, 0); |
| 194 | 188 |
| 195 if (current_pac_url_index_ + 1 >= pac_urls_.size()) { | 189 if (current_pac_url_index_ + 1 >= pac_urls_.size()) { |
| 196 // Nothing left to fall back to. | 190 // Nothing left to fall back to. |
| 197 return error; | 191 return error; |
| 198 } | 192 } |
| 199 | 193 |
| 200 // Advance to next URL in our list. | 194 // Advance to next URL in our list. |
| 201 ++current_pac_url_index_; | 195 ++current_pac_url_index_; |
| 202 | 196 |
| 203 LoadLog::AddStringLiteral(load_log_, "Falling back to next PAC URL..."); | 197 net_log_.AddStringLiteral("Falling back to next PAC URL..."); |
| 204 | 198 |
| 205 next_state_ = GetStartState(); | 199 next_state_ = GetStartState(); |
| 206 | 200 |
| 207 return OK; | 201 return OK; |
| 208 } | 202 } |
| 209 | 203 |
| 210 InitProxyResolver::State InitProxyResolver::GetStartState() const { | 204 InitProxyResolver::State InitProxyResolver::GetStartState() const { |
| 211 return resolver_->expects_pac_bytes() ? | 205 return resolver_->expects_pac_bytes() ? |
| 212 STATE_FETCH_PAC_SCRIPT : STATE_SET_PAC_SCRIPT; | 206 STATE_FETCH_PAC_SCRIPT : STATE_SET_PAC_SCRIPT; |
| 213 } | 207 } |
| 214 | 208 |
| 215 const GURL& InitProxyResolver::current_pac_url() const { | 209 const GURL& InitProxyResolver::current_pac_url() const { |
| 216 DCHECK_LT(current_pac_url_index_, pac_urls_.size()); | 210 DCHECK_LT(current_pac_url_index_, pac_urls_.size()); |
| 217 return pac_urls_[current_pac_url_index_]; | 211 return pac_urls_[current_pac_url_index_]; |
| 218 } | 212 } |
| 219 | 213 |
| 220 void InitProxyResolver::DidCompleteInit() { | 214 void InitProxyResolver::DidCompleteInit() { |
| 221 LoadLog::EndEvent(load_log_, LoadLog::TYPE_INIT_PROXY_RESOLVER); | 215 net_log_.EndEvent(NetLog::TYPE_INIT_PROXY_RESOLVER); |
| 222 } | 216 } |
| 223 | 217 |
| 224 void InitProxyResolver::Cancel() { | 218 void InitProxyResolver::Cancel() { |
| 225 DCHECK_NE(STATE_NONE, next_state_); | 219 DCHECK_NE(STATE_NONE, next_state_); |
| 226 | 220 |
| 227 LoadLog::AddEvent(load_log_, LoadLog::TYPE_CANCELLED); | 221 net_log_.AddEvent(NetLog::TYPE_CANCELLED); |
| 228 | 222 |
| 229 switch (next_state_) { | 223 switch (next_state_) { |
| 230 case STATE_FETCH_PAC_SCRIPT_COMPLETE: | 224 case STATE_FETCH_PAC_SCRIPT_COMPLETE: |
| 231 proxy_script_fetcher_->Cancel(); | 225 proxy_script_fetcher_->Cancel(); |
| 232 break; | 226 break; |
| 233 case STATE_SET_PAC_SCRIPT_COMPLETE: | 227 case STATE_SET_PAC_SCRIPT_COMPLETE: |
| 234 resolver_->CancelSetPacScript(); | 228 resolver_->CancelSetPacScript(); |
| 235 break; | 229 break; |
| 236 default: | 230 default: |
| 237 NOTREACHED(); | 231 NOTREACHED(); |
| 238 break; | 232 break; |
| 239 } | 233 } |
| 240 | 234 |
| 241 DidCompleteInit(); | 235 DidCompleteInit(); |
| 242 } | 236 } |
| 243 | 237 |
| 244 } // namespace net | 238 } // namespace net |
| OLD | NEW |