Chromium Code Reviews| 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" |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 175 base::string16 title = statement->ColumnString16(10); | 175 base::string16 title = statement->ColumnString16(10); |
| 176 | 176 |
| 177 OfflinePageItem item(url, id, client_id, path, file_size, creation_time); | 177 OfflinePageItem item(url, id, client_id, path, file_size, creation_time); |
| 178 item.last_access_time = last_access_time; | 178 item.last_access_time = last_access_time; |
| 179 item.access_count = access_count; | 179 item.access_count = access_count; |
| 180 item.expiration_time = expiration_time; | 180 item.expiration_time = expiration_time; |
| 181 item.title = title; | 181 item.title = title; |
| 182 return item; | 182 return item; |
| 183 } | 183 } |
| 184 | 184 |
| 185 bool Insert(sql::Connection* db, const OfflinePageItem& item) { | |
| 186 // Using 'INSERT OR FAIL' or 'INSERT OR ABORT' in the query below causes debug | |
| 187 // builds to DLOG. | |
| 188 const char kSql[] = | |
| 189 "INSERT OR IGNORE INTO " OFFLINE_PAGES_TABLE_NAME | |
| 190 " (offline_id, online_url, client_namespace, client_id, file_path, " | |
|
dewittj
2016/09/15 14:58:26
Can we parametrize this so that there's no chance
fgorski
2016/09/15 16:53:47
No, and you will see why in the patch that follows
| |
| 191 "file_size, creation_time, last_access_time, access_count, " | |
| 192 "expiration_time, title)" | |
| 193 " VALUES " | |
| 194 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | |
| 195 | |
| 196 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | |
| 197 statement.BindInt64(0, item.offline_id); | |
| 198 statement.BindString(1, item.url.spec()); | |
| 199 statement.BindString(2, item.client_id.name_space); | |
| 200 statement.BindString(3, item.client_id.id); | |
| 201 statement.BindString(4, GetUTF8StringFromPath(item.file_path)); | |
| 202 statement.BindInt64(5, item.file_size); | |
| 203 statement.BindInt64(6, item.creation_time.ToInternalValue()); | |
| 204 statement.BindInt64(7, item.last_access_time.ToInternalValue()); | |
| 205 statement.BindInt(8, item.access_count); | |
| 206 statement.BindInt64(9, item.expiration_time.ToInternalValue()); | |
| 207 statement.BindString16(10, item.title); | |
| 208 return statement.Run() && db->GetLastChangeCount() > 0; | |
|
dewittj
2016/09/15 14:58:26
This function should return ItemActionStatus, so t
fgorski
2016/09/15 16:53:47
Done.
| |
| 209 } | |
| 210 | |
| 185 bool InsertOrReplace(sql::Connection* db, const OfflinePageItem& item) { | 211 bool InsertOrReplace(sql::Connection* db, const OfflinePageItem& item) { |
| 186 const char kSql[] = | 212 const char kSql[] = |
| 187 "INSERT OR REPLACE INTO " OFFLINE_PAGES_TABLE_NAME | 213 "INSERT OR REPLACE INTO " OFFLINE_PAGES_TABLE_NAME |
| 188 " (offline_id, online_url, client_namespace, client_id, file_path, " | 214 " (offline_id, online_url, client_namespace, client_id, file_path, " |
| 189 "file_size, creation_time, last_access_time, access_count, " | 215 "file_size, creation_time, last_access_time, access_count, " |
| 190 "expiration_time, title)" | 216 "expiration_time, title)" |
| 191 " VALUES " | 217 " VALUES " |
| 192 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; | 218 " (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; |
| 193 | 219 |
| 194 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); | 220 sql::Statement statement(db->GetCachedStatement(SQL_FROM_HERE, kSql)); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 273 NotifyLoadResult(runner, callback, | 299 NotifyLoadResult(runner, callback, |
| 274 OfflinePageMetadataStore::STORE_LOAD_FAILED, result); | 300 OfflinePageMetadataStore::STORE_LOAD_FAILED, result); |
| 275 } | 301 } |
| 276 } | 302 } |
| 277 | 303 |
| 278 void AddOfflinePageSync(sql::Connection* db, | 304 void AddOfflinePageSync(sql::Connection* db, |
| 279 scoped_refptr<base::SingleThreadTaskRunner> runner, | 305 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 280 const OfflinePageItem& offline_page, | 306 const OfflinePageItem& offline_page, |
| 281 const OfflinePageMetadataStore::AddCallback& callback) { | 307 const OfflinePageMetadataStore::AddCallback& callback) { |
| 282 // TODO(fgorski): Only insert should happen here. | 308 // TODO(fgorski): Only insert should happen here. |
| 283 InsertOrReplace(db, offline_page); | 309 bool success = Insert(db, offline_page); |
| 284 runner->PostTask(FROM_HERE, | 310 runner->PostTask( |
| 285 base::Bind(callback, OfflinePageMetadataStore::SUCCESS)); | 311 FROM_HERE, |
| 312 base::Bind(callback, success ? OfflinePageMetadataStore::SUCCESS | |
| 313 : OfflinePageMetadataStore::ALREADY_EXISTS)); | |
| 286 } | 314 } |
| 287 | 315 |
| 288 void UpdateOfflinePagesSync( | 316 void UpdateOfflinePagesSync( |
| 289 sql::Connection* db, | 317 sql::Connection* db, |
| 290 scoped_refptr<base::SingleThreadTaskRunner> runner, | 318 scoped_refptr<base::SingleThreadTaskRunner> runner, |
| 291 const std::vector<OfflinePageItem>& pages, | 319 const std::vector<OfflinePageItem>& pages, |
| 292 const OfflinePageMetadataStore::UpdateCallback& callback) { | 320 const OfflinePageMetadataStore::UpdateCallback& callback) { |
| 293 // TODO(fgorski): Update the callback to provide information about all items | 321 // TODO(fgorski): Update the callback to provide information about all items |
| 294 // and not just a high level boolean. | 322 // and not just a high level boolean. |
| 295 sql::Transaction transaction(db); | 323 sql::Transaction transaction(db); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 468 DCHECK(db_.get()); | 496 DCHECK(db_.get()); |
| 469 if (!db_.get()) { | 497 if (!db_.get()) { |
| 470 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, | 498 base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, |
| 471 base::Bind(callback)); | 499 base::Bind(callback)); |
| 472 return false; | 500 return false; |
| 473 } | 501 } |
| 474 return true; | 502 return true; |
| 475 } | 503 } |
| 476 | 504 |
| 477 } // namespace offline_pages | 505 } // namespace offline_pages |
| OLD | NEW |