OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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. |
| 4 |
| 5 #include "chrome/browser/download/download_history.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "chrome/browser/history/download_types.h" |
| 9 #include "chrome/browser/history/history_marshaling.h" |
| 10 #include "chrome/browser/download/download_item.h" |
| 11 #include "chrome/browser/profile.h" |
| 12 |
| 13 // Our download table ID starts at 1, so we use 0 to represent a download that |
| 14 // has started, but has not yet had its data persisted in the table. We use fake |
| 15 // database handles in incognito mode starting at -1 and progressively getting |
| 16 // more negative. |
| 17 // static |
| 18 const int DownloadHistory::kUninitializedHandle = 0; |
| 19 |
| 20 DownloadHistory::DownloadHistory(Profile* profile, DownloadItemMapper* mapper) |
| 21 : profile_(profile), |
| 22 next_fake_db_handle_(kUninitializedHandle - 1), |
| 23 download_item_mapper_(mapper) { |
| 24 DCHECK(profile); |
| 25 DCHECK(mapper); |
| 26 } |
| 27 |
| 28 void DownloadHistory::Load(HistoryService::DownloadQueryCallback* callback) { |
| 29 DCHECK(callback); |
| 30 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 31 if (!hs) { |
| 32 delete callback; |
| 33 return; |
| 34 } |
| 35 hs->QueryDownloads(&history_consumer_, callback); |
| 36 |
| 37 // This is the initial load, so do a cleanup of corrupt in-progress entries. |
| 38 hs->CleanUpInProgressEntries(); |
| 39 } |
| 40 |
| 41 void DownloadHistory::Search(const string16& query, |
| 42 DownloadSearchCallback* callback) { |
| 43 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 44 if (!hs) { |
| 45 delete callback; |
| 46 return; |
| 47 } |
| 48 |
| 49 HistoryService::Handle handle = hs->SearchDownloads( |
| 50 query, |
| 51 &history_consumer_, |
| 52 NewCallback(this, &DownloadHistory::OnSearchDownloadsComplete)); |
| 53 history_consumer_.SetClientData(hs, handle, callback); |
| 54 } |
| 55 |
| 56 void DownloadHistory::AddEntry( |
| 57 const DownloadCreateInfo& info, |
| 58 DownloadItem* download_item, |
| 59 HistoryService::DownloadCreateCallback* callback) { |
| 60 // Do not store the download in the history database for a few special cases: |
| 61 // - incognito mode (that is the point of this mode) |
| 62 // - extensions (users don't think of extension installation as 'downloading') |
| 63 // - temporary download, like in drag-and-drop |
| 64 // - history service is not available (e.g. in tests) |
| 65 // We have to make sure that these handles don't collide with normal db |
| 66 // handles, so we use a negative value. Eventually, they could overlap, but |
| 67 // you'd have to do enough downloading that your ISP would likely stab you in |
| 68 // the neck first. YMMV. |
| 69 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
| 70 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 71 if (download_item->is_otr() || download_item->is_extension_install() || |
| 72 download_item->is_temporary() || !hs) { |
| 73 callback->RunWithParams( |
| 74 history::DownloadCreateRequest::TupleType(info, GetNextFakeDbHandle())); |
| 75 delete callback; |
| 76 return; |
| 77 } |
| 78 |
| 79 hs->CreateDownload(info, &history_consumer_, callback); |
| 80 } |
| 81 |
| 82 void DownloadHistory::UpdateEntry(DownloadItem* download_item) { |
| 83 // Don't store info in the database if the download was initiated while in |
| 84 // incognito mode or if it hasn't been initialized in our database table. |
| 85 if (download_item->db_handle() <= kUninitializedHandle) |
| 86 return; |
| 87 |
| 88 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
| 89 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 90 if (!hs) |
| 91 return; |
| 92 |
| 93 hs->UpdateDownload(download_item->received_bytes(), |
| 94 download_item->state(), |
| 95 download_item->db_handle()); |
| 96 } |
| 97 |
| 98 void DownloadHistory::UpdateDownloadPath(DownloadItem* download_item, |
| 99 const FilePath& new_path) { |
| 100 // No update necessary if the download was initiated while in incognito mode. |
| 101 if (download_item->db_handle() <= kUninitializedHandle) |
| 102 return; |
| 103 |
| 104 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
| 105 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 106 if (hs) |
| 107 hs->UpdateDownloadPath(new_path, download_item->db_handle()); |
| 108 } |
| 109 |
| 110 void DownloadHistory::RemoveEntry(DownloadItem* download_item) { |
| 111 // No update necessary if the download was initiated while in incognito mode. |
| 112 if (download_item->db_handle() <= kUninitializedHandle) |
| 113 return; |
| 114 |
| 115 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
| 116 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 117 if (hs) |
| 118 hs->RemoveDownload(download_item->db_handle()); |
| 119 } |
| 120 |
| 121 void DownloadHistory::RemoveEntriesBetween(const base::Time remove_begin, |
| 122 const base::Time remove_end) { |
| 123 // FIXME(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. |
| 124 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 125 if (hs) |
| 126 hs->RemoveDownloadsBetween(remove_begin, remove_end); |
| 127 } |
| 128 |
| 129 void DownloadHistory::OnSearchDownloadsComplete(HistoryService::Handle handle, |
| 130 std::vector<int64>* results) { |
| 131 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 132 DownloadSearchCallback* callback = |
| 133 history_consumer_.GetClientData(hs, handle); |
| 134 if (!callback) |
| 135 return; |
| 136 |
| 137 std::vector<DownloadItem*> download_items; |
| 138 for (std::vector<int64>::iterator i = results->begin(); |
| 139 i != results->end(); ++i) { |
| 140 DownloadItem* download_item = |
| 141 download_item_mapper_->GetDownloadItemFromDbHandle(*i); |
| 142 if (download_item) |
| 143 download_items.push_back(download_item); |
| 144 } |
| 145 |
| 146 callback->RunWithParams(MakeTuple(download_items)); |
| 147 } |
| 148 |
| 149 int64 DownloadHistory::GetNextFakeDbHandle() { |
| 150 return next_fake_db_handle_--; |
| 151 } |
OLD | NEW |