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_manager_impl.h" | 5 #include "content/browser/download/download_manager_impl.h" |
6 | 6 |
7 #include <iterator> | 7 #include <iterator> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/callback.h" | 10 #include "base/callback.h" |
11 #include "base/debug/alias.h" | 11 #include "base/debug/alias.h" |
12 #include "base/file_util.h" | 12 #include "base/file_util.h" |
13 #include "base/i18n/case_conversion.h" | 13 #include "base/i18n/case_conversion.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/message_loop.h" | 15 #include "base/message_loop.h" |
16 #include "base/stl_util.h" | 16 #include "base/stl_util.h" |
17 #include "base/stringprintf.h" | 17 #include "base/stringprintf.h" |
18 #include "base/supports_user_data.h" | 18 #include "base/supports_user_data.h" |
19 #include "base/synchronization/lock.h" | 19 #include "base/synchronization/lock.h" |
20 #include "base/sys_string_conversions.h" | 20 #include "base/sys_string_conversions.h" |
21 #include "build/build_config.h" | 21 #include "build/build_config.h" |
22 #include "content/browser/download/byte_stream.h" | 22 #include "content/browser/download/byte_stream.h" |
23 #include "content/browser/download/download_create_info.h" | 23 #include "content/browser/download/download_create_info.h" |
24 #include "content/browser/download/download_file_manager.h" | 24 #include "content/browser/download/download_file_factory.h" |
| 25 #include "content/browser/download/download_item_factory.h" |
25 #include "content/browser/download/download_item_impl.h" | 26 #include "content/browser/download/download_item_impl.h" |
26 #include "content/browser/download/download_stats.h" | 27 #include "content/browser/download/download_stats.h" |
27 #include "content/browser/renderer_host/render_view_host_impl.h" | 28 #include "content/browser/renderer_host/render_view_host_impl.h" |
28 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" | 29 #include "content/browser/renderer_host/resource_dispatcher_host_impl.h" |
29 #include "content/browser/web_contents/web_contents_impl.h" | 30 #include "content/browser/web_contents/web_contents_impl.h" |
30 #include "content/public/browser/browser_context.h" | 31 #include "content/public/browser/browser_context.h" |
31 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
32 #include "content/public/browser/content_browser_client.h" | 33 #include "content/public/browser/content_browser_client.h" |
33 #include "content/public/browser/download_interrupt_reasons.h" | 34 #include "content/public/browser/download_interrupt_reasons.h" |
34 #include "content/public/browser/download_manager_delegate.h" | 35 #include "content/public/browser/download_manager_delegate.h" |
(...skipping 11 matching lines...) Expand all Loading... |
46 | 47 |
47 using content::BrowserThread; | 48 using content::BrowserThread; |
48 using content::DownloadId; | 49 using content::DownloadId; |
49 using content::DownloadItem; | 50 using content::DownloadItem; |
50 using content::DownloadPersistentStoreInfo; | 51 using content::DownloadPersistentStoreInfo; |
51 using content::ResourceDispatcherHostImpl; | 52 using content::ResourceDispatcherHostImpl; |
52 using content::WebContents; | 53 using content::WebContents; |
53 | 54 |
54 namespace { | 55 namespace { |
55 | 56 |
56 // This is just used to remember which DownloadItems come from SavePage. | |
57 class SavePageData : public base::SupportsUserData::Data { | |
58 public: | |
59 // A spoonful of syntactic sugar. | |
60 static bool Get(DownloadItem* item) { | |
61 return item->GetUserData(kKey) != NULL; | |
62 } | |
63 | |
64 explicit SavePageData(DownloadItem* item) { | |
65 item->SetUserData(kKey, this); | |
66 } | |
67 | |
68 virtual ~SavePageData() {} | |
69 | |
70 private: | |
71 static const char kKey[]; | |
72 | |
73 DISALLOW_COPY_AND_ASSIGN(SavePageData); | |
74 }; | |
75 | |
76 const char SavePageData::kKey[] = "DownloadItem SavePageData"; | |
77 | |
78 void BeginDownload(scoped_ptr<content::DownloadUrlParameters> params) { | 57 void BeginDownload(scoped_ptr<content::DownloadUrlParameters> params) { |
79 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 58 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
80 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and | 59 // ResourceDispatcherHost{Base} is-not-a URLRequest::Delegate, and |
81 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so | 60 // DownloadUrlParameters can-not include resource_dispatcher_host_impl.h, so |
82 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. | 61 // we must down cast. RDHI is the only subclass of RDH as of 2012 May 4. |
83 scoped_ptr<net::URLRequest> request( | 62 scoped_ptr<net::URLRequest> request( |
84 params->resource_context()->GetRequestContext()->CreateRequest( | 63 params->resource_context()->GetRequestContext()->CreateRequest( |
85 params->url(), NULL)); | 64 params->url(), NULL)); |
86 request->set_referrer(params->referrer().url.spec()); | 65 request->set_referrer(params->referrer().url.spec()); |
87 webkit_glue::ConfigureURLRequestForReferrerPolicy( | 66 webkit_glue::ConfigureURLRequestForReferrerPolicy( |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 116 |
138 bool operator!=(const MapValueIteratorAdapter& that) const { | 117 bool operator!=(const MapValueIteratorAdapter& that) const { |
139 return iter_ != that.iter_; | 118 return iter_ != that.iter_; |
140 } | 119 } |
141 | 120 |
142 private: | 121 private: |
143 base::hash_map<int64, DownloadItem*>::const_iterator iter_; | 122 base::hash_map<int64, DownloadItem*>::const_iterator iter_; |
144 // Allow copy and assign. | 123 // Allow copy and assign. |
145 }; | 124 }; |
146 | 125 |
147 void EnsureNoPendingDownloadsOnFile(scoped_refptr<DownloadFileManager> dfm, | 126 void EnsureNoPendingDownloadJobsOnFile(bool* result) { |
148 bool* result) { | 127 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE)); |
149 if (dfm->NumberOfActiveDownloads()) | 128 *result = (content::DownloadFile::GetNumberOfDownloadFiles() == 0); |
150 *result = false; | |
151 BrowserThread::PostTask( | 129 BrowserThread::PostTask( |
152 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); | 130 BrowserThread::UI, FROM_HERE, MessageLoop::QuitClosure()); |
153 } | 131 } |
154 | 132 |
155 void EnsureNoPendingDownloadJobsOnIO(bool* result) { | |
156 scoped_refptr<DownloadFileManager> download_file_manager = | |
157 ResourceDispatcherHostImpl::Get()->download_file_manager(); | |
158 BrowserThread::PostTask( | |
159 BrowserThread::FILE, FROM_HERE, | |
160 base::Bind(&EnsureNoPendingDownloadsOnFile, | |
161 download_file_manager, result)); | |
162 } | |
163 | |
164 class DownloadItemFactoryImpl : public content::DownloadItemFactory { | 133 class DownloadItemFactoryImpl : public content::DownloadItemFactory { |
165 public: | 134 public: |
166 DownloadItemFactoryImpl() {} | 135 DownloadItemFactoryImpl() {} |
167 virtual ~DownloadItemFactoryImpl() {} | 136 virtual ~DownloadItemFactoryImpl() {} |
168 | 137 |
169 virtual DownloadItemImpl* CreatePersistedItem( | 138 virtual DownloadItemImpl* CreatePersistedItem( |
170 DownloadItemImplDelegate* delegate, | 139 DownloadItemImplDelegate* delegate, |
171 content::DownloadId download_id, | 140 content::DownloadId download_id, |
172 const content::DownloadPersistentStoreInfo& info, | 141 const content::DownloadPersistentStoreInfo& info, |
173 const net::BoundNetLog& bound_net_log) OVERRIDE { | 142 const net::BoundNetLog& bound_net_log) OVERRIDE { |
(...skipping 17 matching lines...) Expand all Loading... |
191 const std::string& mime_type, | 160 const std::string& mime_type, |
192 const net::BoundNetLog& bound_net_log) OVERRIDE { | 161 const net::BoundNetLog& bound_net_log) OVERRIDE { |
193 return new DownloadItemImpl(delegate, path, url, download_id, | 162 return new DownloadItemImpl(delegate, path, url, download_id, |
194 mime_type, bound_net_log); | 163 mime_type, bound_net_log); |
195 } | 164 } |
196 }; | 165 }; |
197 | 166 |
198 } // namespace | 167 } // namespace |
199 | 168 |
200 DownloadManagerImpl::DownloadManagerImpl( | 169 DownloadManagerImpl::DownloadManagerImpl( |
201 DownloadFileManager* file_manager, | |
202 scoped_ptr<content::DownloadItemFactory> factory, | |
203 net::NetLog* net_log) | 170 net::NetLog* net_log) |
204 : factory_(factory.Pass()), | 171 : item_factory_(new DownloadItemFactoryImpl()), |
| 172 file_factory_(new content::DownloadFileFactory()), |
205 history_size_(0), | 173 history_size_(0), |
206 shutdown_needed_(false), | 174 shutdown_needed_(false), |
207 browser_context_(NULL), | 175 browser_context_(NULL), |
208 file_manager_(file_manager), | |
209 delegate_(NULL), | 176 delegate_(NULL), |
210 net_log_(net_log) { | 177 net_log_(net_log) { |
211 DCHECK(file_manager); | |
212 if (!factory_.get()) | |
213 factory_.reset(new DownloadItemFactoryImpl()); | |
214 } | 178 } |
215 | 179 |
216 DownloadManagerImpl::~DownloadManagerImpl() { | 180 DownloadManagerImpl::~DownloadManagerImpl() { |
217 DCHECK(!shutdown_needed_); | 181 DCHECK(!shutdown_needed_); |
218 } | 182 } |
219 | 183 |
220 DownloadId DownloadManagerImpl::GetNextId() { | 184 DownloadId DownloadManagerImpl::GetNextId() { |
221 DownloadId id; | 185 DownloadId id; |
222 if (delegate_) | 186 if (delegate_) |
223 id = delegate_->GetNextId(); | 187 id = delegate_->GetNextId(); |
224 if (!id.IsValid()) { | 188 if (!id.IsValid()) { |
225 static int next_id; | 189 static int next_id; |
226 id = DownloadId(browser_context_, ++next_id); | 190 id = DownloadId(browser_context_, ++next_id); |
227 } | 191 } |
228 | 192 |
229 return id; | 193 return id; |
230 } | 194 } |
231 | 195 |
232 DownloadFileManager* DownloadManagerImpl::GetDownloadFileManager() { | 196 void DownloadManagerImpl::DetermineDownloadTarget( |
233 return file_manager_; | 197 DownloadItemImpl* item, const DownloadTargetCallback& callback) { |
| 198 // Note that this next call relies on |
| 199 // DownloadItemImplDelegate::DownloadTargetCallback and |
| 200 // DownloadManagerDelegate::DownloadTargetCallback having the same |
| 201 // type. If the types ever diverge, gasket code will need to |
| 202 // be written here. |
| 203 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { |
| 204 FilePath target_path = item->GetForcedFilePath(); |
| 205 // TODO(asanka): Determine a useful path if |target_path| is empty. |
| 206 callback.Run(target_path, |
| 207 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 208 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 209 target_path); |
| 210 } |
234 } | 211 } |
235 | 212 |
236 void DownloadManagerImpl::ReadyForDownloadCompletion( | 213 void DownloadManagerImpl::ReadyForDownloadCompletion( |
237 DownloadItemImpl* item, const base::Closure& complete_callback) { | 214 DownloadItemImpl* item, const base::Closure& complete_callback) { |
238 if (!delegate_ || | 215 if (!delegate_ || |
239 delegate_->ShouldCompleteDownload(item, complete_callback)) { | 216 delegate_->ShouldCompleteDownload(item, complete_callback)) { |
240 complete_callback.Run(); | 217 complete_callback.Run(); |
241 } | 218 } |
242 // Otherwise, the delegate has accepted responsibility to run the | 219 // Otherwise, the delegate has accepted responsibility to run the |
243 // callback when the download is ready for completion. | 220 // callback when the download is ready for completion. |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 // anything left. | 288 // anything left. |
312 | 289 |
313 // We delete the downloads before clearing the active_downloads_ map | 290 // We delete the downloads before clearing the active_downloads_ map |
314 // so that downloads in the COMPLETING_INTERNAL state (which will have | 291 // so that downloads in the COMPLETING_INTERNAL state (which will have |
315 // ignored the Cancel() above) will still show up in active_downloads_ | 292 // ignored the Cancel() above) will still show up in active_downloads_ |
316 // in order to satisfy the invariants enforced in AssertStateConsistent(). | 293 // in order to satisfy the invariants enforced in AssertStateConsistent(). |
317 STLDeleteValues(&downloads_); | 294 STLDeleteValues(&downloads_); |
318 active_downloads_.clear(); | 295 active_downloads_.clear(); |
319 downloads_.clear(); | 296 downloads_.clear(); |
320 | 297 |
321 DCHECK(file_manager_); | |
322 BrowserThread::PostTask( | |
323 BrowserThread::FILE, FROM_HERE, | |
324 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, | |
325 file_manager_, make_scoped_refptr(this))); | |
326 | |
327 // We'll have nothing more to report to the observers after this point. | 298 // We'll have nothing more to report to the observers after this point. |
328 observers_.Clear(); | 299 observers_.Clear(); |
329 | 300 |
330 file_manager_ = NULL; | |
331 if (delegate_) | 301 if (delegate_) |
332 delegate_->Shutdown(); | 302 delegate_->Shutdown(); |
333 delegate_ = NULL; | 303 delegate_ = NULL; |
334 } | 304 } |
335 | 305 |
336 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 306 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
337 DCHECK(browser_context); | 307 DCHECK(browser_context); |
338 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 308 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
339 shutdown_needed_ = true; | 309 shutdown_needed_ = true; |
340 | 310 |
341 browser_context_ = browser_context; | 311 browser_context_ = browser_context; |
342 | 312 |
343 return true; | 313 return true; |
344 } | 314 } |
345 | 315 |
346 // We have received a message from DownloadFileManager about a new download. | |
347 DownloadItem* DownloadManagerImpl::StartDownload( | 316 DownloadItem* DownloadManagerImpl::StartDownload( |
348 scoped_ptr<DownloadCreateInfo> info, | 317 scoped_ptr<DownloadCreateInfo> info, |
349 scoped_ptr<content::ByteStreamReader> stream) { | 318 scoped_ptr<content::ByteStreamReader> stream) { |
350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 319 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
351 | 320 |
352 // |bound_net_log| will be used for logging both the download item's and | 321 net::BoundNetLog bound_net_log = |
353 // the download file's events. | 322 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
354 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); | |
355 | 323 |
356 // If info->download_id was unknown on entry to this function, it was | 324 FilePath default_download_directory; |
357 // assigned in CreateDownloadItem. | |
358 DownloadId download_id = info->download_id; | |
359 | |
360 if (delegate_) { | 325 if (delegate_) { |
361 FilePath website_save_directory; // Unused | 326 FilePath website_save_directory; // Unused |
362 bool skip_dir_check = false; // Unused | 327 bool skip_dir_check = false; // Unused |
363 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, | 328 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
364 &info->default_download_directory, &skip_dir_check); | 329 &default_download_directory, &skip_dir_check); |
365 } | 330 } |
366 | 331 |
367 DownloadFileManager::CreateDownloadFileCallback callback( | 332 // We create the DownloadItem before the DownloadFile because the |
368 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, | 333 // DownloadItem already needs to handle a state in which there is |
369 this, download_id.local())); | 334 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) |
| 335 DownloadItemImpl* download = |
| 336 CreateDownloadItem(info.get(), bound_net_log); |
| 337 scoped_ptr<content::DownloadFile> download_file( |
| 338 file_factory_->CreateFile( |
| 339 info->save_info.Pass(), default_download_directory, |
| 340 info->url(), info->referrer_url, |
| 341 info->received_bytes, delegate_->GenerateFileHash(), |
| 342 stream.Pass(), bound_net_log, |
| 343 download->DestinationObserverAsWeakPtr())); |
| 344 download->Start(download_file.Pass()); |
370 | 345 |
371 BrowserThread::PostTask( | 346 // Delay notification until after Start() so that download_file is bound |
372 BrowserThread::FILE, FROM_HERE, | 347 // to download and all the usual setters (e.g. Cancel) work. |
373 base::Bind(&DownloadFileManager::CreateDownloadFile, | 348 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
374 file_manager_, base::Passed(info.Pass()), | |
375 base::Passed(stream.Pass()), make_scoped_refptr(this), | |
376 (delegate_ && delegate_->GenerateFileHash()), bound_net_log, | |
377 callback)); | |
378 | 349 |
379 return GetDownload(download_id.local()); | 350 return download; |
380 } | |
381 | |
382 void DownloadManagerImpl::OnDownloadFileCreated( | |
383 int32 download_id, content::DownloadInterruptReason reason) { | |
384 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
385 OnDownloadInterrupted(download_id, reason); | |
386 // TODO(rdsmith): It makes no sense to continue along the | |
387 // regular download path after we've gotten an error. But it's | |
388 // the way the code has historically worked, and this allows us | |
389 // to get the download persisted and observers of the download manager | |
390 // notified, so tests work. When we execute all side effects of cancel | |
391 // (including queue removal) immedately rather than waiting for | |
392 // persistence we should replace this comment with a "return;". | |
393 } | |
394 | |
395 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | |
396 if (download_iter == active_downloads_.end()) | |
397 return; | |
398 | |
399 DownloadItemImpl* download = download_iter->second; | |
400 content::DownloadTargetCallback callback = | |
401 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, | |
402 this, download_id); | |
403 if (!delegate_ || !delegate_->DetermineDownloadTarget(download, callback)) { | |
404 FilePath target_path = download->GetForcedFilePath(); | |
405 // TODO(asanka): Determine a useful path if |target_path| is empty. | |
406 callback.Run(target_path, | |
407 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
408 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
409 target_path); | |
410 } | |
411 } | |
412 | |
413 void DownloadManagerImpl::OnDownloadTargetDetermined( | |
414 int32 download_id, | |
415 const FilePath& target_path, | |
416 DownloadItem::TargetDisposition disposition, | |
417 content::DownloadDangerType danger_type, | |
418 const FilePath& intermediate_path) { | |
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
420 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | |
421 if (download_iter != active_downloads_.end()) { | |
422 // Once DownloadItem::OnDownloadTargetDetermined() is called, we expect a | |
423 // DownloadRenamedToIntermediateName() callback. This is necessary for the | |
424 // download to proceed. | |
425 download_iter->second->OnDownloadTargetDetermined( | |
426 target_path, disposition, danger_type, intermediate_path); | |
427 } | |
428 } | 351 } |
429 | 352 |
430 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { | 353 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
432 for (DownloadMap::iterator it = downloads_.begin(); | 355 for (DownloadMap::iterator it = downloads_.begin(); |
433 it != downloads_.end(); ++it) { | 356 it != downloads_.end(); ++it) { |
434 DownloadItemImpl* item = it->second; | 357 DownloadItemImpl* item = it->second; |
435 if (item->IsPersisted()) | 358 if (item->IsPersisted()) |
436 CheckForFileRemoval(item); | 359 CheckForFileRemoval(item); |
437 } | 360 } |
(...skipping 26 matching lines...) Expand all Loading... |
464 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { | 387 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 388 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
466 if (ContainsKey(downloads_, download_id)) | 389 if (ContainsKey(downloads_, download_id)) |
467 downloads_[download_id]->OnDownloadedFileRemoved(); | 390 downloads_[download_id]->OnDownloadedFileRemoved(); |
468 } | 391 } |
469 | 392 |
470 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 393 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
471 return browser_context_; | 394 return browser_context_; |
472 } | 395 } |
473 | 396 |
474 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( | 397 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( |
475 DownloadCreateInfo* info) { | 398 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { |
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 399 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
477 | 400 |
478 net::BoundNetLog bound_net_log = | |
479 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | |
480 if (!info->download_id.IsValid()) | 401 if (!info->download_id.IsValid()) |
481 info->download_id = GetNextId(); | 402 info->download_id = GetNextId(); |
482 DownloadItemImpl* download = factory_->CreateActiveItem( | 403 DownloadItemImpl* download = item_factory_->CreateActiveItem( |
483 this, *info, | 404 this, *info, |
484 scoped_ptr<DownloadRequestHandleInterface>( | 405 scoped_ptr<DownloadRequestHandleInterface>( |
485 new DownloadRequestHandle(info->request_handle)).Pass(), | 406 new DownloadRequestHandle(info->request_handle)).Pass(), |
486 bound_net_log); | 407 bound_net_log); |
487 | 408 |
488 DCHECK(!ContainsKey(downloads_, download->GetId())); | 409 DCHECK(!ContainsKey(downloads_, download->GetId())); |
489 downloads_[download->GetId()] = download; | 410 downloads_[download->GetId()] = download; |
490 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 411 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
491 active_downloads_[download->GetId()] = download; | 412 active_downloads_[download->GetId()] = download; |
492 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | |
493 | 413 |
494 return bound_net_log; | 414 return download; |
495 } | 415 } |
496 | 416 |
497 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 417 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
498 const FilePath& main_file_path, | 418 const FilePath& main_file_path, |
499 const GURL& page_url, | 419 const GURL& page_url, |
500 const std::string& mime_type, | 420 const std::string& mime_type, |
501 DownloadItem::Observer* observer) { | 421 DownloadItem::Observer* observer) { |
502 net::BoundNetLog bound_net_log = | 422 net::BoundNetLog bound_net_log = |
503 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 423 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
504 DownloadItemImpl* download = factory_->CreateSavePageItem( | 424 DownloadItemImpl* download = item_factory_->CreateSavePageItem( |
505 this, | 425 this, |
506 main_file_path, | 426 main_file_path, |
507 page_url, | 427 page_url, |
508 GetNextId(), | 428 GetNextId(), |
509 mime_type, | 429 mime_type, |
510 bound_net_log); | 430 bound_net_log); |
511 | 431 |
512 download->AddObserver(observer); | 432 download->AddObserver(observer); |
513 | 433 |
514 DCHECK(!ContainsKey(downloads_, download->GetId())); | 434 DCHECK(!ContainsKey(downloads_, download->GetId())); |
515 downloads_[download->GetId()] = download; | 435 downloads_[download->GetId()] = download; |
516 DCHECK(!SavePageData::Get(download)); | |
517 new SavePageData(download); | |
518 DCHECK(SavePageData::Get(download)); | |
519 | 436 |
520 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 437 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
521 | 438 |
522 // Will notify the observer in the callback. | 439 // Will notify the observer in the callback. |
523 if (delegate_) | 440 if (delegate_) |
524 delegate_->AddItemToPersistentStore(download); | 441 delegate_->AddItemToPersistentStore(download); |
525 | 442 |
526 return download; | 443 return download; |
527 } | 444 } |
528 | 445 |
529 void DownloadManagerImpl::UpdateDownload(int32 download_id, | |
530 int64 bytes_so_far, | |
531 int64 bytes_per_sec, | |
532 const std::string& hash_state) { | |
533 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
534 DownloadMap::iterator it = active_downloads_.find(download_id); | |
535 if (it != active_downloads_.end()) { | |
536 DownloadItemImpl* download = it->second; | |
537 if (download->IsInProgress()) { | |
538 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); | |
539 if (delegate_) | |
540 delegate_->UpdateItemInPersistentStore(download); | |
541 } | |
542 } | |
543 } | |
544 | |
545 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, | |
546 int64 size, | |
547 const std::string& hash) { | |
548 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
549 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | |
550 << " size = " << size; | |
551 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
552 | |
553 // If it's not in active_downloads_, that means it was cancelled; just | |
554 // ignore the notification. | |
555 if (active_downloads_.count(download_id) == 0) | |
556 return; | |
557 | |
558 DownloadItemImpl* download = active_downloads_[download_id]; | |
559 // TODO(rdsmith): Make OnAllDataSaved call MaybeCompleteDownload() directly. | |
560 // This would allow MaybeCompleteDownload() to be private to the | |
561 // DownloadItemImpl. It can't currently be done because SavePackage | |
562 // calls OnAllDataSaved and shouldn't initiate the download cascade. | |
563 download->OnAllDataSaved(size, hash); | |
564 download->MaybeCompleteDownload(); | |
565 } | |
566 | |
567 void DownloadManagerImpl::AssertStateConsistent( | 446 void DownloadManagerImpl::AssertStateConsistent( |
568 DownloadItemImpl* download) const { | 447 DownloadItemImpl* download) const { |
569 CHECK(ContainsKey(downloads_, download->GetId())); | 448 CHECK(ContainsKey(downloads_, download->GetId())); |
570 | 449 |
571 int64 state = download->GetState(); | 450 int64 state = download->GetState(); |
572 base::debug::Alias(&state); | 451 base::debug::Alias(&state); |
573 if (ContainsKey(active_downloads_, download->GetId())) { | 452 if (ContainsKey(active_downloads_, download->GetId())) { |
574 if (download->IsPersisted()) | 453 if (download->IsPersisted()) |
575 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 454 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
576 if (DownloadItem::IN_PROGRESS != download->GetState()) | 455 if (DownloadItem::IN_PROGRESS != download->GetState()) |
(...skipping 27 matching lines...) Expand all Loading... |
604 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { | 483 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
605 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 484 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
606 | 485 |
607 VLOG(20) << __FUNCTION__ << "()" | 486 VLOG(20) << __FUNCTION__ << "()" |
608 << " download = " << download->DebugString(true); | 487 << " download = " << download->DebugString(true); |
609 | 488 |
610 RemoveFromActiveList(download); | 489 RemoveFromActiveList(download); |
611 // This function is called from the DownloadItem, so DI state | 490 // This function is called from the DownloadItem, so DI state |
612 // should already have been updated. | 491 // should already have been updated. |
613 AssertStateConsistent(download); | 492 AssertStateConsistent(download); |
614 | |
615 DCHECK(file_manager_); | |
616 download->OffThreadCancel(); | |
617 } | |
618 | |
619 void DownloadManagerImpl::OnDownloadInterrupted( | |
620 int32 download_id, | |
621 content::DownloadInterruptReason reason) { | |
622 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
623 | |
624 if (!ContainsKey(active_downloads_, download_id)) | |
625 return; | |
626 active_downloads_[download_id]->Interrupt(reason); | |
627 } | 493 } |
628 | 494 |
629 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 495 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
630 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 496 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
631 DCHECK(download); | 497 DCHECK(download); |
632 | 498 |
633 // Clean up will happen when the history system create callback runs if we | 499 // Clean up will happen when the history system create callback runs if we |
634 // don't have a valid db_handle yet. | 500 // don't have a valid db_handle yet. |
635 if (download->IsPersisted()) { | 501 if (download->IsPersisted()) { |
636 active_downloads_.erase(download->GetId()); | 502 active_downloads_.erase(download->GetId()); |
637 if (delegate_) | 503 if (delegate_) |
638 delegate_->UpdateItemInPersistentStore(download); | 504 delegate_->UpdateItemInPersistentStore(download); |
639 } | 505 } |
640 } | 506 } |
641 | 507 |
| 508 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
| 509 scoped_ptr<content::DownloadItemFactory> item_factory) { |
| 510 item_factory_ = item_factory.Pass(); |
| 511 } |
| 512 |
| 513 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
| 514 scoped_ptr<content::DownloadFileFactory> file_factory) { |
| 515 file_factory_ = file_factory.Pass(); |
| 516 } |
| 517 |
| 518 content::DownloadFileFactory* |
| 519 DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
| 520 return file_factory_.get(); |
| 521 } |
| 522 |
642 int DownloadManagerImpl::RemoveDownloadItems( | 523 int DownloadManagerImpl::RemoveDownloadItems( |
643 const DownloadItemImplVector& pending_deletes) { | 524 const DownloadItemImplVector& pending_deletes) { |
644 if (pending_deletes.empty()) | 525 if (pending_deletes.empty()) |
645 return 0; | 526 return 0; |
646 | 527 |
647 // Delete from internal maps. | 528 // Delete from internal maps. |
648 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 529 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
649 it != pending_deletes.end(); | 530 it != pending_deletes.end(); |
650 ++it) { | 531 ++it) { |
651 DownloadItemImpl* download = *it; | 532 DownloadItemImpl* download = *it; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
739 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 620 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
740 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 621 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
741 std::vector<DownloadPersistentStoreInfo>* entries) { | 622 std::vector<DownloadPersistentStoreInfo>* entries) { |
742 history_size_ = entries->size(); | 623 history_size_ = entries->size(); |
743 for (size_t i = 0; i < entries->size(); ++i) { | 624 for (size_t i = 0; i < entries->size(); ++i) { |
744 int64 db_handle = entries->at(i).db_handle; | 625 int64 db_handle = entries->at(i).db_handle; |
745 base::debug::Alias(&db_handle); | 626 base::debug::Alias(&db_handle); |
746 | 627 |
747 net::BoundNetLog bound_net_log = | 628 net::BoundNetLog bound_net_log = |
748 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 629 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
749 DownloadItemImpl* download = factory_->CreatePersistedItem( | 630 DownloadItemImpl* download = item_factory_->CreatePersistedItem( |
750 this, GetNextId(), entries->at(i), bound_net_log); | 631 this, GetNextId(), entries->at(i), bound_net_log); |
751 DCHECK(!ContainsKey(downloads_, download->GetId())); | 632 DCHECK(!ContainsKey(downloads_, download->GetId())); |
752 downloads_[download->GetId()] = download; | 633 downloads_[download->GetId()] = download; |
753 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 634 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
754 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 635 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
755 << " download = " << download->DebugString(true); | 636 << " download = " << download->DebugString(true); |
756 } | 637 } |
757 NotifyModelChanged(); | 638 NotifyModelChanged(); |
758 CheckForHistoryFilesRemoval(); | 639 CheckForHistoryFilesRemoval(); |
759 } | 640 } |
(...skipping 11 matching lines...) Expand all Loading... |
771 ++history_size_; | 652 ++history_size_; |
772 | 653 |
773 // Show in the appropriate browser UI. | 654 // Show in the appropriate browser UI. |
774 // This includes buttons to save or cancel, for a dangerous download. | 655 // This includes buttons to save or cancel, for a dangerous download. |
775 ShowDownloadInBrowser(download); | 656 ShowDownloadInBrowser(download); |
776 | 657 |
777 // Inform interested objects about the new download. | 658 // Inform interested objects about the new download. |
778 NotifyModelChanged(); | 659 NotifyModelChanged(); |
779 } | 660 } |
780 | 661 |
781 | |
782 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, | 662 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, |
783 int64 db_handle) { | 663 int64 db_handle) { |
784 // It's valid that we don't find a matching item, i.e. on shutdown. | 664 // It's valid that we don't find a matching item, i.e. on shutdown. |
785 if (!ContainsKey(downloads_, download_id)) | 665 if (!ContainsKey(downloads_, download_id)) |
786 return; | 666 return; |
787 | 667 |
788 DownloadItemImpl* item = downloads_[download_id]; | 668 DownloadItemImpl* item = downloads_[download_id]; |
789 AddDownloadItemToHistory(item, db_handle); | 669 AddDownloadItemToHistory(item, db_handle); |
790 if (SavePageData::Get(item)) { | 670 if (item->IsSavePackageDownload()) { |
791 OnSavePageItemAddedToPersistentStore(item); | 671 OnSavePageItemAddedToPersistentStore(item); |
792 } else { | 672 } else { |
793 OnDownloadItemAddedToPersistentStore(item); | 673 OnDownloadItemAddedToPersistentStore(item); |
794 } | 674 } |
795 } | 675 } |
796 | 676 |
797 // Once the new DownloadItem has been committed to the persistent store, | 677 // Once the new DownloadItem has been committed to the persistent store, |
798 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), | 678 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), |
799 // show it in the browser (TODO(benjhayden) the ui should observe us instead), | 679 // show it in the browser (TODO(benjhayden) the ui should observe us instead), |
800 // and notify observers (TODO(benjhayden) observers should be able to see the | 680 // and notify observers (TODO(benjhayden) observers should be able to see the |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
945 ++num_unopened; | 825 ++num_unopened; |
946 } | 826 } |
947 download_stats::RecordOpensOutstanding(num_unopened); | 827 download_stats::RecordOpensOutstanding(num_unopened); |
948 } | 828 } |
949 | 829 |
950 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 830 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
951 DownloadItemImpl* download) { | 831 DownloadItemImpl* download) { |
952 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 832 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
953 // download->GetFullPath() is only expected to be meaningful after this | 833 // download->GetFullPath() is only expected to be meaningful after this |
954 // callback is received. Therefore we can now add the download to a persistent | 834 // callback is received. Therefore we can now add the download to a persistent |
955 // store. If the rename failed, we receive an OnDownloadInterrupted() call | 835 // store. If the rename failed, we processed an interrupt |
956 // before we receive the DownloadRenamedToIntermediateName() call. | 836 // before we receive the DownloadRenamedToIntermediateName() call. |
957 if (delegate_) { | 837 if (delegate_) { |
958 delegate_->AddItemToPersistentStore(download); | 838 delegate_->AddItemToPersistentStore(download); |
959 } else { | 839 } else { |
960 OnItemAddedToPersistentStore(download->GetId(), | 840 OnItemAddedToPersistentStore(download->GetId(), |
961 DownloadItem::kUninitializedHandle); | 841 DownloadItem::kUninitializedHandle); |
962 } | 842 } |
963 } | 843 } |
964 | 844 |
965 void DownloadManagerImpl::DownloadRenamedToFinalName( | 845 void DownloadManagerImpl::DownloadRenamedToFinalName( |
966 DownloadItemImpl* download) { | 846 DownloadItemImpl* download) { |
967 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 847 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
968 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 848 // If the rename failed, we processed an interrupt before we get here. |
969 // receive the DownloadRenamedToFinalName() call. | |
970 if (delegate_) { | 849 if (delegate_) { |
971 delegate_->UpdatePathForItemInPersistentStore( | 850 delegate_->UpdatePathForItemInPersistentStore( |
972 download, download->GetFullPath()); | 851 download, download->GetFullPath()); |
973 } | 852 } |
974 } | 853 } |
OLD | NEW |