| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/download/download_crx_util.h" | 8 #include "chrome/browser/download/download_crx_util.h" |
| 9 #include "chrome/browser/history/history_marshaling.h" | 9 #include "chrome/browser/history/history_marshaling.h" |
| 10 #include "chrome/browser/history/history_service_factory.h" | 10 #include "chrome/browser/history/history_service_factory.h" |
| 11 #include "chrome/browser/profiles/profile.h" | 11 #include "chrome/browser/profiles/profile.h" |
| 12 #include "content/public/browser/download_item.h" | 12 #include "content/public/browser/download_item.h" |
| 13 #include "content/public/browser/download_persistent_store_info.h" | 13 #include "content/public/browser/download_persistent_store_info.h" |
| 14 | 14 |
| 15 using content::DownloadItem; | 15 using content::DownloadItem; |
| 16 using content::DownloadPersistentStoreInfo; | 16 using content::DownloadPersistentStoreInfo; |
| 17 | 17 |
| 18 DownloadHistory::DownloadHistory(Profile* profile) | 18 DownloadHistory::DownloadHistory(Profile* profile) |
| 19 : profile_(profile), | 19 : profile_(profile), |
| 20 next_fake_db_handle_(DownloadItem::kUninitializedHandle - 1) { | 20 next_fake_db_handle_(DownloadItem::kUninitializedHandle - 1) { |
| 21 DCHECK(profile); | 21 DCHECK(profile); |
| 22 } | 22 } |
| 23 | 23 |
| 24 DownloadHistory::~DownloadHistory() {} | 24 DownloadHistory::~DownloadHistory() {} |
| 25 | 25 |
| 26 void DownloadHistory::GetNextId( | 26 void DownloadHistory::GetNextId( |
| 27 const HistoryService::DownloadNextIdCallback& callback) { | 27 const HistoryService::DownloadNextIdCallback& callback) { |
| 28 HistoryService* hs = | 28 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( |
| 29 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 29 profile_, Profile::EXPLICIT_ACCESS); |
| 30 if (!hs) | 30 if (!hs) |
| 31 return; | 31 return; |
| 32 | 32 |
| 33 hs->GetNextDownloadId(&history_consumer_, callback); | 33 hs->GetNextDownloadId(&history_consumer_, callback); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void DownloadHistory::Load( | 36 void DownloadHistory::Load( |
| 37 const HistoryService::DownloadQueryCallback& callback) { | 37 const HistoryService::DownloadQueryCallback& callback) { |
| 38 HistoryService* hs = | 38 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( |
| 39 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 39 profile_, Profile::EXPLICIT_ACCESS); |
| 40 if (!hs) | 40 if (!hs) |
| 41 return; | 41 return; |
| 42 | 42 |
| 43 hs->QueryDownloads(&history_consumer_, callback); | 43 hs->QueryDownloads(&history_consumer_, callback); |
| 44 | 44 |
| 45 // This is the initial load, so do a cleanup of corrupt in-progress entries. | 45 // This is the initial load, so do a cleanup of corrupt in-progress entries. |
| 46 hs->CleanUpInProgressEntries(); | 46 hs->CleanUpInProgressEntries(); |
| 47 } | 47 } |
| 48 | 48 |
| 49 void DownloadHistory::CheckVisitedReferrerBefore( | 49 void DownloadHistory::CheckVisitedReferrerBefore( |
| 50 int32 download_id, | 50 int32 download_id, |
| 51 const GURL& referrer_url, | 51 const GURL& referrer_url, |
| 52 const VisitedBeforeDoneCallback& callback) { | 52 const VisitedBeforeDoneCallback& callback) { |
| 53 if (referrer_url.is_valid()) { | 53 if (referrer_url.is_valid()) { |
| 54 HistoryService* hs = | 54 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( |
| 55 HistoryServiceFactory::GetForProfile(profile_, | 55 profile_, Profile::EXPLICIT_ACCESS); |
| 56 Profile::EXPLICIT_ACCESS); | |
| 57 if (hs) { | 56 if (hs) { |
| 58 HistoryService::Handle handle = | 57 HistoryService::Handle handle = |
| 59 hs->GetVisibleVisitCountToHost(referrer_url, &history_consumer_, | 58 hs->GetVisibleVisitCountToHost(referrer_url, &history_consumer_, |
| 60 base::Bind(&DownloadHistory::OnGotVisitCountToHost, | 59 base::Bind(&DownloadHistory::OnGotVisitCountToHost, |
| 61 base::Unretained(this))); | 60 base::Unretained(this))); |
| 62 visited_before_requests_[handle] = callback; | 61 visited_before_requests_[handle] = callback; |
| 63 return; | 62 return; |
| 64 } | 63 } |
| 65 } | 64 } |
| 66 callback.Run(false); | 65 callback.Run(false); |
| 67 } | 66 } |
| 68 | 67 |
| 69 void DownloadHistory::AddEntry( | 68 void DownloadHistory::AddEntry( |
| 70 DownloadItem* download_item, | 69 DownloadItem* download_item, |
| 71 const HistoryService::DownloadCreateCallback& callback) { | 70 const HistoryService::DownloadCreateCallback& callback) { |
| 72 DCHECK(download_item); | 71 DCHECK(download_item); |
| 73 // Do not store the download in the history database for a few special cases: | 72 // Do not store the download in the history database for a few special cases: |
| 74 // - incognito mode (that is the point of this mode) | 73 // - incognito mode (that is the point of this mode) |
| 75 // - extensions (users don't think of extension installation as 'downloading') | 74 // - extensions (users don't think of extension installation as 'downloading') |
| 76 // - temporary download, like in drag-and-drop | 75 // - temporary download, like in drag-and-drop |
| 77 // - history service is not available (e.g. in tests) | 76 // - history service is not available (e.g. in tests) |
| 78 // We have to make sure that these handles don't collide with normal db | 77 // We have to make sure that these handles don't collide with normal db |
| 79 // handles, so we use a negative value. Eventually, they could overlap, but | 78 // handles, so we use a negative value. Eventually, they could overlap, but |
| 80 // you'd have to do enough downloading that your ISP would likely stab you in | 79 // you'd have to do enough downloading that your ISP would likely stab you in |
| 81 // the neck first. YMMV. | 80 // the neck first. YMMV. |
| 82 HistoryService* hs = | 81 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( |
| 83 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 82 profile_, Profile::EXPLICIT_ACCESS); |
| 84 if (download_item->IsOtr() || | 83 if (download_item->IsOtr() || |
| 85 download_crx_util::IsExtensionDownload(*download_item) || | 84 download_crx_util::IsExtensionDownload(*download_item) || |
| 86 download_item->IsTemporary() || !hs) { | 85 download_item->IsTemporary() || !hs) { |
| 87 callback.Run(download_item->GetId(), GetNextFakeDbHandle()); | 86 callback.Run(download_item->GetId(), GetNextFakeDbHandle()); |
| 88 return; | 87 return; |
| 89 } | 88 } |
| 90 | 89 |
| 91 int32 id = download_item->GetId(); | 90 int32 id = download_item->GetId(); |
| 92 DownloadPersistentStoreInfo history_info = | 91 DownloadPersistentStoreInfo history_info = |
| 93 download_item->GetPersistentStoreInfo(); | 92 download_item->GetPersistentStoreInfo(); |
| 94 hs->CreateDownload(id, history_info, &history_consumer_, callback); | 93 hs->CreateDownload(id, history_info, &history_consumer_, callback); |
| 95 } | 94 } |
| 96 | 95 |
| 97 void DownloadHistory::UpdateEntry(DownloadItem* download_item) { | 96 void DownloadHistory::UpdateEntry(DownloadItem* download_item) { |
| 98 // Don't store info in the database if the download was initiated while in | 97 // Don't store info in the database if the download was initiated while in |
| 99 // incognito mode or if it hasn't been initialized in our database table. | 98 // incognito mode or if it hasn't been initialized in our database table. |
| 100 if (download_item->GetDbHandle() <= DownloadItem::kUninitializedHandle) | 99 if (download_item->GetDbHandle() <= DownloadItem::kUninitializedHandle) |
| 101 return; | 100 return; |
| 102 | 101 |
| 103 HistoryService* hs = | 102 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( |
| 104 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 103 profile_, Profile::EXPLICIT_ACCESS); |
| 105 if (!hs) | 104 if (!hs) |
| 106 return; | 105 return; |
| 107 hs->UpdateDownload(download_item->GetPersistentStoreInfo()); | 106 hs->UpdateDownload(download_item->GetPersistentStoreInfo()); |
| 108 } | 107 } |
| 109 | 108 |
| 110 void DownloadHistory::UpdateDownloadPath(DownloadItem* download_item, | 109 void DownloadHistory::UpdateDownloadPath(DownloadItem* download_item, |
| 111 const FilePath& new_path) { | 110 const FilePath& new_path) { |
| 112 // No update necessary if the download was initiated while in incognito mode. | 111 // No update necessary if the download was initiated while in incognito mode. |
| 113 if (download_item->GetDbHandle() <= DownloadItem::kUninitializedHandle) | 112 if (download_item->GetDbHandle() <= DownloadItem::kUninitializedHandle) |
| 114 return; | 113 return; |
| 115 | 114 |
| 116 HistoryService* hs = | 115 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( |
| 117 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 116 profile_, Profile::EXPLICIT_ACCESS); |
| 118 if (hs) | 117 if (hs) |
| 119 hs->UpdateDownloadPath(new_path, download_item->GetDbHandle()); | 118 hs->UpdateDownloadPath(new_path, download_item->GetDbHandle()); |
| 120 } | 119 } |
| 121 | 120 |
| 122 void DownloadHistory::RemoveEntry(DownloadItem* download_item) { | 121 void DownloadHistory::RemoveEntry(DownloadItem* download_item) { |
| 123 // No update necessary if the download was initiated while in incognito mode. | 122 // No update necessary if the download was initiated while in incognito mode. |
| 124 if (download_item->GetDbHandle() <= DownloadItem::kUninitializedHandle) | 123 if (download_item->GetDbHandle() <= DownloadItem::kUninitializedHandle) |
| 125 return; | 124 return; |
| 126 | 125 |
| 127 HistoryService* hs = | 126 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( |
| 128 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 127 profile_, Profile::EXPLICIT_ACCESS); |
| 129 if (hs) | 128 if (hs) |
| 130 hs->RemoveDownload(download_item->GetDbHandle()); | 129 hs->RemoveDownload(download_item->GetDbHandle()); |
| 131 } | 130 } |
| 132 | 131 |
| 133 void DownloadHistory::RemoveEntriesBetween(const base::Time remove_begin, | 132 void DownloadHistory::RemoveEntriesBetween(const base::Time remove_begin, |
| 134 const base::Time remove_end) { | 133 const base::Time remove_end) { |
| 135 HistoryService* hs = | 134 HistoryService* hs = HistoryServiceFactory::GetForProfileIfExists( |
| 136 HistoryServiceFactory::GetForProfile(profile_, Profile::EXPLICIT_ACCESS); | 135 profile_, Profile::EXPLICIT_ACCESS); |
| 137 if (hs) | 136 if (hs) |
| 138 hs->RemoveDownloadsBetween(remove_begin, remove_end); | 137 hs->RemoveDownloadsBetween(remove_begin, remove_end); |
| 139 } | 138 } |
| 140 | 139 |
| 141 int64 DownloadHistory::GetNextFakeDbHandle() { | 140 int64 DownloadHistory::GetNextFakeDbHandle() { |
| 142 return next_fake_db_handle_--; | 141 return next_fake_db_handle_--; |
| 143 } | 142 } |
| 144 | 143 |
| 145 void DownloadHistory::OnGotVisitCountToHost(HistoryService::Handle handle, | 144 void DownloadHistory::OnGotVisitCountToHost(HistoryService::Handle handle, |
| 146 bool found_visits, | 145 bool found_visits, |
| 147 int count, | 146 int count, |
| 148 base::Time first_visit) { | 147 base::Time first_visit) { |
| 149 VisitedBeforeRequestsMap::iterator request = | 148 VisitedBeforeRequestsMap::iterator request = |
| 150 visited_before_requests_.find(handle); | 149 visited_before_requests_.find(handle); |
| 151 DCHECK(request != visited_before_requests_.end()); | 150 DCHECK(request != visited_before_requests_.end()); |
| 152 VisitedBeforeDoneCallback callback = request->second; | 151 VisitedBeforeDoneCallback callback = request->second; |
| 153 visited_before_requests_.erase(request); | 152 visited_before_requests_.erase(request); |
| 154 callback.Run(found_visits && count && | 153 callback.Run(found_visits && count && |
| 155 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); | 154 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); |
| 156 } | 155 } |
| OLD | NEW |