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, | 184 scoped_ptr<content::DownloadItemFactory> item_factory, |
216 scoped_ptr<content::DownloadItemFactory> factory, | 185 scoped_ptr<content::DownloadFileFactory> file_factory, |
217 net::NetLog* net_log) | 186 net::NetLog* net_log) |
218 : factory_(factory.Pass()), | 187 : item_factory_(item_factory.Pass()), |
188 file_factory_(file_factory.Pass()), | |
219 history_size_(0), | 189 history_size_(0), |
220 shutdown_needed_(false), | 190 shutdown_needed_(false), |
221 browser_context_(NULL), | 191 browser_context_(NULL), |
222 file_manager_(file_manager), | |
223 delegate_(NULL), | 192 delegate_(NULL), |
224 net_log_(net_log) { | 193 net_log_(net_log) { |
225 DCHECK(file_manager); | 194 if (!item_factory_.get()) |
226 if (!factory_.get()) | 195 item_factory_.reset(new DownloadItemFactoryImpl()); |
227 factory_.reset(new DownloadItemFactoryImpl()); | 196 if (!file_factory_.get()) |
197 file_factory_.reset(new content::DownloadFileFactory()); | |
228 } | 198 } |
229 | 199 |
230 DownloadManagerImpl::~DownloadManagerImpl() { | 200 DownloadManagerImpl::~DownloadManagerImpl() { |
231 DCHECK(!shutdown_needed_); | 201 DCHECK(!shutdown_needed_); |
232 } | 202 } |
233 | 203 |
234 DownloadId DownloadManagerImpl::GetNextId() { | 204 DownloadId DownloadManagerImpl::GetNextId() { |
235 DownloadId id; | 205 DownloadId id; |
236 if (delegate_) | 206 if (delegate_) |
237 id = delegate_->GetNextId(); | 207 id = delegate_->GetNextId(); |
238 if (!id.IsValid()) { | 208 if (!id.IsValid()) { |
239 static int next_id; | 209 static int next_id; |
240 id = DownloadId(browser_context_, ++next_id); | 210 id = DownloadId(browser_context_, ++next_id); |
241 } | 211 } |
242 | 212 |
243 return id; | 213 return id; |
244 } | 214 } |
245 | 215 |
246 DownloadFileManager* DownloadManagerImpl::GetDownloadFileManager() { | 216 void DownloadManagerImpl::DelegateStart(DownloadItemImpl* item) { |
asanka
2012/09/24 20:43:53
Instead of DelegateStart(), would it make sense to
Randy Smith (Not in Mondays)
2012/09/26 21:01:05
Given that I'm adding DelegateStart(), I agree wit
| |
247 return file_manager_; | 217 content::DownloadTargetCallback callback = |
218 base::Bind(&DownloadManagerImpl::OnDownloadTargetDetermined, | |
219 this, item->GetId()); | |
220 if (!delegate_ || !delegate_->DetermineDownloadTarget(item, callback)) { | |
221 FilePath target_path = item->GetForcedFilePath(); | |
222 // TODO(asanka): Determine a useful path if |target_path| is empty. | |
223 callback.Run(target_path, | |
224 DownloadItem::TARGET_DISPOSITION_OVERWRITE, | |
225 content::DOWNLOAD_DANGER_TYPE_NOT_DANGEROUS, | |
226 target_path); | |
227 } | |
248 } | 228 } |
249 | 229 |
250 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { | 230 bool DownloadManagerImpl::ShouldOpenDownload(DownloadItemImpl* item) { |
251 if (!delegate_) | 231 if (!delegate_) |
252 return true; | 232 return true; |
253 | 233 |
254 return delegate_->ShouldOpenDownload(item); | 234 return delegate_->ShouldOpenDownload(item); |
255 } | 235 } |
256 | 236 |
257 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { | 237 bool DownloadManagerImpl::ShouldOpenFileBasedOnExtension(const FilePath& path) { |
(...skipping 15 matching lines...) Expand all Loading... | |
273 void DownloadManagerImpl::Shutdown() { | 253 void DownloadManagerImpl::Shutdown() { |
274 VLOG(20) << __FUNCTION__ << "()" | 254 VLOG(20) << __FUNCTION__ << "()" |
275 << " shutdown_needed_ = " << shutdown_needed_; | 255 << " shutdown_needed_ = " << shutdown_needed_; |
276 if (!shutdown_needed_) | 256 if (!shutdown_needed_) |
277 return; | 257 return; |
278 shutdown_needed_ = false; | 258 shutdown_needed_ = false; |
279 | 259 |
280 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); | 260 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown(this)); |
281 // TODO(benjhayden): Consider clearing observers_. | 261 // TODO(benjhayden): Consider clearing observers_. |
282 | 262 |
283 DCHECK(file_manager_); | 263 // 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 | 264 |
289 AssertContainersConsistent(); | 265 AssertContainersConsistent(); |
290 | 266 |
291 // Go through all downloads in downloads_. Dangerous ones we need to | 267 // Go through all downloads in downloads_. Dangerous ones we need to |
292 // remove on disk, and in progress ones we need to cancel. | 268 // remove on disk, and in progress ones we need to cancel. |
293 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { | 269 for (DownloadMap::iterator it = downloads_.begin(); it != downloads_.end();) { |
294 DownloadItemImpl* download = it->second; | 270 DownloadItemImpl* download = it->second; |
295 | 271 |
296 // Save iterator from potential erases in this set done by called code. | 272 // Save iterator from potential erases in this set done by called code. |
297 // Iterators after an erasure point are still valid for lists and | 273 // 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 | 296 // and all in progress downloads have been cancelled. We can now delete |
321 // anything left. | 297 // anything left. |
322 | 298 |
323 active_downloads_.clear(); | 299 active_downloads_.clear(); |
324 STLDeleteValues(&downloads_); | 300 STLDeleteValues(&downloads_); |
325 downloads_.clear(); | 301 downloads_.clear(); |
326 | 302 |
327 // We'll have nothing more to report to the observers after this point. | 303 // We'll have nothing more to report to the observers after this point. |
328 observers_.Clear(); | 304 observers_.Clear(); |
329 | 305 |
330 file_manager_ = NULL; | |
331 if (delegate_) | 306 if (delegate_) |
332 delegate_->Shutdown(); | 307 delegate_->Shutdown(); |
333 delegate_ = NULL; | 308 delegate_ = NULL; |
334 } | 309 } |
335 | 310 |
336 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { | 311 bool DownloadManagerImpl::Init(content::BrowserContext* browser_context) { |
337 DCHECK(browser_context); | 312 DCHECK(browser_context); |
338 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 313 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
339 shutdown_needed_ = true; | 314 shutdown_needed_ = true; |
340 | 315 |
341 browser_context_ = browser_context; | 316 browser_context_ = browser_context; |
342 | 317 |
343 return true; | 318 return true; |
344 } | 319 } |
345 | 320 |
346 // We have received a message from DownloadFileManager about a new download. | |
347 content::DownloadId DownloadManagerImpl::StartDownload( | 321 content::DownloadId DownloadManagerImpl::StartDownload( |
348 scoped_ptr<DownloadCreateInfo> info, | 322 scoped_ptr<DownloadCreateInfo> info, |
349 scoped_ptr<content::ByteStreamReader> stream) { | 323 scoped_ptr<content::ByteStreamReader> stream) { |
350 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 324 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
351 | 325 |
352 // |bound_net_log| will be used for logging both the download item's and | 326 net::BoundNetLog bound_net_log = |
353 // the download file's events. | 327 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
354 net::BoundNetLog bound_net_log = CreateDownloadItem(info.get()); | |
355 | 328 |
356 // If info->download_id was unknown on entry to this function, it was | 329 FilePath default_download_directory; |
357 // assigned in CreateDownloadItem. | |
358 DownloadId download_id = info->download_id; | |
359 | |
360 if (delegate_) { | 330 if (delegate_) { |
361 FilePath website_save_directory; // Unused | 331 FilePath website_save_directory; // Unused |
362 bool skip_dir_check = false; // Unused | 332 bool skip_dir_check = false; // Unused |
363 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, | 333 delegate_->GetSaveDir(GetBrowserContext(), &website_save_directory, |
364 &info->default_download_directory, &skip_dir_check); | 334 &default_download_directory, &skip_dir_check); |
365 } | 335 } |
366 | 336 |
367 DownloadFileManager::CreateDownloadFileCallback callback( | 337 // We create the DownloadItem before the DownloadFile because the |
368 base::Bind(&DownloadManagerImpl::OnDownloadFileCreated, | 338 // DownloadItem already needs to handle a state in which there is |
369 this, download_id.local())); | 339 // no associated DownloadFile (history downloads, !IN_PROGRESS downloads) |
340 DownloadItemImpl* download = | |
341 CreateDownloadItem(info.get(), bound_net_log); | |
342 scoped_ptr<content::DownloadFile> download_file( | |
343 file_factory_->CreateFile( | |
344 info->save_info, default_download_directory, | |
345 info->url(), info->referrer_url, | |
346 info->received_bytes, delegate_->GenerateFileHash(), | |
347 stream.Pass(), bound_net_log, | |
348 download->DestinationObserverAsWeakPtr())); | |
349 download->Start(download_file.Pass()); | |
370 | 350 |
371 BrowserThread::PostTask( | 351 // Delay notification until after Start() so that download_file is bound |
372 BrowserThread::FILE, FROM_HERE, | 352 // to download and all the usual setters (e.g. Cancel) work. |
373 base::Bind(&DownloadFileManager::CreateDownloadFile, | 353 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 | 354 |
379 return download_id; | 355 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 } | 356 } |
412 | 357 |
413 void DownloadManagerImpl::OnDownloadTargetDetermined( | 358 void DownloadManagerImpl::OnDownloadTargetDetermined( |
414 int32 download_id, | 359 int32 download_id, |
415 const FilePath& target_path, | 360 const FilePath& target_path, |
416 DownloadItem::TargetDisposition disposition, | 361 DownloadItem::TargetDisposition disposition, |
417 content::DownloadDangerType danger_type, | 362 content::DownloadDangerType danger_type, |
418 const FilePath& intermediate_path) { | 363 const FilePath& intermediate_path) { |
419 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 364 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
420 DownloadMap::iterator download_iter = active_downloads_.find(download_id); | 365 DownloadMap::iterator download_iter = active_downloads_.find(download_id); |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
464 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { | 409 void DownloadManagerImpl::OnFileRemovalDetected(int32 download_id) { |
465 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 410 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
466 if (ContainsKey(downloads_, download_id)) | 411 if (ContainsKey(downloads_, download_id)) |
467 downloads_[download_id]->OnDownloadedFileRemoved(); | 412 downloads_[download_id]->OnDownloadedFileRemoved(); |
468 } | 413 } |
469 | 414 |
470 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { | 415 content::BrowserContext* DownloadManagerImpl::GetBrowserContext() const { |
471 return browser_context_; | 416 return browser_context_; |
472 } | 417 } |
473 | 418 |
474 net::BoundNetLog DownloadManagerImpl::CreateDownloadItem( | 419 DownloadItemImpl* DownloadManagerImpl::CreateDownloadItem( |
475 DownloadCreateInfo* info) { | 420 DownloadCreateInfo* info, const net::BoundNetLog& bound_net_log) { |
476 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 421 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
477 | 422 |
478 net::BoundNetLog bound_net_log = | |
479 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | |
480 if (!info->download_id.IsValid()) | 423 if (!info->download_id.IsValid()) |
481 info->download_id = GetNextId(); | 424 info->download_id = GetNextId(); |
482 DownloadItemImpl* download = factory_->CreateActiveItem( | 425 DownloadItemImpl* download = item_factory_->CreateActiveItem( |
483 this, *info, | 426 this, *info, |
484 scoped_ptr<DownloadRequestHandleInterface>( | 427 scoped_ptr<DownloadRequestHandleInterface>( |
485 new DownloadRequestHandle(info->request_handle)).Pass(), | 428 new DownloadRequestHandle(info->request_handle)).Pass(), |
486 bound_net_log); | 429 bound_net_log); |
487 | 430 |
488 DCHECK(!ContainsKey(downloads_, download->GetId())); | 431 DCHECK(!ContainsKey(downloads_, download->GetId())); |
489 downloads_[download->GetId()] = download; | 432 downloads_[download->GetId()] = download; |
490 DCHECK(!ContainsKey(active_downloads_, download->GetId())); | 433 DCHECK(!ContainsKey(active_downloads_, download->GetId())); |
491 active_downloads_[download->GetId()] = download; | 434 active_downloads_[download->GetId()] = download; |
492 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | |
493 | 435 |
494 return bound_net_log; | 436 return download; |
495 } | 437 } |
496 | 438 |
497 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( | 439 DownloadItemImpl* DownloadManagerImpl::CreateSavePackageDownloadItem( |
498 const FilePath& main_file_path, | 440 const FilePath& main_file_path, |
499 const GURL& page_url, | 441 const GURL& page_url, |
500 const std::string& mime_type, | 442 const std::string& mime_type, |
501 DownloadItem::Observer* observer) { | 443 DownloadItem::Observer* observer) { |
502 net::BoundNetLog bound_net_log = | 444 net::BoundNetLog bound_net_log = |
503 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 445 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
504 DownloadItemImpl* download = factory_->CreateSavePageItem( | 446 DownloadItemImpl* download = item_factory_->CreateSavePageItem( |
505 this, | 447 this, |
506 main_file_path, | 448 main_file_path, |
507 page_url, | 449 page_url, |
508 GetNextId(), | 450 GetNextId(), |
509 mime_type, | 451 mime_type, |
510 bound_net_log); | 452 bound_net_log); |
511 | 453 |
512 download->AddObserver(observer); | 454 download->AddObserver(observer); |
513 | 455 |
514 DCHECK(!ContainsKey(downloads_, download->GetId())); | 456 DCHECK(!ContainsKey(downloads_, download->GetId())); |
515 downloads_[download->GetId()] = download; | 457 downloads_[download->GetId()] = download; |
516 DCHECK(!SavePageData::Get(download)); | |
517 new SavePageData(download); | |
518 DCHECK(SavePageData::Get(download)); | |
519 | 458 |
520 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 459 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
521 | 460 |
522 // Will notify the observer in the callback. | 461 // Will notify the observer in the callback. |
523 if (delegate_) | 462 if (delegate_) |
524 delegate_->AddItemToPersistentStore(download); | 463 delegate_->AddItemToPersistentStore(download); |
525 | 464 |
526 return download; | 465 return download; |
527 } | 466 } |
528 | 467 |
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( | 468 void DownloadManagerImpl::AssertStateConsistent( |
564 DownloadItemImpl* download) const { | 469 DownloadItemImpl* download) const { |
565 CHECK(ContainsKey(downloads_, download->GetId())); | 470 CHECK(ContainsKey(downloads_, download->GetId())); |
566 | 471 |
567 int64 state = download->GetState(); | 472 int64 state = download->GetState(); |
568 base::debug::Alias(&state); | 473 base::debug::Alias(&state); |
569 if (ContainsKey(active_downloads_, download->GetId())) { | 474 if (ContainsKey(active_downloads_, download->GetId())) { |
570 if (download->IsPersisted()) | 475 if (download->IsPersisted()) |
571 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); | 476 CHECK_EQ(DownloadItem::IN_PROGRESS, download->GetState()); |
572 if (DownloadItem::IN_PROGRESS != download->GetState()) | 477 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) { | 579 void DownloadManagerImpl::DownloadStopped(DownloadItemImpl* download) { |
675 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 580 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
676 | 581 |
677 VLOG(20) << __FUNCTION__ << "()" | 582 VLOG(20) << __FUNCTION__ << "()" |
678 << " download = " << download->DebugString(true); | 583 << " download = " << download->DebugString(true); |
679 | 584 |
680 RemoveFromActiveList(download); | 585 RemoveFromActiveList(download); |
681 // This function is called from the DownloadItem, so DI state | 586 // This function is called from the DownloadItem, so DI state |
682 // should already have been updated. | 587 // should already have been updated. |
683 AssertStateConsistent(download); | 588 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 } | 589 } |
698 | 590 |
699 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { | 591 void DownloadManagerImpl::RemoveFromActiveList(DownloadItemImpl* download) { |
700 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 592 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
701 DCHECK(download); | 593 DCHECK(download); |
702 | 594 |
703 // Clean up will happen when the history system create callback runs if we | 595 // Clean up will happen when the history system create callback runs if we |
704 // don't have a valid db_handle yet. | 596 // don't have a valid db_handle yet. |
705 if (download->IsPersisted()) { | 597 if (download->IsPersisted()) { |
706 active_downloads_.erase(download->GetId()); | 598 active_downloads_.erase(download->GetId()); |
707 if (delegate_) | 599 if (delegate_) |
708 delegate_->UpdateItemInPersistentStore(download); | 600 delegate_->UpdateItemInPersistentStore(download); |
709 } | 601 } |
710 } | 602 } |
711 | 603 |
604 void DownloadManagerImpl::SetDownloadFileFactoryForTesting( | |
605 scoped_ptr<content::DownloadFileFactory> file_factory) { | |
606 file_factory_ = file_factory.Pass(); | |
607 } | |
608 | |
609 content::DownloadFileFactory* | |
610 DownloadManagerImpl::GetDownloadFileFactoryForTesting() { | |
611 return file_factory_.get(); | |
612 } | |
613 | |
712 int DownloadManagerImpl::RemoveDownloadItems( | 614 int DownloadManagerImpl::RemoveDownloadItems( |
713 const DownloadItemImplVector& pending_deletes) { | 615 const DownloadItemImplVector& pending_deletes) { |
714 if (pending_deletes.empty()) | 616 if (pending_deletes.empty()) |
715 return 0; | 617 return 0; |
716 | 618 |
717 // Delete from internal maps. | 619 // Delete from internal maps. |
718 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); | 620 for (DownloadItemImplVector::const_iterator it = pending_deletes.begin(); |
719 it != pending_deletes.end(); | 621 it != pending_deletes.end(); |
720 ++it) { | 622 ++it) { |
721 DownloadItemImpl* download = *it; | 623 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). | 711 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
810 void DownloadManagerImpl::OnPersistentStoreQueryComplete( | 712 void DownloadManagerImpl::OnPersistentStoreQueryComplete( |
811 std::vector<DownloadPersistentStoreInfo>* entries) { | 713 std::vector<DownloadPersistentStoreInfo>* entries) { |
812 history_size_ = entries->size(); | 714 history_size_ = entries->size(); |
813 for (size_t i = 0; i < entries->size(); ++i) { | 715 for (size_t i = 0; i < entries->size(); ++i) { |
814 int64 db_handle = entries->at(i).db_handle; | 716 int64 db_handle = entries->at(i).db_handle; |
815 base::debug::Alias(&db_handle); | 717 base::debug::Alias(&db_handle); |
816 | 718 |
817 net::BoundNetLog bound_net_log = | 719 net::BoundNetLog bound_net_log = |
818 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); | 720 net::BoundNetLog::Make(net_log_, net::NetLog::SOURCE_DOWNLOAD); |
819 DownloadItemImpl* download = factory_->CreatePersistedItem( | 721 DownloadItemImpl* download = item_factory_->CreatePersistedItem( |
820 this, GetNextId(), entries->at(i), bound_net_log); | 722 this, GetNextId(), entries->at(i), bound_net_log); |
821 DCHECK(!ContainsKey(downloads_, download->GetId())); | 723 DCHECK(!ContainsKey(downloads_, download->GetId())); |
822 downloads_[download->GetId()] = download; | 724 downloads_[download->GetId()] = download; |
823 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); | 725 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadCreated(this, download)); |
824 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 726 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
825 << " download = " << download->DebugString(true); | 727 << " download = " << download->DebugString(true); |
826 } | 728 } |
827 NotifyModelChanged(); | 729 NotifyModelChanged(); |
828 CheckForHistoryFilesRemoval(); | 730 CheckForHistoryFilesRemoval(); |
829 } | 731 } |
(...skipping 11 matching lines...) Expand all Loading... | |
841 ++history_size_; | 743 ++history_size_; |
842 | 744 |
843 // Show in the appropriate browser UI. | 745 // Show in the appropriate browser UI. |
844 // This includes buttons to save or cancel, for a dangerous download. | 746 // This includes buttons to save or cancel, for a dangerous download. |
845 ShowDownloadInBrowser(download); | 747 ShowDownloadInBrowser(download); |
846 | 748 |
847 // Inform interested objects about the new download. | 749 // Inform interested objects about the new download. |
848 NotifyModelChanged(); | 750 NotifyModelChanged(); |
849 } | 751 } |
850 | 752 |
851 | |
852 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, | 753 void DownloadManagerImpl::OnItemAddedToPersistentStore(int32 download_id, |
853 int64 db_handle) { | 754 int64 db_handle) { |
854 // It's valid that we don't find a matching item, i.e. on shutdown. | 755 // It's valid that we don't find a matching item, i.e. on shutdown. |
855 if (!ContainsKey(downloads_, download_id)) | 756 if (!ContainsKey(downloads_, download_id)) |
856 return; | 757 return; |
857 | 758 |
858 DownloadItemImpl* item = downloads_[download_id]; | 759 DownloadItemImpl* item = downloads_[download_id]; |
859 AddDownloadItemToHistory(item, db_handle); | 760 AddDownloadItemToHistory(item, db_handle); |
860 if (SavePageData::Get(item)) { | 761 if (item->IsSavePackageDownload()) { |
861 OnSavePageItemAddedToPersistentStore(item); | 762 OnSavePageItemAddedToPersistentStore(item); |
862 } else { | 763 } else { |
863 OnDownloadItemAddedToPersistentStore(item); | 764 OnDownloadItemAddedToPersistentStore(item); |
864 } | 765 } |
865 } | 766 } |
866 | 767 |
867 // Once the new DownloadItem has been committed to the persistent store, | 768 // 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), | 769 // 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), | 770 // 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 | 771 // and notify observers (TODO(benjhayden) observers should be able to see the |
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1026 ++num_unopened; | 927 ++num_unopened; |
1027 } | 928 } |
1028 download_stats::RecordOpensOutstanding(num_unopened); | 929 download_stats::RecordOpensOutstanding(num_unopened); |
1029 } | 930 } |
1030 | 931 |
1031 void DownloadManagerImpl::DownloadRenamedToIntermediateName( | 932 void DownloadManagerImpl::DownloadRenamedToIntermediateName( |
1032 DownloadItemImpl* download) { | 933 DownloadItemImpl* download) { |
1033 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 934 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1034 // download->GetFullPath() is only expected to be meaningful after this | 935 // download->GetFullPath() is only expected to be meaningful after this |
1035 // callback is received. Therefore we can now add the download to a persistent | 936 // callback is received. Therefore we can now add the download to a persistent |
1036 // store. If the rename failed, we receive an OnDownloadInterrupted() call | 937 // store. If the rename failed, we processed an interrupt |
1037 // before we receive the DownloadRenamedToIntermediateName() call. | 938 // before we receive the DownloadRenamedToIntermediateName() call. |
1038 if (delegate_) { | 939 if (delegate_) { |
1039 delegate_->AddItemToPersistentStore(download); | 940 delegate_->AddItemToPersistentStore(download); |
1040 } else { | 941 } else { |
1041 OnItemAddedToPersistentStore(download->GetId(), | 942 OnItemAddedToPersistentStore(download->GetId(), |
1042 DownloadItem::kUninitializedHandle); | 943 DownloadItem::kUninitializedHandle); |
1043 } | 944 } |
1044 } | 945 } |
1045 | 946 |
1046 void DownloadManagerImpl::DownloadRenamedToFinalName( | 947 void DownloadManagerImpl::DownloadRenamedToFinalName( |
1047 DownloadItemImpl* download) { | 948 DownloadItemImpl* download) { |
1048 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 949 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
1049 // If the rename failed, we receive an OnDownloadInterrupted() call before we | 950 // If the rename failed, we processed an interrupt before we get here. |
1050 // receive the DownloadRenamedToFinalName() call. | |
1051 if (delegate_) { | 951 if (delegate_) { |
1052 delegate_->UpdatePathForItemInPersistentStore( | 952 delegate_->UpdatePathForItemInPersistentStore( |
1053 download, download->GetFullPath()); | 953 download, download->GetFullPath()); |
1054 } | 954 } |
1055 } | 955 } |
OLD | NEW |