| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/precache/core/precache_database.h" | 5 #include "components/precache/core/precache_database.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/location.h" | 9 #include "base/location.h" |
| 10 #include "base/metrics/histogram_macros.h" | 10 #include "base/metrics/histogram_macros.h" |
| 11 #include "base/single_thread_task_runner.h" | 11 #include "base/single_thread_task_runner.h" |
| 12 #include "base/thread_task_runner_handle.h" | 12 #include "base/thread_task_runner_handle.h" |
| 13 #include "base/time/time.h" | 13 #include "base/time/time.h" |
| 14 #include "components/history/core/browser/history_constants.h" | 14 #include "components/history/core/browser/history_constants.h" |
| 15 #include "sql/connection.h" | 15 #include "sql/connection.h" |
| 16 #include "sql/transaction.h" | 16 #include "sql/transaction.h" |
| 17 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 18 | 18 |
| 19 namespace { | 19 namespace { |
| 20 | 20 |
| 21 // The number of days old that an entry in the precache URL table can be before | 21 // The number of days old that an entry in the precache URL table can be before |
| 22 // it is considered "old" and is removed from the table. | 22 // it is considered "old" and is removed from the table. |
| 23 const int kPrecacheHistoryExpiryPeriodDays = 60; | 23 const int kPrecacheHistoryExpiryPeriodDays = 60; |
| 24 | 24 |
| 25 } // namespace | 25 } // namespace |
| 26 | 26 |
| 27 namespace precache { | 27 namespace precache { |
| 28 | 28 |
| 29 UnfinishedWork::UnfinishedWork( |
| 30 const std::list<GURL>& manifest_urls_to_fetch, |
| 31 const std::list<GURL>& resource_urls_to_fetch, |
| 32 int64_t total_response_bytes, |
| 33 int64_t network_response_bytes, |
| 34 int64_t num_manifest_urls_to_fetch, |
| 35 const base::TimeTicks& start_time_of_session) |
| 36 : manifests(manifest_urls_to_fetch), |
| 37 resources(resource_urls_to_fetch), |
| 38 total_bytes(total_response_bytes), |
| 39 network_bytes(network_response_bytes), |
| 40 num_manifest_urls(num_manifest_urls_to_fetch), |
| 41 start_time(start_time_of_session) {} |
| 42 |
| 43 UnfinishedWork::UnfinishedWork( |
| 44 const base::TimeTicks& earliest_start_time_of_session) |
| 45 : total_bytes(0), network_bytes(0), num_manifest_urls(0), |
| 46 start_time(earliest_start_time_of_session) {} |
| 47 |
| 48 UnfinishedWork::~UnfinishedWork() {} |
| 49 |
| 29 PrecacheDatabase::PrecacheDatabase() | 50 PrecacheDatabase::PrecacheDatabase() |
| 30 : is_flush_posted_(false), weak_factory_(this) { | 51 : is_flush_posted_(false), weak_factory_(this) { |
| 31 // A PrecacheDatabase can be constructed on any thread. | 52 // A PrecacheDatabase can be constructed on any thread. |
| 32 thread_checker_.DetachFromThread(); | 53 thread_checker_.DetachFromThread(); |
| 33 } | 54 } |
| 34 | 55 |
| 35 PrecacheDatabase::~PrecacheDatabase() { | 56 PrecacheDatabase::~PrecacheDatabase() { |
| 36 // The destructor must not run on the UI thread, as it may trigger IO | 57 // The destructor must not run on the UI thread, as it may trigger IO |
| 37 // operations via sql::Connection's destructor. | 58 // operations via sql::Connection's destructor. |
| 38 DCHECK(thread_checker_.CalledOnValidThread()); | 59 DCHECK(thread_checker_.CalledOnValidThread()); |
| 39 } | 60 } |
| 40 | 61 |
| 41 bool PrecacheDatabase::Init(const base::FilePath& db_path) { | 62 bool PrecacheDatabase::Init(const base::FilePath& db_path) { |
| 42 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 43 DCHECK(!db_); // Init must only be called once. | 64 DCHECK(!db_); // Init must only be called once. |
| 44 | 65 |
| 45 db_.reset(new sql::Connection()); | 66 db_.reset(new sql::Connection()); |
| 46 db_->set_histogram_tag("Precache"); | 67 db_->set_histogram_tag("Precache"); |
| 47 | 68 |
| 48 if (!db_->Open(db_path)) { | 69 if (!db_->Open(db_path)) { |
| 49 // Don't initialize the URL table if unable to access | 70 // Don't initialize the URL table if unable to access |
| 50 // the database. | 71 // the database. |
| 51 return false; | 72 return false; |
| 52 } | 73 } |
| 53 | 74 |
| 54 if (!precache_url_table_.Init(db_.get())) { | 75 if (!precache_url_table_.Init(db_.get()) || |
| 76 !precache_session_tables_.Init(db_.get())) { |
| 55 // Raze and close the database connection to indicate that it's not usable, | 77 // Raze and close the database connection to indicate that it's not usable, |
| 56 // and so that the database will be created anew next time, in case it's | 78 // and so that the database will be created anew next time, in case it's |
| 57 // corrupted. | 79 // corrupted. |
| 58 db_->RazeAndClose(); | 80 db_->RazeAndClose(); |
| 59 return false; | 81 return false; |
| 60 } | 82 } |
| 61 return true; | 83 return true; |
| 62 } | 84 } |
| 63 | 85 |
| 64 void PrecacheDatabase::DeleteExpiredPrecacheHistory( | 86 void PrecacheDatabase::DeleteExpiredPrecacheHistory( |
| (...skipping 187 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 // Post a delayed task to flush the buffer in 1 second, so that multiple | 274 // Post a delayed task to flush the buffer in 1 second, so that multiple |
| 253 // database writes can be buffered up and flushed together in the same | 275 // database writes can be buffered up and flushed together in the same |
| 254 // transaction. | 276 // transaction. |
| 255 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 277 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| 256 FROM_HERE, base::Bind(&PrecacheDatabase::PostedFlush, | 278 FROM_HERE, base::Bind(&PrecacheDatabase::PostedFlush, |
| 257 weak_factory_.GetWeakPtr()), | 279 weak_factory_.GetWeakPtr()), |
| 258 base::TimeDelta::FromSeconds(1)); | 280 base::TimeDelta::FromSeconds(1)); |
| 259 is_flush_posted_ = true; | 281 is_flush_posted_ = true; |
| 260 } | 282 } |
| 261 | 283 |
| 284 void PrecacheDatabase::GetUnfinishedWork( |
| 285 scoped_refptr<UnfinishedWork> unfinished_work) { |
| 286 base::TimeTicks earliest_start_time = unfinished_work->start_time; |
| 287 precache_session_tables_.GetStatistics( |
| 288 &unfinished_work->total_bytes, |
| 289 &unfinished_work->network_bytes, |
| 290 &unfinished_work->num_manifest_urls, |
| 291 &unfinished_work->start_time); |
| 292 if (unfinished_work->start_time >= earliest_start_time) { |
| 293 precache_session_tables_.GetURLs(&unfinished_work->manifests, |
| 294 &unfinished_work->resources); |
| 295 } |
| 296 } |
| 297 |
| 298 void PrecacheDatabase::SaveUnfinishedWork( |
| 299 scoped_refptr<UnfinishedWork> unfinished_work) { |
| 300 precache_session_tables_.StoreURLs(unfinished_work->manifests, |
| 301 unfinished_work->resources); |
| 302 precache_session_tables_.StoreStatistics( |
| 303 unfinished_work->total_bytes, unfinished_work->network_bytes, |
| 304 unfinished_work->num_manifest_urls, |
| 305 unfinished_work->start_time); |
| 306 } |
| 307 |
| 308 void PrecacheDatabase::ClearPrecacheSessionState() { |
| 309 precache_session_tables_.ClearURLs(); |
| 310 precache_session_tables_.ClearStatistics(); |
| 311 } |
| 312 |
| 313 base::WeakPtr<PrecacheDatabase> PrecacheDatabase::GetWeakPtr() { |
| 314 return weak_factory_.GetWeakPtr(); |
| 315 } |
| 316 |
| 262 } // namespace precache | 317 } // namespace precache |
| OLD | NEW |