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 COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_DATABASE_H_ |
6 #define CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ | 6 #define COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_DATABASE_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
13 #include "content/public/browser/download_item.h" | 13 #include "components/history/core/browser/download_types.h" |
14 #include "sql/meta_table.h" | 14 #include "sql/meta_table.h" |
15 | 15 |
16 namespace sql { | 16 namespace sql { |
17 class Connection; | 17 class Connection; |
18 } | 18 } |
19 | 19 |
20 namespace history { | 20 namespace history { |
21 | 21 |
22 struct DownloadRow; | 22 struct DownloadRow; |
23 | 23 |
24 // Maintains a table of downloads. | 24 // Maintains a table of downloads. |
25 class DownloadDatabase { | 25 class DownloadDatabase { |
26 public: | 26 public: |
27 // Must call InitDownloadTable before using any other functions. | 27 // Must call InitDownloadTable before using any other functions. |
28 DownloadDatabase(); | 28 DownloadDatabase(DownloadInterruptReason download_interrupt_no_reason, |
| 29 DownloadInterruptReason download_interrupt_crash); |
29 virtual ~DownloadDatabase(); | 30 virtual ~DownloadDatabase(); |
30 | 31 |
31 uint32 GetNextDownloadId(); | 32 uint32 GetNextDownloadId(); |
32 | 33 |
33 // Get all the downloads from the database. | 34 // Get all the downloads from the database. |
34 void QueryDownloads( | 35 void QueryDownloads(std::vector<DownloadRow>* results); |
35 std::vector<DownloadRow>* results); | |
36 | 36 |
37 // Update the state of one download. Returns true if successful. | 37 // Update the state of one download. Returns true if successful. |
38 // Does not update |url|, |start_time|; uses |id| only | 38 // Does not update |url|, |start_time|; uses |id| only |
39 // to select the row in the database table to update. | 39 // to select the row in the database table to update. |
40 bool UpdateDownload(const DownloadRow& data); | 40 bool UpdateDownload(const DownloadRow& data); |
41 | 41 |
42 // Create a new database entry for one download and return true if the | 42 // Create a new database entry for one download and return true if the |
43 // creation succeeded, false otherwise. | 43 // creation succeeded, false otherwise. |
44 bool CreateDownload(const DownloadRow& info); | 44 bool CreateDownload(const DownloadRow& info); |
45 | 45 |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 bool MigrateDownloadValidators(); | 77 bool MigrateDownloadValidators(); |
78 | 78 |
79 // Creates the downloads table if needed. | 79 // Creates the downloads table if needed. |
80 bool InitDownloadTable(); | 80 bool InitDownloadTable(); |
81 | 81 |
82 // Used to quickly clear the downloads. First you would drop it, then you | 82 // Used to quickly clear the downloads. First you would drop it, then you |
83 // would re-initialize it. | 83 // would re-initialize it. |
84 bool DropDownloadTable(); | 84 bool DropDownloadTable(); |
85 | 85 |
86 private: | 86 private: |
87 FRIEND_TEST_ALL_PREFIXES( | 87 FRIEND_TEST_ALL_PREFIXES(HistoryBackendDBTest, |
88 HistoryBackendDBTest, ConfirmDownloadInProgressCleanup); | 88 ConfirmDownloadInProgressCleanup); |
89 | |
90 // Values used in the database for DownloadItem::State. | |
91 static const int kStateInvalid; | |
92 static const int kStateInProgress; | |
93 static const int kStateComplete; | |
94 static const int kStateCancelled; | |
95 static const int kStateBug140687; | |
96 static const int kStateInterrupted; | |
97 | |
98 // Values used in the database for DownloadItem::DangerType | |
99 static const int kDangerTypeInvalid; | |
100 static const int kDangerTypeNotDangerous; | |
101 static const int kDangerTypeDangerousFile; | |
102 static const int kDangerTypeDangerousUrl; | |
103 static const int kDangerTypeDangerousContent; | |
104 static const int kDangerTypeMaybeDangerousContent; | |
105 static const int kDangerTypeUncommonContent; | |
106 static const int kDangerTypeUserValidated; | |
107 static const int kDangerTypeDangerousHost; | |
108 static const int kDangerTypePotentiallyUnwanted; | |
109 | 89 |
110 // Fixes state of the download entries. Sometimes entries with IN_PROGRESS | 90 // Fixes state of the download entries. Sometimes entries with IN_PROGRESS |
111 // state are not updated during browser shutdown (particularly when crashing). | 91 // state are not updated during browser shutdown (particularly when crashing). |
112 // On the next start such entries are considered interrupted with | 92 // On the next start such entries are considered interrupted with |
113 // interrupt reason |DOWNLOAD_INTERRUPT_REASON_CRASH|. This function | 93 // interrupt reason |DOWNLOAD_INTERRUPT_REASON_CRASH|. This function |
114 // fixes such entries. | 94 // fixes such entries. |
115 void EnsureInProgressEntriesCleanedUp(); | 95 void EnsureInProgressEntriesCleanedUp(); |
116 | 96 |
117 bool EnsureColumnExists(const std::string& name, const std::string& type); | 97 bool EnsureColumnExists(const std::string& name, const std::string& type); |
118 | 98 |
119 void RemoveDownloadURLs(uint32 id); | 99 void RemoveDownloadURLs(uint32 id); |
120 | 100 |
121 // Utility functions for conversion between DownloadItem types | |
122 // and DownloadDatabase constants. | |
123 static int StateToInt(content::DownloadItem::DownloadState state); | |
124 static content::DownloadItem::DownloadState IntToState(int state); | |
125 static int DangerTypeToInt(content::DownloadDangerType danger_type); | |
126 static content::DownloadDangerType IntToDangerType(int danger_type); | |
127 | |
128 bool owning_thread_set_; | 101 bool owning_thread_set_; |
129 base::PlatformThreadId owning_thread_; | 102 base::PlatformThreadId owning_thread_; |
130 | 103 |
131 // Initialized to false on construction, and checked in all functional | 104 // Initialized to false on construction, and checked in all functional |
132 // routines post-migration in the database for a possible call to | 105 // routines post-migration in the database for a possible call to |
133 // CleanUpInProgressEntries(). This allows us to avoid | 106 // CleanUpInProgressEntries(). This allows us to avoid |
134 // doing the cleanup until after any DB migration and unless we are | 107 // doing the cleanup until after any DB migration and unless we are |
135 // actually use the downloads database. | 108 // actually use the downloads database. |
136 bool in_progress_entry_cleanup_completed_; | 109 bool in_progress_entry_cleanup_completed_; |
137 | 110 |
| 111 // Those constants are defined in the embedder and injected into the |
| 112 // database in the constructor. They represent the interrupt reason |
| 113 // to use for respectively an undefined value and in case of a crash. |
| 114 DownloadInterruptReason download_interrupt_no_reason_; |
| 115 DownloadInterruptReason download_interrupt_crash_; |
| 116 |
138 DISALLOW_COPY_AND_ASSIGN(DownloadDatabase); | 117 DISALLOW_COPY_AND_ASSIGN(DownloadDatabase); |
139 }; | 118 }; |
140 | 119 |
141 } // namespace history | 120 } // namespace history |
142 | 121 |
143 #endif // CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ | 122 #endif // COMPONENTS_HISTORY_CORE_BROWSER_DOWNLOAD_DATABASE_H_ |
OLD | NEW |