Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(759)

Side by Side Diff: chrome/browser/extensions/activity_log/activity_database.cc

Issue 19234003: Extension activity log database refactoring (step 2) (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after
151 early_bound = morning_midnight.ToInternalValue(); 153 early_bound = morning_midnight.ToInternalValue();
152 late_bound = base::Time::Max().ToInternalValue(); 154 late_bound = base::Time::Max().ToInternalValue();
153 } else { 155 } else {
154 base::Time early_time = morning_midnight - 156 base::Time early_time = morning_midnight -
155 base::TimeDelta::FromDays(days_ago); 157 base::TimeDelta::FromDays(days_ago);
156 base::Time late_time = morning_midnight - 158 base::Time late_time = morning_midnight -
157 base::TimeDelta::FromDays(days_ago-1); 159 base::TimeDelta::FromDays(days_ago-1);
158 early_bound = early_time.ToInternalValue(); 160 early_bound = early_time.ToInternalValue();
159 late_bound = late_time.ToInternalValue(); 161 late_bound = late_time.ToInternalValue();
160 } 162 }
161 // Get the DOMActions. 163 std::string query_str = base::StringPrintf(
162 std::string dom_str = base::StringPrintf("SELECT * FROM %s " 164 "SELECT time, action_type, api_name, args, page_url, arg_url, other "
163 "WHERE extension_id=? AND " 165 "FROM %s WHERE extension_id=? AND time>? AND time<=?",
164 "time>? AND time<=?", 166 FullStreamUIPolicy::kTableName);
165 DOMAction::kTableName); 167 sql::Statement query(db_.GetCachedStatement(SQL_FROM_HERE,
166 sql::Statement dom_statement(db_.GetCachedStatement(SQL_FROM_HERE, 168 query_str.c_str()));
167 dom_str.c_str())); 169 query.BindString(0, extension_id);
168 dom_statement.BindString(0, extension_id); 170 query.BindInt64(1, early_bound);
169 dom_statement.BindInt64(1, early_bound); 171 query.BindInt64(2, late_bound);
170 dom_statement.BindInt64(2, late_bound); 172 while (query.is_valid() && query.Step()) {
171 while (dom_statement.is_valid() && dom_statement.Step()) { 173 scoped_ptr<Value> raw_value(base::JSONReader::Read(query.ColumnString(3)));
172 scoped_refptr<DOMAction> action = new DOMAction(dom_statement); 174 scoped_ptr<ListValue> args;
173 actions->push_back(action); 175 if (raw_value && raw_value->IsType(Value::TYPE_LIST)) {
174 } 176 ListValue* list = NULL;
175 // Get the APIActions. 177 if (raw_value.release()->GetAsList(&list)) {
176 std::string api_str = base::StringPrintf("SELECT * FROM %s " 178 args.reset(list);
177 "WHERE extension_id=? AND " 179 }
178 "time>? AND time<=?", 180 } else {
179 APIAction::kTableName); 181 args.reset(new ListValue());
180 sql::Statement api_statement(db_.GetCachedStatement(SQL_FROM_HERE, 182 }
181 api_str.c_str())); 183
182 api_statement.BindString(0, extension_id); 184 GURL page_url(query.ColumnString(4));
183 api_statement.BindInt64(1, early_bound); 185 GURL arg_url(query.ColumnString(5));
184 api_statement.BindInt64(2, late_bound); 186
185 while (api_statement.is_valid() && api_statement.Step()) { 187 raw_value.reset(base::JSONReader::Read(query.ColumnString(6)));
186 scoped_refptr<APIAction> action = new APIAction(api_statement); 188 scoped_ptr<DictionaryValue> other;
187 actions->push_back(action); 189 if (raw_value && raw_value->IsType(Value::TYPE_LIST)) {
188 } 190 DictionaryValue* dict = NULL;
189 // Get the BlockedActions. 191 if (raw_value.release()->GetAsDictionary(&dict)) {
190 std::string blocked_str = base::StringPrintf("SELECT * FROM %s " 192 other.reset(dict);
191 "WHERE extension_id=? AND " 193 }
192 "time>? AND time<=?", 194 } else {
193 BlockedAction::kTableName); 195 other.reset(new DictionaryValue());
194 sql::Statement blocked_statement(db_.GetCachedStatement(SQL_FROM_HERE, 196 }
195 blocked_str.c_str())); 197
196 blocked_statement.BindString(0, extension_id); 198 scoped_refptr<WatchdogAction> action =
197 blocked_statement.BindInt64(1, early_bound); 199 new WatchdogAction(extension_id,
198 blocked_statement.BindInt64(2, late_bound); 200 base::Time::FromInternalValue(query.ColumnInt64(0)),
199 while (blocked_statement.is_valid() && blocked_statement.Step()) { 201 static_cast<Action::ActionType>(query.ColumnInt(1)),
200 scoped_refptr<BlockedAction> action = new BlockedAction(blocked_statement); 202 query.ColumnString(2),
203 args.Pass(),
204 page_url,
205 arg_url,
206 other.Pass());
201 actions->push_back(action); 207 actions->push_back(action);
202 } 208 }
203 // Sort by time (from newest to oldest). 209 // Sort by time (from newest to oldest).
204 std::sort(actions->begin(), actions->end(), SortActionsByTime); 210 std::sort(actions->begin(), actions->end(), SortActionsByTime);
205 return actions.Pass(); 211 return actions.Pass();
206 } 212 }
207 213
208 void ActivityDatabase::Close() { 214 void ActivityDatabase::Close() {
209 timer_.Stop(); 215 timer_.Stop();
210 if (!already_closed_) { 216 if (!already_closed_) {
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
254 } 260 }
255 261
256 void ActivityDatabase::SetTimerForTesting(int ms) { 262 void ActivityDatabase::SetTimerForTesting(int ms) {
257 timer_.Stop(); 263 timer_.Stop();
258 timer_.Start(FROM_HERE, 264 timer_.Start(FROM_HERE,
259 base::TimeDelta::FromMilliseconds(ms), 265 base::TimeDelta::FromMilliseconds(ms),
260 this, 266 this,
261 &ActivityDatabase::RecordBatchedActionsWhileTesting); 267 &ActivityDatabase::RecordBatchedActionsWhileTesting);
262 } 268 }
263 269
270 // static
271 bool ActivityDatabase::InitializeTable(sql::Connection* db,
272 const char* table_name,
273 const char* content_fields[],
274 const char* field_types[],
275 const int num_content_fields) {
276 if (!db->DoesTableExist(table_name)) {
277 std::string table_creator =
278 base::StringPrintf("CREATE TABLE %s (", table_name);
279 for (int i = 0; i < num_content_fields; i++) {
280 table_creator += base::StringPrintf("%s%s %s",
281 i == 0 ? "" : ", ",
282 content_fields[i],
283 field_types[i]);
284 }
285 table_creator += ")";
286 if (!db->Execute(table_creator.c_str()))
287 return false;
288 } else {
289 // In case we ever want to add new fields, this initializes them to be
290 // empty strings.
291 for (int i = 0; i < num_content_fields; i++) {
292 if (!db->DoesColumnExist(table_name, content_fields[i])) {
293 std::string table_updater = base::StringPrintf(
294 "ALTER TABLE %s ADD COLUMN %s %s; ",
295 table_name,
296 content_fields[i],
297 field_types[i]);
298 if (!db->Execute(table_updater.c_str()))
299 return false;
300 }
301 }
302 }
303 return true;
304 }
305
264 } // namespace extensions 306 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698