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

Side by Side Diff: chrome/browser/download/download_history.cc

Issue 7192016: chrome.experimental.downloads (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: merged db_handle, id; onCreated, onErased Created 9 years, 5 months 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
« no previous file with comments | « chrome/browser/download/download_history.h ('k') | chrome/browser/download/download_item.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "chrome/browser/download/download_history.h" 5 #include "chrome/browser/download/download_history.h"
6 6
7 #include "base/bind.h"
7 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "chrome/browser/download/download_item.h"
10 #include "chrome/browser/download/download_manager.h"
11 #include "chrome/browser/download/download_prefs.h"
8 #include "chrome/browser/history/download_history_info.h" 12 #include "chrome/browser/history/download_history_info.h"
9 #include "chrome/browser/history/history_marshaling.h" 13 #include "chrome/browser/history/history_marshaling.h"
10 #include "chrome/browser/download/download_item.h"
11 #include "chrome/browser/profiles/profile.h" 14 #include "chrome/browser/profiles/profile.h"
12 15
13 // Our download table ID starts at 1, so we use 0 to represent a download that 16 namespace {
14 // has started, but has not yet had its data persisted in the table. We use fake 17 int GetNextIdThunk(Profile* profile) {
15 // database handles in incognito mode starting at -1 and progressively getting 18 DCHECK(profile != NULL);
16 // more negative. 19 DCHECK(profile->GetDownloadManager() != NULL);
17 // static 20 DCHECK(profile->GetDownloadManager()->download_prefs() != NULL);
18 const int DownloadHistory::kUninitializedHandle = 0; 21 if ((profile == NULL) ||
22 (profile->GetDownloadManager() == NULL) ||
23 (profile->GetDownloadManager()->download_prefs() == NULL)) return -1;
24 return profile->GetDownloadManager()->download_prefs()->GetNextId();
25 }
26 } // anonymous namespace
19 27
20 DownloadHistory::DownloadHistory(Profile* profile) 28 DownloadHistory::DownloadHistory(Profile* profile)
21 : profile_(profile), 29 : profile_(profile) {
22 next_fake_db_handle_(kUninitializedHandle - 1) {
23 DCHECK(profile); 30 DCHECK(profile);
24 } 31 }
25 32
26 DownloadHistory::~DownloadHistory() { 33 DownloadHistory::~DownloadHistory() {
27 // For any outstanding requests to 34 // For any outstanding requests to
28 // HistoryService::GetVisibleVisitCountToHost(), since they'll be cancelled 35 // HistoryService::GetVisibleVisitCountToHost(), since they'll be cancelled
29 // and thus not call back to OnGotVisitCountToHost(), we need to delete the 36 // and thus not call back to OnGotVisitCountToHost(), we need to delete the
30 // associated VisitedBeforeDoneCallbacks. 37 // associated VisitedBeforeDoneCallbacks.
31 for (VisitedBeforeRequestsMap::iterator i(visited_before_requests_.begin()); 38 for (VisitedBeforeRequestsMap::iterator i(visited_before_requests_.begin());
32 i != visited_before_requests_.end(); ++i) 39 i != visited_before_requests_.end(); ++i)
33 delete i->second.second; 40 delete i->second.second;
34 } 41 }
35 42
36 void DownloadHistory::Load(HistoryService::DownloadQueryCallback* callback) { 43 void DownloadHistory::Load(HistoryService::DownloadQueryCallback* callback) {
37 DCHECK(callback); 44 DCHECK(callback);
38 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); 45 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
39 if (!hs) { 46 if (!hs) {
40 delete callback; 47 delete callback;
41 return; 48 return;
42 } 49 }
43 hs->QueryDownloads(&history_consumer_, callback); 50 hs->QueryDownloads(&history_consumer_,
51 base::Bind(&GetNextIdThunk, profile_),
52 callback);
44 53
45 // This is the initial load, so do a cleanup of corrupt in-progress entries. 54 // This is the initial load, so do a cleanup of corrupt in-progress entries.
46 hs->CleanUpInProgressEntries(); 55 hs->CleanUpInProgressEntries();
47 } 56 }
48 57
49 void DownloadHistory::CheckVisitedReferrerBefore( 58 void DownloadHistory::CheckVisitedReferrerBefore(
50 int32 download_id, 59 int32 download_id,
51 const GURL& referrer_url, 60 const GURL& referrer_url,
52 VisitedBeforeDoneCallback* callback) { 61 VisitedBeforeDoneCallback* callback) {
53 DCHECK(callback); 62 DCHECK(callback);
54 63
55 if (referrer_url.is_valid()) { 64 if (referrer_url.is_valid()) {
56 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); 65 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
57 if (hs) { 66 if (hs) {
58 HistoryService::Handle handle = 67 HistoryService::Handle handle =
59 hs->GetVisibleVisitCountToHost(referrer_url, &history_consumer_, 68 hs->GetVisibleVisitCountToHost(referrer_url, &history_consumer_,
60 NewCallback(this, &DownloadHistory::OnGotVisitCountToHost)); 69 NewCallback(this, &DownloadHistory::OnGotVisitCountToHost));
61 visited_before_requests_[handle] = std::make_pair(download_id, callback); 70 visited_before_requests_[handle] = std::make_pair(download_id, callback);
62 return; 71 return;
63 } 72 }
64 } 73 }
65 callback->Run(download_id, false); 74 callback->Run(download_id, false);
66 delete callback; 75 delete callback;
67 } 76 }
68 77
69 void DownloadHistory::AddEntry( 78 void DownloadHistory::AddEntry(
70 DownloadItem* download_item, 79 DownloadItem* download_item,
71 HistoryService::DownloadCreateCallback* callback) { 80 HistoryService::DownloadCreateCallback* callback) {
72 DCHECK(download_item); 81 DCHECK(download_item);
73 // Do not store the download in the history database for a few special cases:
74 // - incognito mode (that is the point of this mode)
75 // - extensions (users don't think of extension installation as 'downloading')
76 // - temporary download, like in drag-and-drop
77 // - history service is not available (e.g. in tests)
78 // 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
80 // you'd have to do enough downloading that your ISP would likely stab you in
81 // the neck first. YMMV.
82 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); 82 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
83 if (download_item->is_otr() || download_item->is_extension_install() || 83 if (download_item->is_otr() ||
84 download_item->is_temporary() || !hs) { 84 download_item->is_extension_install() ||
85 callback->RunWithParams( 85 download_item->is_temporary() ||
86 history::DownloadCreateRequest::TupleType(download_item->id(), 86 (hs == NULL)) {
87 GetNextFakeDbHandle())); 87 callback->Run(download_item->id());
88 delete callback; 88 delete callback;
89 return; 89 return;
90 } 90 }
91 91
92 int32 id = download_item->id();
93 DownloadHistoryInfo history_info = download_item->GetHistoryInfo(); 92 DownloadHistoryInfo history_info = download_item->GetHistoryInfo();
94 hs->CreateDownload(id, history_info, &history_consumer_, callback); 93 hs->CreateDownload(download_item->id(),
94 history_info,
95 &history_consumer_,
96 callback);
95 } 97 }
96 98
97 void DownloadHistory::UpdateEntry(DownloadItem* download_item) { 99 void DownloadHistory::UpdateEntry(DownloadItem* download_item) {
98 // Don't store info in the database if the download was initiated while in 100 // 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. 101 // incognito mode or if it hasn't been initialized in our database table.
100 if (download_item->db_handle() <= kUninitializedHandle)
101 return;
102
103 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); 102 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
104 if (!hs) 103 if ((download_item == NULL) ||
105 return; 104 (hs == NULL) ||
106 105 !download_item->IsInHistory()) return;
107 hs->UpdateDownload(download_item->received_bytes(), 106 hs->UpdateDownload(download_item->received_bytes(),
108 download_item->state(), 107 download_item->state(),
109 download_item->db_handle()); 108 download_item->id());
110 } 109 }
111 110
112 void DownloadHistory::UpdateDownloadPath(DownloadItem* download_item, 111 void DownloadHistory::UpdateDownloadPath(DownloadItem* download_item,
113 const FilePath& new_path) { 112 const FilePath& new_path) {
114 // No update necessary if the download was initiated while in incognito mode. 113 // No update necessary if the download was initiated while in incognito mode.
115 if (download_item->db_handle() <= kUninitializedHandle)
116 return;
117
118 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); 114 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
119 if (hs) 115 if ((hs == NULL) ||
120 hs->UpdateDownloadPath(new_path, download_item->db_handle()); 116 (download_item == NULL) ||
117 !download_item->IsInHistory()) return;
118 hs->UpdateDownloadPath(new_path, download_item->id());
121 } 119 }
122 120
123 void DownloadHistory::RemoveEntry(DownloadItem* download_item) { 121 void DownloadHistory::RemoveEntry(DownloadItem* download_item) {
124 // 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.
125 if (download_item->db_handle() <= kUninitializedHandle)
126 return;
127
128 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); 123 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
129 if (hs) 124 if ((hs == NULL) ||
130 hs->RemoveDownload(download_item->db_handle()); 125 (download_item == NULL) ||
126 !download_item->IsInHistory()) return;
127 hs->RemoveDownload(download_item->id());
131 } 128 }
132 129
133 void DownloadHistory::RemoveEntriesBetween(const base::Time remove_begin, 130 void DownloadHistory::RemoveEntriesBetween(const base::Time remove_begin,
134 const base::Time remove_end) { 131 const base::Time remove_end) {
135 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS); 132 HistoryService* hs = profile_->GetHistoryService(Profile::EXPLICIT_ACCESS);
136 if (hs) 133 if (hs)
137 hs->RemoveDownloadsBetween(remove_begin, remove_end); 134 hs->RemoveDownloadsBetween(remove_begin, remove_end);
138 } 135 }
139 136
140 int64 DownloadHistory::GetNextFakeDbHandle() {
141 return next_fake_db_handle_--;
142 }
143
144 void DownloadHistory::OnGotVisitCountToHost(HistoryService::Handle handle, 137 void DownloadHistory::OnGotVisitCountToHost(HistoryService::Handle handle,
145 bool found_visits, 138 bool found_visits,
146 int count, 139 int count,
147 base::Time first_visit) { 140 base::Time first_visit) {
148 VisitedBeforeRequestsMap::iterator request = 141 VisitedBeforeRequestsMap::iterator request =
149 visited_before_requests_.find(handle); 142 visited_before_requests_.find(handle);
150 DCHECK(request != visited_before_requests_.end()); 143 DCHECK(request != visited_before_requests_.end());
151 int32 download_id = request->second.first; 144 int32 download_id = request->second.first;
152 VisitedBeforeDoneCallback* callback = request->second.second; 145 VisitedBeforeDoneCallback* callback = request->second.second;
153 visited_before_requests_.erase(request); 146 visited_before_requests_.erase(request);
154 callback->Run(download_id, found_visits && count && 147 callback->Run(download_id, found_visits && count &&
155 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight())); 148 (first_visit.LocalMidnight() < base::Time::Now().LocalMidnight()));
156 delete callback; 149 delete callback;
157 } 150 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_history.h ('k') | chrome/browser/download/download_item.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698