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

Side by Side Diff: chrome/browser/safe_browsing/incident_reporting/download_metadata_manager.cc

Issue 1870003002: Convert //chrome/browser/safe_browsing from scoped_ptr to std::unique_ptr (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 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
OLDNEW
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
9 #include <list> 10 #include <list>
10 #include <utility> 11 #include <utility>
11 12
12 #include "base/bind.h" 13 #include "base/bind.h"
13 #include "base/callback.h" 14 #include "base/callback.h"
14 #include "base/files/file.h" 15 #include "base/files/file.h"
15 #include "base/files/file_util.h" 16 #include "base/files/file_util.h"
16 #include "base/files/important_file_writer.h" 17 #include "base/files/important_file_writer.h"
17 #include "base/location.h" 18 #include "base/location.h"
18 #include "base/macros.h" 19 #include "base/macros.h"
20 #include "base/memory/ptr_util.h"
19 #include "base/metrics/histogram.h" 21 #include "base/metrics/histogram.h"
20 #include "base/sequenced_task_runner.h" 22 #include "base/sequenced_task_runner.h"
21 #include "base/threading/sequenced_worker_pool.h" 23 #include "base/threading/sequenced_worker_pool.h"
22 #include "chrome/common/safe_browsing/csd.pb.h" 24 #include "chrome/common/safe_browsing/csd.pb.h"
23 #include "content/public/browser/browser_context.h" 25 #include "content/public/browser/browser_context.h"
24 #include "content/public/browser/download_item.h" 26 #include "content/public/browser/download_item.h"
25 27
26 namespace safe_browsing { 28 namespace safe_browsing {
27 29
28 namespace { 30 namespace {
(...skipping 24 matching lines...) Expand all
53 FILE_PATH_LITERAL("DownloadMetadata"); 55 FILE_PATH_LITERAL("DownloadMetadata");
54 56
55 57
56 // DownloadItemData ------------------------------------------------------------ 58 // DownloadItemData ------------------------------------------------------------
57 59
58 // A UserData object that holds the ClientDownloadRequest for a download while 60 // A UserData object that holds the ClientDownloadRequest for a download while
59 // it is in progress. 61 // it is in progress.
60 class DownloadItemData : public base::SupportsUserData::Data { 62 class DownloadItemData : public base::SupportsUserData::Data {
61 public: 63 public:
62 // Sets the ClientDownloadRequest for a given DownloadItem. 64 // Sets the ClientDownloadRequest for a given DownloadItem.
63 static void SetRequestForDownload(content::DownloadItem* item, 65 static void SetRequestForDownload(
64 scoped_ptr<ClientDownloadRequest> request); 66 content::DownloadItem* item,
67 std::unique_ptr<ClientDownloadRequest> request);
65 68
66 // Returns the ClientDownloadRequest for a download or null if there is none. 69 // Returns the ClientDownloadRequest for a download or null if there is none.
67 static scoped_ptr<ClientDownloadRequest> TakeRequestForDownload( 70 static std::unique_ptr<ClientDownloadRequest> TakeRequestForDownload(
68 content::DownloadItem* item); 71 content::DownloadItem* item);
69 72
70 private: 73 private:
71 // A unique id for associating metadata with a content::DownloadItem. 74 // A unique id for associating metadata with a content::DownloadItem.
72 static const void* const kKey_; 75 static const void* const kKey_;
73 76
74 explicit DownloadItemData(scoped_ptr<ClientDownloadRequest> request) 77 explicit DownloadItemData(std::unique_ptr<ClientDownloadRequest> request)
75 : request_(std::move(request)) {} 78 : request_(std::move(request)) {}
76 ~DownloadItemData() override {} 79 ~DownloadItemData() override {}
77 80
78 scoped_ptr<ClientDownloadRequest> request_; 81 std::unique_ptr<ClientDownloadRequest> request_;
79 82
80 DISALLOW_COPY_AND_ASSIGN(DownloadItemData); 83 DISALLOW_COPY_AND_ASSIGN(DownloadItemData);
81 }; 84 };
82 85
83 // Make the key's value unique by setting it to its own location. 86 // Make the key's value unique by setting it to its own location.
84 // static 87 // static
85 const void* const DownloadItemData::kKey_ = &DownloadItemData::kKey_; 88 const void* const DownloadItemData::kKey_ = &DownloadItemData::kKey_;
86 89
87 // static 90 // static
88 void DownloadItemData::SetRequestForDownload( 91 void DownloadItemData::SetRequestForDownload(
89 content::DownloadItem* item, 92 content::DownloadItem* item,
90 scoped_ptr<ClientDownloadRequest> request) { 93 std::unique_ptr<ClientDownloadRequest> request) {
91 item->SetUserData(&kKey_, new DownloadItemData(std::move(request))); 94 item->SetUserData(&kKey_, new DownloadItemData(std::move(request)));
92 } 95 }
93 96
94 // static 97 // static
95 scoped_ptr<ClientDownloadRequest> DownloadItemData::TakeRequestForDownload( 98 std::unique_ptr<ClientDownloadRequest> DownloadItemData::TakeRequestForDownload(
96 content::DownloadItem* item) { 99 content::DownloadItem* item) {
97 DownloadItemData* data = 100 DownloadItemData* data =
98 static_cast<DownloadItemData*>(item->GetUserData(&kKey_)); 101 static_cast<DownloadItemData*>(item->GetUserData(&kKey_));
99 if (!data) 102 if (!data)
100 return nullptr; 103 return nullptr;
101 scoped_ptr<ClientDownloadRequest> request = std::move(data->request_); 104 std::unique_ptr<ClientDownloadRequest> request = std::move(data->request_);
102 item->RemoveUserData(&kKey_); 105 item->RemoveUserData(&kKey_);
103 return request; 106 return request;
104 } 107 }
105 108
106 109
107 // Utility functions------------------------------------------------------------ 110 // Utility functions------------------------------------------------------------
108 111
109 // Returns the path to the metadata file for |browser_context|. 112 // Returns the path to the metadata file for |browser_context|.
110 base::FilePath GetMetadataPath(content::BrowserContext* browser_context) { 113 base::FilePath GetMetadataPath(content::BrowserContext* browser_context) {
111 return browser_context->GetPath().Append(kDownloadMetadataBasename); 114 return browser_context->GetPath().Append(kDownloadMetadataBasename);
(...skipping 13 matching lines...) Expand all
125 DownloadMetadata* metadata) { 128 DownloadMetadata* metadata) {
126 using base::File; 129 using base::File;
127 DCHECK(metadata); 130 DCHECK(metadata);
128 MetadataReadResult result = NUM_READ_RESULTS; 131 MetadataReadResult result = NUM_READ_RESULTS;
129 File metadata_file(metadata_path, File::FLAG_OPEN | File::FLAG_READ); 132 File metadata_file(metadata_path, File::FLAG_OPEN | File::FLAG_READ);
130 if (metadata_file.IsValid()) { 133 if (metadata_file.IsValid()) {
131 base::File::Info info; 134 base::File::Info info;
132 if (metadata_file.GetInfo(&info)) { 135 if (metadata_file.GetInfo(&info)) {
133 if (info.size <= INT_MAX) { 136 if (info.size <= INT_MAX) {
134 const int size = static_cast<int>(info.size); 137 const int size = static_cast<int>(info.size);
135 scoped_ptr<char[]> file_data(new char[info.size]); 138 std::unique_ptr<char[]> file_data(new char[info.size]);
136 if (metadata_file.Read(0, file_data.get(), size)) { 139 if (metadata_file.Read(0, file_data.get(), size)) {
137 if (!metadata->ParseFromArray(file_data.get(), size)) 140 if (!metadata->ParseFromArray(file_data.get(), size))
138 result = PARSE_FAILURE; 141 result = PARSE_FAILURE;
139 else if (!MetadataIsValid(*metadata)) 142 else if (!MetadataIsValid(*metadata))
140 result = MALFORMED_DATA; 143 result = MALFORMED_DATA;
141 else 144 else
142 result = READ_SUCCESS; 145 result = READ_SUCCESS;
143 } else { 146 } else {
144 result = READ_FAILURE; 147 result = READ_FAILURE;
145 } 148 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 182
180 // Deletes |metadata_path|. 183 // Deletes |metadata_path|.
181 void DeleteMetadataOnWorkerPool(const base::FilePath& metadata_path) { 184 void DeleteMetadataOnWorkerPool(const base::FilePath& metadata_path) {
182 bool success = base::DeleteFile(metadata_path, false /* not recursive */); 185 bool success = base::DeleteFile(metadata_path, false /* not recursive */);
183 UMA_HISTOGRAM_BOOLEAN("SBIRS.DownloadMetadata.DeleteSuccess", success); 186 UMA_HISTOGRAM_BOOLEAN("SBIRS.DownloadMetadata.DeleteSuccess", success);
184 } 187 }
185 188
186 // Runs |callback| with the DownloadDetails in |download_metadata|. 189 // Runs |callback| with the DownloadDetails in |download_metadata|.
187 void ReturnResults( 190 void ReturnResults(
188 const DownloadMetadataManager::GetDownloadDetailsCallback& callback, 191 const DownloadMetadataManager::GetDownloadDetailsCallback& callback,
189 scoped_ptr<DownloadMetadata> download_metadata) { 192 std::unique_ptr<DownloadMetadata> download_metadata) {
190 if (!download_metadata->has_download_id()) 193 if (!download_metadata->has_download_id())
191 callback.Run(scoped_ptr<ClientIncidentReport_DownloadDetails>()); 194 callback.Run(std::unique_ptr<ClientIncidentReport_DownloadDetails>());
192 else 195 else
193 callback.Run(make_scoped_ptr(download_metadata->release_download())); 196 callback.Run(base::WrapUnique(download_metadata->release_download()));
Nathan Parker 2016/04/11 17:56:44 Should every use of WrapUnique also include <memor
dcheng 2016/04/11 18:31:15 The jury's out on that one. On one hand, it's part
194 } 197 }
195 198
196 } // namespace 199 } // namespace
197 200
198 // Applies operations to the profile's persistent DownloadMetadata as they occur 201 // 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 202 // 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 203 // states: waiting for metatada load, waiting for metadata to load after its
201 // corresponding DownloadManager has gone down, and not waiting for metadata to 204 // corresponding DownloadManager has gone down, and not waiting for metadata to
202 // load. The instance observes all download items beloing to its manager. While 205 // 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 206 // it is waiting for metadata to load, it records all operations on download
(...skipping 15 matching lines...) Expand all
219 // callbacks. 222 // callbacks.
220 void Detach(content::DownloadManager* download_manager); 223 void Detach(content::DownloadManager* download_manager);
221 224
222 // Notifies the context that |download| has been added to its manager. 225 // Notifies the context that |download| has been added to its manager.
223 void OnDownloadCreated(content::DownloadItem* download); 226 void OnDownloadCreated(content::DownloadItem* download);
224 227
225 // Sets |request| as the relevant metadata to persist for |download| if or 228 // Sets |request| as the relevant metadata to persist for |download| if or
226 // when it is complete. If |request| is null, the metadata for |download| 229 // when it is complete. If |request| is null, the metadata for |download|
227 // is/will be removed. 230 // is/will be removed.
228 void SetRequest(content::DownloadItem* download, 231 void SetRequest(content::DownloadItem* download,
229 scoped_ptr<ClientDownloadRequest> request); 232 std::unique_ptr<ClientDownloadRequest> request);
230 233
231 // Gets the persisted DownloadDetails. |callback| will be run immediately if 234 // Gets the persisted DownloadDetails. |callback| will be run immediately if
232 // the data is available. Otherwise, it will be run later on the caller's 235 // the data is available. Otherwise, it will be run later on the caller's
233 // thread. 236 // thread.
234 void GetDownloadDetails(const GetDownloadDetailsCallback& callback); 237 void GetDownloadDetails(const GetDownloadDetailsCallback& callback);
235 238
236 protected: 239 protected:
237 // content::DownloadItem::Observer methods. 240 // content::DownloadItem::Observer methods.
238 void OnDownloadUpdated(content::DownloadItem* download) override; 241 void OnDownloadUpdated(content::DownloadItem* download) override;
239 void OnDownloadOpened(content::DownloadItem* download) override; 242 void OnDownloadOpened(content::DownloadItem* download) override;
(...skipping 20 matching lines...) Expand all
260 263
261 // A mapping of download IDs to their corresponding data. 264 // A mapping of download IDs to their corresponding data.
262 typedef std::map<uint32_t, ItemData> ItemDataMap; 265 typedef std::map<uint32_t, ItemData> ItemDataMap;
263 266
264 ~ManagerContext() override; 267 ~ManagerContext() override;
265 268
266 // Commits |request| to the DownloadDetails for |item|'s BrowserContext. 269 // Commits |request| to the DownloadDetails for |item|'s BrowserContext.
267 // Callbacks will be run immediately if the context had been waiting for a 270 // Callbacks will be run immediately if the context had been waiting for a
268 // load (which will be abandoned). 271 // load (which will be abandoned).
269 void CommitRequest(content::DownloadItem* item, 272 void CommitRequest(content::DownloadItem* item,
270 scoped_ptr<ClientDownloadRequest> request); 273 std::unique_ptr<ClientDownloadRequest> request);
271 274
272 // Posts a task in the worker pool to read the metadata from disk. 275 // Posts a task in the worker pool to read the metadata from disk.
273 void ReadMetadata(); 276 void ReadMetadata();
274 277
275 // Posts a task in the worker pool to write the metadata to disk. 278 // Posts a task in the worker pool to write the metadata to disk.
276 void WriteMetadata(); 279 void WriteMetadata();
277 280
278 // Removes metadata for the context from memory and posts a task in the worker 281 // Removes metadata for the context from memory and posts a task in the worker
279 // pool to delete it on disk. 282 // pool to delete it on disk.
280 void RemoveMetadata(); 283 void RemoveMetadata();
281 284
282 // Clears the |pending_items_| mapping. 285 // Clears the |pending_items_| mapping.
283 void ClearPendingItems(); 286 void ClearPendingItems();
284 287
285 // Runs all |get_details_callbacks_| with the current metadata. 288 // Runs all |get_details_callbacks_| with the current metadata.
286 void RunCallbacks(); 289 void RunCallbacks();
287 290
288 // Returns true if metadata corresponding to |item| is available. 291 // Returns true if metadata corresponding to |item| is available.
289 bool HasMetadataFor(const content::DownloadItem* item) const; 292 bool HasMetadataFor(const content::DownloadItem* item) const;
290 293
291 // A callback run on the main thread with the results from reading the 294 // A callback run on the main thread with the results from reading the
292 // metadata file from disk. 295 // metadata file from disk.
293 void OnMetadataReady(scoped_ptr<DownloadMetadata> download_metadata); 296 void OnMetadataReady(std::unique_ptr<DownloadMetadata> download_metadata);
294 297
295 // Updates the last opened time in the metadata and writes it to disk. 298 // Updates the last opened time in the metadata and writes it to disk.
296 void UpdateLastOpenedTime(const base::Time& last_opened_time); 299 void UpdateLastOpenedTime(const base::Time& last_opened_time);
297 300
298 // A task runner to which read tasks are posted. 301 // A task runner to which read tasks are posted.
299 scoped_refptr<base::SequencedTaskRunner> read_runner_; 302 scoped_refptr<base::SequencedTaskRunner> read_runner_;
300 303
301 // A task runner to which write tasks are posted. 304 // A task runner to which write tasks are posted.
302 scoped_refptr<base::SequencedTaskRunner> write_runner_; 305 scoped_refptr<base::SequencedTaskRunner> write_runner_;
303 306
304 // The path to the metadata file for this context. 307 // The path to the metadata file for this context.
305 base::FilePath metadata_path_; 308 base::FilePath metadata_path_;
306 309
307 // When not LOAD_COMPLETE, the context is waiting for a pending read operation 310 // When not LOAD_COMPLETE, the context is waiting for a pending read operation
308 // to complete. While this is the case, events are temporarily recorded in 311 // to complete. While this is the case, events are temporarily recorded in
309 // |pending_items_|. Once the read completes, pending operations for the item 312 // |pending_items_|. Once the read completes, pending operations for the item
310 // corresponding to the metadata file are applied to the file and all other 313 // corresponding to the metadata file are applied to the file and all other
311 // recorded data are dropped. Queued GetDownloadDetailsCallbacks are run upon 314 // recorded data are dropped. Queued GetDownloadDetailsCallbacks are run upon
312 // read completion as well. The context is moved to the DETACHED_WAIT state if 315 // read completion as well. The context is moved to the DETACHED_WAIT state if
313 // the corresponding DownloadManager goes away while a read operation is 316 // the corresponding DownloadManager goes away while a read operation is
314 // outstanding. When the read subsequently completes, the context is destroyed 317 // outstanding. When the read subsequently completes, the context is destroyed
315 // after the processing described above is performed. 318 // after the processing described above is performed.
316 State state_; 319 State state_;
317 320
318 // The current metadata for the context. May be supplied either by reading 321 // The current metadata for the context. May be supplied either by reading
319 // from the file or by having been set via |SetRequest|. 322 // from the file or by having been set via |SetRequest|.
320 scoped_ptr<DownloadMetadata> download_metadata_; 323 std::unique_ptr<DownloadMetadata> download_metadata_;
321 324
322 // The operation data that accumulates for added download items while the 325 // The operation data that accumulates for added download items while the
323 // metadata file is being read. 326 // metadata file is being read.
324 ItemDataMap pending_items_; 327 ItemDataMap pending_items_;
325 328
326 // Pending callbacks in response to GetDownloadDetails. The callbacks are run 329 // Pending callbacks in response to GetDownloadDetails. The callbacks are run
327 // in order when a pending read operation completes. 330 // in order when a pending read operation completes.
328 std::list<GetDownloadDetailsCallback> get_details_callbacks_; 331 std::list<GetDownloadDetailsCallback> get_details_callbacks_;
329 332
330 base::WeakPtrFactory<ManagerContext> weak_factory_; 333 base::WeakPtrFactory<ManagerContext> weak_factory_;
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 new ManagerContext(read_runner_, write_runner_, download_manager); 374 new ManagerContext(read_runner_, write_runner_, download_manager);
372 } 375 }
373 376
374 void DownloadMetadataManager::SetRequest(content::DownloadItem* item, 377 void DownloadMetadataManager::SetRequest(content::DownloadItem* item,
375 const ClientDownloadRequest* request) { 378 const ClientDownloadRequest* request) {
376 DCHECK(request); 379 DCHECK(request);
377 content::DownloadManager* download_manager = 380 content::DownloadManager* download_manager =
378 GetDownloadManagerForBrowserContext(item->GetBrowserContext()); 381 GetDownloadManagerForBrowserContext(item->GetBrowserContext());
379 DCHECK_EQ(contexts_.count(download_manager), 1U); 382 DCHECK_EQ(contexts_.count(download_manager), 1U);
380 contexts_[download_manager]->SetRequest( 383 contexts_[download_manager]->SetRequest(
381 item, make_scoped_ptr(new ClientDownloadRequest(*request))); 384 item, base::WrapUnique(new ClientDownloadRequest(*request)));
382 } 385 }
383 386
384 void DownloadMetadataManager::GetDownloadDetails( 387 void DownloadMetadataManager::GetDownloadDetails(
385 content::BrowserContext* browser_context, 388 content::BrowserContext* browser_context,
386 const GetDownloadDetailsCallback& callback) { 389 const GetDownloadDetailsCallback& callback) {
387 DCHECK(browser_context); 390 DCHECK(browser_context);
388 // The DownloadManager for |browser_context| may not have been created yet. In 391 // The DownloadManager for |browser_context| may not have been created yet. In
389 // this case, asking for it would cause history to load in the background and 392 // this case, asking for it would cause history to load in the background and
390 // wouldn't really help much. Instead, scan the contexts to see if one belongs 393 // wouldn't really help much. Instead, scan the contexts to see if one belongs
391 // to |browser_context|. If one is not found, read the metadata and return it. 394 // to |browser_context|. If one is not found, read the metadata and return it.
392 scoped_ptr<ClientIncidentReport_DownloadDetails> download_details; 395 std::unique_ptr<ClientIncidentReport_DownloadDetails> download_details;
393 for (const auto& manager_context_pair : contexts_) { 396 for (const auto& manager_context_pair : contexts_) {
394 if (manager_context_pair.first->GetBrowserContext() == browser_context) { 397 if (manager_context_pair.first->GetBrowserContext() == browser_context) {
395 manager_context_pair.second->GetDownloadDetails(callback); 398 manager_context_pair.second->GetDownloadDetails(callback);
396 return; 399 return;
397 } 400 }
398 } 401 }
399 402
400 // Fire off a task to load the details and return them to the caller. 403 // Fire off a task to load the details and return them to the caller.
401 DownloadMetadata* metadata = new DownloadMetadata(); 404 DownloadMetadata* metadata = new DownloadMetadata();
402 read_runner_->PostTaskAndReply( 405 read_runner_->PostTaskAndReply(
403 FROM_HERE, 406 FROM_HERE, base::Bind(&ReadMetadataOnWorkerPool,
404 base::Bind(&ReadMetadataOnWorkerPool, 407 GetMetadataPath(browser_context), metadata),
405 GetMetadataPath(browser_context), 408 base::Bind(&ReturnResults, callback,
406 metadata), 409 base::Passed(base::WrapUnique(metadata))));
407 base::Bind(
408 &ReturnResults, callback, base::Passed(make_scoped_ptr(metadata))));
409 } 410 }
410 411
411 content::DownloadManager* 412 content::DownloadManager*
412 DownloadMetadataManager::GetDownloadManagerForBrowserContext( 413 DownloadMetadataManager::GetDownloadManagerForBrowserContext(
413 content::BrowserContext* context) { 414 content::BrowserContext* context) {
414 return content::BrowserContext::GetDownloadManager(context); 415 return content::BrowserContext::GetDownloadManager(context);
415 } 416 }
416 417
417 void DownloadMetadataManager::OnDownloadCreated( 418 void DownloadMetadataManager::OnDownloadCreated(
418 content::DownloadManager* download_manager, 419 content::DownloadManager* download_manager,
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
470 } 471 }
471 } 472 }
472 473
473 void DownloadMetadataManager::ManagerContext::OnDownloadCreated( 474 void DownloadMetadataManager::ManagerContext::OnDownloadCreated(
474 content::DownloadItem* download) { 475 content::DownloadItem* download) {
475 download->AddObserver(this); 476 download->AddObserver(this);
476 } 477 }
477 478
478 void DownloadMetadataManager::ManagerContext::SetRequest( 479 void DownloadMetadataManager::ManagerContext::SetRequest(
479 content::DownloadItem* download, 480 content::DownloadItem* download,
480 scoped_ptr<ClientDownloadRequest> request) { 481 std::unique_ptr<ClientDownloadRequest> request) {
481 DCHECK(request); 482 DCHECK(request);
482 // Hold on to the request for completion time if the download is in progress. 483 // Hold on to the request for completion time if the download is in progress.
483 // Otherwise, commit the request. 484 // Otherwise, commit the request.
484 if (download->GetState() == content::DownloadItem::IN_PROGRESS) 485 if (download->GetState() == content::DownloadItem::IN_PROGRESS)
485 DownloadItemData::SetRequestForDownload(download, std::move(request)); 486 DownloadItemData::SetRequestForDownload(download, std::move(request));
486 else 487 else
487 CommitRequest(download, std::move(request)); 488 CommitRequest(download, std::move(request));
488 } 489 }
489 490
490 void DownloadMetadataManager::ManagerContext::GetDownloadDetails( 491 void DownloadMetadataManager::ManagerContext::GetDownloadDetails(
491 const GetDownloadDetailsCallback& callback) { 492 const GetDownloadDetailsCallback& callback) {
492 if (state_ != LOAD_COMPLETE) { 493 if (state_ != LOAD_COMPLETE) {
493 get_details_callbacks_.push_back(callback); 494 get_details_callbacks_.push_back(callback);
494 } else { 495 } else {
495 callback.Run(download_metadata_ ? 496 callback.Run(
496 make_scoped_ptr(new ClientIncidentReport_DownloadDetails( 497 download_metadata_
497 download_metadata_->download())) : 498 ? base::WrapUnique(new ClientIncidentReport_DownloadDetails(
498 nullptr); 499 download_metadata_->download()))
500 : nullptr);
499 } 501 }
500 } 502 }
501 503
502 void DownloadMetadataManager::ManagerContext::OnDownloadUpdated( 504 void DownloadMetadataManager::ManagerContext::OnDownloadUpdated(
503 content::DownloadItem* download) { 505 content::DownloadItem* download) {
504 // Persist metadata for this download if it has just completed. 506 // Persist metadata for this download if it has just completed.
505 if (download->GetState() == content::DownloadItem::COMPLETE) { 507 if (download->GetState() == content::DownloadItem::COMPLETE) {
506 // Ignore downloads we don't have a ClientDownloadRequest for. 508 // Ignore downloads we don't have a ClientDownloadRequest for.
507 scoped_ptr<ClientDownloadRequest> request = 509 std::unique_ptr<ClientDownloadRequest> request =
508 DownloadItemData::TakeRequestForDownload(download); 510 DownloadItemData::TakeRequestForDownload(download);
509 if (request) 511 if (request)
510 CommitRequest(download, std::move(request)); 512 CommitRequest(download, std::move(request));
511 } 513 }
512 } 514 }
513 515
514 void DownloadMetadataManager::ManagerContext::OnDownloadOpened( 516 void DownloadMetadataManager::ManagerContext::OnDownloadOpened(
515 content::DownloadItem* download) { 517 content::DownloadItem* download) {
516 const base::Time now = base::Time::Now(); 518 const base::Time now = base::Time::Now();
517 if (state_ != LOAD_COMPLETE) 519 if (state_ != LOAD_COMPLETE)
(...skipping 11 matching lines...) Expand all
529 } 531 }
530 532
531 DownloadMetadataManager::ManagerContext::~ManagerContext() { 533 DownloadMetadataManager::ManagerContext::~ManagerContext() {
532 // A context should not be deleted while waiting for a load to complete. 534 // A context should not be deleted while waiting for a load to complete.
533 DCHECK(pending_items_.empty()); 535 DCHECK(pending_items_.empty());
534 DCHECK(get_details_callbacks_.empty()); 536 DCHECK(get_details_callbacks_.empty());
535 } 537 }
536 538
537 void DownloadMetadataManager::ManagerContext::CommitRequest( 539 void DownloadMetadataManager::ManagerContext::CommitRequest(
538 content::DownloadItem* item, 540 content::DownloadItem* item,
539 scoped_ptr<ClientDownloadRequest> request) { 541 std::unique_ptr<ClientDownloadRequest> request) {
540 DCHECK_EQ(content::DownloadItem::COMPLETE, item->GetState()); 542 DCHECK_EQ(content::DownloadItem::COMPLETE, item->GetState());
541 if (state_ != LOAD_COMPLETE) { 543 if (state_ != LOAD_COMPLETE) {
542 // Abandon the read task since |item| is the new top dog. 544 // Abandon the read task since |item| is the new top dog.
543 weak_factory_.InvalidateWeakPtrs(); 545 weak_factory_.InvalidateWeakPtrs();
544 state_ = LOAD_COMPLETE; 546 state_ = LOAD_COMPLETE;
545 // Drop any recorded operations. 547 // Drop any recorded operations.
546 ClearPendingItems(); 548 ClearPendingItems();
547 } 549 }
548 // Take the request. 550 // Take the request.
549 download_metadata_.reset(new DownloadMetadata); 551 download_metadata_.reset(new DownloadMetadata);
(...skipping 11 matching lines...) Expand all
561 void DownloadMetadataManager::ManagerContext::ReadMetadata() { 563 void DownloadMetadataManager::ManagerContext::ReadMetadata() {
562 DCHECK_NE(state_, LOAD_COMPLETE); 564 DCHECK_NE(state_, LOAD_COMPLETE);
563 565
564 DownloadMetadata* metadata = new DownloadMetadata(); 566 DownloadMetadata* metadata = new DownloadMetadata();
565 // Do not block shutdown on this read since nothing will come of it. 567 // Do not block shutdown on this read since nothing will come of it.
566 read_runner_->PostTaskAndReply( 568 read_runner_->PostTaskAndReply(
567 FROM_HERE, 569 FROM_HERE,
568 base::Bind(&ReadMetadataOnWorkerPool, metadata_path_, metadata), 570 base::Bind(&ReadMetadataOnWorkerPool, metadata_path_, metadata),
569 base::Bind(&DownloadMetadataManager::ManagerContext::OnMetadataReady, 571 base::Bind(&DownloadMetadataManager::ManagerContext::OnMetadataReady,
570 weak_factory_.GetWeakPtr(), 572 weak_factory_.GetWeakPtr(),
571 base::Passed(make_scoped_ptr(metadata)))); 573 base::Passed(base::WrapUnique(metadata))));
572 } 574 }
573 575
574 void DownloadMetadataManager::ManagerContext::WriteMetadata() { 576 void DownloadMetadataManager::ManagerContext::WriteMetadata() {
575 write_runner_->PostTask( 577 write_runner_->PostTask(
576 FROM_HERE, 578 FROM_HERE,
577 base::Bind(&WriteMetadataOnWorkerPool, 579 base::Bind(&WriteMetadataOnWorkerPool,
578 metadata_path_, 580 metadata_path_,
579 base::Owned(new DownloadMetadata(*download_metadata_)))); 581 base::Owned(new DownloadMetadata(*download_metadata_))));
580 } 582 }
581 583
(...skipping 13 matching lines...) Expand all
595 RunCallbacks(); 597 RunCallbacks();
596 } 598 }
597 599
598 void DownloadMetadataManager::ManagerContext::ClearPendingItems() { 600 void DownloadMetadataManager::ManagerContext::ClearPendingItems() {
599 pending_items_.clear(); 601 pending_items_.clear();
600 } 602 }
601 603
602 void DownloadMetadataManager::ManagerContext::RunCallbacks() { 604 void DownloadMetadataManager::ManagerContext::RunCallbacks() {
603 while (!get_details_callbacks_.empty()) { 605 while (!get_details_callbacks_.empty()) {
604 const auto& callback = get_details_callbacks_.front(); 606 const auto& callback = get_details_callbacks_.front();
605 callback.Run(download_metadata_ ? 607 callback.Run(
606 make_scoped_ptr(new ClientIncidentReport_DownloadDetails( 608 download_metadata_
607 download_metadata_->download())) : 609 ? base::WrapUnique(new ClientIncidentReport_DownloadDetails(
608 nullptr); 610 download_metadata_->download()))
611 : nullptr);
609 get_details_callbacks_.pop_front(); 612 get_details_callbacks_.pop_front();
610 } 613 }
611 } 614 }
612 615
613 bool DownloadMetadataManager::ManagerContext::HasMetadataFor( 616 bool DownloadMetadataManager::ManagerContext::HasMetadataFor(
614 const content::DownloadItem* item) const { 617 const content::DownloadItem* item) const {
615 // There must not be metadata if the load is not complete. 618 // There must not be metadata if the load is not complete.
616 DCHECK(state_ == LOAD_COMPLETE || !download_metadata_); 619 DCHECK(state_ == LOAD_COMPLETE || !download_metadata_);
617 return (download_metadata_ && 620 return (download_metadata_ &&
618 download_metadata_->download_id() == item->GetId()); 621 download_metadata_->download_id() == item->GetId());
619 } 622 }
620 623
621 void DownloadMetadataManager::ManagerContext::OnMetadataReady( 624 void DownloadMetadataManager::ManagerContext::OnMetadataReady(
622 scoped_ptr<DownloadMetadata> download_metadata) { 625 std::unique_ptr<DownloadMetadata> download_metadata) {
623 DCHECK_NE(state_, LOAD_COMPLETE); 626 DCHECK_NE(state_, LOAD_COMPLETE);
624 627
625 const bool is_detached = (state_ == DETACHED_WAIT); 628 const bool is_detached = (state_ == DETACHED_WAIT);
626 629
627 // Note that any available data has been read. 630 // Note that any available data has been read.
628 state_ = LOAD_COMPLETE; 631 state_ = LOAD_COMPLETE;
629 if (download_metadata->has_download_id()) 632 if (download_metadata->has_download_id())
630 download_metadata_ = std::move(download_metadata); 633 download_metadata_ = std::move(download_metadata);
631 else 634 else
632 download_metadata_.reset(); 635 download_metadata_.reset();
(...skipping 22 matching lines...) Expand all
655 } 658 }
656 659
657 void DownloadMetadataManager::ManagerContext::UpdateLastOpenedTime( 660 void DownloadMetadataManager::ManagerContext::UpdateLastOpenedTime(
658 const base::Time& last_opened_time) { 661 const base::Time& last_opened_time) {
659 download_metadata_->mutable_download()->set_open_time_msec( 662 download_metadata_->mutable_download()->set_open_time_msec(
660 last_opened_time.ToJavaTime()); 663 last_opened_time.ToJavaTime());
661 WriteMetadata(); 664 WriteMetadata();
662 } 665 }
663 666
664 } // namespace safe_browsing 667 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698