| 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 "chrome/browser/download/download_history.h" | 5 #include "chrome/browser/download/download_history.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "chrome/browser/history/download_history_info.h" | 8 #include "chrome/browser/history/download_history_info.h" |
| 9 #include "chrome/browser/history/history_marshaling.h" | 9 #include "chrome/browser/history/history_marshaling.h" |
| 10 #include "chrome/browser/download/download_item.h" | 10 #include "chrome/browser/download/download_item.h" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 if (!hs) { | 32 if (!hs) { |
| 33 delete callback; | 33 delete callback; |
| 34 return; | 34 return; |
| 35 } | 35 } |
| 36 hs->QueryDownloads(&history_consumer_, callback); | 36 hs->QueryDownloads(&history_consumer_, callback); |
| 37 | 37 |
| 38 // This is the initial load, so do a cleanup of corrupt in-progress entries. | 38 // This is the initial load, so do a cleanup of corrupt in-progress entries. |
| 39 hs->CleanUpInProgressEntries(); | 39 hs->CleanUpInProgressEntries(); |
| 40 } | 40 } |
| 41 | 41 |
| 42 void DownloadHistory::CheckVisitedReferrerBefore( |
| 43 int32 download_id, |
| 44 const GURL& referrer_url, |
| 45 VisitedBeforeDoneCallback* callback) { |
| 46 DCHECK(callback); |
| 47 |
| 48 if (referrer_url.is_valid()) { |
| 49 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 50 if (hs) { |
| 51 HistoryService::Handle handle = hs->GetVisitCountToHost(referrer_url, |
| 52 &history_consumer_, |
| 53 NewCallback(this, &DownloadHistory::OnGotVisitCountToHost)); |
| 54 visited_before_requests_[handle] = std::make_pair(download_id, callback); |
| 55 return; |
| 56 } |
| 57 } |
| 58 callback->Run(download_id, false); |
| 59 } |
| 60 |
| 42 void DownloadHistory::AddEntry( | 61 void DownloadHistory::AddEntry( |
| 43 DownloadItem* download_item, | 62 DownloadItem* download_item, |
| 44 HistoryService::DownloadCreateCallback* callback) { | 63 HistoryService::DownloadCreateCallback* callback) { |
| 45 DCHECK(download_item); | 64 DCHECK(download_item); |
| 46 // Do not store the download in the history database for a few special cases: | 65 // Do not store the download in the history database for a few special cases: |
| 47 // - incognito mode (that is the point of this mode) | 66 // - incognito mode (that is the point of this mode) |
| 48 // - extensions (users don't think of extension installation as 'downloading') | 67 // - extensions (users don't think of extension installation as 'downloading') |
| 49 // - temporary download, like in drag-and-drop | 68 // - temporary download, like in drag-and-drop |
| 50 // - history service is not available (e.g. in tests) | 69 // - history service is not available (e.g. in tests) |
| 51 // We have to make sure that these handles don't collide with normal db | 70 // We have to make sure that these handles don't collide with normal db |
| 52 // handles, so we use a negative value. Eventually, they could overlap, but | 71 // handles, so we use a negative value. Eventually, they could overlap, but |
| 53 // you'd have to do enough downloading that your ISP would likely stab you in | 72 // you'd have to do enough downloading that your ISP would likely stab you in |
| 54 // the neck first. YMMV. | 73 // the neck first. YMMV. |
| 55 // TODO(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. | |
| 56 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 74 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 57 if (download_item->is_otr() || download_item->is_extension_install() || | 75 if (download_item->is_otr() || download_item->is_extension_install() || |
| 58 download_item->is_temporary() || !hs) { | 76 download_item->is_temporary() || !hs) { |
| 59 callback->RunWithParams( | 77 callback->RunWithParams( |
| 60 history::DownloadCreateRequest::TupleType(download_item->id(), | 78 history::DownloadCreateRequest::TupleType(download_item->id(), |
| 61 GetNextFakeDbHandle())); | 79 GetNextFakeDbHandle())); |
| 62 delete callback; | 80 delete callback; |
| 63 return; | 81 return; |
| 64 } | 82 } |
| 65 | 83 |
| 66 int32 id = download_item->id(); | 84 int32 id = download_item->id(); |
| 67 DownloadHistoryInfo history_info = download_item->GetHistoryInfo(); | 85 DownloadHistoryInfo history_info = download_item->GetHistoryInfo(); |
| 68 hs->CreateDownload(id, history_info, &history_consumer_, callback); | 86 hs->CreateDownload(id, history_info, &history_consumer_, callback); |
| 69 } | 87 } |
| 70 | 88 |
| 71 void DownloadHistory::UpdateEntry(DownloadItem* download_item) { | 89 void DownloadHistory::UpdateEntry(DownloadItem* download_item) { |
| 72 // Don't store info in the database if the download was initiated while in | 90 // Don't store info in the database if the download was initiated while in |
| 73 // incognito mode or if it hasn't been initialized in our database table. | 91 // incognito mode or if it hasn't been initialized in our database table. |
| 74 if (download_item->db_handle() <= kUninitializedHandle) | 92 if (download_item->db_handle() <= kUninitializedHandle) |
| 75 return; | 93 return; |
| 76 | 94 |
| 77 // TODO(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. | |
| 78 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 95 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 79 if (!hs) | 96 if (!hs) |
| 80 return; | 97 return; |
| 81 | 98 |
| 82 hs->UpdateDownload(download_item->received_bytes(), | 99 hs->UpdateDownload(download_item->received_bytes(), |
| 83 download_item->state(), | 100 download_item->state(), |
| 84 download_item->db_handle()); | 101 download_item->db_handle()); |
| 85 } | 102 } |
| 86 | 103 |
| 87 void DownloadHistory::UpdateDownloadPath(DownloadItem* download_item, | 104 void DownloadHistory::UpdateDownloadPath(DownloadItem* download_item, |
| 88 const FilePath& new_path) { | 105 const FilePath& new_path) { |
| 89 // No update necessary if the download was initiated while in incognito mode. | 106 // No update necessary if the download was initiated while in incognito mode. |
| 90 if (download_item->db_handle() <= kUninitializedHandle) | 107 if (download_item->db_handle() <= kUninitializedHandle) |
| 91 return; | 108 return; |
| 92 | 109 |
| 93 // TODO(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. | |
| 94 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 110 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 95 if (hs) | 111 if (hs) |
| 96 hs->UpdateDownloadPath(new_path, download_item->db_handle()); | 112 hs->UpdateDownloadPath(new_path, download_item->db_handle()); |
| 97 } | 113 } |
| 98 | 114 |
| 99 void DownloadHistory::RemoveEntry(DownloadItem* download_item) { | 115 void DownloadHistory::RemoveEntry(DownloadItem* download_item) { |
| 100 // No update necessary if the download was initiated while in incognito mode. | 116 // No update necessary if the download was initiated while in incognito mode. |
| 101 if (download_item->db_handle() <= kUninitializedHandle) | 117 if (download_item->db_handle() <= kUninitializedHandle) |
| 102 return; | 118 return; |
| 103 | 119 |
| 104 // TODO(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. | |
| 105 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 120 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 106 if (hs) | 121 if (hs) |
| 107 hs->RemoveDownload(download_item->db_handle()); | 122 hs->RemoveDownload(download_item->db_handle()); |
| 108 } | 123 } |
| 109 | 124 |
| 110 void DownloadHistory::RemoveEntriesBetween(const base::Time remove_begin, | 125 void DownloadHistory::RemoveEntriesBetween(const base::Time remove_begin, |
| 111 const base::Time remove_end) { | 126 const base::Time remove_end) { |
| 112 // TODO(paulg) see bug 958058. EXPLICIT_ACCESS below is wrong. | |
| 113 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); | 127 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); |
| 114 if (hs) | 128 if (hs) |
| 115 hs->RemoveDownloadsBetween(remove_begin, remove_end); | 129 hs->RemoveDownloadsBetween(remove_begin, remove_end); |
| 116 } | 130 } |
| 117 | 131 |
| 118 int64 DownloadHistory::GetNextFakeDbHandle() { | 132 int64 DownloadHistory::GetNextFakeDbHandle() { |
| 119 return next_fake_db_handle_--; | 133 return next_fake_db_handle_--; |
| 120 } | 134 } |
| 135 |
| 136 void DownloadHistory::OnGotVisitCountToHost(HistoryService::Handle handle, |
| 137 bool found_visits, |
| 138 int count, |
| 139 base::Time first_visit) { |
| 140 VisitedBeforeRequestsMap::iterator request = |
| 141 visited_before_requests_.find(handle); |
| 142 DCHECK(request != visited_before_requests_.end()); |
| 143 int32 download_id = request->second.first; |
| 144 VisitedBeforeDoneCallback* callback = request->second.second; |
| 145 visited_before_requests_.erase(request); |
| 146 callback->Run(download_id, found_visits && count && |
| 147 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); |
| 148 } |
| OLD | NEW |