Chromium Code Reviews| 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 // 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 Loading... | |
| 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 ShouldUpdateHistoryResult { |
|
asanka
2016/11/21 20:17:57
use 'enum class' instead. Regular enums pollute th
qinmin
2016/11/21 23:14:54
Done.
| |
| 147 const history::DownloadRow& current) { | 147 NO = 0, |
| 148 UPDATE, | |
| 149 UPDATE_IMMEDIATELY, | |
| 150 }; | |
| 151 | |
| 152 ShouldUpdateHistoryResult ShouldUpdateHistory( | |
| 153 const history::DownloadRow* previous, | |
| 154 const history::DownloadRow& current) { | |
| 155 if (previous == nullptr) | |
| 156 return UPDATE; | |
|
asanka
2016/11/21 20:17:57
Should return UPDATE_IMMEDIATELY here. If this con
qinmin
2016/11/21 23:14:54
Done.
| |
| 157 | |
| 158 // When download path is determined, Chrome should commit the history | |
| 159 // immediately. Otherwise the file will be left permanently on the external | |
| 160 // storage if Chrome crashes right away. | |
| 161 if (previous->current_path != current.current_path) | |
| 162 return UPDATE_IMMEDIATELY; | |
| 163 | |
| 148 // Ignore url_chain, referrer, site_url, http_method, mime_type, | 164 // Ignore url_chain, referrer, site_url, http_method, mime_type, |
| 149 // original_mime_type, start_time, id, and guid. These fields don't change. | 165 // original_mime_type, start_time, id, and guid. These fields don't change. |
| 150 return ((previous == NULL) || | 166 if ((previous->target_path != current.target_path) || |
| 151 (previous->current_path != current.current_path) || | 167 (previous->end_time != current.end_time) || |
| 152 (previous->target_path != current.target_path) || | 168 (previous->received_bytes != current.received_bytes) || |
| 153 (previous->end_time != current.end_time) || | 169 (previous->total_bytes != current.total_bytes) || |
| 154 (previous->received_bytes != current.received_bytes) || | 170 (previous->etag != current.etag) || |
| 155 (previous->total_bytes != current.total_bytes) || | 171 (previous->last_modified != current.last_modified) || |
| 156 (previous->etag != current.etag) || | 172 (previous->state != current.state) || |
| 157 (previous->last_modified != current.last_modified) || | 173 (previous->danger_type != current.danger_type) || |
| 158 (previous->state != current.state) || | 174 (previous->interrupt_reason != current.interrupt_reason) || |
| 159 (previous->danger_type != current.danger_type) || | 175 (previous->hash != current.hash) || |
| 160 (previous->interrupt_reason != current.interrupt_reason) || | 176 (previous->opened != current.opened) || |
| 161 (previous->hash != current.hash) || | 177 (previous->by_ext_id != current.by_ext_id) || |
| 162 (previous->opened != current.opened) || | 178 (previous->by_ext_name != current.by_ext_name)) { |
| 163 (previous->by_ext_id != current.by_ext_id) || | 179 return UPDATE; |
| 164 (previous->by_ext_name != current.by_ext_name)); | 180 } |
| 181 | |
| 182 return NO; | |
| 165 } | 183 } |
| 166 | 184 |
| 167 typedef std::vector<history::DownloadRow> InfoVector; | 185 typedef std::vector<history::DownloadRow> InfoVector; |
| 168 | 186 |
| 169 } // anonymous namespace | 187 } // anonymous namespace |
| 170 | 188 |
| 171 DownloadHistory::HistoryAdapter::HistoryAdapter( | 189 DownloadHistory::HistoryAdapter::HistoryAdapter( |
| 172 history::HistoryService* history) | 190 history::HistoryService* history) |
| 173 : history_(history) { | 191 : history_(history) { |
| 174 } | 192 } |
| 175 DownloadHistory::HistoryAdapter::~HistoryAdapter() {} | 193 DownloadHistory::HistoryAdapter::~HistoryAdapter() {} |
| 176 | 194 |
| 177 void DownloadHistory::HistoryAdapter::QueryDownloads( | 195 void DownloadHistory::HistoryAdapter::QueryDownloads( |
| 178 const history::HistoryService::DownloadQueryCallback& callback) { | 196 const history::HistoryService::DownloadQueryCallback& callback) { |
| 179 history_->QueryDownloads(callback); | 197 history_->QueryDownloads(callback); |
| 180 } | 198 } |
| 181 | 199 |
| 182 void DownloadHistory::HistoryAdapter::CreateDownload( | 200 void DownloadHistory::HistoryAdapter::CreateDownload( |
| 183 const history::DownloadRow& info, | 201 const history::DownloadRow& info, |
| 184 const history::HistoryService::DownloadCreateCallback& callback) { | 202 const history::HistoryService::DownloadCreateCallback& callback) { |
| 185 history_->CreateDownload(info, callback); | 203 history_->CreateDownload(info, callback); |
| 186 } | 204 } |
| 187 | 205 |
| 188 void DownloadHistory::HistoryAdapter::UpdateDownload( | 206 void DownloadHistory::HistoryAdapter::UpdateDownload( |
| 189 const history::DownloadRow& data) { | 207 const history::DownloadRow& data, bool should_commit_immediately) { |
| 190 history_->UpdateDownload(data); | 208 history_->UpdateDownload(data, should_commit_immediately); |
| 191 } | 209 } |
| 192 | 210 |
| 193 void DownloadHistory::HistoryAdapter::RemoveDownloads( | 211 void DownloadHistory::HistoryAdapter::RemoveDownloads( |
| 194 const std::set<uint32_t>& ids) { | 212 const std::set<uint32_t>& ids) { |
| 195 history_->RemoveDownloads(ids); | 213 history_->RemoveDownloads(ids); |
| 196 } | 214 } |
| 197 | 215 |
| 198 DownloadHistory::Observer::Observer() {} | 216 DownloadHistory::Observer::Observer() {} |
| 199 DownloadHistory::Observer::~Observer() {} | 217 DownloadHistory::Observer::~Observer() {} |
| 200 | 218 |
| (...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 if (data->state() == DownloadHistoryData::NOT_PERSISTED) { | 419 if (data->state() == DownloadHistoryData::NOT_PERSISTED) { |
| 402 MaybeAddToHistory(item); | 420 MaybeAddToHistory(item); |
| 403 return; | 421 return; |
| 404 } | 422 } |
| 405 if (item->IsTemporary()) { | 423 if (item->IsTemporary()) { |
| 406 OnDownloadRemoved(notifier_.GetManager(), item); | 424 OnDownloadRemoved(notifier_.GetManager(), item); |
| 407 return; | 425 return; |
| 408 } | 426 } |
| 409 | 427 |
| 410 history::DownloadRow current_info(GetDownloadRow(item)); | 428 history::DownloadRow current_info(GetDownloadRow(item)); |
| 411 bool should_update = ShouldUpdateHistory(data->info(), current_info); | 429 ShouldUpdateHistoryResult should_update_result = |
| 430 ShouldUpdateHistory(data->info(), current_info); | |
| 431 bool should_update = (should_update_result != NO); | |
| 412 UMA_HISTOGRAM_ENUMERATION("Download.HistoryPropagatedUpdate", | 432 UMA_HISTOGRAM_ENUMERATION("Download.HistoryPropagatedUpdate", |
| 413 should_update, 2); | 433 should_update, 2); |
| 414 if (should_update) { | 434 if (should_update) { |
| 415 history_->UpdateDownload(current_info); | 435 history_->UpdateDownload( |
| 436 current_info, should_update_result == UPDATE_IMMEDIATELY); | |
| 416 for (Observer& observer : observers_) | 437 for (Observer& observer : observers_) |
| 417 observer.OnDownloadStored(item, current_info); | 438 observer.OnDownloadStored(item, current_info); |
| 418 } | 439 } |
| 419 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { | 440 if (item->GetState() == content::DownloadItem::IN_PROGRESS) { |
| 420 data->set_info(current_info); | 441 data->set_info(current_info); |
| 421 } else { | 442 } else { |
| 422 data->clear_info(); | 443 data->clear_info(); |
| 423 } | 444 } |
| 424 } | 445 } |
| 425 | 446 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 464 } | 485 } |
| 465 | 486 |
| 466 void DownloadHistory::RemoveDownloadsBatch() { | 487 void DownloadHistory::RemoveDownloadsBatch() { |
| 467 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 488 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 468 IdSet remove_ids; | 489 IdSet remove_ids; |
| 469 removing_ids_.swap(remove_ids); | 490 removing_ids_.swap(remove_ids); |
| 470 history_->RemoveDownloads(remove_ids); | 491 history_->RemoveDownloads(remove_ids); |
| 471 for (Observer& observer : observers_) | 492 for (Observer& observer : observers_) |
| 472 observer.OnDownloadsRemoved(remove_ids); | 493 observer.OnDownloadsRemoved(remove_ids); |
| 473 } | 494 } |
| OLD | NEW |