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