Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(185)

Side by Side Diff: chrome/browser/history/download_database.cc

Issue 8008021: Add new UMA stats to get a handle on Downloads UI Usage (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: fix windows auto_closed_ Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #include "chrome/browser/history/download_database.h" 5 #include "chrome/browser/history/download_database.h"
6 6
7 #include <limits> 7 #include <limits>
8 #include <string>
8 #include <vector> 9 #include <vector>
9 10
10 #include "base/file_path.h" 11 #include "base/file_path.h"
11 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
12 #include "build/build_config.h" 13 #include "build/build_config.h"
13 #include "content/browser/browser_thread.h" 14 #include "content/browser/browser_thread.h"
14 #include "content/browser/download/download_item.h" 15 #include "content/browser/download/download_item.h"
15 #include "content/browser/download/download_persistent_store_info.h" 16 #include "content/browser/download/download_persistent_store_info.h"
16 #include "sql/statement.h" 17 #include "sql/statement.h"
17 18
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 { 19 namespace history {
31 20
32 namespace { 21 namespace {
33 22
23 static const char kSchema[] =
24 "CREATE TABLE downloads ("
25 "id INTEGER PRIMARY KEY," // SQLite-generated primary key.
26 "full_path LONGVARCHAR NOT NULL," // Location of the download on disk.
27 "url LONGVARCHAR NOT NULL," // URL of the downloaded file.
28 "start_time INTEGER NOT NULL," // When the download was started.
29 "received_bytes INTEGER NOT NULL," // Total size downloaded.
30 "total_bytes INTEGER NOT NULL," // Total size of the download.
31 "state INTEGER NOT NULL," // 1=complete, 2=cancelled, 4=interrupted
32 "end_time INTEGER NOT NULL," // When the download completed.
33 "opened INTEGER NOT NULL)"; // 1 if the download has ever been opened else 0
Randy Smith (Not in Mondays) 2011/10/09 23:40:17 nit/suggestion: Line up the comments.
benjhayden 2011/10/10 16:27:11 Done.
34
34 #if defined(OS_POSIX) 35 #if defined(OS_POSIX)
35 36
36 // Binds/reads the given file path to the given column of the given statement. 37 // 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 void BindFilePath(sql::Statement& statement, const FilePath& path, int col) {
38 statement.BindString(col, path.value()); 39 statement.BindString(col, path.value());
39 } 40 }
40 FilePath ColumnFilePath(sql::Statement& statement, int col) { 41 FilePath ColumnFilePath(sql::Statement& statement, int col) {
41 return FilePath(statement.ColumnString(col)); 42 return FilePath(statement.ColumnString(col));
42 } 43 }
43 44
(...skipping 17 matching lines...) Expand all
61 62
62 DownloadDatabase::DownloadDatabase() 63 DownloadDatabase::DownloadDatabase()
63 : owning_thread_set_(false), 64 : owning_thread_set_(false),
64 owning_thread_(0), 65 owning_thread_(0),
65 next_id_(0) { 66 next_id_(0) {
66 } 67 }
67 68
68 DownloadDatabase::~DownloadDatabase() { 69 DownloadDatabase::~DownloadDatabase() {
69 } 70 }
70 71
72 bool DownloadDatabase::EnsureColumnExists(
73 const std::string& name, const std::string& type) {
74 std::string add_col = "ALTER TABLE downloads ADD COLUMN " + name + " " + type;
75 return GetDB().DoesColumnExist("downloads", name.c_str()) ||
76 GetDB().Execute(add_col.c_str());
77 }
78
71 bool DownloadDatabase::InitDownloadTable() { 79 bool DownloadDatabase::InitDownloadTable() {
72 if (!GetDB().DoesTableExist("downloads")) {
73 if (!GetDB().Execute(
74 "CREATE TABLE downloads ("
75 "id INTEGER PRIMARY KEY,"
76 "full_path LONGVARCHAR NOT NULL,"
77 "url LONGVARCHAR NOT NULL,"
78 "start_time INTEGER NOT NULL,"
79 "received_bytes INTEGER NOT NULL,"
80 "total_bytes INTEGER NOT NULL,"
81 "state INTEGER NOT NULL)"))
82 return false;
83 }
84 meta_table_.Init(&GetDB(), 0, 0); 80 meta_table_.Init(&GetDB(), 0, 0);
85 meta_table_.GetValue(kNextDownloadId, &next_id_); 81 meta_table_.GetValue(kNextDownloadId, &next_id_);
86 return true; 82 if (GetDB().DoesTableExist("downloads")) {
83 return EnsureColumnExists("end_time", "INTEGER NOT NULL DEFAULT 0") &&
84 EnsureColumnExists("opened", "INTEGER NOT NULL DEFAULT 0");
85 } else {
86 return GetDB().Execute(kSchema);
87 }
87 } 88 }
88 89
89 bool DownloadDatabase::DropDownloadTable() { 90 bool DownloadDatabase::DropDownloadTable() {
90 return GetDB().Execute("DROP TABLE downloads"); 91 return GetDB().Execute("DROP TABLE downloads");
91 } 92 }
92 93
93 void DownloadDatabase::QueryDownloads( 94 void DownloadDatabase::QueryDownloads(
94 std::vector<DownloadPersistentStoreInfo>* results) { 95 std::vector<DownloadPersistentStoreInfo>* results) {
95 results->clear(); 96 results->clear();
96 97
97 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 98 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
98 "SELECT id, full_path, url, start_time, received_bytes, " 99 "SELECT id, full_path, url, start_time, received_bytes, "
99 "total_bytes, state " 100 "total_bytes, state, end_time, opened "
100 "FROM downloads " 101 "FROM downloads "
101 "ORDER BY start_time")); 102 "ORDER BY start_time"));
102 if (!statement) 103 if (!statement)
103 return; 104 return;
104 105
105 while (statement.Step()) { 106 while (statement.Step()) {
106 DownloadPersistentStoreInfo info; 107 DownloadPersistentStoreInfo info;
107 info.db_handle = statement.ColumnInt64(0); 108 info.db_handle = statement.ColumnInt64(0);
108 109
109 info.path = ColumnFilePath(statement, 1); 110 info.path = ColumnFilePath(statement, 1);
110 info.url = GURL(statement.ColumnString(2)); 111 info.url = GURL(statement.ColumnString(2));
111 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3)); 112 info.start_time = base::Time::FromTimeT(statement.ColumnInt64(3));
112 info.received_bytes = statement.ColumnInt64(4); 113 info.received_bytes = statement.ColumnInt64(4);
113 info.total_bytes = statement.ColumnInt64(5); 114 info.total_bytes = statement.ColumnInt64(5);
114 info.state = statement.ColumnInt(6); 115 info.state = statement.ColumnInt(6);
116 info.end_time = base::Time::FromTimeT(statement.ColumnInt64(7));
117 info.opened = statement.ColumnInt(8) != 0;
115 results->push_back(info); 118 results->push_back(info);
116 } 119 }
117 } 120 }
118 121
119 bool DownloadDatabase::UpdateDownload(int64 received_bytes, 122 bool DownloadDatabase::UpdateDownload(const DownloadPersistentStoreInfo& data) {
120 int32 state, 123 DCHECK(data.db_handle > 0);
121 DownloadID db_handle) {
122 DCHECK(db_handle > 0);
123 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 124 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
124 "UPDATE downloads " 125 "UPDATE downloads "
125 "SET received_bytes=?, state=? WHERE id=?")); 126 "SET received_bytes=?, state=?, end_time=?, opened=? WHERE id=?"));
126 if (!statement) 127 if (!statement)
127 return false; 128 return false;
128 129
129 statement.BindInt64(0, received_bytes); 130 statement.BindInt64(0, data.received_bytes);
130 statement.BindInt(1, state); 131 statement.BindInt(1, data.state);
131 statement.BindInt64(2, db_handle); 132 statement.BindInt64(2, data.end_time.ToTimeT());
133 statement.BindInt(3, (data.opened ? 1 : 0));
134 statement.BindInt64(4, data.db_handle);
132 return statement.Run(); 135 return statement.Run();
133 } 136 }
134 137
135 bool DownloadDatabase::UpdateDownloadPath(const FilePath& path, 138 bool DownloadDatabase::UpdateDownloadPath(const FilePath& path,
136 DownloadID db_handle) { 139 DownloadID db_handle) {
137 DCHECK(db_handle > 0); 140 DCHECK(db_handle > 0);
138 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 141 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
139 "UPDATE downloads SET full_path=? WHERE id=?")); 142 "UPDATE downloads SET full_path=? WHERE id=?"));
140 if (!statement) 143 if (!statement)
141 return false; 144 return false;
(...skipping 17 matching lines...) Expand all
159 const DownloadPersistentStoreInfo& info) { 162 const DownloadPersistentStoreInfo& info) {
160 if (owning_thread_set_) { 163 if (owning_thread_set_) {
161 CHECK_EQ(owning_thread_, base::PlatformThread::CurrentId()); 164 CHECK_EQ(owning_thread_, base::PlatformThread::CurrentId());
162 } else { 165 } else {
163 owning_thread_ = base::PlatformThread::CurrentId(); 166 owning_thread_ = base::PlatformThread::CurrentId();
164 owning_thread_set_ = true; 167 owning_thread_set_ = true;
165 } 168 }
166 169
167 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE, 170 sql::Statement statement(GetDB().GetCachedStatement(SQL_FROM_HERE,
168 "INSERT INTO downloads " 171 "INSERT INTO downloads "
169 "(full_path, url, start_time, received_bytes, total_bytes, state) " 172 "(full_path, url, start_time, received_bytes, total_bytes, state, "
170 "VALUES (?, ?, ?, ?, ?, ?)")); 173 "end_time, opened) "
174 "VALUES (?, ?, ?, ?, ?, ?, ?, ?)"));
171 if (!statement) 175 if (!statement)
172 return 0; 176 return 0;
173 177
174 BindFilePath(statement, info.path, 0); 178 BindFilePath(statement, info.path, 0);
175 statement.BindString(1, info.url.spec()); 179 statement.BindString(1, info.url.spec());
176 statement.BindInt64(2, info.start_time.ToTimeT()); 180 statement.BindInt64(2, info.start_time.ToTimeT());
177 statement.BindInt64(3, info.received_bytes); 181 statement.BindInt64(3, info.received_bytes);
178 statement.BindInt64(4, info.total_bytes); 182 statement.BindInt64(4, info.total_bytes);
179 statement.BindInt(5, info.state); 183 statement.BindInt(5, info.state);
184 statement.BindInt64(6, info.end_time.ToTimeT());
185 statement.BindInt(7, info.opened ? 1 : 0);
180 186
181 if (statement.Run()) { 187 if (statement.Run()) {
182 int64 id = GetDB().GetLastInsertRowId(); 188 int64 id = GetDB().GetLastInsertRowId();
183 189
184 CHECK_EQ(0u, returned_ids_.count(id)); 190 CHECK_EQ(0u, returned_ids_.count(id));
185 returned_ids_.insert(id); 191 returned_ids_.insert(id);
186 192
187 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;} 193 // TODO(benjhayden) if(info.id>next_id_){setvalue;next_id_=info.id;}
188 meta_table_.SetValue(kNextDownloadId, ++next_id_); 194 meta_table_.SetValue(kNextDownloadId, ++next_id_);
189 195
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
242 statement.BindInt64( 248 statement.BindInt64(
243 1, 249 1,
244 end_time ? end_time : std::numeric_limits<int64>::max()); 250 end_time ? end_time : std::numeric_limits<int64>::max());
245 statement.BindInt(2, DownloadItem::COMPLETE); 251 statement.BindInt(2, DownloadItem::COMPLETE);
246 statement.BindInt(3, DownloadItem::CANCELLED); 252 statement.BindInt(3, DownloadItem::CANCELLED);
247 statement.BindInt(4, DownloadItem::INTERRUPTED); 253 statement.BindInt(4, DownloadItem::INTERRUPTED);
248 statement.Run(); 254 statement.Run();
249 } 255 }
250 256
251 } // namespace history 257 } // namespace history
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698