favor DCHECK_CURRENTLY_ON for better logs in chrome/browser/download
[chromium-blink-merge.git] / chrome / browser / download / download_history.cc
blobbecc0e55a9b176f50e096857a088c1eac42359dc
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
3 // found in the LICENSE file.
5 // DownloadHistory manages persisting DownloadItems to the history service by
6 // observing a single DownloadManager and all its DownloadItems using an
7 // AllDownloadItemNotifier.
8 //
9 // DownloadHistory decides whether and when to add items to, remove items from,
10 // and update items in the database. DownloadHistory uses DownloadHistoryData to
11 // store per-DownloadItem data such as whether the item is persisted or being
12 // persisted, and the last history::DownloadRow that was passed to the database.
13 // When the DownloadManager and its delegate (ChromeDownloadManagerDelegate) are
14 // initialized, DownloadHistory is created and queries the HistoryService. When
15 // the HistoryService calls back from QueryDownloads() to QueryCallback(),
16 // DownloadHistory uses DownloadManager::CreateDownloadItem() to inform
17 // DownloadManager of these persisted DownloadItems. CreateDownloadItem()
18 // internally calls OnDownloadCreated(), which normally adds items to the
19 // database, so QueryCallback() uses |loading_id_| to disable adding these items
20 // to the database. If a download is removed via OnDownloadRemoved() while the
21 // item is still being added to the database, DownloadHistory uses
22 // |removed_while_adding_| to remember to remove the item when its ItemAdded()
23 // callback is called. All callbacks are bound with a weak pointer to
24 // DownloadHistory to prevent use-after-free bugs.
25 // ChromeDownloadManagerDelegate owns DownloadHistory, and deletes it in
26 // Shutdown(), which is called by DownloadManagerImpl::Shutdown() after all
27 // DownloadItems are destroyed.
29 #include "chrome/browser/download/download_history.h"
31 #include "base/metrics/histogram.h"
32 #include "chrome/browser/download/download_crx_util.h"
33 #include "components/history/content/browser/download_constants_utils.h"
34 #include "components/history/core/browser/download_database.h"
35 #include "components/history/core/browser/download_row.h"
36 #include "components/history/core/browser/history_service.h"
37 #include "content/public/browser/browser_thread.h"
38 #include "content/public/browser/download_item.h"
39 #include "content/public/browser/download_manager.h"
41 #if defined(ENABLE_EXTENSIONS)
42 #include "chrome/browser/extensions/api/downloads/downloads_api.h"
43 #endif
45 namespace {
47 // Per-DownloadItem data. This information does not belong inside DownloadItem,
48 // and keeping maps in DownloadHistory from DownloadItem to this information is
49 // error-prone and complicated. Unfortunately, DownloadHistory::removing_*_ and
50 // removed_while_adding_ cannot be moved into this class partly because
51 // DownloadHistoryData is destroyed when DownloadItems are destroyed, and we
52 // have no control over when DownloadItems are destroyed.
53 class DownloadHistoryData : public base::SupportsUserData::Data {
54 public:
55 enum PersistenceState {
56 NOT_PERSISTED,
57 PERSISTING,
58 PERSISTED,
61 static DownloadHistoryData* Get(content::DownloadItem* item) {
62 base::SupportsUserData::Data* data = item->GetUserData(kKey);
63 return (data == NULL) ? NULL :
64 static_cast<DownloadHistoryData*>(data);
67 static const DownloadHistoryData* Get(const content::DownloadItem* item) {
68 const base::SupportsUserData::Data* data = item->GetUserData(kKey);
69 return (data == NULL) ? NULL
70 : static_cast<const DownloadHistoryData*>(data);
73 explicit DownloadHistoryData(content::DownloadItem* item)
74 : state_(NOT_PERSISTED),
75 was_restored_from_history_(false) {
76 item->SetUserData(kKey, this);
79 ~DownloadHistoryData() override {}
81 PersistenceState state() const { return state_; }
82 void SetState(PersistenceState s) { state_ = s; }
84 bool was_restored_from_history() const { return was_restored_from_history_; }
85 void set_was_restored_from_history(bool value) {
86 was_restored_from_history_ = value;
89 // This allows DownloadHistory::OnDownloadUpdated() to see what changed in a
90 // DownloadItem if anything, in order to prevent writing to the database
91 // unnecessarily. It is nullified when the item is no longer in progress in
92 // order to save memory.
93 history::DownloadRow* info() { return info_.get(); }
94 void set_info(const history::DownloadRow& i) {
95 info_.reset(new history::DownloadRow(i));
97 void clear_info() {
98 info_.reset();
101 private:
102 static const char kKey[];
104 PersistenceState state_;
105 scoped_ptr<history::DownloadRow> info_;
106 bool was_restored_from_history_;
108 DISALLOW_COPY_AND_ASSIGN(DownloadHistoryData);
111 const char DownloadHistoryData::kKey[] =
112 "DownloadItem DownloadHistoryData";
114 history::DownloadRow GetDownloadRow(
115 content::DownloadItem* item) {
116 std::string by_ext_id, by_ext_name;
117 #if defined(ENABLE_EXTENSIONS)
118 extensions::DownloadedByExtension* by_ext =
119 extensions::DownloadedByExtension::Get(item);
120 if (by_ext) {
121 by_ext_id = by_ext->id();
122 by_ext_name = by_ext->name();
124 #endif
126 return history::DownloadRow(
127 item->GetFullPath(),
128 item->GetTargetFilePath(),
129 item->GetUrlChain(),
130 item->GetReferrerUrl(),
131 item->GetMimeType(),
132 item->GetOriginalMimeType(),
133 item->GetStartTime(),
134 item->GetEndTime(),
135 item->GetETag(),
136 item->GetLastModifiedTime(),
137 item->GetReceivedBytes(),
138 item->GetTotalBytes(),
139 history::ToHistoryDownloadState(item->GetState()),
140 history::ToHistoryDownloadDangerType(item->GetDangerType()),
141 history::ToHistoryDownloadInterruptReason(item->GetLastReason()),
142 history::ToHistoryDownloadId(item->GetId()),
143 item->GetOpened(),
144 by_ext_id,
145 by_ext_name);
148 bool ShouldUpdateHistory(const history::DownloadRow* previous,
149 const history::DownloadRow& current) {
150 // Ignore url, referrer, mime_type, original_mime_type, start_time,
151 // id, db_handle, which don't change.
152 return ((previous == NULL) ||
153 (previous->current_path != current.current_path) ||
154 (previous->target_path != current.target_path) ||
155 (previous->end_time != current.end_time) ||
156 (previous->received_bytes != current.received_bytes) ||
157 (previous->total_bytes != current.total_bytes) ||
158 (previous->etag != current.etag) ||
159 (previous->last_modified != current.last_modified) ||
160 (previous->state != current.state) ||
161 (previous->danger_type != current.danger_type) ||
162 (previous->interrupt_reason != current.interrupt_reason) ||
163 (previous->opened != current.opened) ||
164 (previous->by_ext_id != current.by_ext_id) ||
165 (previous->by_ext_name != current.by_ext_name));
168 typedef std::vector<history::DownloadRow> InfoVector;
170 } // anonymous namespace
172 DownloadHistory::HistoryAdapter::HistoryAdapter(
173 history::HistoryService* history)
174 : history_(history) {
176 DownloadHistory::HistoryAdapter::~HistoryAdapter() {}
178 void DownloadHistory::HistoryAdapter::QueryDownloads(
179 const history::HistoryService::DownloadQueryCallback& callback) {
180 history_->QueryDownloads(callback);
183 void DownloadHistory::HistoryAdapter::CreateDownload(
184 const history::DownloadRow& info,
185 const history::HistoryService::DownloadCreateCallback& callback) {
186 history_->CreateDownload(info, callback);
189 void DownloadHistory::HistoryAdapter::UpdateDownload(
190 const history::DownloadRow& data) {
191 history_->UpdateDownload(data);
194 void DownloadHistory::HistoryAdapter::RemoveDownloads(
195 const std::set<uint32>& ids) {
196 history_->RemoveDownloads(ids);
199 DownloadHistory::Observer::Observer() {}
200 DownloadHistory::Observer::~Observer() {}
202 // static
203 bool DownloadHistory::IsPersisted(const content::DownloadItem* item) {
204 const DownloadHistoryData* data = DownloadHistoryData::Get(item);
205 return data && (data->state() == DownloadHistoryData::PERSISTED);
208 DownloadHistory::DownloadHistory(content::DownloadManager* manager,
209 scoped_ptr<HistoryAdapter> history)
210 : notifier_(manager, this),
211 history_(history.Pass()),
212 loading_id_(content::DownloadItem::kInvalidId),
213 history_size_(0),
214 weak_ptr_factory_(this) {
215 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
216 content::DownloadManager::DownloadVector items;
217 notifier_.GetManager()->GetAllDownloads(&items);
218 for (content::DownloadManager::DownloadVector::const_iterator
219 it = items.begin(); it != items.end(); ++it) {
220 OnDownloadCreated(notifier_.GetManager(), *it);
222 history_->QueryDownloads(base::Bind(
223 &DownloadHistory::QueryCallback, weak_ptr_factory_.GetWeakPtr()));
226 DownloadHistory::~DownloadHistory() {
227 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
228 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadHistoryDestroyed());
229 observers_.Clear();
232 void DownloadHistory::AddObserver(DownloadHistory::Observer* observer) {
233 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
234 observers_.AddObserver(observer);
237 void DownloadHistory::RemoveObserver(DownloadHistory::Observer* observer) {
238 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
239 observers_.RemoveObserver(observer);
242 bool DownloadHistory::WasRestoredFromHistory(
243 const content::DownloadItem* download) const {
244 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
245 const DownloadHistoryData* data = DownloadHistoryData::Get(download);
247 // The OnDownloadCreated handler sets the was_restored_from_history flag when
248 // resetting the loading_id_. So one of the two conditions below will hold for
249 // a download restored from history even if the caller of this method is
250 // racing with our OnDownloadCreated handler.
251 return (data && data->was_restored_from_history()) ||
252 download->GetId() == loading_id_;
255 void DownloadHistory::QueryCallback(scoped_ptr<InfoVector> infos) {
256 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
257 // ManagerGoingDown() may have happened before the history loaded.
258 if (!notifier_.GetManager())
259 return;
260 for (InfoVector::const_iterator it = infos->begin();
261 it != infos->end(); ++it) {
262 loading_id_ = history::ToContentDownloadId(it->id);
263 content::DownloadItem* item = notifier_.GetManager()->CreateDownloadItem(
264 loading_id_,
265 it->current_path,
266 it->target_path,
267 it->url_chain,
268 it->referrer_url,
269 it->mime_type,
270 it->original_mime_type,
271 it->start_time,
272 it->end_time,
273 it->etag,
274 it->last_modified,
275 it->received_bytes,
276 it->total_bytes,
277 history::ToContentDownloadState(it->state),
278 history::ToContentDownloadDangerType(it->danger_type),
279 history::ToContentDownloadInterruptReason(it->interrupt_reason),
280 it->opened);
281 #if defined(ENABLE_EXTENSIONS)
282 if (!it->by_ext_id.empty() && !it->by_ext_name.empty()) {
283 new extensions::DownloadedByExtension(
284 item, it->by_ext_id, it->by_ext_name);
285 item->UpdateObservers();
287 #endif
288 DCHECK_EQ(DownloadHistoryData::PERSISTED,
289 DownloadHistoryData::Get(item)->state());
290 ++history_size_;
292 notifier_.GetManager()->CheckForHistoryFilesRemoval();
295 void DownloadHistory::MaybeAddToHistory(content::DownloadItem* item) {
296 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
298 uint32 download_id = item->GetId();
299 DownloadHistoryData* data = DownloadHistoryData::Get(item);
300 bool removing = removing_ids_.find(download_id) != removing_ids_.end();
302 // TODO(benjhayden): Remove IsTemporary().
303 if (download_crx_util::IsExtensionDownload(*item) ||
304 item->IsTemporary() ||
305 (data->state() != DownloadHistoryData::NOT_PERSISTED) ||
306 removing)
307 return;
309 data->SetState(DownloadHistoryData::PERSISTING);
310 if (data->info() == NULL) {
311 // Keep the info here regardless of whether the item is in progress so that,
312 // when ItemAdded() calls OnDownloadUpdated(), it can decide whether to
313 // Update the db and/or clear the info.
314 data->set_info(GetDownloadRow(item));
317 history_->CreateDownload(*data->info(), base::Bind(
318 &DownloadHistory::ItemAdded, weak_ptr_factory_.GetWeakPtr(),
319 download_id));
320 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored(
321 item, *data->info()));
324 void DownloadHistory::ItemAdded(uint32 download_id, bool success) {
325 if (removed_while_adding_.find(download_id) !=
326 removed_while_adding_.end()) {
327 removed_while_adding_.erase(download_id);
328 if (success)
329 ScheduleRemoveDownload(download_id);
330 return;
333 if (!notifier_.GetManager())
334 return;
336 content::DownloadItem* item = notifier_.GetManager()->GetDownload(
337 download_id);
338 if (!item) {
339 // This item will have called OnDownloadDestroyed(). If the item should
340 // have been removed from history, then it would have also called
341 // OnDownloadRemoved(), which would have put |download_id| in
342 // removed_while_adding_, handled above.
343 return;
346 DownloadHistoryData* data = DownloadHistoryData::Get(item);
348 // The sql INSERT statement failed. Avoid an infinite loop: don't
349 // automatically retry. Retry adding the next time the item is updated by
350 // resetting the state to NOT_PERSISTED.
351 if (!success) {
352 DVLOG(20) << __FUNCTION__ << " INSERT failed id=" << download_id;
353 data->SetState(DownloadHistoryData::NOT_PERSISTED);
354 return;
356 data->SetState(DownloadHistoryData::PERSISTED);
358 UMA_HISTOGRAM_CUSTOM_COUNTS("Download.HistorySize2",
359 history_size_,
360 0/*min*/,
361 (1 << 23)/*max*/,
362 (1 << 7)/*num_buckets*/);
363 ++history_size_;
365 // In case the item changed or became temporary while it was being added.
366 // Don't just update all of the item's observers because we're the only
367 // observer that can also see data->state(), which is the only thing that
368 // ItemAdded() changed.
369 OnDownloadUpdated(notifier_.GetManager(), item);
372 void DownloadHistory::OnDownloadCreated(
373 content::DownloadManager* manager, content::DownloadItem* item) {
374 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
376 // All downloads should pass through OnDownloadCreated exactly once.
377 CHECK(!DownloadHistoryData::Get(item));
378 DownloadHistoryData* data = new DownloadHistoryData(item);
379 if (item->GetId() == loading_id_) {
380 data->SetState(DownloadHistoryData::PERSISTED);
381 data->set_was_restored_from_history(true);
382 loading_id_ = content::DownloadItem::kInvalidId;
384 if (item->GetState() == content::DownloadItem::IN_PROGRESS) {
385 data->set_info(GetDownloadRow(item));
387 MaybeAddToHistory(item);
390 void DownloadHistory::OnDownloadUpdated(
391 content::DownloadManager* manager, content::DownloadItem* item) {
392 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
394 DownloadHistoryData* data = DownloadHistoryData::Get(item);
395 if (data->state() == DownloadHistoryData::NOT_PERSISTED) {
396 MaybeAddToHistory(item);
397 return;
399 if (item->IsTemporary()) {
400 OnDownloadRemoved(notifier_.GetManager(), item);
401 return;
404 history::DownloadRow current_info(GetDownloadRow(item));
405 bool should_update = ShouldUpdateHistory(data->info(), current_info);
406 UMA_HISTOGRAM_ENUMERATION("Download.HistoryPropagatedUpdate",
407 should_update, 2);
408 if (should_update) {
409 history_->UpdateDownload(current_info);
410 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadStored(
411 item, current_info));
413 if (item->GetState() == content::DownloadItem::IN_PROGRESS) {
414 data->set_info(current_info);
415 } else {
416 data->clear_info();
420 void DownloadHistory::OnDownloadOpened(
421 content::DownloadManager* manager, content::DownloadItem* item) {
422 OnDownloadUpdated(manager, item);
425 void DownloadHistory::OnDownloadRemoved(
426 content::DownloadManager* manager, content::DownloadItem* item) {
427 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
429 DownloadHistoryData* data = DownloadHistoryData::Get(item);
430 if (data->state() != DownloadHistoryData::PERSISTED) {
431 if (data->state() == DownloadHistoryData::PERSISTING) {
432 // ScheduleRemoveDownload will be called when history_ calls ItemAdded().
433 removed_while_adding_.insert(item->GetId());
435 return;
437 ScheduleRemoveDownload(item->GetId());
438 // This is important: another OnDownloadRemoved() handler could do something
439 // that synchronously fires an OnDownloadUpdated().
440 data->SetState(DownloadHistoryData::NOT_PERSISTED);
441 // ItemAdded increments history_size_ only if the item wasn't
442 // removed_while_adding_, so the next line does not belong in
443 // ScheduleRemoveDownload().
444 --history_size_;
447 void DownloadHistory::ScheduleRemoveDownload(uint32 download_id) {
448 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
450 // For database efficiency, batch removals together if they happen all at
451 // once.
452 if (removing_ids_.empty()) {
453 content::BrowserThread::PostTask(content::BrowserThread::UI, FROM_HERE,
454 base::Bind(&DownloadHistory::RemoveDownloadsBatch,
455 weak_ptr_factory_.GetWeakPtr()));
457 removing_ids_.insert(download_id);
460 void DownloadHistory::RemoveDownloadsBatch() {
461 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
462 IdSet remove_ids;
463 removing_ids_.swap(remove_ids);
464 history_->RemoveDownloads(remove_ids);
465 FOR_EACH_OBSERVER(Observer, observers_, OnDownloadsRemoved(remove_ids));