OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/safe_browsing/incident_reporting/download_metadata_mana
ger.h" | 5 #include "chrome/browser/safe_browsing/incident_reporting/download_metadata_mana
ger.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | |
10 #include <list> | 9 #include <list> |
| 10 #include <utility> |
11 | 11 |
12 #include "base/bind.h" | 12 #include "base/bind.h" |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/files/file.h" | 14 #include "base/files/file.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/files/important_file_writer.h" | 16 #include "base/files/important_file_writer.h" |
17 #include "base/location.h" | 17 #include "base/location.h" |
18 #include "base/macros.h" | 18 #include "base/macros.h" |
19 #include "base/metrics/histogram.h" | 19 #include "base/metrics/histogram.h" |
20 #include "base/sequenced_task_runner.h" | 20 #include "base/sequenced_task_runner.h" |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
65 | 65 |
66 // Returns the ClientDownloadRequest for a download or null if there is none. | 66 // Returns the ClientDownloadRequest for a download or null if there is none. |
67 static scoped_ptr<ClientDownloadRequest> TakeRequestForDownload( | 67 static scoped_ptr<ClientDownloadRequest> TakeRequestForDownload( |
68 content::DownloadItem* item); | 68 content::DownloadItem* item); |
69 | 69 |
70 private: | 70 private: |
71 // A unique id for associating metadata with a content::DownloadItem. | 71 // A unique id for associating metadata with a content::DownloadItem. |
72 static const void* const kKey_; | 72 static const void* const kKey_; |
73 | 73 |
74 explicit DownloadItemData(scoped_ptr<ClientDownloadRequest> request) | 74 explicit DownloadItemData(scoped_ptr<ClientDownloadRequest> request) |
75 : request_(request.Pass()) {} | 75 : request_(std::move(request)) {} |
76 ~DownloadItemData() override {} | 76 ~DownloadItemData() override {} |
77 | 77 |
78 scoped_ptr<ClientDownloadRequest> request_; | 78 scoped_ptr<ClientDownloadRequest> request_; |
79 | 79 |
80 DISALLOW_COPY_AND_ASSIGN(DownloadItemData); | 80 DISALLOW_COPY_AND_ASSIGN(DownloadItemData); |
81 }; | 81 }; |
82 | 82 |
83 // Make the key's value unique by setting it to its own location. | 83 // Make the key's value unique by setting it to its own location. |
84 // static | 84 // static |
85 const void* const DownloadItemData::kKey_ = &DownloadItemData::kKey_; | 85 const void* const DownloadItemData::kKey_ = &DownloadItemData::kKey_; |
86 | 86 |
87 // static | 87 // static |
88 void DownloadItemData::SetRequestForDownload( | 88 void DownloadItemData::SetRequestForDownload( |
89 content::DownloadItem* item, | 89 content::DownloadItem* item, |
90 scoped_ptr<ClientDownloadRequest> request) { | 90 scoped_ptr<ClientDownloadRequest> request) { |
91 item->SetUserData(&kKey_, new DownloadItemData(request.Pass())); | 91 item->SetUserData(&kKey_, new DownloadItemData(std::move(request))); |
92 } | 92 } |
93 | 93 |
94 // static | 94 // static |
95 scoped_ptr<ClientDownloadRequest> DownloadItemData::TakeRequestForDownload( | 95 scoped_ptr<ClientDownloadRequest> DownloadItemData::TakeRequestForDownload( |
96 content::DownloadItem* item) { | 96 content::DownloadItem* item) { |
97 DownloadItemData* data = | 97 DownloadItemData* data = |
98 static_cast<DownloadItemData*>(item->GetUserData(&kKey_)); | 98 static_cast<DownloadItemData*>(item->GetUserData(&kKey_)); |
99 if (!data) | 99 if (!data) |
100 return nullptr; | 100 return nullptr; |
101 scoped_ptr<ClientDownloadRequest> request = data->request_.Pass(); | 101 scoped_ptr<ClientDownloadRequest> request = std::move(data->request_); |
102 item->RemoveUserData(&kKey_); | 102 item->RemoveUserData(&kKey_); |
103 return request.Pass(); | 103 return request; |
104 } | 104 } |
105 | 105 |
106 | 106 |
107 // Utility functions------------------------------------------------------------ | 107 // Utility functions------------------------------------------------------------ |
108 | 108 |
109 // Returns the path to the metadata file for |browser_context|. | 109 // Returns the path to the metadata file for |browser_context|. |
110 base::FilePath GetMetadataPath(content::BrowserContext* browser_context) { | 110 base::FilePath GetMetadataPath(content::BrowserContext* browser_context) { |
111 return browser_context->GetPath().Append(kDownloadMetadataBasename); | 111 return browser_context->GetPath().Append(kDownloadMetadataBasename); |
112 } | 112 } |
113 | 113 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
183 UMA_HISTOGRAM_BOOLEAN("SBIRS.DownloadMetadata.DeleteSuccess", success); | 183 UMA_HISTOGRAM_BOOLEAN("SBIRS.DownloadMetadata.DeleteSuccess", success); |
184 } | 184 } |
185 | 185 |
186 // Runs |callback| with the DownloadDetails in |download_metadata|. | 186 // Runs |callback| with the DownloadDetails in |download_metadata|. |
187 void ReturnResults( | 187 void ReturnResults( |
188 const DownloadMetadataManager::GetDownloadDetailsCallback& callback, | 188 const DownloadMetadataManager::GetDownloadDetailsCallback& callback, |
189 scoped_ptr<DownloadMetadata> download_metadata) { | 189 scoped_ptr<DownloadMetadata> download_metadata) { |
190 if (!download_metadata->has_download_id()) | 190 if (!download_metadata->has_download_id()) |
191 callback.Run(scoped_ptr<ClientIncidentReport_DownloadDetails>()); | 191 callback.Run(scoped_ptr<ClientIncidentReport_DownloadDetails>()); |
192 else | 192 else |
193 callback.Run(make_scoped_ptr(download_metadata->release_download()).Pass()); | 193 callback.Run(make_scoped_ptr(download_metadata->release_download())); |
194 } | 194 } |
195 | 195 |
196 } // namespace | 196 } // namespace |
197 | 197 |
198 // Applies operations to the profile's persistent DownloadMetadata as they occur | 198 // Applies operations to the profile's persistent DownloadMetadata as they occur |
199 // on its corresponding download item. An instance can be in one of three | 199 // on its corresponding download item. An instance can be in one of three |
200 // states: waiting for metatada load, waiting for metadata to load after its | 200 // states: waiting for metatada load, waiting for metadata to load after its |
201 // corresponding DownloadManager has gone down, and not waiting for metadata to | 201 // corresponding DownloadManager has gone down, and not waiting for metadata to |
202 // load. The instance observes all download items beloing to its manager. While | 202 // load. The instance observes all download items beloing to its manager. While |
203 // it is waiting for metadata to load, it records all operations on download | 203 // it is waiting for metadata to load, it records all operations on download |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
475 download->AddObserver(this); | 475 download->AddObserver(this); |
476 } | 476 } |
477 | 477 |
478 void DownloadMetadataManager::ManagerContext::SetRequest( | 478 void DownloadMetadataManager::ManagerContext::SetRequest( |
479 content::DownloadItem* download, | 479 content::DownloadItem* download, |
480 scoped_ptr<ClientDownloadRequest> request) { | 480 scoped_ptr<ClientDownloadRequest> request) { |
481 DCHECK(request); | 481 DCHECK(request); |
482 // Hold on to the request for completion time if the download is in progress. | 482 // Hold on to the request for completion time if the download is in progress. |
483 // Otherwise, commit the request. | 483 // Otherwise, commit the request. |
484 if (download->GetState() == content::DownloadItem::IN_PROGRESS) | 484 if (download->GetState() == content::DownloadItem::IN_PROGRESS) |
485 DownloadItemData::SetRequestForDownload(download, request.Pass()); | 485 DownloadItemData::SetRequestForDownload(download, std::move(request)); |
486 else | 486 else |
487 CommitRequest(download, request.Pass()); | 487 CommitRequest(download, std::move(request)); |
488 } | 488 } |
489 | 489 |
490 void DownloadMetadataManager::ManagerContext::GetDownloadDetails( | 490 void DownloadMetadataManager::ManagerContext::GetDownloadDetails( |
491 const GetDownloadDetailsCallback& callback) { | 491 const GetDownloadDetailsCallback& callback) { |
492 if (state_ != LOAD_COMPLETE) { | 492 if (state_ != LOAD_COMPLETE) { |
493 get_details_callbacks_.push_back(callback); | 493 get_details_callbacks_.push_back(callback); |
494 } else { | 494 } else { |
495 callback.Run(download_metadata_ ? | 495 callback.Run(download_metadata_ ? |
496 make_scoped_ptr(new ClientIncidentReport_DownloadDetails( | 496 make_scoped_ptr(new ClientIncidentReport_DownloadDetails( |
497 download_metadata_->download())) : | 497 download_metadata_->download())) : |
498 nullptr); | 498 nullptr); |
499 } | 499 } |
500 } | 500 } |
501 | 501 |
502 void DownloadMetadataManager::ManagerContext::OnDownloadUpdated( | 502 void DownloadMetadataManager::ManagerContext::OnDownloadUpdated( |
503 content::DownloadItem* download) { | 503 content::DownloadItem* download) { |
504 // Persist metadata for this download if it has just completed. | 504 // Persist metadata for this download if it has just completed. |
505 if (download->GetState() == content::DownloadItem::COMPLETE) { | 505 if (download->GetState() == content::DownloadItem::COMPLETE) { |
506 // Ignore downloads we don't have a ClientDownloadRequest for. | 506 // Ignore downloads we don't have a ClientDownloadRequest for. |
507 scoped_ptr<ClientDownloadRequest> request = | 507 scoped_ptr<ClientDownloadRequest> request = |
508 DownloadItemData::TakeRequestForDownload(download); | 508 DownloadItemData::TakeRequestForDownload(download); |
509 if (request) | 509 if (request) |
510 CommitRequest(download, request.Pass()); | 510 CommitRequest(download, std::move(request)); |
511 } | 511 } |
512 } | 512 } |
513 | 513 |
514 void DownloadMetadataManager::ManagerContext::OnDownloadOpened( | 514 void DownloadMetadataManager::ManagerContext::OnDownloadOpened( |
515 content::DownloadItem* download) { | 515 content::DownloadItem* download) { |
516 const base::Time now = base::Time::Now(); | 516 const base::Time now = base::Time::Now(); |
517 if (state_ != LOAD_COMPLETE) | 517 if (state_ != LOAD_COMPLETE) |
518 pending_items_[download->GetId()].last_opened_time = now; | 518 pending_items_[download->GetId()].last_opened_time = now; |
519 else if (HasMetadataFor(download)) | 519 else if (HasMetadataFor(download)) |
520 UpdateLastOpenedTime(now); | 520 UpdateLastOpenedTime(now); |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
620 | 620 |
621 void DownloadMetadataManager::ManagerContext::OnMetadataReady( | 621 void DownloadMetadataManager::ManagerContext::OnMetadataReady( |
622 scoped_ptr<DownloadMetadata> download_metadata) { | 622 scoped_ptr<DownloadMetadata> download_metadata) { |
623 DCHECK_NE(state_, LOAD_COMPLETE); | 623 DCHECK_NE(state_, LOAD_COMPLETE); |
624 | 624 |
625 const bool is_detached = (state_ == DETACHED_WAIT); | 625 const bool is_detached = (state_ == DETACHED_WAIT); |
626 | 626 |
627 // Note that any available data has been read. | 627 // Note that any available data has been read. |
628 state_ = LOAD_COMPLETE; | 628 state_ = LOAD_COMPLETE; |
629 if (download_metadata->has_download_id()) | 629 if (download_metadata->has_download_id()) |
630 download_metadata_ = download_metadata.Pass(); | 630 download_metadata_ = std::move(download_metadata); |
631 else | 631 else |
632 download_metadata_.reset(); | 632 download_metadata_.reset(); |
633 | 633 |
634 // Process all operations that had been held while waiting for the metadata. | 634 // Process all operations that had been held while waiting for the metadata. |
635 if (download_metadata_) { | 635 if (download_metadata_) { |
636 const auto& iter = pending_items_.find(download_metadata_->download_id()); | 636 const auto& iter = pending_items_.find(download_metadata_->download_id()); |
637 if (iter != pending_items_.end()) { | 637 if (iter != pending_items_.end()) { |
638 const ItemData& item_data = iter->second; | 638 const ItemData& item_data = iter->second; |
639 if (item_data.removed) | 639 if (item_data.removed) |
640 RemoveMetadata(); | 640 RemoveMetadata(); |
(...skipping 14 matching lines...) Expand all Loading... |
655 } | 655 } |
656 | 656 |
657 void DownloadMetadataManager::ManagerContext::UpdateLastOpenedTime( | 657 void DownloadMetadataManager::ManagerContext::UpdateLastOpenedTime( |
658 const base::Time& last_opened_time) { | 658 const base::Time& last_opened_time) { |
659 download_metadata_->mutable_download()->set_open_time_msec( | 659 download_metadata_->mutable_download()->set_open_time_msec( |
660 last_opened_time.ToJavaTime()); | 660 last_opened_time.ToJavaTime()); |
661 WriteMetadata(); | 661 WriteMetadata(); |
662 } | 662 } |
663 | 663 |
664 } // namespace safe_browsing | 664 } // namespace safe_browsing |
OLD | NEW |