| 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" |
| 11 #include "base/message_loop/message_loop.h" | 11 #include "base/message_loop/message_loop.h" |
| 12 #include "base/rand_util.h" | 12 #include "base/rand_util.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/local_discovery/privet_constants.h" | 14 #include "chrome/browser/local_discovery/privet_constants.h" |
| 15 #include "content/public/browser/browser_thread.h" |
| 15 #include "net/http/http_status_code.h" | 16 #include "net/http/http_status_code.h" |
| 16 #include "net/url_request/url_request_status.h" | 17 #include "net/url_request/url_request_status.h" |
| 17 | 18 |
| 18 namespace local_discovery { | 19 namespace local_discovery { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 const char kXPrivetTokenHeaderPrefix[] = "X-Privet-Token: "; | 22 const char kXPrivetTokenHeaderPrefix[] = "X-Privet-Token: "; |
| 22 const char kXPrivetEmptyToken[] = "\"\""; | 23 const char kXPrivetEmptyToken[] = "\"\""; |
| 23 const int kPrivetMaxRetries = 20; | 24 const int kPrivetMaxRetries = 20; |
| 24 const int kPrivetTimeoutOnError = 5; | 25 const int kPrivetTimeoutOnError = 5; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 61 |
| 61 if (token.empty()) | 62 if (token.empty()) |
| 62 token = kXPrivetEmptyToken; | 63 token = kXPrivetEmptyToken; |
| 63 | 64 |
| 64 url_fetcher_.reset(net::URLFetcher::Create(url_, request_type_, this)); | 65 url_fetcher_.reset(net::URLFetcher::Create(url_, request_type_, this)); |
| 65 url_fetcher_->SetRequestContext(request_context_); | 66 url_fetcher_->SetRequestContext(request_context_); |
| 66 url_fetcher_->AddExtraRequestHeader(std::string(kXPrivetTokenHeaderPrefix) + | 67 url_fetcher_->AddExtraRequestHeader(std::string(kXPrivetTokenHeaderPrefix) + |
| 67 token); | 68 token); |
| 68 | 69 |
| 69 // URLFetcher requires us to set upload data for POST requests. | 70 // URLFetcher requires us to set upload data for POST requests. |
| 70 if (request_type_ == net::URLFetcher::POST) | 71 if (request_type_ == net::URLFetcher::POST) { |
| 71 url_fetcher_->SetUploadData(upload_content_type_, upload_data_); | 72 if (!upload_file_path_.empty()) { |
| 73 url_fetcher_->SetUploadFilePath( |
| 74 upload_content_type_, |
| 75 upload_file_path_, |
| 76 0 /*offset*/, |
| 77 kuint64max /*length*/, |
| 78 content::BrowserThread::GetMessageLoopProxyForThread( |
| 79 content::BrowserThread::FILE)); |
| 80 } else { |
| 81 url_fetcher_->SetUploadData(upload_content_type_, upload_data_); |
| 82 } |
| 83 } |
| 72 | 84 |
| 73 url_fetcher_->Start(); | 85 url_fetcher_->Start(); |
| 74 } else { | 86 } else { |
| 75 delegate_->OnError(this, RETRY_ERROR); | 87 delegate_->OnError(this, RETRY_ERROR); |
| 76 } | 88 } |
| 77 } | 89 } |
| 78 | 90 |
| 79 void PrivetURLFetcher::Start() { | 91 void PrivetURLFetcher::Start() { |
| 80 DCHECK_EQ(tries_, 0); // We haven't called |Start()| yet. | 92 DCHECK_EQ(tries_, 0); // We haven't called |Start()| yet. |
| 81 | 93 |
| 82 if (privet_access_token_.empty() && !allow_empty_privet_token_) { | 94 if (privet_access_token_.empty() && !allow_empty_privet_token_) { |
| 83 RequestTokenRefresh(); | 95 RequestTokenRefresh(); |
| 84 } else { | 96 } else { |
| 85 Try(); | 97 Try(); |
| 86 } | 98 } |
| 87 } | 99 } |
| 88 | 100 |
| 89 void PrivetURLFetcher::SetUploadData(const std::string& upload_content_type, | 101 void PrivetURLFetcher::SetUploadData(const std::string& upload_content_type, |
| 90 const std::string& upload_data) { | 102 const std::string& upload_data) { |
| 103 DCHECK(upload_file_path_.empty()); |
| 91 upload_content_type_ = upload_content_type; | 104 upload_content_type_ = upload_content_type; |
| 92 upload_data_ = upload_data; | 105 upload_data_ = upload_data; |
| 93 } | 106 } |
| 94 | 107 |
| 108 void PrivetURLFetcher::SetUploadFilePath( |
| 109 const std::string& upload_content_type, |
| 110 const base::FilePath& upload_file_path) { |
| 111 DCHECK(upload_data_.empty()); |
| 112 upload_content_type_ = upload_content_type; |
| 113 upload_file_path_ = upload_file_path; |
| 114 } |
| 115 |
| 95 void PrivetURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { | 116 void PrivetURLFetcher::OnURLFetchComplete(const net::URLFetcher* source) { |
| 96 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || | 117 if (source->GetStatus().status() != net::URLRequestStatus::SUCCESS || |
| 97 source->GetResponseCode() == net::HTTP_SERVICE_UNAVAILABLE) { | 118 source->GetResponseCode() == net::HTTP_SERVICE_UNAVAILABLE) { |
| 98 ScheduleRetry(kPrivetTimeoutOnError); | 119 ScheduleRetry(kPrivetTimeoutOnError); |
| 99 return; | 120 return; |
| 100 } | 121 } |
| 101 | 122 |
| 102 if (source->GetResponseCode() != net::HTTP_OK) { | 123 if (source->GetResponseCode() != net::HTTP_OK) { |
| 103 delegate_->OnError(this, RESPONSE_CODE_ERROR); | 124 delegate_->OnError(this, RESPONSE_CODE_ERROR); |
| 104 return; | 125 return; |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 | 219 |
| 199 scoped_ptr<PrivetURLFetcher> PrivetURLFetcherFactory::CreateURLFetcher( | 220 scoped_ptr<PrivetURLFetcher> PrivetURLFetcherFactory::CreateURLFetcher( |
| 200 const GURL& url, net::URLFetcher::RequestType request_type, | 221 const GURL& url, net::URLFetcher::RequestType request_type, |
| 201 PrivetURLFetcher::Delegate* delegate) const { | 222 PrivetURLFetcher::Delegate* delegate) const { |
| 202 return scoped_ptr<PrivetURLFetcher>( | 223 return scoped_ptr<PrivetURLFetcher>( |
| 203 new PrivetURLFetcher(token_, url, request_type, request_context_.get(), | 224 new PrivetURLFetcher(token_, url, request_type, request_context_.get(), |
| 204 delegate)); | 225 delegate)); |
| 205 } | 226 } |
| 206 | 227 |
| 207 } // namespace local_discovery | 228 } // namespace local_discovery |
| OLD | NEW |