Refactor WebsiteSettings to operate on a SecurityInfo
[chromium-blink-merge.git] / chrome / browser / safe_browsing / incident_reporting / download_metadata_manager.h
blob317937a12e01e7e7af4a099924d52bd32f6e8c2a
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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_DOWNLOAD_METADATA_MANAGER_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_DOWNLOAD_METADATA_MANAGER_H_
8 #include <map>
10 #include "base/callback_forward.h"
11 #include "base/macros.h"
12 #include "base/memory/ref_counted.h"
13 #include "content/public/browser/download_manager.h"
15 namespace base {
16 class SequencedTaskRunner;
17 class SequencedWorkerPool;
20 namespace content {
21 class BrowserContext;
22 class DownloadItem;
25 namespace safe_browsing {
27 class ClientDownloadRequest;
28 class ClientIncidentReport_DownloadDetails;
29 class DownloadMetadata;
31 // A browser-wide object that manages the persistent state of metadata
32 // pertaining to a download.
33 class DownloadMetadataManager : public content::DownloadManager::Observer {
34 public:
35 // A callback run when the results of a call to GetDownloadDetails are ready.
36 // The supplied parameter may be null, indicating that there are no persisted
37 // details for the |browser_context| passed to GetDownloadDetails.
38 typedef base::Callback<void(scoped_ptr<ClientIncidentReport_DownloadDetails>)>
39 GetDownloadDetailsCallback;
41 // Constructs a new instance for which disk IO operations will take place in
42 // |worker_pool|.
43 explicit DownloadMetadataManager(
44 const scoped_refptr<base::SequencedWorkerPool>& worker_pool);
46 // Constructor that allows tests to provide a specific runner for
47 // asynchronous tasks.
48 explicit DownloadMetadataManager(
49 const scoped_refptr<base::SequencedTaskRunner>& task_runner);
50 ~DownloadMetadataManager() override;
52 // Adds |download_manager| to the set observed by the metadata manager.
53 void AddDownloadManager(content::DownloadManager* download_manager);
55 // Sets |request| as the relevant metadata to persist for |download| upon
56 // completion. |request| will be persisted when the download completes, or
57 // discarded if the download is cancelled.
58 virtual void SetRequest(content::DownloadItem* download,
59 const ClientDownloadRequest* request);
61 // Gets the persisted DownloadDetails for |browser_context|. |callback| will
62 // be run immediately if the data is available. Otherwise, it will be run
63 // later on the caller's thread.
64 virtual void GetDownloadDetails(content::BrowserContext* browser_context,
65 const GetDownloadDetailsCallback& callback);
67 protected:
68 // Returns the DownloadManager for a given BrowserContext. Virtual for tests.
69 virtual content::DownloadManager* GetDownloadManagerForBrowserContext(
70 content::BrowserContext* context);
72 // content::DownloadManager:Observer methods.
73 void OnDownloadCreated(content::DownloadManager* download_manager,
74 content::DownloadItem* item) override;
75 void ManagerGoingDown(content::DownloadManager* download_manager) override;
77 private:
78 class ManagerContext;
80 // A mapping of DownloadManagers to their corresponding contexts.
81 typedef std::map<content::DownloadManager*, ManagerContext*>
82 ManagerToContextMap;
84 // A task runner to which read tasks are posted.
85 scoped_refptr<base::SequencedTaskRunner> read_runner_;
87 // A task runner to which write tasks are posted.
88 scoped_refptr<base::SequencedTaskRunner> write_runner_;
90 // Contexts for each DownloadManager that has been added and has not yet
91 // "gone down".
92 ManagerToContextMap contexts_;
94 DISALLOW_COPY_AND_ASSIGN(DownloadMetadataManager);
97 } // namespace safe_browsing
99 #endif // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_DOWNLOAD_METADATA_MANAGER_H_