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 <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
| 11 #include "base/memory/weak_ptr.h" |
11 #include "base/threading/platform_thread.h" | 12 #include "base/threading/platform_thread.h" |
12 #include "sql/meta_table.h" | 13 #include "sql/meta_table.h" |
13 | 14 |
14 namespace sql { | 15 namespace sql { |
15 class Connection; | 16 class Connection; |
16 } | 17 } |
17 | 18 |
18 namespace history { | 19 namespace history { |
19 | 20 |
20 struct DownloadRow; | 21 struct DownloadRow; |
21 | 22 |
22 // Maintains a table of downloads. | 23 // Maintains a table of downloads. |
23 class DownloadDatabase { | 24 class DownloadDatabase { |
24 public: | 25 public: |
| 26 // Visible outside of download_database.cc only for testing. |
| 27 static const int kStateInvalid; |
| 28 static const int kStateInProgress; |
| 29 static const int kStateComplete; |
| 30 static const int kStateCancelled; |
| 31 static const int kStateBug140687; |
| 32 static const int kStateInterrupted; |
| 33 |
| 34 static const int kDangerTypeInvalid; |
| 35 static const int kDangerTypeNotDangerous; |
| 36 static const int kDangerTypeDangerousFile; |
| 37 static const int kDangerTypeDangerousUrl; |
| 38 static const int kDangerTypeDangerousContent; |
| 39 static const int kDangerTypeMaybeDangerousContent; |
| 40 static const int kDangerTypeUncommonContent; |
| 41 static const int kDangerTypeUserValidated; |
| 42 static const int kDangerTypeDangerousHost; |
| 43 |
25 // The value of |db_handle| indicating that the associated DownloadItem is not | 44 // The value of |db_handle| indicating that the associated DownloadItem is not |
26 // yet persisted. | 45 // yet persisted. |
27 static const int64 kUninitializedHandle; | 46 static const int64 kUninitializedHandle; |
28 | 47 |
29 // Must call InitDownloadTable before using any other functions. | 48 // Must call InitDownloadTable before using any other functions. |
30 DownloadDatabase(); | 49 DownloadDatabase(); |
31 virtual ~DownloadDatabase(); | 50 virtual ~DownloadDatabase(); |
32 | 51 |
33 int next_download_id() const { return next_id_; } | 52 int next_download_id() const { return next_id_; } |
34 | 53 |
35 // Get all the downloads from the database. | 54 // Get all the downloads from the database. |
36 void QueryDownloads( | 55 void QueryDownloads( |
37 std::vector<DownloadRow>* results); | 56 std::vector<DownloadRow>* results); |
38 | 57 |
39 // Update the state of one download. Returns true if successful. | 58 // Update the state of one download. Returns true if successful. |
40 // Does not update |url|, |start_time|; uses |db_handle| only | 59 // Does not update |url|, |start_time|; uses |db_handle| only |
41 // to select the row in the database table to update. | 60 // to select the row in the database table to update. |
42 bool UpdateDownload(const DownloadRow& data); | 61 bool UpdateDownload(const DownloadRow& data); |
43 | 62 |
44 // Fixes state of the download entries. Sometimes entries with IN_PROGRESS | |
45 // state are not updated during browser shutdown (particularly when crashing). | |
46 // On the next start such entries are considered canceled. This functions | |
47 // fixes such entries. | |
48 bool CleanUpInProgressEntries(); | |
49 | |
50 // Create a new database entry for one download and return its primary db id. | 63 // Create a new database entry for one download and return its primary db id. |
51 int64 CreateDownload(const DownloadRow& info); | 64 int64 CreateDownload(const DownloadRow& info); |
52 | 65 |
53 // Remove |handle| from the database. | 66 // Remove |handle| from the database. |
54 void RemoveDownload(int64 handle); | 67 void RemoveDownload(int64 handle); |
55 | 68 |
56 int CountDownloads(); | 69 int CountDownloads(); |
57 | 70 |
58 protected: | 71 protected: |
59 // Returns the database for the functions in this interface. | 72 // Returns the database for the functions in this interface. |
(...skipping 12 matching lines...) Expand all Loading... |
72 bool MigrateDownloadsReasonPathsAndDangerType(); | 85 bool MigrateDownloadsReasonPathsAndDangerType(); |
73 | 86 |
74 // Creates the downloads table if needed. | 87 // Creates the downloads table if needed. |
75 bool InitDownloadTable(); | 88 bool InitDownloadTable(); |
76 | 89 |
77 // Used to quickly clear the downloads. First you would drop it, then you | 90 // Used to quickly clear the downloads. First you would drop it, then you |
78 // would re-initialize it. | 91 // would re-initialize it. |
79 bool DropDownloadTable(); | 92 bool DropDownloadTable(); |
80 | 93 |
81 private: | 94 private: |
| 95 // Fixes state of the download entries. Sometimes entries with IN_PROGRESS |
| 96 // state are not updated during browser shutdown (particularly when crashing). |
| 97 // On the next start such entries are considered interrupted with |
| 98 // interrupt reason |DOWNLOAD_INTERRUPT_REASON_CRASH|. This functions |
| 99 // fixes such entries. |
| 100 void CleanUpInProgressEntries(); |
| 101 |
82 bool EnsureColumnExists(const std::string& name, const std::string& type); | 102 bool EnsureColumnExists(const std::string& name, const std::string& type); |
83 | 103 |
84 bool owning_thread_set_; | 104 bool owning_thread_set_; |
85 base::PlatformThreadId owning_thread_; | 105 base::PlatformThreadId owning_thread_; |
86 | 106 |
87 int next_id_; | 107 int next_id_; |
88 int next_db_handle_; | 108 int next_db_handle_; |
89 | 109 |
| 110 base::WeakPtrFactory<DownloadDatabase> weak_ptr_factory_; |
| 111 |
90 DISALLOW_COPY_AND_ASSIGN(DownloadDatabase); | 112 DISALLOW_COPY_AND_ASSIGN(DownloadDatabase); |
91 }; | 113 }; |
92 | 114 |
93 } // namespace history | 115 } // namespace history |
94 | 116 |
95 #endif // CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ | 117 #endif // CHROME_BROWSER_HISTORY_DOWNLOAD_DATABASE_H_ |
OLD | NEW |