[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 42eb6b0 | 2008-11-25 23:14:46 | [diff] [blame] | 5 | #include "chrome/browser/history/download_database.h" |
| 6 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 7 | #include <limits> |
| 8 | #include <vector> |
| 9 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 10 | #include "app/sql/connection.h" |
| 11 | #include "app/sql/statement.h" |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 12 | #include "base/file_path.h" |
[email protected] | 9da5f16 | 2009-12-11 16:30:16 | [diff] [blame] | 13 | #include "base/utf_string_conversions.h" |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 14 | #include "build/build_config.h" |
[email protected] | 6c69796d | 2010-07-16 21:41:16 | [diff] [blame^] | 15 | #include "chrome/browser/download/download_item.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 16 | #include "chrome/browser/history/download_types.h" |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 17 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | // Download schema: |
| 19 | // |
| 20 | // id SQLite-generated primary key. |
| 21 | // full_path Location of the download on disk. |
| 22 | // url URL of the downloaded file. |
| 23 | // start_time When the download was started. |
| 24 | // received_bytes Total size downloaded. |
| 25 | // total_bytes Total size of the download. |
| 26 | // state Identifies if this download is completed or not. Not used |
| 27 | // directly by the history system. See DownloadItem's |
| 28 | // DownloadState for where this is used. |
| 29 | |
| 30 | namespace history { |
| 31 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 32 | namespace { |
| 33 | |
| 34 | #if defined(OS_POSIX) |
| 35 | |
| 36 | // Binds/reads the given file path to the given column of the given statement. |
| 37 | void BindFilePath(sql::Statement& statement, const FilePath& path, int col) { |
| 38 | statement.BindString(col, path.value()); |
| 39 | } |
| 40 | FilePath ColumnFilePath(sql::Statement& statement, int col) { |
| 41 | return FilePath(statement.ColumnString(col)); |
| 42 | } |
| 43 | |
| 44 | #else |
| 45 | |
| 46 | // See above. |
| 47 | void BindFilePath(sql::Statement& statement, const FilePath& path, int col) { |
| 48 | statement.BindString(col, UTF16ToUTF8(path.value())); |
| 49 | } |
| 50 | FilePath ColumnFilePath(sql::Statement& statement, int col) { |
| 51 | return FilePath(UTF8ToUTF16(statement.ColumnString(col))); |
| 52 | } |
| 53 | |
| 54 | #endif |
| 55 | |
| 56 | } // namespace |
| 57 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | DownloadDatabase::DownloadDatabase() { |
| 59 | } |
| 60 | |
| 61 | DownloadDatabase::~DownloadDatabase() { |
| 62 | } |
| 63 | |
| 64 | bool DownloadDatabase::InitDownloadTable() { |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 65 | if (!GetDB().DoesTableExist("downloads")) { |
| 66 | if (!GetDB().Execute( |
| 67 | "CREATE TABLE downloads (" |
| 68 | "id INTEGER PRIMARY KEY," |
| 69 | "full_path LONGVARCHAR NOT NULL," |
| 70 | "url LONGVARCHAR NOT NULL," |
| 71 | "start_time INTEGER NOT NULL," |
| 72 | "received_bytes INTEGER NOT NULL," |
| 73 | "total_bytes INTEGER NOT NULL," |
| 74 | "state INTEGER NOT NULL)")) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 75 | return false; |
| 76 | } |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool DownloadDatabase::DropDownloadTable() { |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 81 | return GetDB().Execute("DROP TABLE downloads"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 82 | } |
| 83 | |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 84 | void DownloadDatabase::QueryDownloads( |
| 85 | std::vector<DownloadCreateInfo>* results) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | results->clear(); |
| 87 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 88 | sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 89 | "SELECT id, full_path, url, start_time, received_bytes, " |
| 90 | "total_bytes, state " |
| 91 | "FROM downloads " |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 92 | "ORDER BY start_time")); |
| 93 | if (!statement) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | return; |
| 95 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 96 | while (statement.Step()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | DownloadCreateInfo info; |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 98 | info.db_handle = statement.ColumnInt64(0); |
| 99 | |
| 100 | info.path = ColumnFilePath(statement, 1); |
| 101 | info.url = GURL(statement.ColumnString(2)); |
| 102 | info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3)); |
| 103 | info.received_bytes = statement.ColumnInt64(4); |
| 104 | info.total_bytes = statement.ColumnInt64(5); |
| 105 | info.state = statement.ColumnInt(6); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 106 | results->push_back(info); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | bool DownloadDatabase::UpdateDownload(int64 received_bytes, |
| 111 | int32 state, |
| 112 | DownloadID db_handle) { |
| 113 | DCHECK(db_handle > 0); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 114 | sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 115 | "UPDATE downloads " |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 116 | "SET received_bytes=?, state=? WHERE id=?")); |
| 117 | if (!statement) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 118 | return false; |
| 119 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 120 | statement.BindInt64(0, received_bytes); |
| 121 | statement.BindInt(1, state); |
| 122 | statement.BindInt64(2, db_handle); |
| 123 | return statement.Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 124 | } |
| 125 | |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 126 | bool DownloadDatabase::UpdateDownloadPath(const FilePath& path, |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 127 | DownloadID db_handle) { |
| 128 | DCHECK(db_handle > 0); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 129 | sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 130 | "UPDATE downloads SET full_path=? WHERE id=?")); |
| 131 | if (!statement) |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 132 | return false; |
| 133 | |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 134 | BindFilePath(statement, path, 0); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 135 | statement.BindInt64(1, db_handle); |
| 136 | return statement.Run(); |
[email protected] | 9ccbb37 | 2008-10-10 18:50:32 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 024f2f0 | 2010-04-30 22:51:46 | [diff] [blame] | 139 | bool DownloadDatabase::CleanUpInProgressEntries() { |
| 140 | sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 141 | "UPDATE downloads SET state=? WHERE state=?")); |
| 142 | if (!statement) |
| 143 | return false; |
| 144 | statement.BindInt(0, DownloadItem::CANCELLED); |
| 145 | statement.BindInt(1, DownloadItem::IN_PROGRESS); |
| 146 | return statement.Run(); |
| 147 | } |
| 148 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 149 | int64 DownloadDatabase::CreateDownload(const DownloadCreateInfo& info) { |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 150 | sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | "INSERT INTO downloads " |
| 152 | "(full_path, url, start_time, received_bytes, total_bytes, state) " |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 153 | "VALUES (?, ?, ?, ?, ?, ?)")); |
| 154 | if (!statement) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 155 | return 0; |
| 156 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 157 | BindFilePath(statement, info.path, 0); |
| 158 | statement.BindString(1, info.url.spec()); |
| 159 | statement.BindInt64(2, info.start_time.ToTimeT()); |
| 160 | statement.BindInt64(3, info.received_bytes); |
| 161 | statement.BindInt64(4, info.total_bytes); |
| 162 | statement.BindInt(5, info.state); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 163 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 164 | if (statement.Run()) |
| 165 | return GetDB().GetLastInsertRowId(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | |
| 169 | void DownloadDatabase::RemoveDownload(DownloadID db_handle) { |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 170 | sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
| 171 | "DELETE FROM downloads WHERE id=?")); |
| 172 | if (!statement) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 173 | return; |
| 174 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 175 | statement.BindInt64(0, db_handle); |
| 176 | statement.Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 177 | } |
| 178 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 179 | void DownloadDatabase::RemoveDownloadsBetween(base::Time delete_begin, |
| 180 | base::Time delete_end) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | // This does not use an index. We currently aren't likely to have enough |
| 182 | // downloads where an index by time will give us a lot of benefit. |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 183 | sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 184 | "DELETE FROM downloads WHERE start_time >= ? AND start_time < ? " |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 185 | "AND (State = ? OR State = ?)")); |
| 186 | if (!statement) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 187 | return; |
| 188 | |
| 189 | time_t start_time = delete_begin.ToTimeT(); |
| 190 | time_t end_time = delete_end.ToTimeT(); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 191 | statement.BindInt64(0, start_time); |
| 192 | statement.BindInt64( |
[email protected] | d321644 | 2009-03-05 21:07:27 | [diff] [blame] | 193 | 1, |
| 194 | end_time ? end_time : std::numeric_limits<int64>::max()); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 195 | statement.BindInt(2, DownloadItem::COMPLETE); |
| 196 | statement.BindInt(3, DownloadItem::CANCELLED); |
| 197 | statement.Run(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | void DownloadDatabase::SearchDownloads(std::vector<int64>* results, |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 201 | const string16& search_text) { |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 202 | sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | "SELECT id FROM downloads WHERE url LIKE ? " |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 204 | "OR full_path LIKE ? ORDER BY id")); |
| 205 | if (!statement) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | return; |
| 207 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 208 | std::string text("%"); |
[email protected] | e5366896 | 2010-06-23 15:35:25 | [diff] [blame] | 209 | text.append(UTF16ToUTF8(search_text)); |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 210 | text.push_back('%'); |
| 211 | statement.BindString(0, text); |
| 212 | statement.BindString(1, text); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 213 | |
[email protected] | 765b4450 | 2009-10-02 05:01:42 | [diff] [blame] | 214 | while (statement.Step()) |
| 215 | results->push_back(statement.ColumnInt64(0)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | } // namespace history |