| 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/safe_browsing/two_phase_uploader.h" | 5 #include "chrome/browser/safe_browsing/two_phase_uploader.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <limits> | 9 #include <limits> |
| 10 | 10 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 const char kStartHeader[] = "x-goog-resumable: start"; | 26 const char kStartHeader[] = "x-goog-resumable: start"; |
| 27 | 27 |
| 28 // Header returned on initial response with URL to use for the second phase. | 28 // Header returned on initial response with URL to use for the second phase. |
| 29 const char kLocationHeader[] = "Location"; | 29 const char kLocationHeader[] = "Location"; |
| 30 | 30 |
| 31 const char kUploadContentType[] = "application/octet-stream"; | 31 const char kUploadContentType[] = "application/octet-stream"; |
| 32 | 32 |
| 33 class TwoPhaseUploaderImpl : public net::URLFetcherDelegate, | 33 class TwoPhaseUploaderImpl : public net::URLFetcherDelegate, |
| 34 public TwoPhaseUploader { | 34 public TwoPhaseUploader { |
| 35 public: | 35 public: |
| 36 TwoPhaseUploaderImpl(net::URLRequestContextGetter* url_request_context_getter, | 36 TwoPhaseUploaderImpl( |
| 37 base::TaskRunner* file_task_runner, | 37 net::URLRequestContextGetter* url_request_context_getter, |
| 38 const GURL& base_url, | 38 base::TaskRunner* file_task_runner, |
| 39 const std::string& metadata, | 39 const GURL& base_url, |
| 40 const base::FilePath& file_path, | 40 const std::string& metadata, |
| 41 const ProgressCallback& progress_callback, | 41 const base::FilePath& file_path, |
| 42 const FinishCallback& finish_callback); | 42 const ProgressCallback& progress_callback, |
| 43 const FinishCallback& finish_callback, |
| 44 const net::NetworkTrafficAnnotationTag& traffic_annotation); |
| 43 ~TwoPhaseUploaderImpl() override; | 45 ~TwoPhaseUploaderImpl() override; |
| 44 | 46 |
| 45 // Begins the upload process. | 47 // Begins the upload process. |
| 46 void Start() override; | 48 void Start() override; |
| 47 | 49 |
| 48 // net::URLFetcherDelegate implementation: | 50 // net::URLFetcherDelegate implementation: |
| 49 void OnURLFetchComplete(const net::URLFetcher* source) override; | 51 void OnURLFetchComplete(const net::URLFetcher* source) override; |
| 50 void OnURLFetchUploadProgress(const net::URLFetcher* source, | 52 void OnURLFetchUploadProgress(const net::URLFetcher* source, |
| 51 int64_t current, | 53 int64_t current, |
| 52 int64_t total) override; | 54 int64_t total) override; |
| 53 | 55 |
| 54 private: | 56 private: |
| 55 void UploadMetadata(); | 57 void UploadMetadata(); |
| 56 void UploadFile(); | 58 void UploadFile(); |
| 57 void Finish(int net_error, int response_code, const std::string& response); | 59 void Finish(int net_error, int response_code, const std::string& response); |
| 58 | 60 |
| 59 State state_; | 61 State state_; |
| 60 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; | 62 scoped_refptr<net::URLRequestContextGetter> url_request_context_getter_; |
| 61 scoped_refptr<base::TaskRunner> file_task_runner_; | 63 scoped_refptr<base::TaskRunner> file_task_runner_; |
| 62 GURL base_url_; | 64 GURL base_url_; |
| 63 GURL upload_url_; | 65 GURL upload_url_; |
| 64 std::string metadata_; | 66 std::string metadata_; |
| 65 const base::FilePath file_path_; | 67 const base::FilePath file_path_; |
| 66 ProgressCallback progress_callback_; | 68 ProgressCallback progress_callback_; |
| 67 FinishCallback finish_callback_; | 69 FinishCallback finish_callback_; |
| 70 net::NetworkTrafficAnnotationTag traffic_annotation_; |
| 68 | 71 |
| 69 std::unique_ptr<net::URLFetcher> url_fetcher_; | 72 std::unique_ptr<net::URLFetcher> url_fetcher_; |
| 70 | 73 |
| 71 DISALLOW_COPY_AND_ASSIGN(TwoPhaseUploaderImpl); | 74 DISALLOW_COPY_AND_ASSIGN(TwoPhaseUploaderImpl); |
| 72 }; | 75 }; |
| 73 | 76 |
| 74 TwoPhaseUploaderImpl::TwoPhaseUploaderImpl( | 77 TwoPhaseUploaderImpl::TwoPhaseUploaderImpl( |
| 75 net::URLRequestContextGetter* url_request_context_getter, | 78 net::URLRequestContextGetter* url_request_context_getter, |
| 76 base::TaskRunner* file_task_runner, | 79 base::TaskRunner* file_task_runner, |
| 77 const GURL& base_url, | 80 const GURL& base_url, |
| 78 const std::string& metadata, | 81 const std::string& metadata, |
| 79 const base::FilePath& file_path, | 82 const base::FilePath& file_path, |
| 80 const ProgressCallback& progress_callback, | 83 const ProgressCallback& progress_callback, |
| 81 const FinishCallback& finish_callback) | 84 const FinishCallback& finish_callback, |
| 85 const net::NetworkTrafficAnnotationTag& traffic_annotation) |
| 82 : state_(STATE_NONE), | 86 : state_(STATE_NONE), |
| 83 url_request_context_getter_(url_request_context_getter), | 87 url_request_context_getter_(url_request_context_getter), |
| 84 file_task_runner_(file_task_runner), | 88 file_task_runner_(file_task_runner), |
| 85 base_url_(base_url), | 89 base_url_(base_url), |
| 86 metadata_(metadata), | 90 metadata_(metadata), |
| 87 file_path_(file_path), | 91 file_path_(file_path), |
| 88 progress_callback_(progress_callback), | 92 progress_callback_(progress_callback), |
| 89 finish_callback_(finish_callback) { | 93 finish_callback_(finish_callback), |
| 94 traffic_annotation_(traffic_annotation) { |
| 90 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 91 } | 96 } |
| 92 | 97 |
| 93 TwoPhaseUploaderImpl::~TwoPhaseUploaderImpl() { | 98 TwoPhaseUploaderImpl::~TwoPhaseUploaderImpl() { |
| 94 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 99 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 95 } | 100 } |
| 96 | 101 |
| 97 void TwoPhaseUploaderImpl::Start() { | 102 void TwoPhaseUploaderImpl::Start() { |
| 98 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 103 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 99 DCHECK_EQ(STATE_NONE, state_); | 104 DCHECK_EQ(STATE_NONE, state_); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 161 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 166 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 162 DVLOG(3) << __func__ << " " << source->GetURL().spec() << " " << current | 167 DVLOG(3) << __func__ << " " << source->GetURL().spec() << " " << current |
| 163 << "/" << total; | 168 << "/" << total; |
| 164 if (state_ == UPLOAD_FILE && !progress_callback_.is_null()) | 169 if (state_ == UPLOAD_FILE && !progress_callback_.is_null()) |
| 165 progress_callback_.Run(current, total); | 170 progress_callback_.Run(current, total); |
| 166 } | 171 } |
| 167 | 172 |
| 168 void TwoPhaseUploaderImpl::UploadMetadata() { | 173 void TwoPhaseUploaderImpl::UploadMetadata() { |
| 169 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 174 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 170 state_ = UPLOAD_METADATA; | 175 state_ = UPLOAD_METADATA; |
| 171 url_fetcher_ = | 176 url_fetcher_ = net::URLFetcher::Create(base_url_, net::URLFetcher::POST, this, |
| 172 net::URLFetcher::Create(base_url_, net::URLFetcher::POST, this); | 177 traffic_annotation_); |
| 178 |
| 173 data_use_measurement::DataUseUserData::AttachToFetcher( | 179 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 174 url_fetcher_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); | 180 url_fetcher_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); |
| 175 url_fetcher_->SetRequestContext(url_request_context_getter_.get()); | 181 url_fetcher_->SetRequestContext(url_request_context_getter_.get()); |
| 176 url_fetcher_->SetExtraRequestHeaders(kStartHeader); | 182 url_fetcher_->SetExtraRequestHeaders(kStartHeader); |
| 177 url_fetcher_->SetUploadData(kUploadContentType, metadata_); | 183 url_fetcher_->SetUploadData(kUploadContentType, metadata_); |
| 178 url_fetcher_->Start(); | 184 url_fetcher_->Start(); |
| 179 } | 185 } |
| 180 | 186 |
| 181 void TwoPhaseUploaderImpl::UploadFile() { | 187 void TwoPhaseUploaderImpl::UploadFile() { |
| 182 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 188 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 183 state_ = UPLOAD_FILE; | 189 state_ = UPLOAD_FILE; |
| 184 | 190 |
| 185 url_fetcher_ = | 191 url_fetcher_ = net::URLFetcher::Create(upload_url_, net::URLFetcher::PUT, |
| 186 net::URLFetcher::Create(upload_url_, net::URLFetcher::PUT, this); | 192 this, traffic_annotation_); |
| 187 data_use_measurement::DataUseUserData::AttachToFetcher( | 193 data_use_measurement::DataUseUserData::AttachToFetcher( |
| 188 url_fetcher_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); | 194 url_fetcher_.get(), data_use_measurement::DataUseUserData::SAFE_BROWSING); |
| 189 url_fetcher_->SetRequestContext(url_request_context_getter_.get()); | 195 url_fetcher_->SetRequestContext(url_request_context_getter_.get()); |
| 190 url_fetcher_->SetUploadFilePath(kUploadContentType, file_path_, 0, | 196 url_fetcher_->SetUploadFilePath(kUploadContentType, file_path_, 0, |
| 191 std::numeric_limits<uint64_t>::max(), | 197 std::numeric_limits<uint64_t>::max(), |
| 192 file_task_runner_); | 198 file_task_runner_); |
| 193 url_fetcher_->Start(); | 199 url_fetcher_->Start(); |
| 194 } | 200 } |
| 195 | 201 |
| 196 void TwoPhaseUploaderImpl::Finish(int net_error, | 202 void TwoPhaseUploaderImpl::Finish(int net_error, |
| 197 int response_code, | 203 int response_code, |
| 198 const std::string& response) { | 204 const std::string& response) { |
| 199 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 205 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 200 finish_callback_.Run(state_, net_error, response_code, response); | 206 finish_callback_.Run(state_, net_error, response_code, response); |
| 201 } | 207 } |
| 202 | 208 |
| 203 } // namespace | 209 } // namespace |
| 204 | 210 |
| 205 // static | 211 // static |
| 206 TwoPhaseUploaderFactory* TwoPhaseUploader::factory_ = nullptr; | 212 TwoPhaseUploaderFactory* TwoPhaseUploader::factory_ = nullptr; |
| 207 | 213 |
| 208 // static | 214 // static |
| 209 std::unique_ptr<TwoPhaseUploader> TwoPhaseUploader::Create( | 215 std::unique_ptr<TwoPhaseUploader> TwoPhaseUploader::Create( |
| 210 net::URLRequestContextGetter* url_request_context_getter, | 216 net::URLRequestContextGetter* url_request_context_getter, |
| 211 base::TaskRunner* file_task_runner, | 217 base::TaskRunner* file_task_runner, |
| 212 const GURL& base_url, | 218 const GURL& base_url, |
| 213 const std::string& metadata, | 219 const std::string& metadata, |
| 214 const base::FilePath& file_path, | 220 const base::FilePath& file_path, |
| 215 const ProgressCallback& progress_callback, | 221 const ProgressCallback& progress_callback, |
| 216 const FinishCallback& finish_callback) { | 222 const FinishCallback& finish_callback, |
| 223 const net::NetworkTrafficAnnotationTag& traffic_annotation) { |
| 217 if (!factory_) { | 224 if (!factory_) { |
| 218 return base::WrapUnique(new TwoPhaseUploaderImpl( | 225 return base::WrapUnique(new TwoPhaseUploaderImpl( |
| 219 url_request_context_getter, file_task_runner, base_url, metadata, | 226 url_request_context_getter, file_task_runner, base_url, metadata, |
| 220 file_path, progress_callback, finish_callback)); | 227 file_path, progress_callback, finish_callback, traffic_annotation)); |
| 221 } | 228 } |
| 222 return TwoPhaseUploader::factory_->CreateTwoPhaseUploader( | 229 return TwoPhaseUploader::factory_->CreateTwoPhaseUploader( |
| 223 url_request_context_getter, file_task_runner, base_url, metadata, | 230 url_request_context_getter, file_task_runner, base_url, metadata, |
| 224 file_path, progress_callback, finish_callback); | 231 file_path, progress_callback, finish_callback, traffic_annotation); |
| 225 } | 232 } |
| OLD | NEW |