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(content::DownloadUrlParameters* params) { | 57 void BeginDownload(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 22 matching lines...) Expand all Loading... |
196 }; | 165 }; |
197 | 166 |
198 } // namespace | 167 } // namespace |
199 | 168 |
200 namespace content { | 169 namespace content { |
201 | 170 |
202 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { | 171 bool DownloadManager::EnsureNoPendingDownloadsForTesting() { |
203 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 172 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
204 bool result = true; | 173 bool result = true; |
205 BrowserThread::PostTask( | 174 BrowserThread::PostTask( |
206 BrowserThread::IO, FROM_HERE, | 175 BrowserThread::FILE, FROM_HERE, |
207 base::Bind(&EnsureNoPendingDownloadJobsOnIO, &result)); | 176 base::Bind(&EnsureNoPendingDownloadJobsOnFile, &result)); |
208 MessageLoop::current()->Run(); | 177 MessageLoop::current()->Run(); |
209 return result; | 178 return result; |
210 } | 179 } |
211 | 180 |
212 } // namespace content | 181 } // namespace content |
213 | 182 |
214 DownloadManagerImpl::DownloadManagerImpl( | 183 DownloadManagerImpl::DownloadManagerImpl( |
215 DownloadFileManager* file_manager, | |
216 scoped_ptr<content::DownloadItemFactory> factory, | |
217 net::NetLog* net_log) | 184 net::NetLog* net_log) |
218 : factory_(factory.Pass()), | 185 : item_factory_(new DownloadItemFactoryImpl()), |
| 186 file_factory_(new content::DownloadFileFactory()), |
219 history_size_(0), | 187 history_size_(0), |
220 shutdown_needed_(false), | 188 shutdown_needed_(false), |
221 browser_context_(NULL), | 189 browser_context_(NULL), |
222 file_manager_(file_manager), | |
223 delegate_(NULL), | 190 delegate_(NULL), |
224 net_log_(net_log) { | 191 net_log_(net_log) { |
225 DCHECK(file_manager); | |
226 if (!factory_.get()) | |
227 factory_.reset(new DownloadItemFactoryImpl()); | |
228 } | 192 } |
229 | 193 |
230 DownloadManagerImpl::~DownloadManagerImpl() { | 194 DownloadManagerImpl::~DownloadManagerImpl() { |
231 DCHECK(!shutdown_needed_); | 195 DCHECK(!shutdown_needed_); |
232 } | 196 } |
233 | 197 |
234 DownloadId DownloadManagerImpl::GetNextId() { | 198 DownloadId DownloadManagerImpl::GetNextId() { |
235 DownloadId id; | 199 DownloadId id; |
236 if (delegate_) | 200 if (delegate_) |
237 id = delegate_->GetNextId(); | 201 id = delegate_->GetNextId(); |
238 if (!id.IsValid()) { | 202 if (!id.IsValid()) { |
239 static int next_id; | 203 static int next_id; |
240 id = DownloadId(browser_context_, ++next_id); | 204 id = DownloadId(browser_context_, ++next_id); |
241 } | 205 } |
242 | 206 |
243 return id; | 207 return id; |
244 } | 208 } |
245 | 209 |
246 DownloadFileManager* DownloadManagerImpl::GetDownloadFileManager() { | 210 void DownloadManagerImpl::DetermineDownloadTarget( |
247 return file_manager_; | 211 DownloadItemImpl* item, const DownloadTargetCallback& callback) { |
| 212 // Note that this next call relies on |
| 213 // DownloadItemImplDelegate::DownloadTargetCallback and |
| 214 // DownloadManagerDelegate::DownloadTargetCallback having the same |
| 215 // type. If the types ever diverge, gasket code will need to |
| 216 // be written here. |
| 217 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { |
| 218 FilePath target_path = item->GetForcedFilePath(); |
| 219 // TODO(asanka): Determine a useful path if |target_path| is empty. |
| 220 callback.Run(target_path, |
| 221 DownloadItem::TARGET_DISPOSITION_OVERWRITE, |
| 222 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, |
| 223 target_path); |
| 224 } |
248 } | 225 } |
249 | 226 |
250 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { | 227 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { |
251 if (!delegate_) | 228 if (!delegate_) |
252 return true; | 229 return true; |
253 | 230 |
254 return delegate_->ShouldOpenDownload(item); | 231 return delegate_->ShouldOpenDownload(item); |
255 } | 232 } |
256 | 233 |
257 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { | 234 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 // anything left. | 292 // anything left. |
316 | 293 |
317 // We delete the downloads before clearing the active_downloads_ map | 294 // We delete the downloads before clearing the active_downloads_ map |
318 // so that downloads in the COMPLETING_INTERNAL state (which will have | 295 // so that downloads in the COMPLETING_INTERNAL state (which will have |
319 // ignored the Cancel() above) will still show up in active_downloads_ | 296 // ignored the Cancel() above) will still show up in active_downloads_ |
320 // in order to satisfy the invariants enforced in AssertStateConsistent(). | 297 // in order to satisfy the invariants enforced in AssertStateConsistent(). |
321 STLDeleteValues(&downloads_); | 298 STLDeleteValues(&downloads_); |
322 active_downloads_.clear(); | 299 active_downloads_.clear(); |
323 downloads_.clear(); | 300 downloads_.clear(); |
324 | 301 |
325 DCHECK(file_manager_); | |
326 BrowserThread::PostTask( | |
327 BrowserThread::FILE, FROM_HERE, | |
328 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, | |
329 file_manager_, make_scoped_refptr(this))); | |
330 | |
331 // We'll have nothing more to report to the observers after this point. | 302 // We'll have nothing more to report to the observers after this point. |
332 observers_.Clear(); | 303 observers_.Clear(); |
333 | 304 |
334 file_manager_ = NULL; | |
335 if (delegate_) | 305 if (delegate_) |
336 delegate_->Shutdown(); | 306 delegate_->Shutdown(); |
337 delegate_ = NULL; | 307 delegate_ = NULL; |
338 } | 308 } |
339 | 309 |
340 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 310 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
341 DCHECK(browser_context); | 311 DCHECK(browser_context); |
342 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 312 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
343 shutdown_needed_ = true; | 313 shutdown_needed_ = true; |
344 | 314 |
345 browser_context_ = browser_context; | 315 browser_context_ = browser_context; |
346 | 316 |
347 return true; | 317 return true; |
348 } | 318 } |
349 | 319 |
350 // We have received a message from DownloadFileManager about a new download. | |
351 content::DownloadId DownloadManagerImpl::StartDownload( | 320 content::DownloadId DownloadManagerImpl::StartDownload( |
352 scoped_ptr<DownloadCreateInfo> info, | 321 scoped_ptr<DownloadCreateInfo> info, |
353 scoped_ptr<content::ByteStreamReader> stream) { | 322 scoped_ptr<content::ByteStreamReader> stream) { |
354 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 323 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
355 | 324 |
356 // |bound_net_log| will be used for logging both the download item's and | 325 net::BoundNetLog bound_net_log = |
357 // the download file's events. | 326 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
358 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); | |
359 | 327 |
360 // If info->download_id was unknown on entry to this function, it was | 328 FilePath default_download_directory; |
361 // assigned in CreateDownloadItem. | |
362 DownloadId download_id = info->download_id; | |
363 | |
364 if (delegate_) { | 329 if (delegate_) { |
365 FilePath website_save_directory; // Unused | 330 FilePath website_save_directory; // Unused |
366 bool skip_dir_check = false; // Unused | 331 bool skip_dir_check = false; // Unused |
367 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, | 332 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
368 &info->default_download_directory, &skip_dir_check); | 333 &default_download_directory, &skip_dir_check); |
369 } | 334 } |
370 | 335 |
371 DownloadFileManager::CreateDownloadFileCallback callback( | 336 // We create the DownloadItem before the DownloadFile because the |
372 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, | 337 // DownloadItem already needs to handle a state in which there is |
373 this, download_id.local())); | 338 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) |
| 339 DownloadItemImpl* download = |
| 340 CreateDownloadItem(info.get(), bound_net_log); |
| 341 scoped_ptr<content::DownloadFile> download_file( |
| 342 file_factory_->CreateFile( |
| 343 info->save_info, default_download_directory, |
| 344 info->url(), info->referrer_url, |
| 345 info->received_bytes, delegate_->GenerateFileHash(), |
| 346 stream.Pass(), bound_net_log, |
| 347 download->DestinationObserverAsWeakPtr())); |
| 348 download->Start(download_file.Pass()); |
374 | 349 |
375 BrowserThread::PostTask( | 350 // Delay notification until after Start() so that download_file is bound |
376 BrowserThread::FILE, FROM_HERE, | 351 // to download and all the usual setters (e.g. Cancel) work. |
377 base::Bind(&DownloadFileManager::CreateDownloadFile, | 352 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
378 file_manager_, base::Passed(info.Pass()), | |
379 base::Passed(stream.Pass()), make_scoped_refptr(this), | |
380 (delegate_ && delegate_->GenerateFileHash()), bound_net_log, | |
381 callback)); | |
382 | 353 |
383 return download_id; | 354 return download->GetGlobalId(); |
384 } | |
385 | |
386 void DownloadManagerImpl::OnDownloadFileCreated( | |
387 int32 download_id, content::DownloadInterruptReason reason) { | |
388 if (reason != content::DOWNLOAD_INTERRUPT_REASON_NONE) { | |
389 OnDownloadInterrupted(download_id, reason); | |
390 // TODO(rdsmith): It makes no sense to continue along the | |
391 // regular download path after we've gotten an error. But it's | |
392 // the way the code has historically worked, and this allows us | |
393 // to get the download persisted and observers of the download manager | |
394 // notified, so tests work. When we execute all side effects of cancel | |
395 // (including queue removal) immedately rather than waiting for | |
396 // persistence we should replace this comment with a "return;". | |
397 } | |
398 | |
399 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | |
400 if (download_iter == active_downloads_.end()) | |
401 return; | |
402 | |
403 DownloadItemImpl* download = download_iter->second; | |
404 content::DownloadTargetCallback callback = | |
405 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, | |
406 this, download_id); | |
407 if (!delegate_ || !delegate_->DetermineDownloadTarget(download, callback)) { | |
408 FilePath target_path = download->GetForcedFilePath(); | |
409 // TODO(asanka): Determine a useful path if |target_path| is empty. | |
410 callback.Run(target_path, | |
411 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
412 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
413 target_path); | |
414 } | |
415 } | |
416 | |
417 void DownloadManagerImpl::OnDownloadTargetDetermined( | |
418 int32 download_id, | |
419 const FilePath& target_path, | |
420 DownloadItem::TargetDisposition disposition, | |
421 content::DownloadDangerType danger_type, | |
422 const FilePath& intermediate_path) { | |
423 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
424 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | |
425 if (download_iter != active_downloads_.end()) { | |
426 // Once DownloadItem::OnDownloadTargetDetermined() is called, we expect a | |
427 // DownloadRenamedToIntermediateName() callback. This is necessary for the | |
428 // download to proceed. | |
429 download_iter->second->OnDownloadTargetDetermined( | |
430 target_path, disposition, danger_type, intermediate_path); | |
431 } | |
432 } | 355 } |
433 | 356 |
434 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { | 357 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
435 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 358 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
436 for (DownloadMap::iterator it = downloads_.begin(); | 359 for (DownloadMap::iterator it = downloads_.begin(); |
437 it != downloads_.end(); ++it) { | 360 it != downloads_.end(); ++it) { |
438 DownloadItemImpl* item = it->second; | 361 DownloadItemImpl* item = it->second; |
439 if (item->IsPersisted()) | 362 if (item->IsPersisted()) |
440 CheckForFileRemoval(item); | 363 CheckForFileRemoval(item); |
441 } | 364 } |
(...skipping 26 matching lines...) Expand all Loading... |
468 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { | 391 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
469 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 392 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
470 if (ContainsKey(downloads_, download_id)) | 393 if (ContainsKey(downloads_, download_id)) |
471 downloads_[download_id]->OnDownloadedFileRemoved(); | 394 downloads_[download_id]->OnDownloadedFileRemoved(); |
472 } | 395 } |
473 | 396 |
474 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 397 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
475 return browser_context_; | 398 return browser_context_; |
476 } | 399 } |
477 | 400 |
478 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( | 401 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( |
479 DownloadCreateInfo* info) { | 402 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { |
480 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 403 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
481 | 404 |
482 net::BoundNetLog bound_net_log = | |
483 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | |
484 if (!info->download_id.IsValid()) | 405 if (!info->download_id.IsValid()) |
485 info->download_id = GetNextId(); | 406 info->download_id = GetNextId(); |
486 DownloadItemImpl* download = factory_->CreateActiveItem( | 407 DownloadItemImpl* download = item_factory_->CreateActiveItem( |
487 this, *info, | 408 this, *info, |
488 scoped_ptr<DownloadRequestHandleInterface>( | 409 scoped_ptr<DownloadRequestHandleInterface>( |
489 new DownloadRequestHandle(info->request_handle)).Pass(), | 410 new DownloadRequestHandle(info->request_handle)).Pass(), |
490 bound_net_log); | 411 bound_net_log); |
491 | 412 |
492 DCHECK(!ContainsKey(downloads_, download->GetId())); | 413 DCHECK(!ContainsKey(downloads_, download->GetId())); |
493 downloads_[download->GetId()] = download; | 414 downloads_[download->GetId()] = download; |
494 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 415 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
495 active_downloads_[download->GetId()] = download; | 416 active_downloads_[download->GetId()] = download; |
496 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | |
497 | 417 |
498 return bound_net_log; | 418 return download; |
499 } | 419 } |
500 | 420 |
501 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 421 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
502 const FilePath& main_file_path, | 422 const FilePath& main_file_path, |
503 const GURL& page_url, | 423 const GURL& page_url, |
504 const std::string& mime_type, | 424 const std::string& mime_type, |
505 DownloadItem::Observer* observer) { | 425 DownloadItem::Observer* observer) { |
506 net::BoundNetLog bound_net_log = | 426 net::BoundNetLog bound_net_log = |
507 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 427 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
508 DownloadItemImpl* download = factory_->CreateSavePageItem( | 428 DownloadItemImpl* download = item_factory_->CreateSavePageItem( |
509 this, | 429 this, |
510 main_file_path, | 430 main_file_path, |
511 page_url, | 431 page_url, |
512 GetNextId(), | 432 GetNextId(), |
513 mime_type, | 433 mime_type, |
514 bound_net_log); | 434 bound_net_log); |
515 | 435 |
516 download->AddObserver(observer); | 436 download->AddObserver(observer); |
517 | 437 |
518 DCHECK(!ContainsKey(downloads_, download->GetId())); | 438 DCHECK(!ContainsKey(downloads_, download->GetId())); |
519 downloads_[download->GetId()] = download; | 439 downloads_[download->GetId()] = download; |
520 DCHECK(!SavePageData::Get(download)); | |
521 new SavePageData(download); | |
522 DCHECK(SavePageData::Get(download)); | |
523 | 440 |
524 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 441 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
525 | 442 |
526 // Will notify the observer in the callback. | 443 // Will notify the observer in the callback. |
527 if (delegate_) | 444 if (delegate_) |
528 delegate_->AddItemToPersistentStore(download); | 445 delegate_->AddItemToPersistentStore(download); |
529 | 446 |
530 return download; | 447 return download; |
531 } | 448 } |
532 | 449 |
533 void DownloadManagerImpl::UpdateDownload(int32 download_id, | |
534 int64 bytes_so_far, | |
535 int64 bytes_per_sec, | |
536 const std::string& hash_state) { | |
537 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
538 DownloadMap::iterator it = active_downloads_.find(download_id); | |
539 if (it != active_downloads_.end()) { | |
540 DownloadItemImpl* download = it->second; | |
541 if (download->IsInProgress()) { | |
542 download->UpdateProgress(bytes_so_far, bytes_per_sec, hash_state); | |
543 if (delegate_) | |
544 delegate_->UpdateItemInPersistentStore(download); | |
545 } | |
546 } | |
547 } | |
548 | |
549 void DownloadManagerImpl::OnResponseCompleted(int32 download_id, | |
550 int64 size, | |
551 const std::string& hash) { | |
552 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
553 VLOG(20) << __FUNCTION__ << "()" << " download_id = " << download_id | |
554 << " size = " << size; | |
555 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
556 | |
557 // If it's not in active_downloads_, that means it was cancelled; just | |
558 // ignore the notification. | |
559 if (active_downloads_.count(download_id) == 0) | |
560 return; | |
561 | |
562 DownloadItemImpl* download = active_downloads_[download_id]; | |
563 download->OnAllDataSaved(size, hash); | |
564 MaybeCompleteDownload(download); | |
565 } | |
566 | |
567 void DownloadManagerImpl::AssertStateConsistent( | 450 void DownloadManagerImpl::AssertStateConsistent( |
568 DownloadItemImpl* download) const { | 451 DownloadItemImpl* download) const { |
569 CHECK(ContainsKey(downloads_, download->GetId())); | 452 CHECK(ContainsKey(downloads_, download->GetId())); |
570 | 453 |
571 int64 state = download->GetState(); | 454 int64 state = download->GetState(); |
572 base::debug::Alias(&state); | 455 base::debug::Alias(&state); |
573 if (ContainsKey(active_downloads_, download->GetId())) { | 456 if (ContainsKey(active_downloads_, download->GetId())) { |
574 if (download->IsPersisted()) | 457 if (download->IsPersisted()) |
575 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 458 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
576 if (DownloadItem::IN_PROGRESS != download->GetState()) | 459 if (DownloadItem::IN_PROGRESS != download->GetState()) |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { | 561 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
679 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 562 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
680 | 563 |
681 VLOG(20) << __FUNCTION__ << "()" | 564 VLOG(20) << __FUNCTION__ << "()" |
682 << " download = " << download->DebugString(true); | 565 << " download = " << download->DebugString(true); |
683 | 566 |
684 RemoveFromActiveList(download); | 567 RemoveFromActiveList(download); |
685 // This function is called from the DownloadItem, so DI state | 568 // This function is called from the DownloadItem, so DI state |
686 // should already have been updated. | 569 // should already have been updated. |
687 AssertStateConsistent(download); | 570 AssertStateConsistent(download); |
688 | |
689 DCHECK(file_manager_); | |
690 download->OffThreadCancel(); | |
691 } | |
692 | |
693 void DownloadManagerImpl::OnDownloadInterrupted( | |
694 int32 download_id, | |
695 content::DownloadInterruptReason reason) { | |
696 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
697 | |
698 if (!ContainsKey(active_downloads_, download_id)) | |
699 return; | |
700 active_downloads_[download_id]->Interrupt(reason); | |
701 } | 571 } |
702 | 572 |
703 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 573 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
704 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 574 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
705 DCHECK(download); | 575 DCHECK(download); |
706 | 576 |
707 // Clean up will happen when the history system create callback runs if we | 577 // Clean up will happen when the history system create callback runs if we |
708 // don't have a valid db_handle yet. | 578 // don't have a valid db_handle yet. |
709 if (download->IsPersisted()) { | 579 if (download->IsPersisted()) { |
710 active_downloads_.erase(download->GetId()); | 580 active_downloads_.erase(download->GetId()); |
711 if (delegate_) | 581 if (delegate_) |
712 delegate_->UpdateItemInPersistentStore(download); | 582 delegate_->UpdateItemInPersistentStore(download); |
713 } | 583 } |
714 } | 584 } |
715 | 585 |
| 586 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
| 587 scoped_ptr<content::DownloadItemFactory> item_factory) { |
| 588 item_factory_ = item_factory.Pass(); |
| 589 } |
| 590 |
| 591 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
| 592 scoped_ptr<content::DownloadFileFactory> file_factory) { |
| 593 file_factory_ = file_factory.Pass(); |
| 594 } |
| 595 |
| 596 content::DownloadFileFactory* |
| 597 DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
| 598 return file_factory_.get(); |
| 599 } |
| 600 |
716 int DownloadManagerImpl::RemoveDownloadItems( | 601 int DownloadManagerImpl::RemoveDownloadItems( |
717 const DownloadItemImplVector& pending_deletes) { | 602 const DownloadItemImplVector& pending_deletes) { |
718 if (pending_deletes.empty()) | 603 if (pending_deletes.empty()) |
719 return 0; | 604 return 0; |
720 | 605 |
721 // Delete from internal maps. | 606 // Delete from internal maps. |
722 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 607 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
723 it != pending_deletes.end(); | 608 it != pending_deletes.end(); |
724 ++it) { | 609 ++it) { |
725 DownloadItemImpl* download = *it; | 610 DownloadItemImpl* download = *it; |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
813 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 698 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
814 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 699 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
815 std::vector<DownloadPersistentStoreInfo>* entries) { | 700 std::vector<DownloadPersistentStoreInfo>* entries) { |
816 history_size_ = entries->size(); | 701 history_size_ = entries->size(); |
817 for (size_t i = 0; i < entries->size(); ++i) { | 702 for (size_t i = 0; i < entries->size(); ++i) { |
818 int64 db_handle = entries->at(i).db_handle; | 703 int64 db_handle = entries->at(i).db_handle; |
819 base::debug::Alias(&db_handle); | 704 base::debug::Alias(&db_handle); |
820 | 705 |
821 net::BoundNetLog bound_net_log = | 706 net::BoundNetLog bound_net_log = |
822 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 707 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
823 DownloadItemImpl* download = factory_->CreatePersistedItem( | 708 DownloadItemImpl* download = item_factory_->CreatePersistedItem( |
824 this, GetNextId(), entries->at(i), bound_net_log); | 709 this, GetNextId(), entries->at(i), bound_net_log); |
825 DCHECK(!ContainsKey(downloads_, download->GetId())); | 710 DCHECK(!ContainsKey(downloads_, download->GetId())); |
826 downloads_[download->GetId()] = download; | 711 downloads_[download->GetId()] = download; |
827 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 712 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
828 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 713 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
829 << " download = " << download->DebugString(true); | 714 << " download = " << download->DebugString(true); |
830 } | 715 } |
831 NotifyModelChanged(); | 716 NotifyModelChanged(); |
832 CheckForHistoryFilesRemoval(); | 717 CheckForHistoryFilesRemoval(); |
833 } | 718 } |
(...skipping 11 matching lines...) Expand all Loading... |
845 ++history_size_; | 730 ++history_size_; |
846 | 731 |
847 // Show in the appropriate browser UI. | 732 // Show in the appropriate browser UI. |
848 // This includes buttons to save or cancel, for a dangerous download. | 733 // This includes buttons to save or cancel, for a dangerous download. |
849 ShowDownloadInBrowser(download); | 734 ShowDownloadInBrowser(download); |
850 | 735 |
851 // Inform interested objects about the new download. | 736 // Inform interested objects about the new download. |
852 NotifyModelChanged(); | 737 NotifyModelChanged(); |
853 } | 738 } |
854 | 739 |
855 | |
856 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, | 740 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, |
857 int64 db_handle) { | 741 int64 db_handle) { |
858 // It's valid that we don't find a matching item, i.e. on shutdown. | 742 // It's valid that we don't find a matching item, i.e. on shutdown. |
859 if (!ContainsKey(downloads_, download_id)) | 743 if (!ContainsKey(downloads_, download_id)) |
860 return; | 744 return; |
861 | 745 |
862 DownloadItemImpl* item = downloads_[download_id]; | 746 DownloadItemImpl* item = downloads_[download_id]; |
863 AddDownloadItemToHistory(item, db_handle); | 747 AddDownloadItemToHistory(item, db_handle); |
864 if (SavePageData::Get(item)) { | 748 if (item->IsSavePackageDownload()) { |
865 OnSavePageItemAddedToPersistentStore(item); | 749 OnSavePageItemAddedToPersistentStore(item); |
866 } else { | 750 } else { |
867 OnDownloadItemAddedToPersistentStore(item); | 751 OnDownloadItemAddedToPersistentStore(item); |
868 } | 752 } |
869 } | 753 } |
870 | 754 |
871 // Once the new DownloadItem has been committed to the persistent store, | 755 // Once the new DownloadItem has been committed to the persistent store, |
872 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), | 756 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), |
873 // show it in the browser (TODO(benjhayden) the ui should observe us instead), | 757 // show it in the browser (TODO(benjhayden) the ui should observe us instead), |
874 // and notify observers (TODO(benjhayden) observers should be able to see the | 758 // 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... |
1019 ++num_unopened; | 903 ++num_unopened; |
1020 } | 904 } |
1021 download_stats::RecordOpensOutstanding(num_unopened); | 905 download_stats::RecordOpensOutstanding(num_unopened); |
1022 } | 906 } |
1023 | 907 |
1024 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 908 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
1025 DownloadItemImpl* download) { | 909 DownloadItemImpl* download) { |
1026 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 910 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1027 // download->GetFullPath() is only expected to be meaningful after this | 911 // download->GetFullPath() is only expected to be meaningful after this |
1028 // callback is received. Therefore we can now add the download to a persistent | 912 // callback is received. Therefore we can now add the download to a persistent |
1029 // store. If the rename failed, we receive an OnDownloadInterrupted() call | 913 // store. If the rename failed, we processed an interrupt |
1030 // before we receive the DownloadRenamedToIntermediateName() call. | 914 // before we receive the DownloadRenamedToIntermediateName() call. |
1031 if (delegate_) { | 915 if (delegate_) { |
1032 delegate_->AddItemToPersistentStore(download); | 916 delegate_->AddItemToPersistentStore(download); |
1033 } else { | 917 } else { |
1034 OnItemAddedToPersistentStore(download->GetId(), | 918 OnItemAddedToPersistentStore(download->GetId(), |
1035 DownloadItem::kUninitializedHandle); | 919 DownloadItem::kUninitializedHandle); |
1036 } | 920 } |
1037 } | 921 } |
1038 | 922 |
1039 void DownloadManagerImpl::DownloadRenamedToFinalName( | 923 void DownloadManagerImpl::DownloadRenamedToFinalName( |
1040 DownloadItemImpl* download) { | 924 DownloadItemImpl* download) { |
1041 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 925 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1042 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 926 // If the rename failed, we processed an interrupt before we get here. |
1043 // receive the DownloadRenamedToFinalName() call. | |
1044 if (delegate_) { | 927 if (delegate_) { |
1045 delegate_->UpdatePathForItemInPersistentStore( | 928 delegate_->UpdatePathForItemInPersistentStore( |
1046 download, download->GetFullPath()); | 929 download, download->GetFullPath()); |
1047 } | 930 } |
1048 } | 931 } |
OLD | NEW |