OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/download/download_manager.h" | 5 #include "content/browser/download/download_manager.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/file_util.h" | 11 #include "base/file_util.h" |
12 #include "base/i18n/case_conversion.h" | 12 #include "base/i18n/case_conversion.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
14 #include "base/stl_util.h" | 14 #include "base/stl_util.h" |
15 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
16 #include "base/synchronization/lock.h" | 16 #include "base/synchronization/lock.h" |
17 #include "base/sys_string_conversions.h" | 17 #include "base/sys_string_conversions.h" |
18 #include "build/build_config.h" | 18 #include "build/build_config.h" |
19 #include "content/browser/browser_context.h" | 19 #include "content/browser/browser_context.h" |
20 #include "content/browser/download/download_create_info.h" | 20 #include "content/browser/download/download_create_info.h" |
21 #include "content/browser/download/download_file_manager.h" | 21 #include "content/browser/download/download_file_manager.h" |
| 22 #include "content/browser/download/download_id_factory.h" |
22 #include "content/browser/download/download_item.h" | 23 #include "content/browser/download/download_item.h" |
23 #include "content/browser/download/download_persistent_store_info.h" | 24 #include "content/browser/download/download_persistent_store_info.h" |
24 #include "content/browser/download/download_stats.h" | 25 #include "content/browser/download/download_stats.h" |
25 #include "content/browser/download/download_status_updater.h" | 26 #include "content/browser/download/download_status_updater.h" |
26 #include "content/browser/download/interrupt_reasons.h" | 27 #include "content/browser/download/interrupt_reasons.h" |
27 #include "content/browser/renderer_host/render_process_host.h" | 28 #include "content/browser/renderer_host/render_process_host.h" |
28 #include "content/browser/renderer_host/render_view_host.h" | 29 #include "content/browser/renderer_host/render_view_host.h" |
29 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 30 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
30 #include "content/browser/tab_contents/tab_contents.h" | 31 #include "content/browser/tab_contents/tab_contents.h" |
31 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 request, save_info, true, | 64 request, save_info, true, |
64 DownloadResourceHandler::OnStartedCallback(), | 65 DownloadResourceHandler::OnStartedCallback(), |
65 render_params.render_process_id_, | 66 render_params.render_process_id_, |
66 render_params.render_view_id_, | 67 render_params.render_view_id_, |
67 *context); | 68 *context); |
68 } | 69 } |
69 | 70 |
70 } // namespace | 71 } // namespace |
71 | 72 |
72 DownloadManager::DownloadManager(content::DownloadManagerDelegate* delegate, | 73 DownloadManager::DownloadManager(content::DownloadManagerDelegate* delegate, |
| 74 DownloadIdFactory* id_factory, |
73 DownloadStatusUpdater* status_updater) | 75 DownloadStatusUpdater* status_updater) |
74 : shutdown_needed_(false), | 76 : shutdown_needed_(false), |
75 browser_context_(NULL), | 77 browser_context_(NULL), |
76 next_id_(0), | |
77 file_manager_(NULL), | 78 file_manager_(NULL), |
78 status_updater_((status_updater != NULL) | 79 status_updater_((status_updater != NULL) |
79 ? status_updater->AsWeakPtr() | 80 ? status_updater->AsWeakPtr() |
80 : base::WeakPtr<DownloadStatusUpdater>()), | 81 : base::WeakPtr<DownloadStatusUpdater>()), |
81 delegate_(delegate), | 82 delegate_(delegate), |
| 83 id_factory_(id_factory), |
82 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { | 84 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { |
83 // NOTE(benjhayden): status_updater may be NULL when using | 85 // NOTE(benjhayden): status_updater may be NULL when using |
84 // TestingBrowserProcess. | 86 // TestingBrowserProcess. |
85 if (status_updater_.get() != NULL) | 87 if (status_updater_.get() != NULL) |
86 status_updater_->AddDelegate(this); | 88 status_updater_->AddDelegate(this); |
87 } | 89 } |
88 | 90 |
89 DownloadManager::~DownloadManager() { | 91 DownloadManager::~DownloadManager() { |
90 DCHECK(!shutdown_needed_); | 92 DCHECK(!shutdown_needed_); |
91 if (status_updater_.get() != NULL) | 93 if (status_updater_.get() != NULL) |
92 status_updater_->RemoveDelegate(this); | 94 status_updater_->RemoveDelegate(this); |
93 } | 95 } |
94 | 96 |
| 97 DownloadId DownloadManager::GetNextId() { |
| 98 return id_factory_->GetNextId(); |
| 99 } |
| 100 |
95 void DownloadManager::Shutdown() { | 101 void DownloadManager::Shutdown() { |
96 VLOG(20) << __FUNCTION__ << "()" | 102 VLOG(20) << __FUNCTION__ << "()" |
97 << " shutdown_needed_ = " << shutdown_needed_; | 103 << " shutdown_needed_ = " << shutdown_needed_; |
98 if (!shutdown_needed_) | 104 if (!shutdown_needed_) |
99 return; | 105 return; |
100 shutdown_needed_ = false; | 106 shutdown_needed_ = false; |
101 | 107 |
102 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); | 108 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); |
103 // TODO(benjhayden): Consider clearing observers_. | 109 // TODO(benjhayden): Consider clearing observers_. |
104 | 110 |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
198 // The Incognito Downloads page will get the list of non-Incognito downloads | 204 // The Incognito Downloads page will get the list of non-Incognito downloads |
199 // from its parent profile. | 205 // from its parent profile. |
200 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) | 206 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) |
201 continue; | 207 continue; |
202 | 208 |
203 if (download_item->MatchesQuery(query_lower)) | 209 if (download_item->MatchesQuery(query_lower)) |
204 result->push_back(download_item); | 210 result->push_back(download_item); |
205 } | 211 } |
206 } | 212 } |
207 | 213 |
208 void DownloadManager::OnPersistentStoreGetNextId(int next_id) { | |
209 DVLOG(1) << __FUNCTION__ << " " << next_id; | |
210 base::AutoLock lock(next_id_lock_); | |
211 // TODO(benjhayden) Delay Profile initialization until here, and set next_id_ | |
212 // = next_id. The '+=' works for now because these ids are not yet persisted | |
213 // to the database. GetNextId() can allocate zero or more ids starting from 0, | |
214 // then this callback can increment next_id_, and the items with lower ids | |
215 // won't clash with any other items even though there may be items loaded from | |
216 // the history because items from the history don't have valid ids. | |
217 next_id_ += next_id; | |
218 } | |
219 | |
220 DownloadId DownloadManager::GetNextId() { | |
221 // May be called on any thread via the GetNextIdThunk. | |
222 // TODO(benjhayden) If otr, forward to parent DM. | |
223 base::AutoLock lock(next_id_lock_); | |
224 return DownloadId(this, next_id_++); | |
225 } | |
226 | |
227 DownloadManager::GetNextIdThunkType DownloadManager::GetNextIdThunk() { | |
228 // TODO(benjhayden) If otr, forward to parent DM. | |
229 return base::Bind(&DownloadManager::GetNextId, this); | |
230 } | |
231 | |
232 // Query the history service for information about all persisted downloads. | 214 // Query the history service for information about all persisted downloads. |
233 bool DownloadManager::Init(content::BrowserContext* browser_context) { | 215 bool DownloadManager::Init(content::BrowserContext* browser_context) { |
234 DCHECK(browser_context); | 216 DCHECK(browser_context); |
235 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 217 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
236 shutdown_needed_ = true; | 218 shutdown_needed_ = true; |
237 | 219 |
238 browser_context_ = browser_context; | 220 browser_context_ = browser_context; |
239 | 221 |
240 // In test mode, there may be no ResourceDispatcherHost. In this case it's | 222 // In test mode, there may be no ResourceDispatcherHost. In this case it's |
241 // safe to avoid setting |file_manager_| because we only call a small set of | 223 // safe to avoid setting |file_manager_| because we only call a small set of |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 } | 313 } |
332 } | 314 } |
333 | 315 |
334 void DownloadManager::CreateDownloadItem( | 316 void DownloadManager::CreateDownloadItem( |
335 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { | 317 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { |
336 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 318 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
337 | 319 |
338 DownloadItem* download = new DownloadItem(this, *info, | 320 DownloadItem* download = new DownloadItem(this, *info, |
339 request_handle, | 321 request_handle, |
340 browser_context_->IsOffTheRecord()); | 322 browser_context_->IsOffTheRecord()); |
341 int32 download_id = info->download_id; | 323 int32 download_id = info->download_id.local(); |
342 DCHECK(!ContainsKey(in_progress_, download_id)); | 324 DCHECK(!ContainsKey(in_progress_, download_id)); |
343 | 325 |
344 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. | 326 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. |
345 CHECK(!ContainsKey(active_downloads_, download_id)); | 327 CHECK(!ContainsKey(active_downloads_, download_id)); |
346 downloads_.insert(download); | 328 downloads_.insert(download); |
347 active_downloads_[download_id] = download; | 329 active_downloads_[download_id] = download; |
348 } | 330 } |
349 | 331 |
350 void DownloadManager::ContinueDownloadWithPath(DownloadItem* download, | 332 void DownloadManager::ContinueDownloadWithPath(DownloadItem* download, |
351 const FilePath& chosen_file) { | 333 const FilePath& chosen_file) { |
(...skipping 736 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1088 void DownloadManager::MarkDownloadOpened(DownloadItem* download) { | 1070 void DownloadManager::MarkDownloadOpened(DownloadItem* download) { |
1089 delegate_->UpdateItemInPersistentStore(download); | 1071 delegate_->UpdateItemInPersistentStore(download); |
1090 int num_unopened = 0; | 1072 int num_unopened = 0; |
1091 for (DownloadMap::iterator it = history_downloads_.begin(); | 1073 for (DownloadMap::iterator it = history_downloads_.begin(); |
1092 it != history_downloads_.end(); ++it) { | 1074 it != history_downloads_.end(); ++it) { |
1093 if (it->second->IsComplete() && !it->second->opened()) | 1075 if (it->second->IsComplete() && !it->second->opened()) |
1094 ++num_unopened; | 1076 ++num_unopened; |
1095 } | 1077 } |
1096 download_stats::RecordOpensOutstanding(num_unopened); | 1078 download_stats::RecordOpensOutstanding(num_unopened); |
1097 } | 1079 } |
OLD | NEW |