Upstream version 11.40.271.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / safe_browsing / incident_reporting / download_metadata_manager.h
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.
4
5 #ifndef CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_DOWNLOAD_METADATA_MANAGER_H_
6 #define CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_DOWNLOAD_METADATA_MANAGER_H_
7
8 #include <map>
9
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"
14
15 namespace base {
16 class SequencedTaskRunner;
17 class SequencedWorkerPool;
18 }
19
20 namespace content {
21 class BrowserContext;
22 class DownloadItem;
23 }
24
25 namespace safe_browsing {
26
27 class ClientDownloadRequest;
28 class ClientIncidentReport_DownloadDetails;
29 class DownloadMetadata;
30
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;
40
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);
45
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   virtual ~DownloadMetadataManager();
51
52   // Adds |download_manager| to the set observed by the metadata manager.
53   void AddDownloadManager(content::DownloadManager* download_manager);
54
55   // Sets |request| as the relevant metadata to persist for |download|. If
56   // |request| is null, metadata for the download's BrowserContext are cleared.
57   virtual void SetRequest(content::DownloadItem* download,
58                           const ClientDownloadRequest* request);
59
60   // Gets the persisted DownloadDetails for |browser_context|. |callback| will
61   // be run immediately if the data is available. Otherwise, it will be run
62   // later on the caller's thread.
63   virtual void GetDownloadDetails(content::BrowserContext* browser_context,
64                                   const GetDownloadDetailsCallback& callback);
65
66  protected:
67   // Returns the DownloadManager for a given BrowserContext. Virtual for tests.
68   virtual content::DownloadManager* GetDownloadManagerForBrowserContext(
69       content::BrowserContext* context);
70
71   // content::DownloadManager:Observer methods.
72   void OnDownloadCreated(content::DownloadManager* download_manager,
73                          content::DownloadItem* item) override;
74   void ManagerGoingDown(content::DownloadManager* download_manager) override;
75
76  private:
77   class ManagerContext;
78
79   // A mapping of DownloadManagers to their corresponding contexts.
80   typedef std::map<content::DownloadManager*, ManagerContext*>
81       ManagerToContextMap;
82
83   // A task runner to which read tasks are posted.
84   scoped_refptr<base::SequencedTaskRunner> read_runner_;
85
86   // A task runner to which write tasks are posted.
87   scoped_refptr<base::SequencedTaskRunner> write_runner_;
88
89   // Contexts for each DownloadManager that has been added and has not yet
90   // "gone down".
91   ManagerToContextMap contexts_;
92
93   DISALLOW_COPY_AND_ASSIGN(DownloadMetadataManager);
94 };
95
96 }  // namespace safe_browsing
97
98 #endif  // CHROME_BROWSER_SAFE_BROWSING_INCIDENT_REPORTING_DOWNLOAD_METADATA_MANAGER_H_