OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "components/offline_pages/offline_page_metadata_store_sql.h" | 5 #include "components/offline_pages/offline_page_metadata_store_sql.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/metrics/histogram_macros.h" | 12 #include "base/metrics/histogram_macros.h" |
13 #include "base/sequenced_task_runner.h" | 13 #include "base/sequenced_task_runner.h" |
14 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
15 #include "base/threading/thread_task_runner_handle.h" | 15 #include "base/threading/thread_task_runner_handle.h" |
16 #include "components/offline_pages/offline_page_item.h" | 16 #include "components/offline_pages/offline_page_item.h" |
17 #include "sql/connection.h" | 17 #include "sql/connection.h" |
18 #include "sql/statement.h" | 18 #include "sql/statement.h" |
19 #include "sql/transaction.h" | 19 #include "sql/transaction.h" |
20 | 20 |
21 namespace offline_pages { | 21 namespace offline_pages { |
22 | 22 |
23 using StoreState = OfflinePageMetadataStore::StoreState; | 23 using StoreState = OfflinePageMetadataStore::StoreState; |
| 24 using ItemActionStatus = OfflinePageMetadataStore::ItemActionStatus; |
24 | 25 |
25 namespace { | 26 namespace { |
26 | 27 |
27 // This is a macro instead of a const so that | 28 // This is a macro instead of a const so that |
28 // it can be used inline in other SQL statements below. | 29 // it can be used inline in other SQL statements below. |
29 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" | 30 #define OFFLINE_PAGES_TABLE_NAME "offlinepages_v1" |
30 | 31 |
31 bool CreateOfflinePagesTable(sql::Connection* db) { | 32 bool CreateOfflinePagesTable(sql::Connection* db) { |
32 const char kSql[] = "CREATE TABLE IF NOT EXISTS " OFFLINE_PAGES_TABLE_NAME | 33 const char kSql[] = "CREATE TABLE IF NOT EXISTS " OFFLINE_PAGES_TABLE_NAME |
33 "(offline_id INTEGER PRIMARY KEY NOT NULL," | 34 "(offline_id INTEGER PRIMARY KEY NOT NULL," |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 base::string16 title = statement->ColumnString16(10); | 176 base::string16 title = statement->ColumnString16(10); |
176 | 177 |
177 OfflinePageItem item(url, id, client_id, path, file_size, creation_time); | 178 OfflinePageItem item(url, id, client_id, path, file_size, creation_time); |
178 item.last_access_time = last_access_time; | 179 item.last_access_time = last_access_time; |
179 item.access_count = access_count; | 180 item.access_count = access_count; |
180 item.expiration_time = expiration_time; | 181 item.expiration_time = expiration_time; |
181 item.title = title; | 182 item.title = title; |
182 return item; | 183 return item; |
183 } | 184 } |
184 | 185 |
| 186 ItemActionStatus Insert(sql::Connection* db, const OfflinePageItem& item) { |
| 187 // Using 'INSERT OR FAIL' or 'INSERT OR ABORT' in the query below causes debug |
| 188 // builds to DLOG. |
| 189 const char kSql[] = |
| 190 "INSERT OR IGNORE INTO " OFFLINE_PAGES_TABLE_NAME |
| 191 " (offline_id, online_url, client_namespace, client_id, file_path, " |
| 192 "file_size, creation_time, last_access_time, access_count, " |
| 193 "expiration_time, title)" |
| 194 " VALUES " |
| 195 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
| 196 |
| 197 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| 198 statement.BindInt64(0, item.offline_id); |
| 199 statement.BindString(1, item.url.spec()); |
| 200 statement.BindString(2, item.client_id.name_space); |
| 201 statement.BindString(3, item.client_id.id); |
| 202 statement.BindString(4, GetUTF8StringFromPath(item.file_path)); |
| 203 statement.BindInt64(5, item.file_size); |
| 204 statement.BindInt64(6, item.creation_time.ToInternalValue()); |
| 205 statement.BindInt64(7, item.last_access_time.ToInternalValue()); |
| 206 statement.BindInt(8, item.access_count); |
| 207 statement.BindInt64(9, item.expiration_time.ToInternalValue()); |
| 208 statement.BindString16(10, item.title); |
| 209 if (!statement.Run()) |
| 210 return ItemActionStatus::STORE_ERROR; |
| 211 if (db->GetLastChangeCount() == 0) |
| 212 return ItemActionStatus::ALREADY_EXISTS; |
| 213 return ItemActionStatus::SUCCESS; |
| 214 } |
| 215 |
185 bool InsertOrReplace(sql::Connection* db, const OfflinePageItem& item) { | 216 bool InsertOrReplace(sql::Connection* db, const OfflinePageItem& item) { |
186 const char kSql[] = | 217 const char kSql[] = |
187 "INSERT OR REPLACE INTO " OFFLINE_PAGES_TABLE_NAME | 218 "INSERT OR REPLACE INTO " OFFLINE_PAGES_TABLE_NAME |
188 " (offline_id, online_url, client_namespace, client_id, file_path, " | 219 " (offline_id, online_url, client_namespace, client_id, file_path, " |
189 "file_size, creation_time, last_access_time, access_count, " | 220 "file_size, creation_time, last_access_time, access_count, " |
190 "expiration_time, title)" | 221 "expiration_time, title)" |
191 " VALUES " | 222 " VALUES " |
192 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | 223 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
193 | 224 |
194 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 225 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
272 result.clear(); | 303 result.clear(); |
273 NotifyLoadResult(runner, callback, | 304 NotifyLoadResult(runner, callback, |
274 OfflinePageMetadataStore::STORE_LOAD_FAILED, result); | 305 OfflinePageMetadataStore::STORE_LOAD_FAILED, result); |
275 } | 306 } |
276 } | 307 } |
277 | 308 |
278 void AddOfflinePageSync(sql::Connection* db, | 309 void AddOfflinePageSync(sql::Connection* db, |
279 scoped_refptr<base::SingleThreadTaskRunner> runner, | 310 scoped_refptr<base::SingleThreadTaskRunner> runner, |
280 const OfflinePageItem& offline_page, | 311 const OfflinePageItem& offline_page, |
281 const OfflinePageMetadataStore::AddCallback& callback) { | 312 const OfflinePageMetadataStore::AddCallback& callback) { |
282 // TODO(fgorski): Only insert should happen here. | 313 ItemActionStatus status = Insert(db, offline_page); |
283 InsertOrReplace(db, offline_page); | 314 runner->PostTask(FROM_HERE, base::Bind(callback, status)); |
284 runner->PostTask(FROM_HERE, | |
285 base::Bind(callback, OfflinePageMetadataStore::SUCCESS)); | |
286 } | 315 } |
287 | 316 |
288 void UpdateOfflinePagesSync( | 317 void UpdateOfflinePagesSync( |
289 sql::Connection* db, | 318 sql::Connection* db, |
290 scoped_refptr<base::SingleThreadTaskRunner> runner, | 319 scoped_refptr<base::SingleThreadTaskRunner> runner, |
291 const std::vector<OfflinePageItem>& pages, | 320 const std::vector<OfflinePageItem>& pages, |
292 const OfflinePageMetadataStore::UpdateCallback& callback) { | 321 const OfflinePageMetadataStore::UpdateCallback& callback) { |
293 // TODO(fgorski): Update the callback to provide information about all items | 322 // TODO(fgorski): Update the callback to provide information about all items |
294 // and not just a high level boolean. | 323 // and not just a high level boolean. |
295 sql::Transaction transaction(db); | 324 sql::Transaction transaction(db); |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
468 DCHECK(db_.get()); | 497 DCHECK(db_.get()); |
469 if (!db_.get()) { | 498 if (!db_.get()) { |
470 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 499 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
471 base::Bind(callback)); | 500 base::Bind(callback)); |
472 return false; | 501 return false; |
473 } | 502 } |
474 return true; | 503 return true; |
475 } | 504 } |
476 | 505 |
477 } // namespace offline_pages | 506 } // namespace offline_pages |
OLD | NEW |