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

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

Issue 2890853002: Downloads: replace BrowserThread::FILE with task scheduler. (Closed)
Patch Set: Add scoped COM initialization to quench a couple of assertion failures. Created 3 years, 5 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"
11 #include "base/files/file_util.h" 11 #include "base/files/file_util.h"
12 #include "base/format_macros.h" 12 #include "base/format_macros.h"
13 #include "base/logging.h" 13 #include "base/logging.h"
14 #include "base/pickle.h" 14 #include "base/pickle.h"
15 #include "base/strings/stringprintf.h" 15 #include "base/strings/stringprintf.h"
16 #include "base/threading/thread_restrictions.h" 16 #include "base/threading/thread_restrictions.h"
17 #include "build/build_config.h" 17 #include "build/build_config.h"
18 #include "content/browser/download/download_interrupt_reasons_impl.h" 18 #include "content/browser/download/download_interrupt_reasons_impl.h"
19 #include "content/browser/download/download_net_log_parameters.h" 19 #include "content/browser/download/download_net_log_parameters.h"
20 #include "content/browser/download/download_stats.h" 20 #include "content/browser/download/download_stats.h"
21 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
gab 2017/07/20 16:54:31 We can remove browser_thread.h in many of these I
Sigurður Ásgeirsson 2017/07/20 17:23:21 Done for those where the shoe fits - not that many
22 #include "content/public/browser/content_browser_client.h" 22 #include "content/public/browser/content_browser_client.h"
23 #include "content/public/common/quarantine.h" 23 #include "content/public/common/quarantine.h"
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 DETACH_FROM_SEQUENCE(sequence_checker_);
33 }
32 34
33 BaseFile::~BaseFile() { 35 BaseFile::~BaseFile() {
34 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 36 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
35 if (detached_) 37 if (detached_)
36 Close(); 38 Close();
37 else 39 else
38 Cancel(); // Will delete the file. 40 Cancel(); // Will delete the file.
39 } 41 }
40 42
41 DownloadInterruptReason BaseFile::Initialize( 43 DownloadInterruptReason BaseFile::Initialize(
42 const base::FilePath& full_path, 44 const base::FilePath& full_path,
43 const base::FilePath& default_directory, 45 const base::FilePath& default_directory,
44 base::File file, 46 base::File file,
45 int64_t bytes_so_far, 47 int64_t bytes_so_far,
46 const std::string& hash_so_far, 48 const std::string& hash_so_far,
47 std::unique_ptr<crypto::SecureHash> hash_state, 49 std::unique_ptr<crypto::SecureHash> hash_state,
48 bool is_sparse_file) { 50 bool is_sparse_file) {
49 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 51 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
50 DCHECK(!detached_); 52 DCHECK(!detached_);
51 53
52 if (full_path.empty()) { 54 if (full_path.empty()) {
53 base::FilePath initial_directory(default_directory); 55 base::FilePath initial_directory(default_directory);
54 base::FilePath temp_file; 56 base::FilePath temp_file;
55 if (initial_directory.empty()) { 57 if (initial_directory.empty()) {
56 initial_directory = 58 initial_directory =
57 GetContentClient()->browser()->GetDefaultDownloadDirectory(); 59 GetContentClient()->browser()->GetDefaultDownloadDirectory();
58 } 60 }
59 // |initial_directory| can still be empty if ContentBrowserClient returned 61 // |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, 122 net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_WRITTEN,
121 net::NetLog::Int64Callback("bytes", data_len)); 123 net::NetLog::Int64Callback("bytes", data_len));
122 124
123 if (secure_hash_) 125 if (secure_hash_)
124 secure_hash_->Update(data, data_len); 126 secure_hash_->Update(data, data_len);
125 127
126 return DOWNLOAD_INTERRUPT_REASON_NONE; 128 return DOWNLOAD_INTERRUPT_REASON_NONE;
127 } 129 }
128 130
129 DownloadInterruptReason BaseFile::Rename(const base::FilePath& new_path) { 131 DownloadInterruptReason BaseFile::Rename(const base::FilePath& new_path) {
130 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 132 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
131 DownloadInterruptReason rename_result = DOWNLOAD_INTERRUPT_REASON_NONE; 133 DownloadInterruptReason rename_result = DOWNLOAD_INTERRUPT_REASON_NONE;
132 134
133 // If the new path is same as the old one, there is no need to perform the 135 // If the new path is same as the old one, there is no need to perform the
134 // following renaming logic. 136 // following renaming logic.
135 if (new_path == full_path_) 137 if (new_path == full_path_)
136 return DOWNLOAD_INTERRUPT_REASON_NONE; 138 return DOWNLOAD_INTERRUPT_REASON_NONE;
137 139
138 // Save the information whether the download is in progress because 140 // Save the information whether the download is in progress because
139 // it will be overwritten by closing the file. 141 // it will be overwritten by closing the file.
140 bool was_in_progress = in_progress(); 142 bool was_in_progress = in_progress();
(...skipping 24 matching lines...) Expand all
165 return rename_result == DOWNLOAD_INTERRUPT_REASON_NONE ? open_result 167 return rename_result == DOWNLOAD_INTERRUPT_REASON_NONE ? open_result
166 : rename_result; 168 : rename_result;
167 } 169 }
168 170
169 void BaseFile::Detach() { 171 void BaseFile::Detach() {
170 detached_ = true; 172 detached_ = true;
171 net_log_.AddEvent(net::NetLogEventType::DOWNLOAD_FILE_DETACHED); 173 net_log_.AddEvent(net::NetLogEventType::DOWNLOAD_FILE_DETACHED);
172 } 174 }
173 175
174 void BaseFile::Cancel() { 176 void BaseFile::Cancel() {
175 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 177 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
176 DCHECK(!detached_); 178 DCHECK(!detached_);
177 179
178 net_log_.AddEvent(net::NetLogEventType::CANCELLED); 180 net_log_.AddEvent(net::NetLogEventType::CANCELLED);
179 181
180 Close(); 182 Close();
181 183
182 if (!full_path_.empty()) { 184 if (!full_path_.empty()) {
183 net_log_.AddEvent(net::NetLogEventType::DOWNLOAD_FILE_DELETED); 185 net_log_.AddEvent(net::NetLogEventType::DOWNLOAD_FILE_DELETED);
184 base::DeleteFile(full_path_, false); 186 base::DeleteFile(full_path_, false);
185 } 187 }
186 188
187 Detach(); 189 Detach();
188 } 190 }
189 191
190 std::unique_ptr<crypto::SecureHash> BaseFile::Finish() { 192 std::unique_ptr<crypto::SecureHash> BaseFile::Finish() {
191 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 193 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
192 194
193 // TODO(qinmin): verify that all the holes have been filled. 195 // TODO(qinmin): verify that all the holes have been filled.
194 if (is_sparse_file_) 196 if (is_sparse_file_)
195 CalculatePartialHash(std::string()); 197 CalculatePartialHash(std::string());
196 Close(); 198 Close();
197 return std::move(secure_hash_); 199 return std::move(secure_hash_);
198 } 200 }
199 201
200 std::string BaseFile::DebugString() const { 202 std::string BaseFile::DebugString() const {
201 return base::StringPrintf( 203 return base::StringPrintf(
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 return LogInterruptReason("Verifying prefix hash", 271 return LogInterruptReason("Verifying prefix hash",
270 0, 272 0,
271 DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH); 273 DOWNLOAD_INTERRUPT_REASON_FILE_HASH_MISMATCH);
272 } 274 }
273 } 275 }
274 276
275 return DOWNLOAD_INTERRUPT_REASON_NONE; 277 return DOWNLOAD_INTERRUPT_REASON_NONE;
276 } 278 }
277 279
278 DownloadInterruptReason BaseFile::Open(const std::string& hash_so_far) { 280 DownloadInterruptReason BaseFile::Open(const std::string& hash_so_far) {
279 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 281 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
280 DCHECK(!detached_); 282 DCHECK(!detached_);
281 DCHECK(!full_path_.empty()); 283 DCHECK(!full_path_.empty());
282 284
283 // Create a new file if it is not provided. 285 // Create a new file if it is not provided.
284 if (!file_.IsValid()) { 286 if (!file_.IsValid()) {
285 file_.Initialize(full_path_, 287 file_.Initialize(full_path_,
286 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE | 288 base::File::FLAG_OPEN_ALWAYS | base::File::FLAG_WRITE |
287 base::File::FLAG_READ); 289 base::File::FLAG_READ);
288 if (!file_.IsValid()) { 290 if (!file_.IsValid()) {
289 return LogNetError("Open/Initialize File", 291 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. 334 // The file is shorter than we expected. Our hashes won't be valid.
333 ClearFile(); 335 ClearFile();
334 return LogInterruptReason("Unable to seek to last written point", 0, 336 return LogInterruptReason("Unable to seek to last written point", 0,
335 DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT); 337 DOWNLOAD_INTERRUPT_REASON_FILE_TOO_SHORT);
336 } 338 }
337 339
338 return DOWNLOAD_INTERRUPT_REASON_NONE; 340 return DOWNLOAD_INTERRUPT_REASON_NONE;
339 } 341 }
340 342
341 void BaseFile::Close() { 343 void BaseFile::Close() {
342 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 344 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
343 345
344 if (file_.IsValid()) { 346 if (file_.IsValid()) {
345 // Currently we don't really care about the return value, since if it fails 347 // 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. 348 // theres not much we can do. But we might in the future.
347 file_.Flush(); 349 file_.Flush();
348 ClearFile(); 350 ClearFile();
349 } 351 }
350 } 352 }
351 353
352 void BaseFile::ClearFile() { 354 void BaseFile::ClearFile() {
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 421
420 return GURL(); 422 return GURL();
421 } 423 }
422 424
423 } // namespace 425 } // namespace
424 426
425 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation( 427 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation(
426 const std::string& client_guid, 428 const std::string& client_guid,
427 const GURL& source_url, 429 const GURL& source_url,
428 const GURL& referrer_url) { 430 const GURL& referrer_url) {
429 DCHECK_CURRENTLY_ON(BrowserThread::FILE); 431 DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
430 DCHECK(!detached_); 432 DCHECK(!detached_);
431 DCHECK(!full_path_.empty()); 433 DCHECK(!full_path_.empty());
432 434
433 net_log_.BeginEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED); 435 net_log_.BeginEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED);
434 QuarantineFileResult result = QuarantineFile( 436 QuarantineFileResult result = QuarantineFile(
435 full_path_, GetEffectiveAuthorityURL(source_url, referrer_url), 437 full_path_, GetEffectiveAuthorityURL(source_url, referrer_url),
436 referrer_url, client_guid); 438 referrer_url, client_guid);
437 net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED); 439 net_log_.EndEvent(net::NetLogEventType::DOWNLOAD_FILE_ANNOTATED);
438 switch (result) { 440 switch (result) {
439 case QuarantineFileResult::OK: 441 case QuarantineFileResult::OK:
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 #else // !OS_WIN && !OS_MACOSX && !OS_LINUX 475 #else // !OS_WIN && !OS_MACOSX && !OS_LINUX
474 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation( 476 DownloadInterruptReason BaseFile::AnnotateWithSourceInformation(
475 const std::string& client_guid, 477 const std::string& client_guid,
476 const GURL& source_url, 478 const GURL& source_url,
477 const GURL& referrer_url) { 479 const GURL& referrer_url) {
478 return DOWNLOAD_INTERRUPT_REASON_NONE; 480 return DOWNLOAD_INTERRUPT_REASON_NONE;
479 } 481 }
480 #endif 482 #endif
481 483
482 } // namespace content 484 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698