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 #ifndef CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ | 5 #ifndef CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ |
6 #define CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ | 6 #define CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
12 #include "chrome/browser/history/history_types.h" | 12 #include "chrome/browser/history/history_types.h" |
13 #include "sql/meta_table.h" | 13 #include "sql/meta_table.h" |
14 | 14 |
15 class FilePath; | 15 class FilePath; |
16 | |
17 namespace content { | |
18 struct DownloadPersistentStoreInfo; | 16 struct DownloadPersistentStoreInfo; |
19 } | |
20 | 17 |
21 namespace sql { | 18 namespace sql { |
22 class Connection; | 19 class Connection; |
23 } | 20 } |
24 | 21 |
25 namespace history { | 22 namespace history { |
26 | 23 |
27 // Maintains a table of downloads. | 24 // Maintains a table of downloads. |
28 class DownloadDatabase { | 25 class DownloadDatabase { |
29 public: | 26 public: |
30 // Must call InitDownloadTable before using any other functions. | 27 // Must call InitDownloadTable before using any other functions. |
31 DownloadDatabase(); | 28 DownloadDatabase(); |
32 virtual ~DownloadDatabase(); | 29 virtual ~DownloadDatabase(); |
33 | 30 |
34 int next_download_id() const { return next_id_; } | 31 int next_download_id() const { return next_id_; } |
35 | 32 |
36 // Get all the downloads from the database. | 33 // Get all the downloads from the database. |
37 void QueryDownloads( | 34 void QueryDownloads( |
38 std::vector<content::DownloadPersistentStoreInfo>* results); | 35 std::vector<DownloadPersistentStoreInfo>* results); |
39 | 36 |
40 // Update the state of one download. Returns true if successful. | 37 // Update the state of one download. Returns true if successful. |
41 // Does not update |url|, |start_time|, |total_bytes|; uses |db_handle| only | 38 // Does not update |url|, |start_time|; uses |db_handle| only |
42 // to select the row in the database table to update. | 39 // to select the row in the database table to update. |
43 bool UpdateDownload(const content::DownloadPersistentStoreInfo& data); | 40 bool UpdateDownload(const DownloadPersistentStoreInfo& data); |
44 | |
45 // Update the path of one download. Returns true if successful. | |
46 bool UpdateDownloadPath(const FilePath& path, DownloadID db_handle); | |
47 | 41 |
48 // Fixes state of the download entries. Sometimes entries with IN_PROGRESS | 42 // Fixes state of the download entries. Sometimes entries with IN_PROGRESS |
49 // state are not updated during browser shutdown (particularly when crashing). | 43 // state are not updated during browser shutdown (particularly when crashing). |
50 // On the next start such entries are considered canceled. This functions | 44 // On the next start such entries are considered canceled. This functions |
51 // fixes such entries. | 45 // fixes such entries. |
52 bool CleanUpInProgressEntries(); | 46 bool CleanUpInProgressEntries(); |
53 | 47 |
54 // Create a new database entry for one download and return its primary db id. | 48 // Create a new database entry for one download and return its primary db id. |
55 int64 CreateDownload(const content::DownloadPersistentStoreInfo& info); | 49 int64 CreateDownload(const DownloadPersistentStoreInfo& info); |
56 | 50 |
57 // Remove a download from the database. | 51 // Remove all |handles| from the database. |
58 void RemoveDownload(DownloadID db_handle); | 52 void RemoveDownloads(const std::set<int64>& handles); |
59 | |
60 // Remove all completed downloads that started after |remove_begin| | |
61 // (inclusive) and before |remove_end|. You may use null Time values | |
62 // to do an unbounded delete in either direction. This function ignores | |
63 // all downloads that are in progress or are waiting to be cancelled. | |
64 bool RemoveDownloadsBetween(base::Time remove_begin, base::Time remove_end); | |
65 | 53 |
66 protected: | 54 protected: |
67 // Returns the database for the functions in this interface. | 55 // Returns the database for the functions in this interface. |
68 virtual sql::Connection& GetDB() = 0; | 56 virtual sql::Connection& GetDB() = 0; |
69 | 57 |
70 // Returns the meta-table object for the functions in this interface. | 58 // Returns the meta-table object for the functions in this interface. |
71 virtual sql::MetaTable& GetMetaTable() = 0; | 59 virtual sql::MetaTable& GetMetaTable() = 0; |
72 | 60 |
73 // Returns true if able to successfully rewrite the invalid values for the | 61 // Returns true if able to successfully rewrite the invalid values for the |
74 // |state| field from 3 to 4. Returns false if there was an error fixing the | 62 // |state| field from 3 to 4. Returns false if there was an error fixing the |
75 // database. See http://crbug.com/140687 | 63 // database. See http://crbug.com/140687 |
76 bool MigrateDownloadsState(); | 64 bool MigrateDownloadsState(); |
77 | 65 |
78 // Creates the downloads table if needed. | 66 // Creates the downloads table if needed. |
79 bool InitDownloadTable(); | 67 bool InitDownloadTable(); |
80 | 68 |
81 // Used to quickly clear the downloads. First you would drop it, then you | 69 // Used to quickly clear the downloads. First you would drop it, then you |
82 // would re-initialize it. | 70 // would re-initialize it. |
83 bool DropDownloadTable(); | 71 bool DropDownloadTable(); |
84 | 72 |
85 private: | 73 private: |
86 // TODO(rdsmith): Remove after http://crbug.com/96627 has been resolved. | 74 bool EnsureColumnExists(const std::string& name, const std::string& type); |
87 void CheckThread(); | |
88 | 75 |
89 bool EnsureColumnExists(const std::string& name, const std::string& type); | 76 int CountDownloads(); |
90 | 77 |
91 bool owning_thread_set_; | 78 bool owning_thread_set_; |
92 base::PlatformThreadId owning_thread_; | 79 base::PlatformThreadId owning_thread_; |
93 | 80 |
94 int next_id_; | 81 int next_id_; |
95 int next_db_handle_; | 82 int next_db_handle_; |
96 | 83 |
97 DISALLOW_COPY_AND_ASSIGN(DownloadDatabase); | 84 DISALLOW_COPY_AND_ASSIGN(DownloadDatabase); |
98 }; | 85 }; |
99 | 86 |
100 } // namespace history | 87 } // namespace history |
101 | 88 |
102 #endif // CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ | 89 #endif // CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ |
OLD | NEW |