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 "base/task.h" | 18 #include "base/task.h" |
19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
20 #include "content/browser/browser_context.h" | 20 #include "content/browser/browser_context.h" |
21 #include "content/browser/browser_thread.h" | 21 #include "content/browser/browser_thread.h" |
22 #include "content/browser/download/download_create_info.h" | 22 #include "content/browser/download/download_create_info.h" |
23 #include "content/browser/download/download_file_manager.h" | 23 #include "content/browser/download/download_file_manager.h" |
24 #include "content/browser/download/download_item.h" | 24 #include "content/browser/download/download_item.h" |
25 #include "content/browser/download/download_id_factory.h" | |
Miranda Callahan
2011/10/27 17:00:19
nit: alpha
benjhayden
2011/10/27 19:04:41
Done.
| |
25 #include "content/browser/download/download_persistent_store_info.h" | 26 #include "content/browser/download/download_persistent_store_info.h" |
26 #include "content/browser/download/download_stats.h" | 27 #include "content/browser/download/download_stats.h" |
27 #include "content/browser/download/download_status_updater.h" | 28 #include "content/browser/download/download_status_updater.h" |
28 #include "content/browser/download/interrupt_reasons.h" | 29 #include "content/browser/download/interrupt_reasons.h" |
29 #include "content/browser/renderer_host/render_process_host.h" | 30 #include "content/browser/renderer_host/render_process_host.h" |
30 #include "content/browser/renderer_host/render_view_host.h" | 31 #include "content/browser/renderer_host/render_view_host.h" |
31 #include "content/browser/renderer_host/resource_dispatcher_host.h" | 32 #include "content/browser/renderer_host/resource_dispatcher_host.h" |
32 #include "content/browser/tab_contents/tab_contents.h" | 33 #include "content/browser/tab_contents/tab_contents.h" |
33 #include "content/public/browser/content_browser_client.h" | 34 #include "content/public/browser/content_browser_client.h" |
34 #include "content/public/browser/download_manager_delegate.h" | 35 #include "content/public/browser/download_manager_delegate.h" |
(...skipping 18 matching lines...) Expand all Loading... | |
53 true, | 54 true, |
54 DownloadResourceHandler::OnStartedCallback(), | 55 DownloadResourceHandler::OnStartedCallback(), |
55 render_process_id, | 56 render_process_id, |
56 render_view_id, | 57 render_view_id, |
57 *context); | 58 *context); |
58 } | 59 } |
59 | 60 |
60 } // namespace | 61 } // namespace |
61 | 62 |
62 DownloadManager::DownloadManager(content::DownloadManagerDelegate* delegate, | 63 DownloadManager::DownloadManager(content::DownloadManagerDelegate* delegate, |
64 DownloadIdFactory* id_factory, | |
63 DownloadStatusUpdater* status_updater) | 65 DownloadStatusUpdater* status_updater) |
64 : shutdown_needed_(false), | 66 : shutdown_needed_(false), |
65 browser_context_(NULL), | 67 browser_context_(NULL), |
66 next_id_(0), | |
67 file_manager_(NULL), | 68 file_manager_(NULL), |
68 status_updater_((status_updater != NULL) | 69 status_updater_((status_updater != NULL) |
69 ? status_updater->AsWeakPtr() | 70 ? status_updater->AsWeakPtr() |
70 : base::WeakPtr<DownloadStatusUpdater>()), | 71 : base::WeakPtr<DownloadStatusUpdater>()), |
71 delegate_(delegate), | 72 delegate_(delegate), |
73 id_factory_(id_factory), | |
72 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { | 74 largest_db_handle_in_history_(DownloadItem::kUninitializedHandle) { |
73 // NOTE(benjhayden): status_updater may be NULL when using | 75 // NOTE(benjhayden): status_updater may be NULL when using |
74 // TestingBrowserProcess. | 76 // TestingBrowserProcess. |
75 if (status_updater_.get() != NULL) | 77 if (status_updater_.get() != NULL) |
76 status_updater_->AddDelegate(this); | 78 status_updater_->AddDelegate(this); |
77 } | 79 } |
78 | 80 |
79 DownloadManager::~DownloadManager() { | 81 DownloadManager::~DownloadManager() { |
80 DCHECK(!shutdown_needed_); | 82 DCHECK(!shutdown_needed_); |
81 if (status_updater_.get() != NULL) | 83 if (status_updater_.get() != NULL) |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 // The Incognito Downloads page will get the list of non-Incognito downloads | 191 // The Incognito Downloads page will get the list of non-Incognito downloads |
190 // from its parent profile. | 192 // from its parent profile. |
191 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) | 193 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) |
192 continue; | 194 continue; |
193 | 195 |
194 if (download_item->MatchesQuery(query_lower)) | 196 if (download_item->MatchesQuery(query_lower)) |
195 result->push_back(download_item); | 197 result->push_back(download_item); |
196 } | 198 } |
197 } | 199 } |
198 | 200 |
199 void DownloadManager::OnPersistentStoreGetNextId(int next_id) { | |
200 DVLOG(1) << __FUNCTION__ << " " << next_id; | |
201 base::AutoLock lock(next_id_lock_); | |
202 // TODO(benjhayden) Delay Profile initialization until here, and set next_id_ | |
203 // = next_id. The '+=' works for now because these ids are not yet persisted | |
204 // to the database. GetNextId() can allocate zero or more ids starting from 0, | |
205 // then this callback can increment next_id_, and the items with lower ids | |
206 // won't clash with any other items even though there may be items loaded from | |
207 // the history because items from the history don't have valid ids. | |
208 next_id_ += next_id; | |
209 } | |
210 | |
211 DownloadId DownloadManager::GetNextId() { | |
212 // May be called on any thread via the GetNextIdThunk. | |
213 // TODO(benjhayden) If otr, forward to parent DM. | |
214 base::AutoLock lock(next_id_lock_); | |
215 return DownloadId(this, next_id_++); | |
216 } | |
217 | |
218 DownloadManager::GetNextIdThunkType DownloadManager::GetNextIdThunk() { | |
219 // TODO(benjhayden) If otr, forward to parent DM. | |
220 return base::Bind(&DownloadManager::GetNextId, this); | |
221 } | |
222 | |
223 // Query the history service for information about all persisted downloads. | 201 // Query the history service for information about all persisted downloads. |
224 bool DownloadManager::Init(content::BrowserContext* browser_context) { | 202 bool DownloadManager::Init(content::BrowserContext* browser_context) { |
225 DCHECK(browser_context); | 203 DCHECK(browser_context); |
226 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; | 204 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; |
227 shutdown_needed_ = true; | 205 shutdown_needed_ = true; |
228 | 206 |
229 browser_context_ = browser_context; | 207 browser_context_ = browser_context; |
230 | 208 |
231 // In test mode, there may be no ResourceDispatcherHost. In this case it's | 209 // In test mode, there may be no ResourceDispatcherHost. In this case it's |
232 // safe to avoid setting |file_manager_| because we only call a small set of | 210 // safe to avoid setting |file_manager_| because we only call a small set of |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
325 } | 303 } |
326 } | 304 } |
327 | 305 |
328 void DownloadManager::CreateDownloadItem( | 306 void DownloadManager::CreateDownloadItem( |
329 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { | 307 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { |
330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 308 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
331 | 309 |
332 DownloadItem* download = new DownloadItem(this, *info, | 310 DownloadItem* download = new DownloadItem(this, *info, |
333 request_handle, | 311 request_handle, |
334 browser_context_->IsOffTheRecord()); | 312 browser_context_->IsOffTheRecord()); |
335 int32 download_id = info->download_id; | 313 int32 download_id = info->download_id.local(); |
336 DCHECK(!ContainsKey(in_progress_, download_id)); | 314 DCHECK(!ContainsKey(in_progress_, download_id)); |
337 | 315 |
338 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. | 316 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. |
339 CHECK(!ContainsKey(active_downloads_, download_id)); | 317 CHECK(!ContainsKey(active_downloads_, download_id)); |
340 downloads_.insert(download); | 318 downloads_.insert(download); |
341 active_downloads_[download_id] = download; | 319 active_downloads_[download_id] = download; |
342 } | 320 } |
343 | 321 |
344 void DownloadManager::ContinueDownloadWithPath(DownloadItem* download, | 322 void DownloadManager::ContinueDownloadWithPath(DownloadItem* download, |
345 const FilePath& chosen_file) { | 323 const FilePath& chosen_file) { |
(...skipping 476 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
822 | 800 |
823 // The history service has retrieved all download entries. 'entries' contains | 801 // The history service has retrieved all download entries. 'entries' contains |
824 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). | 802 // 'DownloadPersistentStoreInfo's in sorted order (by ascending start_time). |
825 void DownloadManager::OnPersistentStoreQueryComplete( | 803 void DownloadManager::OnPersistentStoreQueryComplete( |
826 std::vector<DownloadPersistentStoreInfo>* entries) { | 804 std::vector<DownloadPersistentStoreInfo>* entries) { |
827 // TODO(rdsmith): Remove this and related logic when | 805 // TODO(rdsmith): Remove this and related logic when |
828 // http://crbug.com/85408 is fixed. | 806 // http://crbug.com/85408 is fixed. |
829 largest_db_handle_in_history_ = 0; | 807 largest_db_handle_in_history_ = 0; |
830 | 808 |
831 for (size_t i = 0; i < entries->size(); ++i) { | 809 for (size_t i = 0; i < entries->size(); ++i) { |
832 DownloadItem* download = new DownloadItem(this, entries->at(i)); | 810 DownloadItem* download = new DownloadItem(this, entries->at(i), |
811 id_factory_->GetNextId()); | |
833 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. | 812 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. |
834 CHECK(!ContainsKey(history_downloads_, download->db_handle())); | 813 CHECK(!ContainsKey(history_downloads_, download->db_handle())); |
835 downloads_.insert(download); | 814 downloads_.insert(download); |
836 history_downloads_[download->db_handle()] = download; | 815 history_downloads_[download->db_handle()] = download; |
837 VLOG(20) << __FUNCTION__ << "()" << i << ">" | 816 VLOG(20) << __FUNCTION__ << "()" << i << ">" |
838 << " download = " << download->DebugString(true); | 817 << " download = " << download->DebugString(true); |
839 | 818 |
840 if (download->db_handle() > largest_db_handle_in_history_) | 819 if (download->db_handle() > largest_db_handle_in_history_) |
841 largest_db_handle_in_history_ = download->db_handle(); | 820 largest_db_handle_in_history_ = download->db_handle(); |
842 } | 821 } |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1085 void DownloadManager::MarkDownloadOpened(DownloadItem* download) { | 1064 void DownloadManager::MarkDownloadOpened(DownloadItem* download) { |
1086 delegate_->UpdateItemInPersistentStore(download); | 1065 delegate_->UpdateItemInPersistentStore(download); |
1087 int num_unopened = 0; | 1066 int num_unopened = 0; |
1088 for (DownloadMap::iterator it = history_downloads_.begin(); | 1067 for (DownloadMap::iterator it = history_downloads_.begin(); |
1089 it != history_downloads_.end(); ++it) { | 1068 it != history_downloads_.end(); ++it) { |
1090 if (it->second->IsComplete() && !it->second->opened()) | 1069 if (it->second->IsComplete() && !it->second->opened()) |
1091 ++num_unopened; | 1070 ++num_unopened; |
1092 } | 1071 } |
1093 download_stats::RecordOpensOutstanding(num_unopened); | 1072 download_stats::RecordOpensOutstanding(num_unopened); |
1094 } | 1073 } |
OLD | NEW |