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 LOG(WARNING) << "INITING"; |
| 57 if (did_init_) |
| 58 return; |
60 did_init_ = true; | 59 did_init_ = true; |
61 if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) | 60 DCHECK(GetActivityLogTaskRunner()->RunsTasksInCurrentSequence()); |
62 DCHECK_CURRENTLY_ON(BrowserThread::DB); | |
63 db_.set_histogram_tag("Activity"); | 61 db_.set_histogram_tag("Activity"); |
| 62 LOG(WARNING) << "Set callback"; |
64 db_.set_error_callback( | 63 db_.set_error_callback( |
65 base::Bind(&ActivityDatabase::DatabaseErrorCallback, | 64 base::Bind(&ActivityDatabase::DatabaseErrorCallback, |
66 base::Unretained(this))); | 65 base::Unretained(this))); |
67 db_.set_page_size(4096); | 66 db_.set_page_size(4096); |
68 db_.set_cache_size(32); | 67 db_.set_cache_size(32); |
69 | 68 |
70 // This db does not use [meta] table, store mmap status data elsewhere. | 69 // This db does not use [meta] table, store mmap status data elsewhere. |
71 db_.set_mmap_alt_status(); | 70 db_.set_mmap_alt_status(); |
72 | 71 |
73 if (!db_.Open(db_name)) { | 72 if (!db_.Open(db_name)) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
133 this, | 132 this, |
134 &ActivityDatabase::RecordBatchedActions); | 133 &ActivityDatabase::RecordBatchedActions); |
135 } else if (!batch_mode && batch_mode_) { | 134 } else if (!batch_mode && batch_mode_) { |
136 timer_.Stop(); | 135 timer_.Stop(); |
137 RecordBatchedActions(); | 136 RecordBatchedActions(); |
138 } | 137 } |
139 batch_mode_ = batch_mode; | 138 batch_mode_ = batch_mode; |
140 } | 139 } |
141 | 140 |
142 sql::Connection* ActivityDatabase::GetSqlConnection() { | 141 sql::Connection* ActivityDatabase::GetSqlConnection() { |
143 if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) | 142 DCHECK(GetActivityLogTaskRunner()->RunsTasksInCurrentSequence()); |
144 DCHECK_CURRENTLY_ON(BrowserThread::DB); | |
145 if (valid_db_) { | 143 if (valid_db_) { |
146 return &db_; | 144 return &db_; |
147 } else { | 145 } else { |
148 LOG(WARNING) << "Activity log database is not valid"; | |
149 return NULL; | 146 return NULL; |
150 } | 147 } |
151 } | 148 } |
152 | 149 |
153 void ActivityDatabase::Close() { | 150 void ActivityDatabase::Close() { |
154 timer_.Stop(); | 151 timer_.Stop(); |
155 if (!already_closed_) { | 152 if (!already_closed_) { |
156 RecordBatchedActions(); | 153 RecordBatchedActions(); |
157 db_.reset_error_callback(); | 154 db_.reset_error_callback(); |
158 } | 155 } |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 field_types[i]); | 231 field_types[i]); |
235 if (!db->Execute(table_updater.c_str())) | 232 if (!db->Execute(table_updater.c_str())) |
236 return false; | 233 return false; |
237 } | 234 } |
238 } | 235 } |
239 } | 236 } |
240 return true; | 237 return true; |
241 } | 238 } |
242 | 239 |
243 } // namespace extensions | 240 } // namespace extensions |
OLD | NEW |