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

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

Issue 19690003: Extension activity log database refactoring (step 3) (Closed) Base URL: http://git.chromium.org/chromium/src.git@refactor2
Patch Set: Do not set bad BlockedChromeActivityDetail::Reason values 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/json/json_reader.h"
8 #include "base/logging.h" 8 #include "base/logging.h"
9 #include "base/strings/string_util.h" 9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h" 10 #include "base/strings/stringprintf.h"
11 #include "base/threading/thread.h" 11 #include "base/threading/thread.h"
12 #include "base/threading/thread_checker.h" 12 #include "base/threading/thread_checker.h"
13 #include "base/time/clock.h" 13 #include "base/time/clock.h"
14 #include "base/time/time.h" 14 #include "base/time/time.h"
15 #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" 16 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h"
17 #include "chrome/common/chrome_switches.h" 17 #include "chrome/common/chrome_switches.h"
18 #include "sql/error_delegate_util.h" 18 #include "sql/error_delegate_util.h"
19 #include "sql/transaction.h" 19 #include "sql/transaction.h"
20 #include "third_party/sqlite/sqlite3.h" 20 #include "third_party/sqlite/sqlite3.h"
21 21
22 #if defined(OS_MACOSX) 22 #if defined(OS_MACOSX)
23 #include "base/mac/mac_util.h" 23 #include "base/mac/mac_util.h"
24 #endif 24 #endif
25 25
26 using content::BrowserThread; 26 using content::BrowserThread;
27 27
28 namespace {
29
30 bool SortActionsByTime(const scoped_refptr<extensions::Action> a,
31 const scoped_refptr<extensions::Action> b) {
32 return a->time() > b->time();
33 }
34
35 } // namespace
36
37 namespace extensions { 28 namespace extensions {
38 29
39 ActivityDatabase::ActivityDatabase(ActivityDatabase::Delegate* delegate) 30 ActivityDatabase::ActivityDatabase(ActivityDatabase::Delegate* delegate)
40 : delegate_(delegate), 31 : delegate_(delegate),
41 testing_clock_(NULL), 32 testing_clock_(NULL),
42 valid_db_(false), 33 valid_db_(false),
43 already_closed_(false), 34 already_closed_(false),
44 did_init_(false) { 35 did_init_(false) {
45 // We don't batch commits when in testing mode. 36 // We don't batch commits when in testing mode.
46 batch_mode_ = !(CommandLine::ForCurrentProcess()-> 37 batch_mode_ = !(CommandLine::ForCurrentProcess()->
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after
156 late_bound = base::Time::Max().ToInternalValue(); 147 late_bound = base::Time::Max().ToInternalValue();
157 } else { 148 } else {
158 base::Time early_time = morning_midnight - 149 base::Time early_time = morning_midnight -
159 base::TimeDelta::FromDays(days_ago); 150 base::TimeDelta::FromDays(days_ago);
160 base::Time late_time = morning_midnight - 151 base::Time late_time = morning_midnight -
161 base::TimeDelta::FromDays(days_ago-1); 152 base::TimeDelta::FromDays(days_ago-1);
162 early_bound = early_time.ToInternalValue(); 153 early_bound = early_time.ToInternalValue();
163 late_bound = late_time.ToInternalValue(); 154 late_bound = late_time.ToInternalValue();
164 } 155 }
165 std::string query_str = base::StringPrintf( 156 std::string query_str = base::StringPrintf(
166 "SELECT time, action_type, api_name, args, page_url, arg_url, other " 157 "SELECT time, action_type, api_name, args, page_url, page_title, "
167 "FROM %s WHERE extension_id=? AND time>? AND time<=?", 158 "arg_url, other "
159 "FROM %s WHERE extension_id=? AND time>? AND time<=? "
160 "ORDER BY time DESC",
168 FullStreamUIPolicy::kTableName); 161 FullStreamUIPolicy::kTableName);
169 sql::Statement query(db_.GetCachedStatement(SQL_FROM_HERE, 162 sql::Statement query(db_.GetCachedStatement(SQL_FROM_HERE,
170 query_str.c_str())); 163 query_str.c_str()));
171 query.BindString(0, extension_id); 164 query.BindString(0, extension_id);
172 query.BindInt64(1, early_bound); 165 query.BindInt64(1, early_bound);
173 query.BindInt64(2, late_bound); 166 query.BindInt64(2, late_bound);
174 while (query.is_valid() && query.Step()) { 167 while (query.is_valid() && query.Step()) {
175 scoped_ptr<Value> raw_value(base::JSONReader::Read(query.ColumnString(3))); 168 scoped_refptr<Action> action =
176 scoped_ptr<ListValue> args; 169 new Action(extension_id,
177 if (raw_value && raw_value->IsType(Value::TYPE_LIST)) { 170 base::Time::FromInternalValue(query.ColumnInt64(0)),
178 args.reset(static_cast<ListValue*>(raw_value.release())); 171 static_cast<Action::ActionType>(query.ColumnInt(1)),
179 } else { 172 query.ColumnString(2));
180 args.reset(new ListValue()); 173
174 if (query.ColumnType(3) != sql::COLUMN_TYPE_NULL) {
175 scoped_ptr<Value> parsed_value(
176 base::JSONReader::Read(query.ColumnString(3)));
177 if (parsed_value && parsed_value->IsType(Value::TYPE_LIST)) {
178 action->set_args(
179 make_scoped_ptr(static_cast<ListValue*>(parsed_value.release())));
180 } else {
181 LOG(WARNING) << "Unable to parse args: '" << query.ColumnString(3)
182 << "'";
183 }
181 } 184 }
182 185
183 GURL page_url(query.ColumnString(4)); 186 GURL page_url(query.ColumnString(4));
184 GURL arg_url(query.ColumnString(5)); 187 action->set_page_url(page_url);
185 188
186 raw_value.reset(base::JSONReader::Read(query.ColumnString(6))); 189 action->set_page_title(query.ColumnString(5));
187 scoped_ptr<DictionaryValue> other; 190
188 if (raw_value && raw_value->IsType(Value::TYPE_DICTIONARY)) { 191 GURL arg_url(query.ColumnString(6));
189 other.reset(static_cast<DictionaryValue*>(raw_value.release())); 192 action->set_arg_url(arg_url);
190 } else { 193
191 other.reset(new DictionaryValue()); 194 if (query.ColumnType(7) != sql::COLUMN_TYPE_NULL) {
195 scoped_ptr<Value> parsed_value(
196 base::JSONReader::Read(query.ColumnString(7)));
197 if (parsed_value && parsed_value->IsType(Value::TYPE_DICTIONARY)) {
198 action->set_other(make_scoped_ptr(
199 static_cast<DictionaryValue*>(parsed_value.release())));
200 } else {
201 LOG(WARNING) << "Unable to parse other: '" << query.ColumnString(7)
202 << "'";
203 }
192 } 204 }
193 205
194 scoped_refptr<WatchdogAction> action =
195 new WatchdogAction(extension_id,
196 base::Time::FromInternalValue(query.ColumnInt64(0)),
197 static_cast<Action::ActionType>(query.ColumnInt(1)),
198 query.ColumnString(2),
199 args.Pass(),
200 page_url,
201 arg_url,
202 other.Pass());
203 actions->push_back(action); 206 actions->push_back(action);
204 } 207 }
205 // Sort by time (from newest to oldest).
206 std::sort(actions->begin(), actions->end(), SortActionsByTime);
207 return actions.Pass(); 208 return actions.Pass();
208 } 209 }
209 210
210 void ActivityDatabase::Close() { 211 void ActivityDatabase::Close() {
211 timer_.Stop(); 212 timer_.Stop();
212 if (!already_closed_) { 213 if (!already_closed_) {
213 RecordBatchedActions(); 214 RecordBatchedActions();
214 db_.reset_error_callback(); 215 db_.reset_error_callback();
215 } 216 }
216 valid_db_ = false; 217 valid_db_ = false;
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after
293 field_types[i]); 294 field_types[i]);
294 if (!db->Execute(table_updater.c_str())) 295 if (!db->Execute(table_updater.c_str()))
295 return false; 296 return false;
296 } 297 }
297 } 298 }
298 } 299 }
299 return true; 300 return true;
300 } 301 }
301 302
302 } // namespace extensions 303 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698