Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "chrome/browser/local_discovery/privet_url_fetcher.h" | 5 #include "chrome/browser/local_discovery/privet_url_fetcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 100 const std::string& token) { | 100 const std::string& token) { |
| 101 TokenMapHolder::GetInstance()->map[host] = token; | 101 TokenMapHolder::GetInstance()->map[host] = token; |
| 102 } | 102 } |
| 103 | 103 |
| 104 // static | 104 // static |
| 105 void PrivetURLFetcher::ResetTokenMapForTests() { | 105 void PrivetURLFetcher::ResetTokenMapForTests() { |
| 106 TokenMapHolder::GetInstance()->map.clear(); | 106 TokenMapHolder::GetInstance()->map.clear(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 void PrivetURLFetcher::SetMaxRetries(int max_retries) { | 109 void PrivetURLFetcher::SetMaxRetries(int max_retries) { |
| 110 DCHECK(tries_ == 0); | 110 DCHECK_EQ(tries_, 0); |
| 111 max_retries_ = max_retries; | 111 max_retries_ = max_retries; |
| 112 } | 112 } |
| 113 | 113 |
| 114 void PrivetURLFetcher::DoNotRetryOnTransientError() { | 114 void PrivetURLFetcher::DoNotRetryOnTransientError() { |
| 115 DCHECK(tries_ == 0); | 115 DCHECK_EQ(tries_, 0); |
| 116 do_not_retry_on_transient_error_ = true; | 116 do_not_retry_on_transient_error_ = true; |
| 117 } | 117 } |
| 118 | 118 |
| 119 void PrivetURLFetcher::SendEmptyPrivetToken() { | 119 void PrivetURLFetcher::SendEmptyPrivetToken() { |
| 120 DCHECK(tries_ == 0); | 120 DCHECK_EQ(tries_, 0); |
| 121 send_empty_privet_token_ = true; | 121 send_empty_privet_token_ = true; |
| 122 } | 122 } |
| 123 | 123 |
| 124 std::string PrivetURLFetcher::GetPrivetAccessToken() { | 124 std::string PrivetURLFetcher::GetPrivetAccessToken() { |
| 125 if (send_empty_privet_token_) { | 125 if (send_empty_privet_token_) { |
| 126 return std::string(); | 126 return std::string(); |
| 127 } | 127 } |
| 128 | 128 |
| 129 TokenMapHolder* token_map_holder = TokenMapHolder::GetInstance(); | 129 TokenMapHolder* token_map_holder = TokenMapHolder::GetInstance(); |
| 130 TokenMap::iterator found = token_map_holder->map.find(GetHostString()); | 130 TokenMap::iterator found = token_map_holder->map.find(GetHostString()); |
| 131 return found != token_map_holder->map.end() ? found->second : std::string(); | 131 return found != token_map_holder->map.end() ? found->second : std::string(); |
| 132 } | 132 } |
| 133 | 133 |
| 134 std::string PrivetURLFetcher::GetHostString() { | 134 std::string PrivetURLFetcher::GetHostString() { |
| 135 return url_.GetOrigin().spec(); | 135 return url_.GetOrigin().spec(); |
| 136 } | 136 } |
| 137 | 137 |
| 138 void PrivetURLFetcher::SaveResponseToFile() { | 138 void PrivetURLFetcher::SaveResponseToFile() { |
| 139 DCHECK(tries_ == 0); | 139 DCHECK_EQ(tries_, 0); |
| 140 make_response_file_ = true; | 140 make_response_file_ = true; |
| 141 } | 141 } |
| 142 | 142 |
| 143 void PrivetURLFetcher::V3Mode() { | 143 void PrivetURLFetcher::V3Mode() { |
| 144 v3_mode_ = true; | 144 v3_mode_ = true; |
| 145 } | 145 } |
| 146 | 146 |
| 147 void PrivetURLFetcher::SetByteRange(int start, int end) { | 147 void PrivetURLFetcher::SetByteRange(int start, int end) { |
| 148 DCHECK(tries_ == 0); | 148 DCHECK_EQ(tries_, 0); |
| 149 byte_range_start_ = start; | 149 byte_range_start_ = start; |
| 150 byte_range_end_ = end; | 150 byte_range_end_ = end; |
| 151 has_byte_range_ = true; | 151 has_byte_range_ = true; |
| 152 } | 152 } |
| 153 | 153 |
| 154 void PrivetURLFetcher::Try() { | 154 void PrivetURLFetcher::Try() { |
| 155 tries_++; | 155 tries_++; |
| 156 if (tries_ <= max_retries_) { | 156 if (tries_ <= max_retries_) { |
| 157 DVLOG(1) << "Try no. " << tries_; | 157 DVLOG(1) << "Try number: " << tries_; |
|
Aleksey Shlyapnikov
2015/10/30 21:40:00
Try number -> Attempt
Vitaly Buka (NO REVIEWS)
2015/10/30 21:49:32
Done.
| |
| 158 url_fetcher_ = net::URLFetcher::Create(url_, request_type_, this); | 158 url_fetcher_ = net::URLFetcher::Create(url_, request_type_, this); |
| 159 // Privet requests are relevant to hosts on local network only. | 159 // Privet requests are relevant to hosts on local network only. |
| 160 url_fetcher_->SetLoadFlags( | 160 url_fetcher_->SetLoadFlags( |
| 161 url_fetcher_->GetLoadFlags() | net::LOAD_BYPASS_PROXY | | 161 url_fetcher_->GetLoadFlags() | net::LOAD_BYPASS_PROXY | |
| 162 net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SEND_COOKIES); | 162 net::LOAD_DISABLE_CACHE | net::LOAD_DO_NOT_SEND_COOKIES); |
| 163 url_fetcher_->SetRequestContext(context_getter_.get()); | 163 url_fetcher_->SetRequestContext(context_getter_.get()); |
| 164 | 164 |
| 165 if (v3_mode_) { | 165 if (v3_mode_) { |
| 166 url_fetcher_->AddExtraRequestHeader( | 166 url_fetcher_->AddExtraRequestHeader( |
| 167 std::string(kPrivetV3AuthTokenHeaderPrefix) + | 167 std::string(kPrivetV3AuthTokenHeaderPrefix) + |
| (...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 } | 391 } |
| 392 | 392 |
| 393 bool PrivetURLFetcher::PrivetErrorTransient(const std::string& error) { | 393 bool PrivetURLFetcher::PrivetErrorTransient(const std::string& error) { |
| 394 return (error == kPrivetErrorDeviceBusy) || | 394 return (error == kPrivetErrorDeviceBusy) || |
| 395 (error == kPrivetV3ErrorDeviceBusy) || | 395 (error == kPrivetV3ErrorDeviceBusy) || |
| 396 (error == kPrivetErrorPendingUserAction) || | 396 (error == kPrivetErrorPendingUserAction) || |
| 397 (error == kPrivetErrorPrinterBusy); | 397 (error == kPrivetErrorPrinterBusy); |
| 398 } | 398 } |
| 399 | 399 |
| 400 } // namespace local_discovery | 400 } // namespace local_discovery |
| OLD | NEW |