Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 |
| 45 const char* FullStreamUIPolicy::kTableName = "activitylog_full"; | |
| 46 const char* FullStreamUIPolicy::kTableContentFields[] = { | |
| 47 "extension_id", "time", "action_type", "api_name", "args", "page_url", | |
| 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); | |
| 55 | |
| 39 // TODO(dbabic) This would be a fine error handler for all sql-based policies, | 56 // TODO(dbabic) This would be a fine error handler for all sql-based policies, |
|
felt
2013/07/16 06:44:03
can you delete this todo? At this point it seems t
mvrable
2013/07/16 18:12:35
Done.
| |
| 40 // so it would make sense to introduce another class in the hierarchy, | 57 // so it would make sense to introduce another class in the hierarchy, |
| 41 // SQLiteBasedPolicy as a super class of FullStreamUIPolicy and move this | 58 // SQLiteBasedPolicy as a super class of FullStreamUIPolicy and move this |
| 42 // error handler (as well as other SQLite-related functionality) there. | 59 // error handler (as well as other SQLite-related functionality) there. |
| 43 | 60 |
| 44 FullStreamUIPolicy::FullStreamUIPolicy(Profile* profile) | 61 FullStreamUIPolicy::FullStreamUIPolicy(Profile* profile) |
| 45 : ActivityLogPolicy(profile) { | 62 : ActivityLogPolicy(profile) { |
| 46 db_ = new ActivityDatabase(this); | 63 db_ = new ActivityDatabase(this); |
| 47 FilePath database_name = profile_base_path_.Append( | 64 FilePath database_name = profile_base_path_.Append( |
| 48 chrome::kExtensionActivityLogFilename); | 65 chrome::kExtensionActivityLogFilename); |
| 49 ScheduleAndForget(db_, &ActivityDatabase::Init, database_name); | 66 ScheduleAndForget(db_, &ActivityDatabase::Init, database_name); |
| 50 } | 67 } |
| 51 | 68 |
| 52 bool FullStreamUIPolicy::OnDatabaseInit(sql::Connection* db) { | 69 bool FullStreamUIPolicy::OnDatabaseInit(sql::Connection* db) { |
| 53 if (!DOMAction::InitializeTable(db)) | 70 // Drop old database tables. |
| 54 return false; | 71 for (size_t i = 0; i < arraysize(kObsoleteTables); i++) { |
| 55 if (!APIAction::InitializeTable(db)) | 72 const char* table_name = kObsoleteTables[i]; |
| 56 return false; | 73 if (db->DoesTableExist(table_name)) { |
| 57 if (!BlockedAction::InitializeTable(db)) | 74 std::string drop_statement = |
| 58 return false; | 75 base::StringPrintf("DROP TABLE %s", table_name); |
| 76 if (!db->Execute(drop_statement.c_str())) { | |
| 77 return false; | |
| 78 } | |
| 79 } | |
| 80 } | |
| 59 | 81 |
| 60 return true; | 82 // Create the unified activity log entry table. |
| 83 return ActivityDatabase::InitializeTable(db, | |
| 84 kTableName, | |
| 85 kTableContentFields, | |
| 86 kTableFieldTypes, | |
| 87 arraysize(kTableContentFields)); | |
| 61 } | 88 } |
| 62 | 89 |
| 63 void FullStreamUIPolicy::OnDatabaseClose() { | 90 void FullStreamUIPolicy::OnDatabaseClose() { |
| 64 delete this; | 91 delete this; |
| 65 } | 92 } |
| 66 | 93 |
| 67 void FullStreamUIPolicy::Close() { | 94 void FullStreamUIPolicy::Close() { |
| 68 // The policy object should have never been created if there's no DB thread. | 95 // The policy object should have never been created if there's no DB thread. |
| 69 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); | 96 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); |
| 70 ScheduleAndForget(db_, &ActivityDatabase::Close); | 97 ScheduleAndForget(db_, &ActivityDatabase::Close); |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 94 return std::string(kKeyDomainAction); | 121 return std::string(kKeyDomainAction); |
| 95 case PARAM_KEY_URL_TITLE: | 122 case PARAM_KEY_URL_TITLE: |
| 96 return std::string(kKeyURLTitle); | 123 return std::string(kKeyURLTitle); |
| 97 case PARAM_KEY_DETAILS_STRING: | 124 case PARAM_KEY_DETAILS_STRING: |
| 98 return std::string(kKeyDetailsString); | 125 return std::string(kKeyDetailsString); |
| 99 default: | 126 default: |
| 100 return std::string(); | 127 return std::string(); |
| 101 } | 128 } |
| 102 } | 129 } |
| 103 | 130 |
| 104 std::string FullStreamUIPolicy::ProcessArguments( | 131 scoped_ptr<base::ListValue> FullStreamUIPolicy::ProcessArguments( |
| 105 ActionType action_type, | 132 ActionType action_type, |
| 106 const std::string& name, | 133 const std::string& name, |
| 107 const base::ListValue* args) const { | 134 const base::ListValue* args) const { |
| 135 if (args) { | |
|
felt
2013/07/16 06:44:03
nit: don't need { }
mvrable
2013/07/16 18:12:35
Done.
| |
| 136 return make_scoped_ptr(args->DeepCopy()); | |
| 137 } else { | |
| 138 return scoped_ptr<base::ListValue>(); | |
| 139 } | |
| 140 } | |
| 141 | |
| 142 std::string FullStreamUIPolicy::JoinArguments( | |
| 143 ActionType action_type, | |
| 144 const std::string& name, | |
| 145 const base::ListValue* args) const { | |
| 108 std::string processed_args; | 146 std::string processed_args; |
| 109 if (args) { | 147 if (args) { |
| 110 base::ListValue::const_iterator it = args->begin(); | 148 base::ListValue::const_iterator it = args->begin(); |
| 111 // TODO(felt,dbabic) Think about replacing the loop with a single | 149 // TODO(felt,dbabic) Think about replacing the loop with a single |
| 112 // call to SerializeAndOmitBinaryValues. | 150 // call to SerializeAndOmitBinaryValues. |
| 113 for (; it != args->end(); ++it) { | 151 for (; it != args->end(); ++it) { |
| 114 std::string arg; | 152 std::string arg; |
| 115 JSONStringValueSerializer serializer(&arg); | 153 JSONStringValueSerializer serializer(&arg); |
| 116 if (serializer.SerializeAndOmitBinaryValues(**it)) { | 154 if (serializer.SerializeAndOmitBinaryValues(**it)) { |
| 117 if (it != args->begin()) { | 155 if (it != args->begin()) { |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 129 std::string& details_string) const { | 167 std::string& details_string) const { |
| 130 JSONStringValueSerializer serializer(&details_string); | 168 JSONStringValueSerializer serializer(&details_string); |
| 131 serializer.Serialize(details); | 169 serializer.Serialize(details); |
| 132 } | 170 } |
| 133 | 171 |
| 134 void FullStreamUIPolicy::ProcessAction( | 172 void FullStreamUIPolicy::ProcessAction( |
| 135 ActionType action_type, | 173 ActionType action_type, |
| 136 const std::string& extension_id, | 174 const std::string& extension_id, |
| 137 const std::string& name, | 175 const std::string& name, |
| 138 const GURL& url_param, | 176 const GURL& url_param, |
| 139 const base::ListValue* args, | 177 const base::ListValue* args_in, |
| 140 const DictionaryValue* details) { | 178 const DictionaryValue* details) { |
| 141 std::string concatenated_args = ProcessArguments(action_type, name, args); | 179 scoped_ptr<base::ListValue> args = |
| 180 ProcessArguments(action_type, name, args_in); | |
| 181 std::string concatenated_args = JoinArguments(action_type, name, args.get()); | |
| 142 const Time now = Time::Now(); | 182 const Time now = Time::Now(); |
| 143 scoped_refptr<Action> action; | 183 scoped_refptr<Action> action; |
| 144 std::string extra; | 184 std::string extra; |
| 145 if (details) { | 185 if (details) { |
| 146 details->GetString(GetKey(PARAM_KEY_EXTRA), &extra); | 186 details->GetString(GetKey(PARAM_KEY_EXTRA), &extra); |
| 147 } | 187 } |
| 148 | 188 |
| 149 switch (action_type) { | 189 switch (action_type) { |
| 150 case ACTION_API: { | 190 case ACTION_API: { |
| 151 action = new APIAction( | 191 action = new APIAction( |
| 152 extension_id, | 192 extension_id, |
| 153 now, | 193 now, |
| 154 APIAction::CALL, | 194 APIAction::CALL, |
| 155 name, | 195 name, |
| 156 concatenated_args, | 196 concatenated_args, |
| 197 *args, | |
| 157 extra); | 198 extra); |
| 158 break; | 199 break; |
| 159 } | 200 } |
| 160 case ACTION_EVENT: { | 201 case ACTION_EVENT: { |
| 161 action = new APIAction( | 202 action = new APIAction( |
| 162 extension_id, | 203 extension_id, |
| 163 now, | 204 now, |
| 164 APIAction::EVENT_CALLBACK, | 205 APIAction::EVENT_CALLBACK, |
| 165 name, | 206 name, |
| 166 concatenated_args, | 207 concatenated_args, |
| 208 *args, | |
| 167 extra); | 209 extra); |
| 168 break; | 210 break; |
| 169 } | 211 } |
| 170 case ACTION_BLOCKED: { | 212 case ACTION_BLOCKED: { |
| 171 int reason = 0; | 213 int reason = 0; |
| 172 if (details) { | 214 if (details) { |
| 173 details->GetInteger(GetKey(PARAM_KEY_REASON), &reason); | 215 details->GetInteger(GetKey(PARAM_KEY_REASON), &reason); |
| 174 } | 216 } |
| 175 | 217 |
| 176 action = new BlockedAction( | 218 action = new BlockedAction( |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 223 break; | 265 break; |
| 224 } | 266 } |
| 225 default: | 267 default: |
| 226 NOTREACHED(); | 268 NOTREACHED(); |
| 227 } | 269 } |
| 228 | 270 |
| 229 ScheduleAndForget(db_, &ActivityDatabase::RecordAction, action); | 271 ScheduleAndForget(db_, &ActivityDatabase::RecordAction, action); |
| 230 } | 272 } |
| 231 | 273 |
| 232 } // namespace extensions | 274 } // namespace extensions |
| OLD | NEW |