| 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, 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 15 matching lines...) Expand all Loading... |
| 273 void DownloadManagerImpl::Shutdown() { | 250 void DownloadManagerImpl::Shutdown() { |
| 274 VLOG(20) << __FUNCTION__ << "()" | 251 VLOG(20) << __FUNCTION__ << "()" |
| 275 << " shutdown_needed_ = " << shutdown_needed_; | 252 << " shutdown_needed_ = " << shutdown_needed_; |
| 276 if (!shutdown_needed_) | 253 if (!shutdown_needed_) |
| 277 return; | 254 return; |
| 278 shutdown_needed_ = false; | 255 shutdown_needed_ = false; |
| 279 | 256 |
| 280 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); | 257 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
| 281 // TODO(benjhayden): Consider clearing observers_. | 258 // TODO(benjhayden): Consider clearing observers_. |
| 282 | 259 |
| 283 DCHECK(file_manager_); | 260 // The DownloadFiles will be canceled and deleted by their DownloadItems. |
| 284 BrowserThread::PostTask( | |
| 285 BrowserThread::FILE, FROM_HERE, | |
| 286 base::Bind(&DownloadFileManager::OnDownloadManagerShutdown, | |
| 287 file_manager_, make_scoped_refptr(this))); | |
| 288 | 261 |
| 289 AssertContainersConsistent(); | 262 AssertContainersConsistent(); |
| 290 | 263 |
| 291 // Go through all downloads in downloads_. Dangerous ones we need to | 264 // Go through all downloads in downloads_. Dangerous ones we need to |
| 292 // remove on disk, and in progress ones we need to cancel. | 265 // remove on disk, and in progress ones we need to cancel. |
| 293 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { | 266 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
| 294 DownloadItemImpl* download = it->second; | 267 DownloadItemImpl* download = it->second; |
| 295 | 268 |
| 296 // Save iterator from potential erases in this set done by called code. | 269 // Save iterator from potential erases in this set done by called code. |
| 297 // Iterators after an erasure point are still valid for lists and | 270 // Iterators after an erasure point are still valid for lists and |
| (...skipping 22 matching lines...) Expand all Loading... |
| 320 // and all in progress downloads have been cancelled. We can now delete | 293 // and all in progress downloads have been cancelled. We can now delete |
| 321 // anything left. | 294 // anything left. |
| 322 | 295 |
| 323 active_downloads_.clear(); | 296 active_downloads_.clear(); |
| 324 STLDeleteValues(&downloads_); | 297 STLDeleteValues(&downloads_); |
| 325 downloads_.clear(); | 298 downloads_.clear(); |
| 326 | 299 |
| 327 // We'll have nothing more to report to the observers after this point. | 300 // We'll have nothing more to report to the observers after this point. |
| 328 observers_.Clear(); | 301 observers_.Clear(); |
| 329 | 302 |
| 330 file_manager_ = NULL; | |
| 331 if (delegate_) | 303 if (delegate_) |
| 332 delegate_->Shutdown(); | 304 delegate_->Shutdown(); |
| 333 delegate_ = NULL; | 305 delegate_ = NULL; |
| 334 } | 306 } |
| 335 | 307 |
| 336 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 308 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
| 337 DCHECK(browser_context); | 309 DCHECK(browser_context); |
| 338 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 310 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
| 339 shutdown_needed_ = true; | 311 shutdown_needed_ = true; |
| 340 | 312 |
| 341 browser_context_ = browser_context; | 313 browser_context_ = browser_context; |
| 342 | 314 |
| 343 return true; | 315 return true; |
| 344 } | 316 } |
| 345 | 317 |
| 346 // We have received a message from DownloadFileManager about a new download. | |
| 347 content::DownloadId DownloadManagerImpl::StartDownload( | 318 content::DownloadId DownloadManagerImpl::StartDownload( |
| 348 scoped_ptr<DownloadCreateInfo> info, | 319 scoped_ptr<DownloadCreateInfo> info, |
| 349 scoped_ptr<content::ByteStreamReader> stream) { | 320 scoped_ptr<content::ByteStreamReader> stream) { |
| 350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 321 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 351 | 322 |
| 352 // |bound_net_log| will be used for logging both the download item's and | 323 net::BoundNetLog bound_net_log = |
| 353 // the download file's events. | 324 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 354 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); | |
| 355 | 325 |
| 356 // If info->download_id was unknown on entry to this function, it was | 326 FilePath default_download_directory; |
| 357 // assigned in CreateDownloadItem. | |
| 358 DownloadId download_id = info->download_id; | |
| 359 | |
| 360 if (delegate_) { | 327 if (delegate_) { |
| 361 FilePath website_save_directory; // Unused | 328 FilePath website_save_directory; // Unused |
| 362 bool skip_dir_check = false; // Unused | 329 bool skip_dir_check = false; // Unused |
| 363 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, | 330 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
| 364 &info->default_download_directory, &skip_dir_check); | 331 &default_download_directory, &skip_dir_check); |
| 365 } | 332 } |
| 366 | 333 |
| 367 DownloadFileManager::CreateDownloadFileCallback callback( | 334 // We create the DownloadItem before the DownloadFile because the |
| 368 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, | 335 // DownloadItem already needs to handle a state in which there is |
| 369 this, download_id.local())); | 336 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) |
| 337 DownloadItemImpl* download = |
| 338 CreateDownloadItem(info.get(), bound_net_log); |
| 339 scoped_ptr<content::DownloadFile> download_file( |
| 340 file_factory_->CreateFile( |
| 341 info->save_info, default_download_directory, |
| 342 info->url(), info->referrer_url, |
| 343 info->received_bytes, delegate_->GenerateFileHash(), |
| 344 stream.Pass(), bound_net_log, |
| 345 download->DestinationObserverAsWeakPtr())); |
| 346 download->Start(download_file.Pass()); |
| 370 | 347 |
| 371 BrowserThread::PostTask( | 348 // Delay notification until after Start() so that download_file is bound |
| 372 BrowserThread::FILE, FROM_HERE, | 349 // to download and all the usual setters (e.g. Cancel) work. |
| 373 base::Bind(&DownloadFileManager::CreateDownloadFile, | 350 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 | 351 |
| 379 return download_id; | 352 return download->GetGlobalId(); |
| 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 } | 353 } |
| 429 | 354 |
| 430 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { | 355 void DownloadManagerImpl::CheckForHistoryFilesRemoval() { |
| 431 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 356 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 432 for (DownloadMap::iterator it = downloads_.begin(); | 357 for (DownloadMap::iterator it = downloads_.begin(); |
| 433 it != downloads_.end(); ++it) { | 358 it != downloads_.end(); ++it) { |
| 434 DownloadItemImpl* item = it->second; | 359 DownloadItemImpl* item = it->second; |
| 435 if (item->IsPersisted()) | 360 if (item->IsPersisted()) |
| 436 CheckForFileRemoval(item); | 361 CheckForFileRemoval(item); |
| 437 } | 362 } |
| (...skipping 26 matching lines...) Expand all Loading... |
| 464 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { | 389 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
| 465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 390 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 466 if (ContainsKey(downloads_, download_id)) | 391 if (ContainsKey(downloads_, download_id)) |
| 467 downloads_[download_id]->OnDownloadedFileRemoved(); | 392 downloads_[download_id]->OnDownloadedFileRemoved(); |
| 468 } | 393 } |
| 469 | 394 |
| 470 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 395 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
| 471 return browser_context_; | 396 return browser_context_; |
| 472 } | 397 } |
| 473 | 398 |
| 474 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( | 399 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( |
| 475 DownloadCreateInfo* info) { | 400 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { |
| 476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 401 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 477 | 402 |
| 478 net::BoundNetLog bound_net_log = | |
| 479 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | |
| 480 if (!info->download_id.IsValid()) | 403 if (!info->download_id.IsValid()) |
| 481 info->download_id = GetNextId(); | 404 info->download_id = GetNextId(); |
| 482 DownloadItemImpl* download = factory_->CreateActiveItem( | 405 DownloadItemImpl* download = item_factory_->CreateActiveItem( |
| 483 this, *info, | 406 this, *info, |
| 484 scoped_ptr<DownloadRequestHandleInterface>( | 407 scoped_ptr<DownloadRequestHandleInterface>( |
| 485 new DownloadRequestHandle(info->request_handle)).Pass(), | 408 new DownloadRequestHandle(info->request_handle)).Pass(), |
| 486 bound_net_log); | 409 bound_net_log); |
| 487 | 410 |
| 488 DCHECK(!ContainsKey(downloads_, download->GetId())); | 411 DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 489 downloads_[download->GetId()] = download; | 412 downloads_[download->GetId()] = download; |
| 490 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 413 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
| 491 active_downloads_[download->GetId()] = download; | 414 active_downloads_[download->GetId()] = download; |
| 492 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | |
| 493 | 415 |
| 494 return bound_net_log; | 416 return download; |
| 495 } | 417 } |
| 496 | 418 |
| 497 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 419 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
| 498 const FilePath& main_file_path, | 420 const FilePath& main_file_path, |
| 499 const GURL& page_url, | 421 const GURL& page_url, |
| 500 const std::string& mime_type, | 422 const std::string& mime_type, |
| 501 DownloadItem::Observer* observer) { | 423 DownloadItem::Observer* observer) { |
| 502 net::BoundNetLog bound_net_log = | 424 net::BoundNetLog bound_net_log = |
| 503 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 425 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 504 DownloadItemImpl* download = factory_->CreateSavePageItem( | 426 DownloadItemImpl* download = item_factory_->CreateSavePageItem( |
| 505 this, | 427 this, |
| 506 main_file_path, | 428 main_file_path, |
| 507 page_url, | 429 page_url, |
| 508 GetNextId(), | 430 GetNextId(), |
| 509 mime_type, | 431 mime_type, |
| 510 bound_net_log); | 432 bound_net_log); |
| 511 | 433 |
| 512 download->AddObserver(observer); | 434 download->AddObserver(observer); |
| 513 | 435 |
| 514 DCHECK(!ContainsKey(downloads_, download->GetId())); | 436 DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 515 downloads_[download->GetId()] = download; | 437 downloads_[download->GetId()] = download; |
| 516 DCHECK(!SavePageData::Get(download)); | |
| 517 new SavePageData(download); | |
| 518 DCHECK(SavePageData::Get(download)); | |
| 519 | 438 |
| 520 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 439 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 521 | 440 |
| 522 // Will notify the observer in the callback. | 441 // Will notify the observer in the callback. |
| 523 if (delegate_) | 442 if (delegate_) |
| 524 delegate_->AddItemToPersistentStore(download); | 443 delegate_->AddItemToPersistentStore(download); |
| 525 | 444 |
| 526 return download; | 445 return download; |
| 527 } | 446 } |
| 528 | 447 |
| 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 download->OnAllDataSaved(size, hash); | |
| 560 MaybeCompleteDownload(download); | |
| 561 } | |
| 562 | |
| 563 void DownloadManagerImpl::AssertStateConsistent( | 448 void DownloadManagerImpl::AssertStateConsistent( |
| 564 DownloadItemImpl* download) const { | 449 DownloadItemImpl* download) const { |
| 565 CHECK(ContainsKey(downloads_, download->GetId())); | 450 CHECK(ContainsKey(downloads_, download->GetId())); |
| 566 | 451 |
| 567 int64 state = download->GetState(); | 452 int64 state = download->GetState(); |
| 568 base::debug::Alias(&state); | 453 base::debug::Alias(&state); |
| 569 if (ContainsKey(active_downloads_, download->GetId())) { | 454 if (ContainsKey(active_downloads_, download->GetId())) { |
| 570 if (download->IsPersisted()) | 455 if (download->IsPersisted()) |
| 571 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 456 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
| 572 if (DownloadItem::IN_PROGRESS != download->GetState()) | 457 if (DownloadItem::IN_PROGRESS != download->GetState()) |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 674 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { | 559 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
| 675 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 560 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 676 | 561 |
| 677 VLOG(20) << __FUNCTION__ << "()" | 562 VLOG(20) << __FUNCTION__ << "()" |
| 678 << " download = " << download->DebugString(true); | 563 << " download = " << download->DebugString(true); |
| 679 | 564 |
| 680 RemoveFromActiveList(download); | 565 RemoveFromActiveList(download); |
| 681 // This function is called from the DownloadItem, so DI state | 566 // This function is called from the DownloadItem, so DI state |
| 682 // should already have been updated. | 567 // should already have been updated. |
| 683 AssertStateConsistent(download); | 568 AssertStateConsistent(download); |
| 684 | |
| 685 DCHECK(file_manager_); | |
| 686 download->OffThreadCancel(); | |
| 687 } | |
| 688 | |
| 689 void DownloadManagerImpl::OnDownloadInterrupted( | |
| 690 int32 download_id, | |
| 691 content::DownloadInterruptReason reason) { | |
| 692 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 693 | |
| 694 if (!ContainsKey(active_downloads_, download_id)) | |
| 695 return; | |
| 696 active_downloads_[download_id]->Interrupt(reason); | |
| 697 } | 569 } |
| 698 | 570 |
| 699 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 571 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
| 700 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 572 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 701 DCHECK(download); | 573 DCHECK(download); |
| 702 | 574 |
| 703 // Clean up will happen when the history system create callback runs if we | 575 // Clean up will happen when the history system create callback runs if we |
| 704 // don't have a valid db_handle yet. | 576 // don't have a valid db_handle yet. |
| 705 if (download->IsPersisted()) { | 577 if (download->IsPersisted()) { |
| 706 active_downloads_.erase(download->GetId()); | 578 active_downloads_.erase(download->GetId()); |
| 707 if (delegate_) | 579 if (delegate_) |
| 708 delegate_->UpdateItemInPersistentStore(download); | 580 delegate_->UpdateItemInPersistentStore(download); |
| 709 } | 581 } |
| 710 } | 582 } |
| 711 | 583 |
| 584 void DownloadManagerImpl::SetDownloadItemFactoryForTesting( |
| 585 scoped_ptr<content::DownloadItemFactory> item_factory) { |
| 586 item_factory_ = item_factory.Pass(); |
| 587 } |
| 588 |
| 589 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( |
| 590 scoped_ptr<content::DownloadFileFactory> file_factory) { |
| 591 file_factory_ = file_factory.Pass(); |
| 592 } |
| 593 |
| 594 content::DownloadFileFactory* |
| 595 DownloadManagerImpl::GetDownloadFileFactoryForTesting() { |
| 596 return file_factory_.get(); |
| 597 } |
| 598 |
| 712 int DownloadManagerImpl::RemoveDownloadItems( | 599 int DownloadManagerImpl::RemoveDownloadItems( |
| 713 const DownloadItemImplVector& pending_deletes) { | 600 const DownloadItemImplVector& pending_deletes) { |
| 714 if (pending_deletes.empty()) | 601 if (pending_deletes.empty()) |
| 715 return 0; | 602 return 0; |
| 716 | 603 |
| 717 // Delete from internal maps. | 604 // Delete from internal maps. |
| 718 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 605 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
| 719 it != pending_deletes.end(); | 606 it != pending_deletes.end(); |
| 720 ++it) { | 607 ++it) { |
| 721 DownloadItemImpl* download = *it; | 608 DownloadItemImpl* download = *it; |
| (...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 809 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 696 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
| 810 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 697 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
| 811 std::vector<DownloadPersistentStoreInfo>* entries) { | 698 std::vector<DownloadPersistentStoreInfo>* entries) { |
| 812 history_size_ = entries->size(); | 699 history_size_ = entries->size(); |
| 813 for (size_t i = 0; i < entries->size(); ++i) { | 700 for (size_t i = 0; i < entries->size(); ++i) { |
| 814 int64 db_handle = entries->at(i).db_handle; | 701 int64 db_handle = entries->at(i).db_handle; |
| 815 base::debug::Alias(&db_handle); | 702 base::debug::Alias(&db_handle); |
| 816 | 703 |
| 817 net::BoundNetLog bound_net_log = | 704 net::BoundNetLog bound_net_log = |
| 818 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 705 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
| 819 DownloadItemImpl* download = factory_->CreatePersistedItem( | 706 DownloadItemImpl* download = item_factory_->CreatePersistedItem( |
| 820 this, GetNextId(), entries->at(i), bound_net_log); | 707 this, GetNextId(), entries->at(i), bound_net_log); |
| 821 DCHECK(!ContainsKey(downloads_, download->GetId())); | 708 DCHECK(!ContainsKey(downloads_, download->GetId())); |
| 822 downloads_[download->GetId()] = download; | 709 downloads_[download->GetId()] = download; |
| 823 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 710 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
| 824 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 711 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
| 825 << " download = " << download->DebugString(true); | 712 << " download = " << download->DebugString(true); |
| 826 } | 713 } |
| 827 NotifyModelChanged(); | 714 NotifyModelChanged(); |
| 828 CheckForHistoryFilesRemoval(); | 715 CheckForHistoryFilesRemoval(); |
| 829 } | 716 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 841 ++history_size_; | 728 ++history_size_; |
| 842 | 729 |
| 843 // Show in the appropriate browser UI. | 730 // Show in the appropriate browser UI. |
| 844 // This includes buttons to save or cancel, for a dangerous download. | 731 // This includes buttons to save or cancel, for a dangerous download. |
| 845 ShowDownloadInBrowser(download); | 732 ShowDownloadInBrowser(download); |
| 846 | 733 |
| 847 // Inform interested objects about the new download. | 734 // Inform interested objects about the new download. |
| 848 NotifyModelChanged(); | 735 NotifyModelChanged(); |
| 849 } | 736 } |
| 850 | 737 |
| 851 | |
| 852 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, | 738 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, |
| 853 int64 db_handle) { | 739 int64 db_handle) { |
| 854 // It's valid that we don't find a matching item, i.e. on shutdown. | 740 // It's valid that we don't find a matching item, i.e. on shutdown. |
| 855 if (!ContainsKey(downloads_, download_id)) | 741 if (!ContainsKey(downloads_, download_id)) |
| 856 return; | 742 return; |
| 857 | 743 |
| 858 DownloadItemImpl* item = downloads_[download_id]; | 744 DownloadItemImpl* item = downloads_[download_id]; |
| 859 AddDownloadItemToHistory(item, db_handle); | 745 AddDownloadItemToHistory(item, db_handle); |
| 860 if (SavePageData::Get(item)) { | 746 if (item->IsSavePackageDownload()) { |
| 861 OnSavePageItemAddedToPersistentStore(item); | 747 OnSavePageItemAddedToPersistentStore(item); |
| 862 } else { | 748 } else { |
| 863 OnDownloadItemAddedToPersistentStore(item); | 749 OnDownloadItemAddedToPersistentStore(item); |
| 864 } | 750 } |
| 865 } | 751 } |
| 866 | 752 |
| 867 // Once the new DownloadItem has been committed to the persistent store, | 753 // Once the new DownloadItem has been committed to the persistent store, |
| 868 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), | 754 // associate it with its db_handle (TODO(benjhayden) merge db_handle with id), |
| 869 // show it in the browser (TODO(benjhayden) the ui should observe us instead), | 755 // show it in the browser (TODO(benjhayden) the ui should observe us instead), |
| 870 // and notify observers (TODO(benjhayden) observers should be able to see the | 756 // 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... |
| 1015 ++num_unopened; | 901 ++num_unopened; |
| 1016 } | 902 } |
| 1017 download_stats::RecordOpensOutstanding(num_unopened); | 903 download_stats::RecordOpensOutstanding(num_unopened); |
| 1018 } | 904 } |
| 1019 | 905 |
| 1020 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 906 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
| 1021 DownloadItemImpl* download) { | 907 DownloadItemImpl* download) { |
| 1022 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 908 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1023 // download->GetFullPath() is only expected to be meaningful after this | 909 // download->GetFullPath() is only expected to be meaningful after this |
| 1024 // callback is received. Therefore we can now add the download to a persistent | 910 // callback is received. Therefore we can now add the download to a persistent |
| 1025 // store. If the rename failed, we receive an OnDownloadInterrupted() call | 911 // store. If the rename failed, we processed an interrupt |
| 1026 // before we receive the DownloadRenamedToIntermediateName() call. | 912 // before we receive the DownloadRenamedToIntermediateName() call. |
| 1027 if (delegate_) { | 913 if (delegate_) { |
| 1028 delegate_->AddItemToPersistentStore(download); | 914 delegate_->AddItemToPersistentStore(download); |
| 1029 } else { | 915 } else { |
| 1030 OnItemAddedToPersistentStore(download->GetId(), | 916 OnItemAddedToPersistentStore(download->GetId(), |
| 1031 DownloadItem::kUninitializedHandle); | 917 DownloadItem::kUninitializedHandle); |
| 1032 } | 918 } |
| 1033 } | 919 } |
| 1034 | 920 |
| 1035 void DownloadManagerImpl::DownloadRenamedToFinalName( | 921 void DownloadManagerImpl::DownloadRenamedToFinalName( |
| 1036 DownloadItemImpl* download) { | 922 DownloadItemImpl* download) { |
| 1037 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 923 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 1038 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 924 // If the rename failed, we processed an interrupt before we get here. |
| 1039 // receive the DownloadRenamedToFinalName() call. | |
| 1040 if (delegate_) { | 925 if (delegate_) { |
| 1041 delegate_->UpdatePathForItemInPersistentStore( | 926 delegate_->UpdatePathForItemInPersistentStore( |
| 1042 download, download->GetFullPath()); | 927 download, download->GetFullPath()); |
| 1043 } | 928 } |
| 1044 } | 929 } |
| OLD | NEW |