Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(346)

Side by Side Diff: content/browser/download/download_manager.cc

Issue 8401001: Fix history importing by delaying DownloadManager creation. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: merge Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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/download/download_create_info.h" 21 #include "content/browser/download/download_create_info.h"
22 #include "content/browser/download/download_file_manager.h" 22 #include "content/browser/download/download_file_manager.h"
23 #include "content/browser/download/download_id_factory.h"
23 #include "content/browser/download/download_item.h" 24 #include "content/browser/download/download_item.h"
24 #include "content/browser/download/download_persistent_store_info.h" 25 #include "content/browser/download/download_persistent_store_info.h"
25 #include "content/browser/download/download_stats.h" 26 #include "content/browser/download/download_stats.h"
26 #include "content/browser/download/download_status_updater.h" 27 #include "content/browser/download/download_status_updater.h"
27 #include "content/browser/download/interrupt_reasons.h" 28 #include "content/browser/download/interrupt_reasons.h"
28 #include "content/browser/renderer_host/render_process_host.h" 29 #include "content/browser/renderer_host/render_process_host.h"
29 #include "content/browser/renderer_host/render_view_host.h" 30 #include "content/browser/renderer_host/render_view_host.h"
30 #include "content/browser/renderer_host/resource_dispatcher_host.h" 31 #include "content/browser/renderer_host/resource_dispatcher_host.h"
31 #include "content/browser/tab_contents/tab_contents.h" 32 #include "content/browser/tab_contents/tab_contents.h"
32 #include "content/public/browser/browser_thread.h" 33 #include "content/public/browser/browser_thread.h"
(...skipping 20 matching lines...) Expand all
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)
82 status_updater_->RemoveDelegate(this); 84 status_updater_->RemoveDelegate(this);
83 } 85 }
84 86
87 DownloadId DownloadManager::GetNextId() {
88 return id_factory_->GetNextId();
89 }
90
85 void DownloadManager::Shutdown() { 91 void DownloadManager::Shutdown() {
86 VLOG(20) << __FUNCTION__ << "()" 92 VLOG(20) << __FUNCTION__ << "()"
87 << " shutdown_needed_ = " << shutdown_needed_; 93 << " shutdown_needed_ = " << shutdown_needed_;
88 if (!shutdown_needed_) 94 if (!shutdown_needed_)
89 return; 95 return;
90 shutdown_needed_ = false; 96 shutdown_needed_ = false;
91 97
92 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown()); 98 FOR_EACH_OBSERVER(Observer, observers_, ManagerGoingDown());
93 // TODO(benjhayden): Consider clearing observers_. 99 // TODO(benjhayden): Consider clearing observers_.
94 100
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
189 // The Incognito Downloads page will get the list of non-Incognito downloads 195 // The Incognito Downloads page will get the list of non-Incognito downloads
190 // from its parent profile. 196 // from its parent profile.
191 if (browser_context_->IsOffTheRecord() != download_item->is_otr()) 197 if (browser_context_->IsOffTheRecord() != download_item->is_otr())
192 continue; 198 continue;
193 199
194 if (download_item->MatchesQuery(query_lower)) 200 if (download_item->MatchesQuery(query_lower))
195 result->push_back(download_item); 201 result->push_back(download_item);
196 } 202 }
197 } 203 }
198 204
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. 205 // Query the history service for information about all persisted downloads.
224 bool DownloadManager::Init(content::BrowserContext* browser_context) { 206 bool DownloadManager::Init(content::BrowserContext* browser_context) {
225 DCHECK(browser_context); 207 DCHECK(browser_context);
226 DCHECK(!shutdown_needed_) << "DownloadManager already initialized."; 208 DCHECK(!shutdown_needed_) << "DownloadManager already initialized.";
227 shutdown_needed_ = true; 209 shutdown_needed_ = true;
228 210
229 browser_context_ = browser_context; 211 browser_context_ = browser_context;
230 212
231 // In test mode, there may be no ResourceDispatcherHost. In this case it's 213 // 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 214 // 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
325 } 307 }
326 } 308 }
327 309
328 void DownloadManager::CreateDownloadItem( 310 void DownloadManager::CreateDownloadItem(
329 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) { 311 DownloadCreateInfo* info, const DownloadRequestHandle& request_handle) {
330 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 312 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
331 313
332 DownloadItem* download = new DownloadItem(this, *info, 314 DownloadItem* download = new DownloadItem(this, *info,
333 request_handle, 315 request_handle,
334 browser_context_->IsOffTheRecord()); 316 browser_context_->IsOffTheRecord());
335 int32 download_id = info->download_id; 317 int32 download_id = info->download_id.local();
336 DCHECK(!ContainsKey(in_progress_, download_id)); 318 DCHECK(!ContainsKey(in_progress_, download_id));
337 319
338 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved. 320 // TODO(rdsmith): Remove after http://crbug.com/85408 resolved.
339 CHECK(!ContainsKey(active_downloads_, download_id)); 321 CHECK(!ContainsKey(active_downloads_, download_id));
340 downloads_.insert(download); 322 downloads_.insert(download);
341 active_downloads_[download_id] = download; 323 active_downloads_[download_id] = download;
342 } 324 }
343 325
344 void DownloadManager::ContinueDownloadWithPath(DownloadItem* download, 326 void DownloadManager::ContinueDownloadWithPath(DownloadItem* download,
345 const FilePath& chosen_file) { 327 const FilePath& chosen_file) {
(...skipping 739 matching lines...) Expand 10 before | Expand all | Expand 10 after
1085 void DownloadManager::MarkDownloadOpened(DownloadItem* download) { 1067 void DownloadManager::MarkDownloadOpened(DownloadItem* download) {
1086 delegate_->UpdateItemInPersistentStore(download); 1068 delegate_->UpdateItemInPersistentStore(download);
1087 int num_unopened = 0; 1069 int num_unopened = 0;
1088 for (DownloadMap::iterator it = history_downloads_.begin(); 1070 for (DownloadMap::iterator it = history_downloads_.begin();
1089 it != history_downloads_.end(); ++it) { 1071 it != history_downloads_.end(); ++it) {
1090 if (it->second->IsComplete() && !it->second->opened()) 1072 if (it->second->IsComplete() && !it->second->opened())
1091 ++num_unopened; 1073 ++num_unopened;
1092 } 1074 }
1093 download_stats::RecordOpensOutstanding(num_unopened); 1075 download_stats::RecordOpensOutstanding(num_unopened);
1094 } 1076 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698