Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(699)

Side by Side Diff: content/browser/download/base_file.cc

Issue 2890853002: Downloads: replace BrowserThread::FILE with task scheduler. (Closed)
Patch Set: Add a missing mock expectation. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "content/browser/download/base_file.h" 5 #include "content/browser/download/base_file.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/files/file.h" 10 #include "base/files/file.h"
(...skipping 13 matching lines...) Expand all
24 #include "crypto/secure_hash.h" 24 #include "crypto/secure_hash.h"
25 #include "net/base/net_errors.h" 25 #include "net/base/net_errors.h"
26 #include "net/log/net_log.h" 26 #include "net/log/net_log.h"
27 #include "net/log/net_log_event_type.h" 27 #include "net/log/net_log_event_type.h"
28 28
29 namespace content { 29 namespace content {
30 30
31 BaseFile::BaseFile(const net::NetLogWithSource& net_log) : net_log_(net_log) {} 31 BaseFile::BaseFile(const net::NetLogWithSource& net_log) : net_log_(net_log) {}
32 32
33 BaseFile::~BaseFile() { 33 BaseFile::~BaseFile() {
34 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 34 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
35 if (detached_) 35 if (detached_)
36 Close(); 36 Close();
37 else 37 else
38 Cancel(); // Will delete the file. 38 Cancel(); // Will delete the file.
39 } 39 }
40 40
41 DownloadInterruptReason BaseFile::Initialize( 41 DownloadInterruptReason BaseFile::Initialize(
42 const base::FilePath& full_path, 42 const base::FilePath& full_path,
43 const base::FilePath& default_directory, 43 const base::FilePath& default_directory,
44 base::File file, 44 base::File file,
45 int64_t bytes_so_far, 45 int64_t bytes_so_far,
46 const std::string& hash_so_far, 46 const std::string& hash_so_far,
47 std::unique_ptr<crypto::SecureHash> hash_state, 47 std::unique_ptr<crypto::SecureHash> hash_state,
48 bool is_sparse_file) { 48 bool is_sparse_file) {
49 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 49 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
50 DCHECK(!detached_); 50 DCHECK(!detached_);
51 51
52 if (full_path.empty()) { 52 if (full_path.empty()) {
53 base::FilePath initial_directory(default_directory); 53 base::FilePath initial_directory(default_directory);
54 base::FilePath temp_file; 54 base::FilePath temp_file;
55 if (initial_directory.empty()) { 55 if (initial_directory.empty()) {
56 initial_directory = 56 initial_directory =
57 GetContentClient()->browser()->GetDefaultDownloadDirectory(); 57 GetContentClient()->browser()->GetDefaultDownloadDirectory();
58 } 58 }
59 // |initial_directory| can still be empty if ContentBrowserClient returned 59 // |initial_directory| can still be empty if ContentBrowserClient returned
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_WRITTEN, 120 net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_WRITTEN,
121 net::NetLog::Int64Callback("bytes", data_len)); 121 net::NetLog::Int64Callback("bytes", data_len));
122 122
123 if (secure_hash_) 123 if (secure_hash_)
124 secure_hash_->Update(data, data_len); 124 secure_hash_->Update(data, data_len);
125 125
126 return DOWNLOAD_INTERRUPT_REASON_NONE; 126 return DOWNLOAD_INTERRUPT_REASON_NONE;
127 } 127 }
128 128
129 DownloadInterruptReason BaseFile::Rename(const base::FilePath& new_path) { 129 DownloadInterruptReason BaseFile::Rename(const base::FilePath& new_path) {
130 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 130 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
131 DownloadInterruptReason rename_result = DOWNLOAD_INTERRUPT_REASON_NONE; 131 DownloadInterruptReason rename_result = DOWNLOAD_INTERRUPT_REASON_NONE;
132 132
133 // If the new path is same as the old one, there is no need to perform the 133 // If the new path is same as the old one, there is no need to perform the
134 // following renaming logic. 134 // following renaming logic.
135 if (new_path == full_path_) 135 if (new_path == full_path_)
136 return DOWNLOAD_INTERRUPT_REASON_NONE; 136 return DOWNLOAD_INTERRUPT_REASON_NONE;
137 137
138 // Save the information whether the download is in progress because 138 // Save the information whether the download is in progress because
139 // it will be overwritten by closing the file. 139 // it will be overwritten by closing the file.
140 bool was_in_progress = in_progress(); 140 bool was_in_progress = in_progress();
(...skipping 24 matching lines...) Expand all
165 return rename_result == DOWNLOAD_INTERRUPT_REASON_NONE ? open_result 165 return rename_result == DOWNLOAD_INTERRUPT_REASON_NONE ? open_result
166 : rename_result; 166 : rename_result;
167 } 167 }
168 168
169 void BaseFile::Detach() { 169 void BaseFile::Detach() {
170 detached_ = true; 170 detached_ = true;
171 net_log_.AddEvent(net::NetLogEventType::DOWNLOAD_FILE_DETACHED); 171 net_log_.AddEvent(net::NetLogEventType::DOWNLOAD_FILE_DETACHED);
172 } 172 }
173 173
174 void BaseFile::Cancel() { 174 void BaseFile::Cancel() {
175 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 175 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
176 DCHECK(!detached_); 176 DCHECK(!detached_);
177 177
178 net_log_.AddEvent(net::NetLogEventType::CANCELLED); 178 net_log_.AddEvent(net::NetLogEventType::CANCELLED);
179 179
180 Close(); 180 Close();
181 181
182 if (!full_path_.empty()) { 182 if (!full_path_.empty()) {
183 net_log_.AddEvent(net::NetLogEventType::DOWNLOAD_FILE_DELETED); 183 net_log_.AddEvent(net::NetLogEventType::DOWNLOAD_FILE_DELETED);
184 base::DeleteFile(full_path_, false); 184 base::DeleteFile(full_path_, false);
185 } 185 }
186 186
187 Detach(); 187 Detach();
188 } 188 }
189 189
190 std::unique_ptr<crypto::SecureHash> BaseFile::Finish() { 190 std::unique_ptr<crypto::SecureHash> BaseFile::Finish() {
191 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 191 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
192 192
193 // TODO(qinmin): verify that all the holes have been filled. 193 // TODO(qinmin): verify that all the holes have been filled.
194 if (is_sparse_file_) 194 if (is_sparse_file_)
195 CalculatePartialHash(std::string()); 195 CalculatePartialHash(std::string());
196 Close(); 196 Close();
197 return std::move(secure_hash_); 197 return std::move(secure_hash_);
198 } 198 }
199 199
200 std::string BaseFile::DebugString() const { 200 std::string BaseFile::DebugString() const {
201 return base::StringPrintf( 201 return base::StringPrintf(
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 return LogInterruptReason("Verifying prefix hash", 269 return LogInterruptReason("Verifying prefix hash",
270 0, 270 0,
271 DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); 271 DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH);
272 } 272 }
273 } 273 }
274 274
275 return DOWNLOAD_INTERRUPT_REASON_NONE; 275 return DOWNLOAD_INTERRUPT_REASON_NONE;
276 } 276 }
277 277
278 DownloadInterruptReason BaseFile::Open(const std::string& hash_so_far) { 278 DownloadInterruptReason BaseFile::Open(const std::string& hash_so_far) {
279 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 279 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
280 DCHECK(!detached_); 280 DCHECK(!detached_);
281 DCHECK(!full_path_.empty()); 281 DCHECK(!full_path_.empty());
282 282
283 // Create a new file if it is not provided. 283 // Create a new file if it is not provided.
284 if (!file_.IsValid()) { 284 if (!file_.IsValid()) {
285 file_.Initialize(full_path_, 285 file_.Initialize(full_path_,
286 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE | 286 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE |
287 base::File::FLAG_READ); 287 base::File::FLAG_READ);
288 if (!file_.IsValid()) { 288 if (!file_.IsValid()) {
289 return LogNetError("Open/Initialize File", 289 return LogNetError("Open/Initialize File",
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 // The file is shorter than we expected. Our hashes won't be valid. 332 // The file is shorter than we expected. Our hashes won't be valid.
333 ClearFile(); 333 ClearFile();
334 return LogInterruptReason("Unable to seek to last written point", 0, 334 return LogInterruptReason("Unable to seek to last written point", 0,
335 DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); 335 DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT);
336 } 336 }
337 337
338 return DOWNLOAD_INTERRUPT_REASON_NONE; 338 return DOWNLOAD_INTERRUPT_REASON_NONE;
339 } 339 }
340 340
341 void BaseFile::Close() { 341 void BaseFile::Close() {
342 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 342 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
343 343
344 if (file_.IsValid()) { 344 if (file_.IsValid()) {
345 // Currently we don't really care about the return value, since if it fails 345 // Currently we don't really care about the return value, since if it fails
346 // theres not much we can do. But we might in the future. 346 // theres not much we can do. But we might in the future.
347 file_.Flush(); 347 file_.Flush();
348 ClearFile(); 348 ClearFile();
349 } 349 }
350 } 350 }
351 351
352 void BaseFile::ClearFile() { 352 void BaseFile::ClearFile() {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 419
420 return GURL(); 420 return GURL();
421 } 421 }
422 422
423 } // namespace 423 } // namespace
424 424
425 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation( 425 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation(
426 const std::string& client_guid, 426 const std::string& client_guid,
427 const GURL& source_url, 427 const GURL& source_url,
428 const GURL& referrer_url) { 428 const GURL& referrer_url) {
429 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 429 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
430 DCHECK(!detached_); 430 DCHECK(!detached_);
431 DCHECK(!full_path_.empty()); 431 DCHECK(!full_path_.empty());
432 432
433 net_log_.BeginEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED); 433 net_log_.BeginEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED);
434 QuarantineFileResult result = QuarantineFile( 434 QuarantineFileResult result = QuarantineFile(
435 full_path_, GetEffectiveAuthorityURL(source_url, referrer_url), 435 full_path_, GetEffectiveAuthorityURL(source_url, referrer_url),
436 referrer_url, client_guid); 436 referrer_url, client_guid);
437 net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED); 437 net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED);
438 switch (result) { 438 switch (result) {
439 case QuarantineFileResult::OK: 439 case QuarantineFileResult::OK:
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 #else // !OS_WIN && !OS_MACOSX && !OS_LINUX 473 #else // !OS_WIN && !OS_MACOSX && !OS_LINUX
474 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation( 474 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation(
475 const std::string& client_guid, 475 const std::string& client_guid,
476 const GURL& source_url, 476 const GURL& source_url,
477 const GURL& referrer_url) { 477 const GURL& referrer_url) {
478 return DOWNLOAD_INTERRUPT_REASON_NONE; 478 return DOWNLOAD_INTERRUPT_REASON_NONE;
479 } 479 }
480 #endif 480 #endif
481 481
482 } // namespace content 482 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698