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

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

Issue 2508503002: Fix an issue that temp files are left permanently on storage after chrome crash (Closed)
Patch Set: rebase Created 4 years 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
OLDNEW
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 // DownloadHistory manages persisting DownloadItems to the history service by 5 // DownloadHistory manages persisting DownloadItems to the history service by
6 // observing a single DownloadManager and all its DownloadItems using an 6 // observing a single DownloadManager and all its DownloadItems using an
7 // AllDownloadItemNotifier. 7 // AllDownloadItemNotifier.
8 // 8 //
9 // DownloadHistory decides whether and when to add items to, remove items from, 9 // DownloadHistory decides whether and when to add items to, remove items from,
10 // and update items in the database. DownloadHistory uses DownloadHistoryData to 10 // and update items in the database. DownloadHistory uses DownloadHistoryData to
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 item->GetEndTime(), item->GetETag(), item->GetLastModifiedTime(), 136 item->GetEndTime(), item->GetETag(), item->GetLastModifiedTime(),
137 item->GetReceivedBytes(), item->GetTotalBytes(), 137 item->GetReceivedBytes(), item->GetTotalBytes(),
138 history::ToHistoryDownloadState(item->GetState()), 138 history::ToHistoryDownloadState(item->GetState()),
139 history::ToHistoryDownloadDangerType(item->GetDangerType()), 139 history::ToHistoryDownloadDangerType(item->GetDangerType()),
140 history::ToHistoryDownloadInterruptReason(item->GetLastReason()), 140 history::ToHistoryDownloadInterruptReason(item->GetLastReason()),
141 std::string(), // Hash value (not available yet) 141 std::string(), // Hash value (not available yet)
142 history::ToHistoryDownloadId(item->GetId()), item->GetGuid(), 142 history::ToHistoryDownloadId(item->GetId()), item->GetGuid(),
143 item->GetOpened(), by_ext_id, by_ext_name); 143 item->GetOpened(), by_ext_id, by_ext_name);
144 } 144 }
145 145
146 bool ShouldUpdateHistory(const history::DownloadRow* previous, 146 enum class ShouldUpdateHistoryResult {
147 const history::DownloadRow& current) { 147 NO,
148 UPDATE,
149 UPDATE_IMMEDIATELY,
150 };
151
152 ShouldUpdateHistoryResult ShouldUpdateHistory(
153 const history::DownloadRow* previous,
154 const history::DownloadRow& current) {
155 // When download path is determined, Chrome should commit the history
156 // immediately. Otherwise the file will be left permanently on the external
157 // storage if Chrome crashes right away.
158 // TODO(qinmin): this doesn't solve all the issues. When download starts,
159 // Chrome will write the http response data to a temporary file, and later
160 // rename it. If Chrome is killed before committing the history here,
161 // that temporary file will still get permanently left.
162 // See http://crbug.com/664677.
163 if (previous == nullptr || previous->current_path != current.current_path)
164 return ShouldUpdateHistoryResult::UPDATE_IMMEDIATELY;
165
148 // Ignore url_chain, referrer, site_url, http_method, mime_type, 166 // Ignore url_chain, referrer, site_url, http_method, mime_type,
149 // original_mime_type, start_time, id, and guid. These fields don't change. 167 // original_mime_type, start_time, id, and guid. These fields don't change.
150 return ((previous == NULL) || 168 if ((previous->target_path != current.target_path) ||
151 (previous->current_path != current.current_path) || 169 (previous->end_time != current.end_time) ||
152 (previous->target_path != current.target_path) || 170 (previous->received_bytes != current.received_bytes) ||
153 (previous->end_time != current.end_time) || 171 (previous->total_bytes != current.total_bytes) ||
154 (previous->received_bytes != current.received_bytes) || 172 (previous->etag != current.etag) ||
155 (previous->total_bytes != current.total_bytes) || 173 (previous->last_modified != current.last_modified) ||
156 (previous->etag != current.etag) || 174 (previous->state != current.state) ||
157 (previous->last_modified != current.last_modified) || 175 (previous->danger_type != current.danger_type) ||
158 (previous->state != current.state) || 176 (previous->interrupt_reason != current.interrupt_reason) ||
159 (previous->danger_type != current.danger_type) || 177 (previous->hash != current.hash) ||
160 (previous->interrupt_reason != current.interrupt_reason) || 178 (previous->opened != current.opened) ||
161 (previous->hash != current.hash) || 179 (previous->by_ext_id != current.by_ext_id) ||
162 (previous->opened != current.opened) || 180 (previous->by_ext_name != current.by_ext_name)) {
163 (previous->by_ext_id != current.by_ext_id) || 181 return ShouldUpdateHistoryResult::UPDATE;
164 (previous->by_ext_name != current.by_ext_name)); 182 }
183
184 return ShouldUpdateHistoryResult::NO;
165 } 185 }
166 186
167 typedef std::vector<history::DownloadRow> InfoVector; 187 typedef std::vector<history::DownloadRow> InfoVector;
168 188
169 } // anonymous namespace 189 } // anonymous namespace
170 190
171 DownloadHistory::HistoryAdapter::HistoryAdapter( 191 DownloadHistory::HistoryAdapter::HistoryAdapter(
172 history::HistoryService* history) 192 history::HistoryService* history)
173 : history_(history) { 193 : history_(history) {
174 } 194 }
175 DownloadHistory::HistoryAdapter::~HistoryAdapter() {} 195 DownloadHistory::HistoryAdapter::~HistoryAdapter() {}
176 196
177 void DownloadHistory::HistoryAdapter::QueryDownloads( 197 void DownloadHistory::HistoryAdapter::QueryDownloads(
178 const history::HistoryService::DownloadQueryCallback& callback) { 198 const history::HistoryService::DownloadQueryCallback& callback) {
179 history_->QueryDownloads(callback); 199 history_->QueryDownloads(callback);
180 } 200 }
181 201
182 void DownloadHistory::HistoryAdapter::CreateDownload( 202 void DownloadHistory::HistoryAdapter::CreateDownload(
183 const history::DownloadRow& info, 203 const history::DownloadRow& info,
184 const history::HistoryService::DownloadCreateCallback& callback) { 204 const history::HistoryService::DownloadCreateCallback& callback) {
185 history_->CreateDownload(info, callback); 205 history_->CreateDownload(info, callback);
186 } 206 }
187 207
188 void DownloadHistory::HistoryAdapter::UpdateDownload( 208 void DownloadHistory::HistoryAdapter::UpdateDownload(
189 const history::DownloadRow& data) { 209 const history::DownloadRow& data, bool should_commit_immediately) {
190 history_->UpdateDownload(data); 210 history_->UpdateDownload(data, should_commit_immediately);
191 } 211 }
192 212
193 void DownloadHistory::HistoryAdapter::RemoveDownloads( 213 void DownloadHistory::HistoryAdapter::RemoveDownloads(
194 const std::set<uint32_t>& ids) { 214 const std::set<uint32_t>& ids) {
195 history_->RemoveDownloads(ids); 215 history_->RemoveDownloads(ids);
196 } 216 }
197 217
198 DownloadHistory::Observer::Observer() {} 218 DownloadHistory::Observer::Observer() {}
199 DownloadHistory::Observer::~Observer() {} 219 DownloadHistory::Observer::~Observer() {}
200 220
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 if (data->state() == DownloadHistoryData::NOT_PERSISTED) { 421 if (data->state() == DownloadHistoryData::NOT_PERSISTED) {
402 MaybeAddToHistory(item); 422 MaybeAddToHistory(item);
403 return; 423 return;
404 } 424 }
405 if (item->IsTemporary()) { 425 if (item->IsTemporary()) {
406 OnDownloadRemoved(notifier_.GetManager(), item); 426 OnDownloadRemoved(notifier_.GetManager(), item);
407 return; 427 return;
408 } 428 }
409 429
410 history::DownloadRow current_info(GetDownloadRow(item)); 430 history::DownloadRow current_info(GetDownloadRow(item));
411 bool should_update = ShouldUpdateHistory(data->info(), current_info); 431 ShouldUpdateHistoryResult should_update_result =
432 ShouldUpdateHistory(data->info(), current_info);
433 bool should_update = (should_update_result != ShouldUpdateHistoryResult::NO);
412 UMA_HISTOGRAM_ENUMERATION("Download.HistoryPropagatedUpdate", 434 UMA_HISTOGRAM_ENUMERATION("Download.HistoryPropagatedUpdate",
413 should_update, 2); 435 should_update, 2);
414 if (should_update) { 436 if (should_update) {
415 history_->UpdateDownload(current_info); 437 history_->UpdateDownload(
438 current_info,
439 should_update_result == ShouldUpdateHistoryResult::UPDATE_IMMEDIATELY);
416 for (Observer& observer : observers_) 440 for (Observer& observer : observers_)
417 observer.OnDownloadStored(item, current_info); 441 observer.OnDownloadStored(item, current_info);
418 } 442 }
419 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { 443 if (item->GetState() == content::DownloadItem::IN_PROGRESS) {
420 data->set_info(current_info); 444 data->set_info(current_info);
421 } else { 445 } else {
422 data->clear_info(); 446 data->clear_info();
423 } 447 }
424 } 448 }
425 449
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 } 488 }
465 489
466 void DownloadHistory::RemoveDownloadsBatch() { 490 void DownloadHistory::RemoveDownloadsBatch() {
467 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 491 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
468 IdSet remove_ids; 492 IdSet remove_ids;
469 removing_ids_.swap(remove_ids); 493 removing_ids_.swap(remove_ids);
470 history_->RemoveDownloads(remove_ids); 494 history_->RemoveDownloads(remove_ids);
471 for (Observer& observer : observers_) 495 for (Observer& observer : observers_)
472 observer.OnDownloadsRemoved(remove_ids); 496 observer.OnDownloadsRemoved(remove_ids);
473 } 497 }
OLDNEW
« no previous file with comments | « chrome/browser/download/download_history.h ('k') | chrome/browser/download/download_history_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698