| 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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 PrivetURLFetcher::PrivetURLFetcher( | 65 PrivetURLFetcher::PrivetURLFetcher( |
| 66 const GURL& url, | 66 const GURL& url, |
| 67 net::URLFetcher::RequestType request_type, | 67 net::URLFetcher::RequestType request_type, |
| 68 net::URLRequestContextGetter* request_context, | 68 net::URLRequestContextGetter* request_context, |
| 69 PrivetURLFetcher::Delegate* delegate) | 69 PrivetURLFetcher::Delegate* delegate) |
| 70 : url_(url), | 70 : url_(url), |
| 71 request_type_(request_type), | 71 request_type_(request_type), |
| 72 request_context_(request_context), | 72 request_context_(request_context), |
| 73 delegate_(delegate), | 73 delegate_(delegate), |
| 74 do_not_retry_on_transient_error_(false), | 74 do_not_retry_on_transient_error_(false), |
| 75 allow_empty_privet_token_(false), | 75 send_empty_privet_token_(false), |
| 76 has_byte_range_(false), | 76 has_byte_range_(false), |
| 77 make_response_file_(false), | 77 make_response_file_(false), |
| 78 byte_range_start_(0), | 78 byte_range_start_(0), |
| 79 byte_range_end_(0), | 79 byte_range_end_(0), |
| 80 tries_(0), | 80 tries_(0), |
| 81 weak_factory_(this) {} | 81 weak_factory_(this) {} |
| 82 | 82 |
| 83 PrivetURLFetcher::~PrivetURLFetcher() { | 83 PrivetURLFetcher::~PrivetURLFetcher() { |
| 84 } | 84 } |
| 85 | 85 |
| 86 // static | 86 // static |
| 87 void PrivetURLFetcher::SetTokenForHost(const std::string& host, | 87 void PrivetURLFetcher::SetTokenForHost(const std::string& host, |
| 88 const std::string& token) { | 88 const std::string& token) { |
| 89 TokenMapHolder::GetInstance()->map[host] = token; | 89 TokenMapHolder::GetInstance()->map[host] = token; |
| 90 } | 90 } |
| 91 | 91 |
| 92 // static | 92 // static |
| 93 void PrivetURLFetcher::ResetTokenMapForTests() { | 93 void PrivetURLFetcher::ResetTokenMapForTests() { |
| 94 TokenMapHolder::GetInstance()->map.clear(); | 94 TokenMapHolder::GetInstance()->map.clear(); |
| 95 } | 95 } |
| 96 | 96 |
| 97 void PrivetURLFetcher::DoNotRetryOnTransientError() { | 97 void PrivetURLFetcher::DoNotRetryOnTransientError() { |
| 98 DCHECK(tries_ == 0); | 98 DCHECK(tries_ == 0); |
| 99 do_not_retry_on_transient_error_ = true; | 99 do_not_retry_on_transient_error_ = true; |
| 100 } | 100 } |
| 101 | 101 |
| 102 void PrivetURLFetcher::AllowEmptyPrivetToken() { | 102 void PrivetURLFetcher::SendEmptyPrivetToken() { |
| 103 DCHECK(tries_ == 0); | 103 DCHECK(tries_ == 0); |
| 104 allow_empty_privet_token_ = true; | 104 send_empty_privet_token_ = true; |
| 105 } | 105 } |
| 106 | 106 |
| 107 std::string PrivetURLFetcher::GetPrivetAccessToken() { | 107 std::string PrivetURLFetcher::GetPrivetAccessToken() { |
| 108 if (send_empty_privet_token_) { |
| 109 return std::string(); |
| 110 } |
| 111 |
| 108 TokenMapHolder* token_map_holder = TokenMapHolder::GetInstance(); | 112 TokenMapHolder* token_map_holder = TokenMapHolder::GetInstance(); |
| 109 TokenMap::iterator found = token_map_holder->map.find(GetHostString()); | 113 TokenMap::iterator found = token_map_holder->map.find(GetHostString()); |
| 110 return found != token_map_holder->map.end() ? found->second : std::string(); | 114 return found != token_map_holder->map.end() ? found->second : std::string(); |
| 111 } | 115 } |
| 112 | 116 |
| 113 std::string PrivetURLFetcher::GetHostString() { | 117 std::string PrivetURLFetcher::GetHostString() { |
| 114 return url_.GetOrigin().spec(); | 118 return url_.GetOrigin().spec(); |
| 115 } | 119 } |
| 116 | 120 |
| 117 void PrivetURLFetcher::SaveResponseToFile() { | 121 void PrivetURLFetcher::SaveResponseToFile() { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 | 170 |
| 167 url_fetcher_->Start(); | 171 url_fetcher_->Start(); |
| 168 } else { | 172 } else { |
| 169 delegate_->OnError(this, RETRY_ERROR); | 173 delegate_->OnError(this, RETRY_ERROR); |
| 170 } | 174 } |
| 171 } | 175 } |
| 172 | 176 |
| 173 void PrivetURLFetcher::Start() { | 177 void PrivetURLFetcher::Start() { |
| 174 DCHECK_EQ(tries_, 0); // We haven't called |Start()| yet. | 178 DCHECK_EQ(tries_, 0); // We haven't called |Start()| yet. |
| 175 | 179 |
| 176 std::string privet_access_token = GetPrivetAccessToken(); | 180 if (!send_empty_privet_token_) { |
| 177 if (privet_access_token.empty() && !allow_empty_privet_token_) { | 181 std::string privet_access_token; |
| 178 RequestTokenRefresh(); | 182 privet_access_token = GetPrivetAccessToken(); |
| 179 } else { | 183 if (privet_access_token.empty()) { |
| 180 Try(); | 184 RequestTokenRefresh(); |
| 185 return; |
| 186 } |
| 181 } | 187 } |
| 188 |
| 189 Try(); |
| 182 } | 190 } |
| 183 | 191 |
| 184 void PrivetURLFetcher::SetUploadData(const std::string& upload_content_type, | 192 void PrivetURLFetcher::SetUploadData(const std::string& upload_content_type, |
| 185 const std::string& upload_data) { | 193 const std::string& upload_data) { |
| 186 DCHECK(upload_file_path_.empty()); | 194 DCHECK(upload_file_path_.empty()); |
| 187 upload_content_type_ = upload_content_type; | 195 upload_content_type_ = upload_content_type; |
| 188 upload_data_ = upload_data; | 196 upload_data_ = upload_data; |
| 189 } | 197 } |
| 190 | 198 |
| 191 void PrivetURLFetcher::SetUploadFilePath( | 199 void PrivetURLFetcher::SetUploadFilePath( |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 335 } | 343 } |
| 336 } | 344 } |
| 337 | 345 |
| 338 bool PrivetURLFetcher::PrivetErrorTransient(const std::string& error) { | 346 bool PrivetURLFetcher::PrivetErrorTransient(const std::string& error) { |
| 339 return (error == kPrivetErrorDeviceBusy) || | 347 return (error == kPrivetErrorDeviceBusy) || |
| 340 (error == kPrivetErrorPendingUserAction) || | 348 (error == kPrivetErrorPendingUserAction) || |
| 341 (error == kPrivetErrorPrinterBusy); | 349 (error == kPrivetErrorPrinterBusy); |
| 342 } | 350 } |
| 343 | 351 |
| 344 } // namespace local_discovery | 352 } // namespace local_discovery |
| OLD | NEW |