OLD | NEW |
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/download_file_impl.h" | 5 #include "content/browser/download/download_file_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
59 | 59 |
60 DownloadFileImpl::~DownloadFileImpl() { | 60 DownloadFileImpl::~DownloadFileImpl() { |
61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
62 --number_active_objects_; | 62 --number_active_objects_; |
63 } | 63 } |
64 | 64 |
65 void DownloadFileImpl::Initialize(const InitializeCallback& callback) { | 65 void DownloadFileImpl::Initialize(const InitializeCallback& callback) { |
66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 66 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
67 | 67 |
68 update_timer_.reset(new base::RepeatingTimer<DownloadFileImpl>()); | 68 update_timer_.reset(new base::RepeatingTimer<DownloadFileImpl>()); |
69 net::Error net_result = file_.Initialize(default_download_directory_); | 69 content::DownloadInterruptReason result = |
70 if (net_result != net::OK) { | 70 file_.Initialize(default_download_directory_); |
| 71 if (result != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
71 BrowserThread::PostTask( | 72 BrowserThread::PostTask( |
72 BrowserThread::UI, FROM_HERE, base::Bind( | 73 BrowserThread::UI, FROM_HERE, base::Bind(callback, result)); |
73 callback, content::ConvertNetErrorToInterruptReason( | |
74 net_result, content::DOWNLOAD_INTERRUPT_FROM_DISK))); | |
75 return; | 74 return; |
76 } | 75 } |
77 | 76 |
78 stream_reader_->RegisterCallback( | 77 stream_reader_->RegisterCallback( |
79 base::Bind(&DownloadFileImpl::StreamActive, weak_factory_.GetWeakPtr())); | 78 base::Bind(&DownloadFileImpl::StreamActive, weak_factory_.GetWeakPtr())); |
80 | 79 |
81 download_start_ = base::TimeTicks::Now(); | 80 download_start_ = base::TimeTicks::Now(); |
82 | 81 |
83 // Initial pull from the straw. | 82 // Initial pull from the straw. |
84 StreamActive(); | 83 StreamActive(); |
85 | 84 |
86 BrowserThread::PostTask( | 85 BrowserThread::PostTask( |
87 BrowserThread::UI, FROM_HERE, base::Bind( | 86 BrowserThread::UI, FROM_HERE, base::Bind( |
88 callback, content::DOWNLOAD_INTERRUPT_REASON_NONE)); | 87 callback, content::DOWNLOAD_INTERRUPT_REASON_NONE)); |
89 | 88 |
90 ++number_active_objects_; | 89 ++number_active_objects_; |
91 } | 90 } |
92 | 91 |
93 content::DownloadInterruptReason DownloadFileImpl::AppendDataToFile( | 92 content::DownloadInterruptReason DownloadFileImpl::AppendDataToFile( |
94 const char* data, size_t data_len) { | 93 const char* data, size_t data_len) { |
95 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 94 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
96 | 95 |
97 if (!update_timer_->IsRunning()) { | 96 if (!update_timer_->IsRunning()) { |
98 update_timer_->Start(FROM_HERE, | 97 update_timer_->Start(FROM_HERE, |
99 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), | 98 base::TimeDelta::FromMilliseconds(kUpdatePeriodMs), |
100 this, &DownloadFileImpl::SendUpdate); | 99 this, &DownloadFileImpl::SendUpdate); |
101 } | 100 } |
102 return content::ConvertNetErrorToInterruptReason( | 101 return file_.AppendDataToFile(data, data_len); |
103 file_.AppendDataToFile(data, data_len), | |
104 content::DOWNLOAD_INTERRUPT_FROM_DISK); | |
105 } | 102 } |
106 | 103 |
107 void DownloadFileImpl::Rename(const FilePath& full_path, | 104 void DownloadFileImpl::Rename(const FilePath& full_path, |
108 bool overwrite_existing_file, | 105 bool overwrite_existing_file, |
109 const RenameCompletionCallback& callback) { | 106 const RenameCompletionCallback& callback) { |
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); | 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
111 | 108 |
112 FilePath new_path(full_path); | 109 FilePath new_path(full_path); |
113 if (!overwrite_existing_file) { | 110 if (!overwrite_existing_file) { |
114 // Make the file unique if requested. | 111 // Make the file unique if requested. |
115 int uniquifier = | 112 int uniquifier = |
116 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); | 113 file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL("")); |
117 if (uniquifier > 0) { | 114 if (uniquifier > 0) { |
118 new_path = new_path.InsertBeforeExtensionASCII( | 115 new_path = new_path.InsertBeforeExtensionASCII( |
119 StringPrintf(" (%d)", uniquifier)); | 116 StringPrintf(" (%d)", uniquifier)); |
120 } | 117 } |
121 } | 118 } |
122 | 119 |
123 net::Error rename_error = file_.Rename(new_path); | 120 content::DownloadInterruptReason reason = file_.Rename(new_path); |
124 content::DownloadInterruptReason reason( | 121 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { |
125 content::DOWNLOAD_INTERRUPT_REASON_NONE); | |
126 if (net::OK != rename_error) { | |
127 // Make sure our information is updated, since we're about to | 122 // Make sure our information is updated, since we're about to |
128 // error out. | 123 // error out. |
129 SendUpdate(); | 124 SendUpdate(); |
130 | 125 |
131 // Null out callback so that we don't do any more stream processing. | 126 // Null out callback so that we don't do any more stream processing. |
132 stream_reader_->RegisterCallback(base::Closure()); | 127 stream_reader_->RegisterCallback(base::Closure()); |
133 | 128 |
134 reason = | |
135 content::ConvertNetErrorToInterruptReason( | |
136 rename_error, | |
137 content::DOWNLOAD_INTERRUPT_FROM_DISK); | |
138 new_path.clear(); | 129 new_path.clear(); |
139 } | 130 } |
140 | 131 |
141 BrowserThread::PostTask( | 132 BrowserThread::PostTask( |
142 BrowserThread::UI, FROM_HERE, | 133 BrowserThread::UI, FROM_HERE, |
143 base::Bind(callback, reason, new_path)); | 134 base::Bind(callback, reason, new_path)); |
144 } | 135 } |
145 | 136 |
146 void DownloadFileImpl::Detach(base::Closure callback) { | 137 void DownloadFileImpl::Detach(base::Closure callback) { |
147 // Doing the annotation here leaves a small window during | 138 // Doing the annotation here leaves a small window during |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
297 BrowserThread::UI, FROM_HERE, | 288 BrowserThread::UI, FROM_HERE, |
298 base::Bind(&content::DownloadDestinationObserver::DestinationUpdate, | 289 base::Bind(&content::DownloadDestinationObserver::DestinationUpdate, |
299 observer_, BytesSoFar(), CurrentSpeed(), GetHashState())); | 290 observer_, BytesSoFar(), CurrentSpeed(), GetHashState())); |
300 } | 291 } |
301 | 292 |
302 // static | 293 // static |
303 int content::DownloadFile::GetNumberOfDownloadFiles() { | 294 int content::DownloadFile::GetNumberOfDownloadFiles() { |
304 return number_active_objects_; | 295 return number_active_objects_; |
305 } | 296 } |
306 | 297 |
OLD | NEW |