| 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 <string> | 5 #include <string> |
| 6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/json/json_reader.h" |
| 7 #include "base/logging.h" | 8 #include "base/logging.h" |
| 8 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 9 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| 10 #include "base/threading/thread.h" | 11 #include "base/threading/thread.h" |
| 11 #include "base/threading/thread_checker.h" | 12 #include "base/threading/thread_checker.h" |
| 12 #include "base/time/clock.h" | 13 #include "base/time/clock.h" |
| 13 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 14 #include "chrome/browser/extensions/activity_log/activity_database.h" | 15 #include "chrome/browser/extensions/activity_log/activity_database.h" |
| 16 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" |
| 15 #include "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
| 16 #include "sql/error_delegate_util.h" | 18 #include "sql/error_delegate_util.h" |
| 17 #include "sql/transaction.h" | 19 #include "sql/transaction.h" |
| 18 #include "third_party/sqlite/sqlite3.h" | 20 #include "third_party/sqlite/sqlite3.h" |
| 19 | 21 |
| 20 #if defined(OS_MACOSX) | 22 #if defined(OS_MACOSX) |
| 21 #include "base/mac/mac_util.h" | 23 #include "base/mac/mac_util.h" |
| 22 #endif | 24 #endif |
| 23 | 25 |
| 24 using content::BrowserThread; | 26 using content::BrowserThread; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 early_bound = morning_midnight.ToInternalValue(); | 155 early_bound = morning_midnight.ToInternalValue(); |
| 154 late_bound = base::Time::Max().ToInternalValue(); | 156 late_bound = base::Time::Max().ToInternalValue(); |
| 155 } else { | 157 } else { |
| 156 base::Time early_time = morning_midnight - | 158 base::Time early_time = morning_midnight - |
| 157 base::TimeDelta::FromDays(days_ago); | 159 base::TimeDelta::FromDays(days_ago); |
| 158 base::Time late_time = morning_midnight - | 160 base::Time late_time = morning_midnight - |
| 159 base::TimeDelta::FromDays(days_ago-1); | 161 base::TimeDelta::FromDays(days_ago-1); |
| 160 early_bound = early_time.ToInternalValue(); | 162 early_bound = early_time.ToInternalValue(); |
| 161 late_bound = late_time.ToInternalValue(); | 163 late_bound = late_time.ToInternalValue(); |
| 162 } | 164 } |
| 163 // Get the DOMActions. | 165 std::string query_str = base::StringPrintf( |
| 164 std::string dom_str = base::StringPrintf("SELECT * FROM %s " | 166 "SELECT time, action_type, api_name, args, page_url, arg_url, other " |
| 165 "WHERE extension_id=? AND " | 167 "FROM %s WHERE extension_id=? AND time>? AND time<=?", |
| 166 "time>? AND time<=?", | 168 FullStreamUIPolicy::kTableName); |
| 167 DOMAction::kTableName); | 169 sql::Statement query(db_.GetCachedStatement(SQL_FROM_HERE, |
| 168 sql::Statement dom_statement(db_.GetCachedStatement(SQL_FROM_HERE, | 170 query_str.c_str())); |
| 169 dom_str.c_str())); | 171 query.BindString(0, extension_id); |
| 170 dom_statement.BindString(0, extension_id); | 172 query.BindInt64(1, early_bound); |
| 171 dom_statement.BindInt64(1, early_bound); | 173 query.BindInt64(2, late_bound); |
| 172 dom_statement.BindInt64(2, late_bound); | 174 while (query.is_valid() && query.Step()) { |
| 173 while (dom_statement.is_valid() && dom_statement.Step()) { | 175 scoped_ptr<Value> raw_value(base::JSONReader::Read(query.ColumnString(3))); |
| 174 actions->push_back(new DOMAction(dom_statement)); | 176 scoped_ptr<ListValue> args; |
| 175 } | 177 if (raw_value && raw_value->IsType(Value::TYPE_LIST)) { |
| 176 // Get the APIActions. | 178 args.reset(static_cast<ListValue*>(raw_value.release())); |
| 177 std::string api_str = base::StringPrintf("SELECT * FROM %s " | 179 } else { |
| 178 "WHERE extension_id=? AND " | 180 args.reset(new ListValue()); |
| 179 "time>? AND time<=?", | 181 } |
| 180 APIAction::kTableName); | 182 |
| 181 sql::Statement api_statement(db_.GetCachedStatement(SQL_FROM_HERE, | 183 GURL page_url(query.ColumnString(4)); |
| 182 api_str.c_str())); | 184 GURL arg_url(query.ColumnString(5)); |
| 183 api_statement.BindString(0, extension_id); | 185 |
| 184 api_statement.BindInt64(1, early_bound); | 186 raw_value.reset(base::JSONReader::Read(query.ColumnString(6))); |
| 185 api_statement.BindInt64(2, late_bound); | 187 scoped_ptr<DictionaryValue> other; |
| 186 while (api_statement.is_valid() && api_statement.Step()) { | 188 if (raw_value && raw_value->IsType(Value::TYPE_DICTIONARY)) { |
| 187 actions->push_back(new APIAction(api_statement)); | 189 other.reset(static_cast<DictionaryValue*>(raw_value.release())); |
| 188 } | 190 } else { |
| 189 // Get the BlockedActions. | 191 other.reset(new DictionaryValue()); |
| 190 std::string blocked_str = base::StringPrintf("SELECT * FROM %s " | 192 } |
| 191 "WHERE extension_id=? AND " | 193 |
| 192 "time>? AND time<=?", | 194 scoped_refptr<WatchdogAction> action = |
| 193 BlockedAction::kTableName); | 195 new WatchdogAction(extension_id, |
| 194 sql::Statement blocked_statement(db_.GetCachedStatement(SQL_FROM_HERE, | 196 base::Time::FromInternalValue(query.ColumnInt64(0)), |
| 195 blocked_str.c_str())); | 197 static_cast<Action::ActionType>(query.ColumnInt(1)), |
| 196 blocked_statement.BindString(0, extension_id); | 198 query.ColumnString(2), |
| 197 blocked_statement.BindInt64(1, early_bound); | 199 args.Pass(), |
| 198 blocked_statement.BindInt64(2, late_bound); | 200 page_url, |
| 199 while (blocked_statement.is_valid() && blocked_statement.Step()) { | 201 arg_url, |
| 200 actions->push_back(new BlockedAction(blocked_statement)); | 202 other.Pass()); |
| 203 actions->push_back(action); |
| 201 } | 204 } |
| 202 // Sort by time (from newest to oldest). | 205 // Sort by time (from newest to oldest). |
| 203 std::sort(actions->begin(), actions->end(), SortActionsByTime); | 206 std::sort(actions->begin(), actions->end(), SortActionsByTime); |
| 204 return actions.Pass(); | 207 return actions.Pass(); |
| 205 } | 208 } |
| 206 | 209 |
| 207 void ActivityDatabase::Close() { | 210 void ActivityDatabase::Close() { |
| 208 timer_.Stop(); | 211 timer_.Stop(); |
| 209 if (!already_closed_) { | 212 if (!already_closed_) { |
| 210 RecordBatchedActions(); | 213 RecordBatchedActions(); |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 } | 256 } |
| 254 | 257 |
| 255 void ActivityDatabase::SetTimerForTesting(int ms) { | 258 void ActivityDatabase::SetTimerForTesting(int ms) { |
| 256 timer_.Stop(); | 259 timer_.Stop(); |
| 257 timer_.Start(FROM_HERE, | 260 timer_.Start(FROM_HERE, |
| 258 base::TimeDelta::FromMilliseconds(ms), | 261 base::TimeDelta::FromMilliseconds(ms), |
| 259 this, | 262 this, |
| 260 &ActivityDatabase::RecordBatchedActionsWhileTesting); | 263 &ActivityDatabase::RecordBatchedActionsWhileTesting); |
| 261 } | 264 } |
| 262 | 265 |
| 266 // static |
| 267 bool ActivityDatabase::InitializeTable(sql::Connection* db, |
| 268 const char* table_name, |
| 269 const char* content_fields[], |
| 270 const char* field_types[], |
| 271 const int num_content_fields) { |
| 272 if (!db->DoesTableExist(table_name)) { |
| 273 std::string table_creator = |
| 274 base::StringPrintf("CREATE TABLE %s (", table_name); |
| 275 for (int i = 0; i < num_content_fields; i++) { |
| 276 table_creator += base::StringPrintf("%s%s %s", |
| 277 i == 0 ? "" : ", ", |
| 278 content_fields[i], |
| 279 field_types[i]); |
| 280 } |
| 281 table_creator += ")"; |
| 282 if (!db->Execute(table_creator.c_str())) |
| 283 return false; |
| 284 } else { |
| 285 // In case we ever want to add new fields, this initializes them to be |
| 286 // empty strings. |
| 287 for (int i = 0; i < num_content_fields; i++) { |
| 288 if (!db->DoesColumnExist(table_name, content_fields[i])) { |
| 289 std::string table_updater = base::StringPrintf( |
| 290 "ALTER TABLE %s ADD COLUMN %s %s; ", |
| 291 table_name, |
| 292 content_fields[i], |
| 293 field_types[i]); |
| 294 if (!db->Execute(table_updater.c_str())) |
| 295 return false; |
| 296 } |
| 297 } |
| 298 } |
| 299 return true; |
| 300 } |
| 301 |
| 263 } // namespace extensions | 302 } // namespace extensions |
| OLD | NEW |