| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/extensions/activity_log/activity_database.h" | 5 #include "chrome/browser/extensions/activity_log/activity_database.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 13 #include "base/threading/thread.h" | |
| 14 #include "base/threading/thread_checker.h" | |
| 15 #include "base/time/clock.h" | 13 #include "base/time/clock.h" |
| 16 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 17 #include "build/build_config.h" | 15 #include "build/build_config.h" |
| 16 #include "chrome/browser/extensions/activity_log/activity_log_task_runner.h" |
| 18 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | 17 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" |
| 19 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 20 #include "sql/error_delegate_util.h" | 19 #include "sql/error_delegate_util.h" |
| 21 #include "sql/init_status.h" | 20 #include "sql/init_status.h" |
| 22 #include "sql/transaction.h" | 21 #include "sql/transaction.h" |
| 23 #include "third_party/sqlite/sqlite3.h" | 22 #include "third_party/sqlite/sqlite3.h" |
| 24 | 23 |
| 25 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
| 26 #include "base/mac/mac_util.h" | 25 #include "base/mac/mac_util.h" |
| 27 #endif | 26 #endif |
| 28 | 27 |
| 29 using content::BrowserThread; | |
| 30 | |
| 31 namespace extensions { | 28 namespace extensions { |
| 32 | 29 |
| 33 // A size threshold at which data should be flushed to the database. The | 30 // A size threshold at which data should be flushed to the database. The |
| 34 // ActivityDatabase will signal the Delegate to write out data based on a | 31 // ActivityDatabase will signal the Delegate to write out data based on a |
| 35 // periodic timer, but will also initiate a flush if AdviseFlush indicates that | 32 // periodic timer, but will also initiate a flush if AdviseFlush indicates that |
| 36 // more than kSizeThresholdForFlush action records are queued in memory. This | 33 // more than kSizeThresholdForFlush action records are queued in memory. This |
| 37 // should be set large enough that write costs can be amortized across many | 34 // should be set large enough that write costs can be amortized across many |
| 38 // records, but not so large that too much space can be tied up holding records | 35 // records, but not so large that too much space can be tied up holding records |
| 39 // in memory. | 36 // in memory. |
| 40 static const int kSizeThresholdForFlush = 200; | 37 static const int kSizeThresholdForFlush = 200; |
| 41 | 38 |
| 42 ActivityDatabase::ActivityDatabase(ActivityDatabase::Delegate* delegate) | 39 ActivityDatabase::ActivityDatabase(ActivityDatabase::Delegate* delegate) |
| 43 : delegate_(delegate), | 40 : delegate_(delegate), |
| 44 valid_db_(false), | 41 valid_db_(false), |
| 45 batch_mode_(true), | 42 batch_mode_(true), |
| 46 already_closed_(false), | 43 already_closed_(false), |
| 47 did_init_(false) { | 44 did_init_(false) { |
| 48 if (base::CommandLine::ForCurrentProcess()->HasSwitch( | 45 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 49 switches::kEnableExtensionActivityLogTesting)) { | 46 switches::kEnableExtensionActivityLogTesting)) { |
| 50 batching_period_ = base::TimeDelta::FromSeconds(10); | 47 batching_period_ = base::TimeDelta::FromSeconds(10); |
| 51 } else { | 48 } else { |
| 52 batching_period_ = base::TimeDelta::FromMinutes(2); | 49 batching_period_ = base::TimeDelta::FromMinutes(2); |
| 53 } | 50 } |
| 54 } | 51 } |
| 55 | 52 |
| 56 ActivityDatabase::~ActivityDatabase() {} | 53 ActivityDatabase::~ActivityDatabase() {} |
| 57 | 54 |
| 58 void ActivityDatabase::Init(const base::FilePath& db_name) { | 55 void ActivityDatabase::Init(const base::FilePath& db_name) { |
| 59 if (did_init_) return; | 56 if (did_init_) |
| 57 return; |
| 60 did_init_ = true; | 58 did_init_ = true; |
| 61 if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) | 59 DCHECK(GetActivityLogTaskRunner()->RunsTasksInCurrentSequence()); |
| 62 DCHECK_CURRENTLY_ON(BrowserThread::DB); | |
| 63 db_.set_histogram_tag("Activity"); | 60 db_.set_histogram_tag("Activity"); |
| 64 db_.set_error_callback( | 61 db_.set_error_callback( |
| 65 base::Bind(&ActivityDatabase::DatabaseErrorCallback, | 62 base::Bind(&ActivityDatabase::DatabaseErrorCallback, |
| 66 base::Unretained(this))); | 63 base::Unretained(this))); |
| 67 db_.set_page_size(4096); | 64 db_.set_page_size(4096); |
| 68 db_.set_cache_size(32); | 65 db_.set_cache_size(32); |
| 69 | 66 |
| 70 // This db does not use [meta] table, store mmap status data elsewhere. | 67 // This db does not use [meta] table, store mmap status data elsewhere. |
| 71 db_.set_mmap_alt_status(); | 68 db_.set_mmap_alt_status(); |
| 72 | 69 |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 133 this, | 130 this, |
| 134 &ActivityDatabase::RecordBatchedActions); | 131 &ActivityDatabase::RecordBatchedActions); |
| 135 } else if (!batch_mode && batch_mode_) { | 132 } else if (!batch_mode && batch_mode_) { |
| 136 timer_.Stop(); | 133 timer_.Stop(); |
| 137 RecordBatchedActions(); | 134 RecordBatchedActions(); |
| 138 } | 135 } |
| 139 batch_mode_ = batch_mode; | 136 batch_mode_ = batch_mode; |
| 140 } | 137 } |
| 141 | 138 |
| 142 sql::Connection* ActivityDatabase::GetSqlConnection() { | 139 sql::Connection* ActivityDatabase::GetSqlConnection() { |
| 143 if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) | 140 DCHECK(GetActivityLogTaskRunner()->RunsTasksInCurrentSequence()); |
| 144 DCHECK_CURRENTLY_ON(BrowserThread::DB); | |
| 145 if (valid_db_) { | 141 if (valid_db_) { |
| 146 return &db_; | 142 return &db_; |
| 147 } else { | 143 } else { |
| 148 LOG(WARNING) << "Activity log database is not valid"; | |
| 149 return NULL; | 144 return NULL; |
| 150 } | 145 } |
| 151 } | 146 } |
| 152 | 147 |
| 153 void ActivityDatabase::Close() { | 148 void ActivityDatabase::Close() { |
| 154 timer_.Stop(); | 149 timer_.Stop(); |
| 155 if (!already_closed_) { | 150 if (!already_closed_) { |
| 156 RecordBatchedActions(); | 151 RecordBatchedActions(); |
| 157 db_.reset_error_callback(); | 152 db_.reset_error_callback(); |
| 158 } | 153 } |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 234 field_types[i]); | 229 field_types[i]); |
| 235 if (!db->Execute(table_updater.c_str())) | 230 if (!db->Execute(table_updater.c_str())) |
| 236 return false; | 231 return false; |
| 237 } | 232 } |
| 238 } | 233 } |
| 239 } | 234 } |
| 240 return true; | 235 return true; |
| 241 } | 236 } |
| 242 | 237 |
| 243 } // namespace extensions | 238 } // namespace extensions |
| OLD | NEW |