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

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

Issue 19234003: Extension activity log database refactoring (step 2) (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Address some reviewer comments 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 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "base/files/file_path.h" 5 #include "base/files/file_path.h"
6 #include "base/json/json_string_value_serializer.h" 6 #include "base/json/json_string_value_serializer.h"
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/strings/string16.h" 8 #include "base/strings/string16.h"
9 #include "base/strings/stringprintf.h"
9 #include "chrome/browser/extensions/activity_log/activity_database.h" 10 #include "chrome/browser/extensions/activity_log/activity_database.h"
10 #include "chrome/browser/extensions/activity_log/api_actions.h" 11 #include "chrome/browser/extensions/activity_log/api_actions.h"
11 #include "chrome/browser/extensions/activity_log/blocked_actions.h" 12 #include "chrome/browser/extensions/activity_log/blocked_actions.h"
12 #include "chrome/browser/extensions/activity_log/dom_actions.h" 13 #include "chrome/browser/extensions/activity_log/dom_actions.h"
13 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" 14 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h"
14 #include "chrome/browser/profiles/profile.h" 15 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/common/chrome_constants.h" 16 #include "chrome/common/chrome_constants.h"
16 #include "chrome/common/extensions/dom_action_types.h" 17 #include "chrome/common/extensions/dom_action_types.h"
17 #include "chrome/common/extensions/extension.h" 18 #include "chrome/common/extensions/extension.h"
18 #include "sql/error_delegate_util.h" 19 #include "sql/error_delegate_util.h"
19 #include "url/gurl.h" 20 #include "url/gurl.h"
20 21
21 using base::Callback; 22 using base::Callback;
22 using base::FilePath; 23 using base::FilePath;
23 using base::Time; 24 using base::Time;
24 using base::Unretained; 25 using base::Unretained;
25 using content::BrowserThread; 26 using content::BrowserThread;
26 27
27 namespace { 28 namespace {
28 29
29 // Key strings for passing parameters to the ProcessAction member function. 30 // Key strings for passing parameters to the ProcessAction member function.
30 const char kKeyReason[] = "fsuip.reason"; 31 const char kKeyReason[] = "fsuip.reason";
31 const char kKeyDomainAction[] = "fsuip.domact"; 32 const char kKeyDomainAction[] = "fsuip.domact";
32 const char kKeyURLTitle[] = "fsuip.urltitle"; 33 const char kKeyURLTitle[] = "fsuip.urltitle";
33 const char kKeyDetailsString[] = "fsuip.details"; 34 const char kKeyDetailsString[] = "fsuip.details";
34 35
36 // Obsolete database tables: these should be dropped from the database if
37 // found.
38 const char* kObsoleteTables[] = {"activitylog_apis", "activitylog_blocked",
39 "activitylog_urls"};
40
35 } // namespace 41 } // namespace
36 42
37 namespace extensions { 43 namespace extensions {
38 44
39 // TODO(dbabic) This would be a fine error handler for all sql-based policies, 45 const char* FullStreamUIPolicy::kTableName = "activitylog_full";
40 // so it would make sense to introduce another class in the hierarchy, 46 const char* FullStreamUIPolicy::kTableContentFields[] = {
41 // SQLiteBasedPolicy as a super class of FullStreamUIPolicy and move this 47 "extension_id", "time", "action_type", "api_name", "args", "page_url",
42 // error handler (as well as other SQLite-related functionality) there. 48 "arg_url", "other"
49 };
50 const char* FullStreamUIPolicy::kTableFieldTypes[] = {
51 "LONGVARCHAR NOT NULL", "INTEGER", "INTEGER", "LONGVARCHAR", "LONGVARCHAR",
52 "LONGVARCHAR", "LONGVARCHAR", "LONGVARCHAR"
53 };
54 const int FullStreamUIPolicy::kTableFieldCount = arraysize(kTableContentFields);
43 55
44 FullStreamUIPolicy::FullStreamUIPolicy(Profile* profile) 56 FullStreamUIPolicy::FullStreamUIPolicy(Profile* profile)
45 : ActivityLogPolicy(profile) { 57 : ActivityLogPolicy(profile) {
46 db_ = new ActivityDatabase(this); 58 db_ = new ActivityDatabase(this);
47 FilePath database_name = profile_base_path_.Append( 59 FilePath database_name = profile_base_path_.Append(
48 chrome::kExtensionActivityLogFilename); 60 chrome::kExtensionActivityLogFilename);
49 ScheduleAndForget(db_, &ActivityDatabase::Init, database_name); 61 ScheduleAndForget(db_, &ActivityDatabase::Init, database_name);
50 } 62 }
51 63
52 bool FullStreamUIPolicy::OnDatabaseInit(sql::Connection* db) { 64 bool FullStreamUIPolicy::OnDatabaseInit(sql::Connection* db) {
53 if (!DOMAction::InitializeTable(db)) 65 // Drop old database tables.
54 return false; 66 for (size_t i = 0; i < arraysize(kObsoleteTables); i++) {
55 if (!APIAction::InitializeTable(db)) 67 const char* table_name = kObsoleteTables[i];
56 return false; 68 if (db->DoesTableExist(table_name)) {
57 if (!BlockedAction::InitializeTable(db)) 69 std::string drop_statement =
58 return false; 70 base::StringPrintf("DROP TABLE %s", table_name);
71 if (!db->Execute(drop_statement.c_str())) {
72 return false;
73 }
74 }
75 }
59 76
60 return true; 77 // Create the unified activity log entry table.
78 return ActivityDatabase::InitializeTable(db,
79 kTableName,
80 kTableContentFields,
81 kTableFieldTypes,
82 arraysize(kTableContentFields));
61 } 83 }
62 84
63 void FullStreamUIPolicy::OnDatabaseClose() { 85 void FullStreamUIPolicy::OnDatabaseClose() {
64 delete this; 86 delete this;
65 } 87 }
66 88
67 void FullStreamUIPolicy::Close() { 89 void FullStreamUIPolicy::Close() {
68 // The policy object should have never been created if there's no DB thread. 90 // The policy object should have never been created if there's no DB thread.
69 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); 91 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB));
70 ScheduleAndForget(db_, &ActivityDatabase::Close); 92 ScheduleAndForget(db_, &ActivityDatabase::Close);
(...skipping 23 matching lines...) Expand all
94 return std::string(kKeyDomainAction); 116 return std::string(kKeyDomainAction);
95 case PARAM_KEY_URL_TITLE: 117 case PARAM_KEY_URL_TITLE:
96 return std::string(kKeyURLTitle); 118 return std::string(kKeyURLTitle);
97 case PARAM_KEY_DETAILS_STRING: 119 case PARAM_KEY_DETAILS_STRING:
98 return std::string(kKeyDetailsString); 120 return std::string(kKeyDetailsString);
99 default: 121 default:
100 return std::string(); 122 return std::string();
101 } 123 }
102 } 124 }
103 125
104 std::string FullStreamUIPolicy::ProcessArguments( 126 scoped_ptr<base::ListValue> FullStreamUIPolicy::ProcessArguments(
105 ActionType action_type, 127 ActionType action_type,
106 const std::string& name, 128 const std::string& name,
107 const base::ListValue* args) const { 129 const base::ListValue* args) const {
130 if (args)
131 return make_scoped_ptr(args->DeepCopy());
132 else
133 return scoped_ptr<base::ListValue>();
134 }
135
136 std::string FullStreamUIPolicy::JoinArguments(
137 ActionType action_type,
138 const std::string& name,
139 const base::ListValue* args) const {
108 std::string processed_args; 140 std::string processed_args;
109 if (args) { 141 if (args) {
110 base::ListValue::const_iterator it = args->begin(); 142 base::ListValue::const_iterator it = args->begin();
111 // TODO(felt,dbabic) Think about replacing the loop with a single 143 // TODO(felt,dbabic) Think about replacing the loop with a single
112 // call to SerializeAndOmitBinaryValues. 144 // call to SerializeAndOmitBinaryValues.
113 for (; it != args->end(); ++it) { 145 for (; it != args->end(); ++it) {
114 std::string arg; 146 std::string arg;
115 JSONStringValueSerializer serializer(&arg); 147 JSONStringValueSerializer serializer(&arg);
116 if (serializer.SerializeAndOmitBinaryValues(**it)) { 148 if (serializer.SerializeAndOmitBinaryValues(**it)) {
117 if (it != args->begin()) { 149 if (it != args->begin()) {
(...skipping 11 matching lines...) Expand all
129 std::string& details_string) const { 161 std::string& details_string) const {
130 JSONStringValueSerializer serializer(&details_string); 162 JSONStringValueSerializer serializer(&details_string);
131 serializer.Serialize(details); 163 serializer.Serialize(details);
132 } 164 }
133 165
134 void FullStreamUIPolicy::ProcessAction( 166 void FullStreamUIPolicy::ProcessAction(
135 ActionType action_type, 167 ActionType action_type,
136 const std::string& extension_id, 168 const std::string& extension_id,
137 const std::string& name, 169 const std::string& name,
138 const GURL& url_param, 170 const GURL& url_param,
139 const base::ListValue* args, 171 const base::ListValue* args_in,
140 const DictionaryValue* details) { 172 const DictionaryValue* details) {
141 std::string concatenated_args = ProcessArguments(action_type, name, args); 173 scoped_ptr<base::ListValue> args =
174 ProcessArguments(action_type, name, args_in);
175 std::string concatenated_args = JoinArguments(action_type, name, args.get());
142 const Time now = Time::Now(); 176 const Time now = Time::Now();
143 scoped_refptr<Action> action; 177 scoped_refptr<Action> action;
144 std::string extra; 178 std::string extra;
145 if (details) { 179 if (details) {
146 details->GetString(GetKey(PARAM_KEY_EXTRA), &extra); 180 details->GetString(GetKey(PARAM_KEY_EXTRA), &extra);
147 } 181 }
148 182
149 switch (action_type) { 183 switch (action_type) {
150 case ACTION_API: { 184 case ACTION_API: {
151 action = new APIAction( 185 action = new APIAction(
152 extension_id, 186 extension_id,
153 now, 187 now,
154 APIAction::CALL, 188 APIAction::CALL,
155 name, 189 name,
156 concatenated_args, 190 concatenated_args,
191 *args,
157 extra); 192 extra);
158 break; 193 break;
159 } 194 }
160 case ACTION_EVENT: { 195 case ACTION_EVENT: {
161 action = new APIAction( 196 action = new APIAction(
162 extension_id, 197 extension_id,
163 now, 198 now,
164 APIAction::EVENT_CALLBACK, 199 APIAction::EVENT_CALLBACK,
165 name, 200 name,
166 concatenated_args, 201 concatenated_args,
202 *args,
167 extra); 203 extra);
168 break; 204 break;
169 } 205 }
170 case ACTION_BLOCKED: { 206 case ACTION_BLOCKED: {
171 int reason = 0; 207 int reason = 0;
172 if (details) { 208 if (details) {
173 details->GetInteger(GetKey(PARAM_KEY_REASON), &reason); 209 details->GetInteger(GetKey(PARAM_KEY_REASON), &reason);
174 } 210 }
175 211
176 action = new BlockedAction( 212 action = new BlockedAction(
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 break; 259 break;
224 } 260 }
225 default: 261 default:
226 NOTREACHED(); 262 NOTREACHED();
227 } 263 }
228 264
229 ScheduleAndForget(db_, &ActivityDatabase::RecordAction, action); 265 ScheduleAndForget(db_, &ActivityDatabase::RecordAction, action);
230 } 266 }
231 267
232 } // namespace extensions 268 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698