OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_item_impl.h" | 5 #include "content/browser/download/download_item_impl.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/format_macros.h" | 12 #include "base/format_macros.h" |
13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
14 #include "base/i18n/string_search.h" | 14 #include "base/i18n/string_search.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
16 #include "base/metrics/histogram.h" | 16 #include "base/metrics/histogram.h" |
17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
18 #include "base/utf_string_conversions.h" | 18 #include "base/utf_string_conversions.h" |
19 #include "content/browser/download/download_create_info.h" | 19 #include "content/browser/download/download_create_info.h" |
20 #include "content/browser/download/download_file.h" | 20 #include "content/browser/download/download_file.h" |
21 #include "content/browser/download/download_file_manager.h" | 21 #include "content/browser/download/download_file_manager.h" |
22 #include "content/browser/download/download_id.h" | 22 #include "content/browser/download/download_id.h" |
23 #include "content/browser/download/download_manager.h" | |
24 #include "content/browser/download/download_persistent_store_info.h" | 23 #include "content/browser/download/download_persistent_store_info.h" |
25 #include "content/browser/download/download_request_handle.h" | 24 #include "content/browser/download/download_request_handle.h" |
26 #include "content/browser/download/download_stats.h" | 25 #include "content/browser/download/download_stats.h" |
27 #include "content/browser/download/interrupt_reasons.h" | 26 #include "content/browser/download/interrupt_reasons.h" |
28 #include "content/browser/tab_contents/tab_contents.h" | 27 #include "content/browser/tab_contents/tab_contents.h" |
29 #include "content/public/browser/browser_thread.h" | 28 #include "content/public/browser/browser_thread.h" |
30 #include "content/public/browser/content_browser_client.h" | 29 #include "content/public/browser/content_browser_client.h" |
31 #include "content/public/browser/download_manager_delegate.h" | |
32 #include "net/base/net_util.h" | 30 #include "net/base/net_util.h" |
33 | 31 |
34 using content::BrowserThread; | 32 using content::BrowserThread; |
35 | 33 |
36 // A DownloadItem normally goes through the following states: | 34 // A DownloadItem normally goes through the following states: |
37 // * Created (when download starts) | 35 // * Created (when download starts) |
38 // * Made visible to consumers (e.g. Javascript) after the | 36 // * Made visible to consumers (e.g. Javascript) after the |
39 // destination file has been determined. | 37 // destination file has been determined. |
40 // * Entered into the history database. | 38 // * Entered into the history database. |
41 // * Made visible in the download shelf. | 39 // * Made visible in the download shelf. |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
111 virtual void PauseRequest() const OVERRIDE {} | 109 virtual void PauseRequest() const OVERRIDE {} |
112 virtual void ResumeRequest() const OVERRIDE {} | 110 virtual void ResumeRequest() const OVERRIDE {} |
113 virtual void CancelRequest() const OVERRIDE {} | 111 virtual void CancelRequest() const OVERRIDE {} |
114 virtual std::string DebugString() const OVERRIDE { | 112 virtual std::string DebugString() const OVERRIDE { |
115 return "Null DownloadRequestHandle"; | 113 return "Null DownloadRequestHandle"; |
116 } | 114 } |
117 }; | 115 }; |
118 | 116 |
119 } // namespace | 117 } // namespace |
120 | 118 |
| 119 // Infrastructure in DownloadItemImpl::Delegate to assert invariant that |
| 120 // delegate always outlives all attached DownloadItemImpls. |
| 121 DownloadItemImpl::Delegate::Delegate() |
| 122 : count_(0) {} |
| 123 |
| 124 DownloadItemImpl::Delegate::~Delegate() { |
| 125 DCHECK_EQ(0, count_); |
| 126 } |
| 127 |
| 128 void DownloadItemImpl::Delegate::Attach() { |
| 129 ++count_; |
| 130 } |
| 131 |
| 132 void DownloadItemImpl::Delegate::Detach() { |
| 133 DCHECK_LT(0, count_); |
| 134 --count_; |
| 135 } |
| 136 |
121 // Our download table ID starts at 1, so we use 0 to represent a download that | 137 // Our download table ID starts at 1, so we use 0 to represent a download that |
122 // has started, but has not yet had its data persisted in the table. We use fake | 138 // has started, but has not yet had its data persisted in the table. We use fake |
123 // database handles in incognito mode starting at -1 and progressively getting | 139 // database handles in incognito mode starting at -1 and progressively getting |
124 // more negative. | 140 // more negative. |
125 | 141 |
126 // Constructor for reading from the history service. | 142 // Constructor for reading from the history service. |
127 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, | 143 DownloadItemImpl::DownloadItemImpl(Delegate* delegate, |
| 144 DownloadId download_id, |
128 const DownloadPersistentStoreInfo& info) | 145 const DownloadPersistentStoreInfo& info) |
129 : download_id_(download_manager->GetNextId()), | 146 : download_id_(download_id), |
130 full_path_(info.path), | 147 full_path_(info.path), |
131 url_chain_(1, info.url), | 148 url_chain_(1, info.url), |
132 referrer_url_(info.referrer_url), | 149 referrer_url_(info.referrer_url), |
133 total_bytes_(info.total_bytes), | 150 total_bytes_(info.total_bytes), |
134 received_bytes_(info.received_bytes), | 151 received_bytes_(info.received_bytes), |
135 bytes_per_sec_(0), | 152 bytes_per_sec_(0), |
136 start_tick_(base::TimeTicks()), | 153 start_tick_(base::TimeTicks()), |
137 state_(static_cast<DownloadState>(info.state)), | 154 state_(static_cast<DownloadState>(info.state)), |
138 start_time_(info.start_time), | 155 start_time_(info.start_time), |
139 end_time_(info.end_time), | 156 end_time_(info.end_time), |
140 db_handle_(info.db_handle), | 157 db_handle_(info.db_handle), |
141 download_manager_(download_manager), | 158 delegate_(delegate), |
142 is_paused_(false), | 159 is_paused_(false), |
143 open_when_complete_(false), | 160 open_when_complete_(false), |
144 file_externally_removed_(false), | 161 file_externally_removed_(false), |
145 safety_state_(SAFE), | 162 safety_state_(SAFE), |
146 auto_opened_(false), | 163 auto_opened_(false), |
147 is_otr_(false), | 164 is_otr_(false), |
148 is_temporary_(false), | 165 is_temporary_(false), |
149 all_data_saved_(false), | 166 all_data_saved_(false), |
150 opened_(info.opened), | 167 opened_(info.opened), |
151 open_enabled_(true), | 168 open_enabled_(true), |
152 delegate_delayed_complete_(false) { | 169 delegate_delayed_complete_(false) { |
| 170 delegate_->Attach(); |
153 if (IsInProgress()) | 171 if (IsInProgress()) |
154 state_ = CANCELLED; | 172 state_ = CANCELLED; |
155 if (IsComplete()) | 173 if (IsComplete()) |
156 all_data_saved_ = true; | 174 all_data_saved_ = true; |
157 Init(false /* not actively downloading */); | 175 Init(false /* not actively downloading */); |
158 } | 176 } |
159 | 177 |
160 // Constructing for a regular download: | 178 // Constructing for a regular download: |
161 DownloadItemImpl::DownloadItemImpl( | 179 DownloadItemImpl::DownloadItemImpl( |
162 DownloadManager* download_manager, | 180 Delegate* delegate, |
163 const DownloadCreateInfo& info, | 181 const DownloadCreateInfo& info, |
164 DownloadRequestHandleInterface* request_handle, | 182 DownloadRequestHandleInterface* request_handle, |
165 bool is_otr) | 183 bool is_otr) |
166 : state_info_(info.original_name, info.save_info.file_path, | 184 : state_info_(info.original_name, info.save_info.file_path, |
167 info.has_user_gesture, info.transition_type, | 185 info.has_user_gesture, info.transition_type, |
168 info.prompt_user_for_save_location, info.path_uniquifier, | 186 info.prompt_user_for_save_location, info.path_uniquifier, |
169 DownloadStateInfo::NOT_DANGEROUS), | 187 DownloadStateInfo::NOT_DANGEROUS), |
170 request_handle_(request_handle), | 188 request_handle_(request_handle), |
171 download_id_(info.download_id), | 189 download_id_(info.download_id), |
172 full_path_(info.path), | 190 full_path_(info.path), |
173 url_chain_(info.url_chain), | 191 url_chain_(info.url_chain), |
174 referrer_url_(info.referrer_url), | 192 referrer_url_(info.referrer_url), |
175 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), | 193 suggested_filename_(UTF16ToUTF8(info.save_info.suggested_name)), |
176 content_disposition_(info.content_disposition), | 194 content_disposition_(info.content_disposition), |
177 mime_type_(info.mime_type), | 195 mime_type_(info.mime_type), |
178 original_mime_type_(info.original_mime_type), | 196 original_mime_type_(info.original_mime_type), |
179 referrer_charset_(info.referrer_charset), | 197 referrer_charset_(info.referrer_charset), |
180 total_bytes_(info.total_bytes), | 198 total_bytes_(info.total_bytes), |
181 received_bytes_(0), | 199 received_bytes_(0), |
182 bytes_per_sec_(0), | 200 bytes_per_sec_(0), |
183 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 201 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
184 start_tick_(base::TimeTicks::Now()), | 202 start_tick_(base::TimeTicks::Now()), |
185 state_(IN_PROGRESS), | 203 state_(IN_PROGRESS), |
186 start_time_(info.start_time), | 204 start_time_(info.start_time), |
187 db_handle_(DownloadItem::kUninitializedHandle), | 205 db_handle_(DownloadItem::kUninitializedHandle), |
188 download_manager_(download_manager), | 206 delegate_(delegate), |
189 is_paused_(false), | 207 is_paused_(false), |
190 open_when_complete_(false), | 208 open_when_complete_(false), |
191 file_externally_removed_(false), | 209 file_externally_removed_(false), |
192 safety_state_(SAFE), | 210 safety_state_(SAFE), |
193 auto_opened_(false), | 211 auto_opened_(false), |
194 is_otr_(is_otr), | 212 is_otr_(is_otr), |
195 is_temporary_(!info.save_info.file_path.empty()), | 213 is_temporary_(!info.save_info.file_path.empty()), |
196 all_data_saved_(false), | 214 all_data_saved_(false), |
197 opened_(false), | 215 opened_(false), |
198 open_enabled_(true), | 216 open_enabled_(true), |
199 delegate_delayed_complete_(false) { | 217 delegate_delayed_complete_(false) { |
| 218 delegate_->Attach(); |
200 Init(true /* actively downloading */); | 219 Init(true /* actively downloading */); |
201 } | 220 } |
202 | 221 |
203 // Constructing for the "Save Page As..." feature: | 222 // Constructing for the "Save Page As..." feature: |
204 DownloadItemImpl::DownloadItemImpl(DownloadManager* download_manager, | 223 DownloadItemImpl::DownloadItemImpl(Delegate* delegate, |
205 const FilePath& path, | 224 const FilePath& path, |
206 const GURL& url, | 225 const GURL& url, |
207 bool is_otr, | 226 bool is_otr, |
208 DownloadId download_id) | 227 DownloadId download_id) |
209 : request_handle_(new NullDownloadRequestHandle()), | 228 : request_handle_(new NullDownloadRequestHandle()), |
210 download_id_(download_id), | 229 download_id_(download_id), |
211 full_path_(path), | 230 full_path_(path), |
212 url_chain_(1, url), | 231 url_chain_(1, url), |
213 referrer_url_(GURL()), | 232 referrer_url_(GURL()), |
214 total_bytes_(0), | 233 total_bytes_(0), |
215 received_bytes_(0), | 234 received_bytes_(0), |
216 bytes_per_sec_(0), | 235 bytes_per_sec_(0), |
217 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), | 236 last_reason_(DOWNLOAD_INTERRUPT_REASON_NONE), |
218 start_tick_(base::TimeTicks::Now()), | 237 start_tick_(base::TimeTicks::Now()), |
219 state_(IN_PROGRESS), | 238 state_(IN_PROGRESS), |
220 start_time_(base::Time::Now()), | 239 start_time_(base::Time::Now()), |
221 db_handle_(DownloadItem::kUninitializedHandle), | 240 db_handle_(DownloadItem::kUninitializedHandle), |
222 download_manager_(download_manager), | 241 delegate_(delegate), |
223 is_paused_(false), | 242 is_paused_(false), |
224 open_when_complete_(false), | 243 open_when_complete_(false), |
225 file_externally_removed_(false), | 244 file_externally_removed_(false), |
226 safety_state_(SAFE), | 245 safety_state_(SAFE), |
227 auto_opened_(false), | 246 auto_opened_(false), |
228 is_otr_(is_otr), | 247 is_otr_(is_otr), |
229 is_temporary_(false), | 248 is_temporary_(false), |
230 all_data_saved_(false), | 249 all_data_saved_(false), |
231 opened_(false), | 250 opened_(false), |
232 open_enabled_(true), | 251 open_enabled_(true), |
233 delegate_delayed_complete_(false) { | 252 delegate_delayed_complete_(false) { |
| 253 delegate_->Attach(); |
234 Init(true /* actively downloading */); | 254 Init(true /* actively downloading */); |
235 } | 255 } |
236 | 256 |
237 DownloadItemImpl::~DownloadItemImpl() { | 257 DownloadItemImpl::~DownloadItemImpl() { |
238 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 258 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
239 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 259 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
240 | 260 |
241 TransitionTo(REMOVING); | 261 TransitionTo(REMOVING); |
242 download_manager_->AssertQueueStateConsistent(this); | 262 delegate_->AssertStateConsistent(this); |
| 263 delegate_->Detach(); |
243 } | 264 } |
244 | 265 |
245 void DownloadItemImpl::AddObserver(Observer* observer) { | 266 void DownloadItemImpl::AddObserver(Observer* observer) { |
246 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 267 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
247 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 268 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
248 | 269 |
249 observers_.AddObserver(observer); | 270 observers_.AddObserver(observer); |
250 } | 271 } |
251 | 272 |
252 void DownloadItemImpl::RemoveObserver(Observer* observer) { | 273 void DownloadItemImpl::RemoveObserver(Observer* observer) { |
(...skipping 12 matching lines...) Expand all Loading... |
265 | 286 |
266 bool DownloadItemImpl::CanShowInFolder() { | 287 bool DownloadItemImpl::CanShowInFolder() { |
267 return !IsCancelled() && !file_externally_removed_; | 288 return !IsCancelled() && !file_externally_removed_; |
268 } | 289 } |
269 | 290 |
270 bool DownloadItemImpl::CanOpenDownload() { | 291 bool DownloadItemImpl::CanOpenDownload() { |
271 return !file_externally_removed_; | 292 return !file_externally_removed_; |
272 } | 293 } |
273 | 294 |
274 bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() { | 295 bool DownloadItemImpl::ShouldOpenFileBasedOnExtension() { |
275 return download_manager_->delegate()->ShouldOpenFileBasedOnExtension( | 296 return delegate_->ShouldOpenFileBasedOnExtension(GetUserVerifiedFilePath()); |
276 GetUserVerifiedFilePath()); | |
277 } | 297 } |
278 | 298 |
279 void DownloadItemImpl::OpenDownload() { | 299 void DownloadItemImpl::OpenDownload() { |
280 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 300 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
281 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 301 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
282 | 302 |
283 if (IsPartialDownload()) { | 303 if (IsPartialDownload()) { |
284 open_when_complete_ = !open_when_complete_; | 304 open_when_complete_ = !open_when_complete_; |
285 return; | 305 return; |
286 } | 306 } |
287 | 307 |
288 if (!IsComplete() || file_externally_removed_) | 308 if (!IsComplete() || file_externally_removed_) |
289 return; | 309 return; |
290 | 310 |
291 // Ideally, we want to detect errors in opening and report them, but we | 311 // Ideally, we want to detect errors in opening and report them, but we |
292 // don't generally have the proper interface for that to the external | 312 // don't generally have the proper interface for that to the external |
293 // program that opens the file. So instead we spawn a check to update | 313 // program that opens the file. So instead we spawn a check to update |
294 // the UI if the file has been deleted in parallel with the open. | 314 // the UI if the file has been deleted in parallel with the open. |
295 download_manager_->CheckForFileRemoval(this); | 315 delegate_->CheckForFileRemoval(this); |
296 download_stats::RecordOpen(GetEndTime(), !GetOpened()); | 316 download_stats::RecordOpen(GetEndTime(), !GetOpened()); |
297 opened_ = true; | 317 opened_ = true; |
298 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); | 318 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadOpened(this)); |
299 download_manager_->MarkDownloadOpened(this); | 319 delegate_->DownloadOpened(this); |
300 | 320 |
301 // For testing: If download opening is disabled on this item, | 321 // For testing: If download opening is disabled on this item, |
302 // make the rest of the routine a no-op. | 322 // make the rest of the routine a no-op. |
303 if (!open_enabled_) | 323 if (!open_enabled_) |
304 return; | 324 return; |
305 | 325 |
306 content::GetContentClient()->browser()->OpenItem(GetFullPath()); | 326 content::GetContentClient()->browser()->OpenItem(GetFullPath()); |
307 } | 327 } |
308 | 328 |
309 void DownloadItemImpl::ShowDownloadInShell() { | 329 void DownloadItemImpl::ShowDownloadInShell() { |
310 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 330 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
311 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 331 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
312 | 332 |
313 content::GetContentClient()->browser()->ShowItemInFolder(GetFullPath()); | 333 content::GetContentClient()->browser()->ShowItemInFolder(GetFullPath()); |
314 } | 334 } |
315 | 335 |
316 void DownloadItemImpl::DangerousDownloadValidated() { | 336 void DownloadItemImpl::DangerousDownloadValidated() { |
317 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 337 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
318 DCHECK_EQ(DANGEROUS, GetSafetyState()); | 338 DCHECK_EQ(DANGEROUS, GetSafetyState()); |
319 | 339 |
320 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", | 340 UMA_HISTOGRAM_ENUMERATION("Download.DangerousDownloadValidated", |
321 GetDangerType(), | 341 GetDangerType(), |
322 DownloadStateInfo::DANGEROUS_TYPE_MAX); | 342 DownloadStateInfo::DANGEROUS_TYPE_MAX); |
323 | 343 |
324 safety_state_ = DANGEROUS_BUT_VALIDATED; | 344 safety_state_ = DANGEROUS_BUT_VALIDATED; |
325 UpdateObservers(); | 345 UpdateObservers(); |
326 | 346 |
327 download_manager_->MaybeCompleteDownload(this); | 347 delegate_->MaybeCompleteDownload(this); |
328 } | 348 } |
329 | 349 |
330 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { | 350 void DownloadItemImpl::UpdateSize(int64 bytes_so_far) { |
331 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 351 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
332 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 352 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
333 | 353 |
334 received_bytes_ = bytes_so_far; | 354 received_bytes_ = bytes_so_far; |
335 | 355 |
336 // If we've received more data than we were expecting (bad server info?), | 356 // If we've received more data than we were expecting (bad server info?), |
337 // revert to 'unknown size mode'. | 357 // revert to 'unknown size mode'. |
(...skipping 30 matching lines...) Expand all Loading... |
368 if (!IsPartialDownload()) { | 388 if (!IsPartialDownload()) { |
369 // Small downloads might be complete before this method has | 389 // Small downloads might be complete before this method has |
370 // a chance to run. | 390 // a chance to run. |
371 return; | 391 return; |
372 } | 392 } |
373 | 393 |
374 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); | 394 download_stats::RecordDownloadCount(download_stats::CANCELLED_COUNT); |
375 | 395 |
376 TransitionTo(CANCELLED); | 396 TransitionTo(CANCELLED); |
377 if (user_cancel) | 397 if (user_cancel) |
378 download_manager_->DownloadCancelledInternal(this); | 398 delegate_->DownloadCancelled(this); |
379 } | 399 } |
380 | 400 |
381 void DownloadItemImpl::MarkAsComplete() { | 401 void DownloadItemImpl::MarkAsComplete() { |
382 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 402 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
383 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 403 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
384 | 404 |
385 DCHECK(all_data_saved_); | 405 DCHECK(all_data_saved_); |
386 end_time_ = base::Time::Now(); | 406 end_time_ = base::Time::Now(); |
387 TransitionTo(COMPLETE); | 407 TransitionTo(COMPLETE); |
388 } | 408 } |
(...skipping 12 matching lines...) Expand all Loading... |
401 all_data_saved_ = true; | 421 all_data_saved_ = true; |
402 UpdateSize(size); | 422 UpdateSize(size); |
403 hash_ = final_hash; | 423 hash_ = final_hash; |
404 } | 424 } |
405 | 425 |
406 void DownloadItemImpl::OnDownloadedFileRemoved() { | 426 void DownloadItemImpl::OnDownloadedFileRemoved() { |
407 file_externally_removed_ = true; | 427 file_externally_removed_ = true; |
408 UpdateObservers(); | 428 UpdateObservers(); |
409 } | 429 } |
410 | 430 |
| 431 void DownloadItemImpl::MaybeCompleteDownload() { |
| 432 // TODO(rdsmith): Move logic for this function here. |
| 433 delegate_->MaybeCompleteDownload(this); |
| 434 } |
| 435 |
411 void DownloadItemImpl::Completed() { | 436 void DownloadItemImpl::Completed() { |
412 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 437 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
413 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 438 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
414 | 439 |
415 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); | 440 VLOG(20) << __FUNCTION__ << "() " << DebugString(false); |
416 | 441 |
417 DCHECK(all_data_saved_); | 442 DCHECK(all_data_saved_); |
418 end_time_ = base::Time::Now(); | 443 end_time_ = base::Time::Now(); |
419 TransitionTo(COMPLETE); | 444 TransitionTo(COMPLETE); |
420 download_manager_->DownloadCompleted(GetId()); | 445 delegate_->DownloadCompleted(this); |
421 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); | 446 download_stats::RecordDownloadCompleted(start_tick_, received_bytes_); |
422 | 447 |
423 if (auto_opened_) { | 448 if (auto_opened_) { |
424 // If it was already handled by the delegate, do nothing. | 449 // If it was already handled by the delegate, do nothing. |
425 } else if (GetOpenWhenComplete() || | 450 } else if (GetOpenWhenComplete() || |
426 ShouldOpenFileBasedOnExtension() || | 451 ShouldOpenFileBasedOnExtension() || |
427 IsTemporary()) { | 452 IsTemporary()) { |
428 // If the download is temporary, like in drag-and-drop, do not open it but | 453 // If the download is temporary, like in drag-and-drop, do not open it but |
429 // we still need to set it auto-opened so that it can be removed from the | 454 // we still need to set it auto-opened so that it can be removed from the |
430 // download shelf. | 455 // download shelf. |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
496 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 521 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
497 base::Bind(&DeleteDownloadedFile, full_path_)); | 522 base::Bind(&DeleteDownloadedFile, full_path_)); |
498 Remove(); | 523 Remove(); |
499 // We have now been deleted. | 524 // We have now been deleted. |
500 } | 525 } |
501 | 526 |
502 void DownloadItemImpl::Remove() { | 527 void DownloadItemImpl::Remove() { |
503 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 528 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
504 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 529 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
505 | 530 |
506 download_manager_->AssertQueueStateConsistent(this); | 531 delegate_->AssertStateConsistent(this); |
507 Cancel(true); | 532 Cancel(true); |
508 download_manager_->AssertQueueStateConsistent(this); | 533 delegate_->AssertStateConsistent(this); |
509 | 534 |
510 TransitionTo(REMOVING); | 535 TransitionTo(REMOVING); |
511 download_manager_->RemoveDownload(db_handle_); | 536 delegate_->DownloadRemoved(this); |
512 // We have now been deleted. | 537 // We have now been deleted. |
513 } | 538 } |
514 | 539 |
515 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { | 540 bool DownloadItemImpl::TimeRemaining(base::TimeDelta* remaining) const { |
516 if (total_bytes_ <= 0) | 541 if (total_bytes_ <= 0) |
517 return false; // We never received the content_length for this download. | 542 return false; // We never received the content_length for this download. |
518 | 543 |
519 int64 speed = CurrentSpeed(); | 544 int64 speed = CurrentSpeed(); |
520 if (speed == 0) | 545 if (speed == 0) |
521 return false; | 546 return false; |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
575 | 600 |
576 VLOG(20) << __FUNCTION__ << "()" | 601 VLOG(20) << __FUNCTION__ << "()" |
577 << " needs rename = " << NeedsRename() | 602 << " needs rename = " << NeedsRename() |
578 << " " << DebugString(true); | 603 << " " << DebugString(true); |
579 DCHECK_NE(DANGEROUS, GetSafetyState()); | 604 DCHECK_NE(DANGEROUS, GetSafetyState()); |
580 DCHECK(file_manager); | 605 DCHECK(file_manager); |
581 | 606 |
582 if (NeedsRename()) { | 607 if (NeedsRename()) { |
583 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 608 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, |
584 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, | 609 base::Bind(&DownloadFileManager::RenameCompletingDownloadFile, |
585 file_manager, GetGlobalId(), | 610 file_manager, download_id_, |
586 GetTargetFilePath(), GetSafetyState() == SAFE)); | 611 GetTargetFilePath(), GetSafetyState() == SAFE)); |
587 return; | 612 return; |
588 } | 613 } |
589 | 614 |
590 Completed(); | 615 Completed(); |
591 | 616 |
592 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 617 BrowserThread::PostTask( |
593 base::Bind(&DownloadFileManager::CompleteDownload, | 618 BrowserThread::FILE, FROM_HERE, |
594 file_manager, GetGlobalId())); | 619 base::Bind(&DownloadFileManager::CompleteDownload, |
| 620 file_manager, download_id_)); |
595 } | 621 } |
596 | 622 |
597 void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { | 623 void DownloadItemImpl::OnDownloadRenamedToFinalName(const FilePath& full_path) { |
598 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 624 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
599 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 625 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
600 | 626 |
601 VLOG(20) << __FUNCTION__ << "()" | 627 VLOG(20) << __FUNCTION__ << "()" |
602 << " full_path = \"" << full_path.value() << "\"" | 628 << " full_path = \"" << full_path.value() << "\"" |
603 << " needed rename = " << NeedsRename() | 629 << " needed rename = " << NeedsRename() |
604 << " " << DebugString(false); | 630 << " " << DebugString(false); |
605 DCHECK(NeedsRename()); | 631 DCHECK(NeedsRename()); |
606 | 632 |
607 Rename(full_path); | 633 Rename(full_path); |
608 | 634 |
609 if (download_manager_->delegate()->ShouldOpenDownload(this)) { | 635 if (delegate_->ShouldOpenDownload(this)) { |
610 Completed(); | 636 Completed(); |
611 } else { | 637 } else { |
612 delegate_delayed_complete_ = true; | 638 delegate_delayed_complete_ = true; |
613 } | 639 } |
614 } | 640 } |
615 | 641 |
616 bool DownloadItemImpl::MatchesQuery(const string16& query) const { | 642 bool DownloadItemImpl::MatchesQuery(const string16& query) const { |
617 if (query.empty()) | 643 if (query.empty()) |
618 return true; | 644 return true; |
619 | 645 |
620 DCHECK_EQ(query, base::i18n::ToLower(query)); | 646 DCHECK_EQ(query, base::i18n::ToLower(query)); |
621 | 647 |
622 string16 url_raw(UTF8ToUTF16(GetURL().spec())); | 648 string16 url_raw(UTF8ToUTF16(GetURL().spec())); |
623 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw)) | 649 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_raw)) |
624 return true; | 650 return true; |
625 | 651 |
626 // TODO(phajdan.jr): write a test case for the following code. | 652 // TODO(phajdan.jr): write a test case for the following code. |
627 // A good test case would be: | 653 // A good test case would be: |
628 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", | 654 // "/\xe4\xbd\xa0\xe5\xa5\xbd\xe4\xbd\xa0\xe5\xa5\xbd", |
629 // L"/\x4f60\x597d\x4f60\x597d", | 655 // L"/\x4f60\x597d\x4f60\x597d", |
630 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" | 656 // "/%E4%BD%A0%E5%A5%BD%E4%BD%A0%E5%A5%BD" |
631 std::string languages; | 657 std::string languages; |
632 TabContents* tab = GetTabContents(); | 658 languages = content::GetContentClient()->browser()->GetAcceptLangs( |
633 if (tab) { | 659 BrowserContext()); |
634 languages = content::GetContentClient()->browser()->GetAcceptLangs( | |
635 tab->browser_context()); | |
636 } | |
637 string16 url_formatted(net::FormatUrl(GetURL(), languages)); | 660 string16 url_formatted(net::FormatUrl(GetURL(), languages)); |
638 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) | 661 if (base::i18n::StringSearchIgnoringCaseAndAccents(query, url_formatted)) |
639 return true; | 662 return true; |
640 | 663 |
641 string16 path(GetFullPath().LossyDisplayName()); | 664 string16 path(GetFullPath().LossyDisplayName()); |
642 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path); | 665 return base::i18n::StringSearchIgnoringCaseAndAccents(query, path); |
643 } | 666 } |
644 | 667 |
645 void DownloadItemImpl::SetFileCheckResults(const DownloadStateInfo& state) { | 668 void DownloadItemImpl::SetFileCheckResults(const DownloadStateInfo& state) { |
646 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 669 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
698 TabContents* DownloadItemImpl::GetTabContents() const { | 721 TabContents* DownloadItemImpl::GetTabContents() const { |
699 // TODO(rdsmith): Remove null check after removing GetTabContents() from | 722 // TODO(rdsmith): Remove null check after removing GetTabContents() from |
700 // paths that might be used by DownloadItems created from history import. | 723 // paths that might be used by DownloadItems created from history import. |
701 // Currently such items have null request_handle_s, where other items | 724 // Currently such items have null request_handle_s, where other items |
702 // (regular and SavePackage downloads) have actual objects off the pointer. | 725 // (regular and SavePackage downloads) have actual objects off the pointer. |
703 if (request_handle_.get()) | 726 if (request_handle_.get()) |
704 return request_handle_->GetTabContents(); | 727 return request_handle_->GetTabContents(); |
705 return NULL; | 728 return NULL; |
706 } | 729 } |
707 | 730 |
| 731 content::BrowserContext* DownloadItemImpl::BrowserContext() const { |
| 732 return delegate_->BrowserContext(); |
| 733 } |
| 734 |
708 FilePath DownloadItemImpl::GetTargetFilePath() const { | 735 FilePath DownloadItemImpl::GetTargetFilePath() const { |
709 return full_path_.DirName().Append(state_info_.target_name); | 736 return full_path_.DirName().Append(state_info_.target_name); |
710 } | 737 } |
711 | 738 |
712 FilePath DownloadItemImpl::GetFileNameToReportUser() const { | 739 FilePath DownloadItemImpl::GetFileNameToReportUser() const { |
713 if (state_info_.path_uniquifier > 0) { | 740 if (state_info_.path_uniquifier > 0) { |
714 FilePath name(state_info_.target_name); | 741 FilePath name(state_info_.target_name); |
715 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); | 742 DownloadFile::AppendNumberToPath(&name, state_info_.path_uniquifier); |
716 return name; | 743 return name; |
717 } | 744 } |
718 return state_info_.target_name; | 745 return state_info_.target_name; |
719 } | 746 } |
720 | 747 |
721 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { | 748 FilePath DownloadItemImpl::GetUserVerifiedFilePath() const { |
722 return (safety_state_ == DownloadItem::SAFE) ? | 749 return (safety_state_ == DownloadItem::SAFE) ? |
723 GetTargetFilePath() : full_path_; | 750 GetTargetFilePath() : full_path_; |
724 } | 751 } |
725 | 752 |
726 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { | 753 void DownloadItemImpl::OffThreadCancel(DownloadFileManager* file_manager) { |
727 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 754 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
728 request_handle_->CancelRequest(); | 755 request_handle_->CancelRequest(); |
729 | 756 |
730 BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE, | 757 BrowserThread::PostTask( |
731 base::Bind(&DownloadFileManager::CancelDownload, | 758 BrowserThread::FILE, FROM_HERE, |
732 file_manager, GetGlobalId())); | 759 base::Bind(&DownloadFileManager::CancelDownload, |
| 760 file_manager, download_id_)); |
733 } | 761 } |
734 | 762 |
735 void DownloadItemImpl::Init(bool active) { | 763 void DownloadItemImpl::Init(bool active) { |
736 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. | 764 // TODO(rdsmith): Change to DCHECK after http://crbug.com/85408 resolved. |
737 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 765 CHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
738 | 766 |
739 UpdateTarget(); | 767 UpdateTarget(); |
740 if (active) { | 768 if (active) { |
741 download_stats::RecordDownloadCount(download_stats::START_COUNT); | 769 download_stats::RecordDownloadCount(download_stats::START_COUNT); |
742 } | 770 } |
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
854 total_bytes_ = total_bytes; | 882 total_bytes_ = total_bytes; |
855 } | 883 } |
856 const std::string& DownloadItemImpl::GetHash() const { return hash_; } | 884 const std::string& DownloadItemImpl::GetHash() const { return hash_; } |
857 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } | 885 int64 DownloadItemImpl::GetReceivedBytes() const { return received_bytes_; } |
858 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } | 886 int32 DownloadItemImpl::GetId() const { return download_id_.local(); } |
859 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } | 887 DownloadId DownloadItemImpl::GetGlobalId() const { return download_id_; } |
860 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } | 888 base::Time DownloadItemImpl::GetStartTime() const { return start_time_; } |
861 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } | 889 base::Time DownloadItemImpl::GetEndTime() const { return end_time_; } |
862 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } | 890 void DownloadItemImpl::SetDbHandle(int64 handle) { db_handle_ = handle; } |
863 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } | 891 int64 DownloadItemImpl::GetDbHandle() const { return db_handle_; } |
864 DownloadManager* DownloadItemImpl::GetDownloadManager() { | |
865 return download_manager_; | |
866 } | |
867 bool DownloadItemImpl::IsPaused() const { return is_paused_; } | 892 bool DownloadItemImpl::IsPaused() const { return is_paused_; } |
868 bool DownloadItemImpl::GetOpenWhenComplete() const { | 893 bool DownloadItemImpl::GetOpenWhenComplete() const { |
869 return open_when_complete_; | 894 return open_when_complete_; |
870 } | 895 } |
871 void DownloadItemImpl::SetOpenWhenComplete(bool open) { | 896 void DownloadItemImpl::SetOpenWhenComplete(bool open) { |
872 open_when_complete_ = open; | 897 open_when_complete_ = open; |
873 } | 898 } |
874 bool DownloadItemImpl::GetFileExternallyRemoved() const { | 899 bool DownloadItemImpl::GetFileExternallyRemoved() const { |
875 return file_externally_removed_; | 900 return file_externally_removed_; |
876 } | 901 } |
(...skipping 15 matching lines...) Expand all Loading... |
892 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } | 917 void DownloadItemImpl::SetOpened(bool opened) { opened_ = opened; } |
893 bool DownloadItemImpl::GetOpened() const { return opened_; } | 918 bool DownloadItemImpl::GetOpened() const { return opened_; } |
894 InterruptReason DownloadItemImpl::GetLastReason() const { | 919 InterruptReason DownloadItemImpl::GetLastReason() const { |
895 return last_reason_; | 920 return last_reason_; |
896 } | 921 } |
897 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } | 922 DownloadStateInfo DownloadItemImpl::GetStateInfo() const { return state_info_; } |
898 bool DownloadItemImpl::NeedsRename() const { | 923 bool DownloadItemImpl::NeedsRename() const { |
899 return state_info_.target_name != full_path_.BaseName(); | 924 return state_info_.target_name != full_path_.BaseName(); |
900 } | 925 } |
901 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } | 926 void DownloadItemImpl::MockDownloadOpenForTesting() { open_enabled_ = false; } |
OLD | NEW |