| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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 <string> | 5 #include <string> |
| 6 #include "base/logging.h" | 6 #include "base/logging.h" |
| 7 #include "base/stringprintf.h" | 7 #include "base/stringprintf.h" |
| 8 #include "chrome/browser/extensions/api_actions.h" | 8 #include "chrome/browser/extensions/api_actions.h" |
| 9 #include "content/public/browser/browser_thread.h" | |
| 10 | |
| 11 using content::BrowserThread; | |
| 12 | 9 |
| 13 namespace extensions { | 10 namespace extensions { |
| 14 | 11 |
| 15 const char* APIAction::kTableName = "activitylog_apis"; | |
| 16 const char* APIAction::kTableStructure = "(" | |
| 17 "extension_id LONGVARCHAR NOT NULL, " | |
| 18 "time INTEGER NOT NULL, " | |
| 19 "api_type LONGVARCHAR NOT NULL, " | |
| 20 "api_action_type LONGVARCHAR NOT NULL, " | |
| 21 "target_type LONGVARCHAR NOT NULL, " | |
| 22 "api_call LONGVARCHAR NOT NULL, " | |
| 23 "extra LONGVARCHAR NOT NULL)"; | |
| 24 | |
| 25 APIAction::APIAction(const std::string& extension_id, | |
| 26 const base::Time& time, | |
| 27 const Type type, | |
| 28 const Verb verb, | |
| 29 const Target target, | |
| 30 const std::string& api_call, | |
| 31 const std::string& extra) | |
| 32 : extension_id_(extension_id), | |
| 33 time_(time), | |
| 34 type_(type), | |
| 35 verb_(verb), | |
| 36 target_(target), | |
| 37 api_call_(api_call), | |
| 38 extra_(extra) { } | |
| 39 | |
| 40 APIAction::~APIAction() { | |
| 41 } | |
| 42 | |
| 43 // static | 12 // static |
| 44 bool APIAction::InitializeTable(sql::Connection* db) { | 13 bool Action::InitializeTableInternal(sql::Connection* db, |
| 45 if (!db->DoesTableExist(kTableName)) { | 14 const char* table_name, |
| 15 const char* basic_fields, |
| 16 const char* content_fields[], |
| 17 const int num_content_fields) { |
| 18 if (!db->DoesTableExist(table_name)) { |
| 46 std::string table_creator = base::StringPrintf( | 19 std::string table_creator = base::StringPrintf( |
| 47 "CREATE TABLE %s %s", kTableName, kTableStructure); | 20 "CREATE TABLE %s (%s", table_name, basic_fields); |
| 21 for (int i = 0; i < num_content_fields; i++) { |
| 22 table_creator += base::StringPrintf(", %s LONGVARCHAR", |
| 23 content_fields[i]); |
| 24 } |
| 25 table_creator += ")"; |
| 48 if (!db->Execute(table_creator.c_str())) | 26 if (!db->Execute(table_creator.c_str())) |
| 49 return false; | 27 return false; |
| 50 } else if (!db->DoesColumnExist(kTableName, "api_type")) { | 28 } else { |
| 51 // Old versions of the table lack the api_type column. Add it if | 29 // In case we ever want to add new fields, this initializes them to be |
| 52 // needed, with values defaulting to "CALL". | 30 // empty strings. |
| 53 // | 31 for (int i = 0; i < num_content_fields; i++) { |
| 54 // TODO(mvrable): Remove this update code once we're fairly certain that | 32 if (!db->DoesColumnExist(table_name, content_fields[i])) { |
| 55 // everyone will have converted to the new schema. | 33 std::string table_updater = base::StringPrintf( |
| 56 std::string table_updater = base::StringPrintf( | 34 "ALTER TABLE %s ADD COLUMN %s LONGVARCHAR; ", |
| 57 "ALTER TABLE %s ADD COLUMN api_type LONGVARCHAR; " | 35 table_name, |
| 58 "UPDATE %s SET api_type = 'CALL'", | 36 content_fields[i]); |
| 59 kTableName, kTableName); | 37 if (!db->Execute(table_updater.c_str())) |
| 60 if (!db->Execute(table_updater.c_str())) | 38 return false; |
| 61 return false; | 39 } |
| 40 } |
| 62 } | 41 } |
| 63 return true; | 42 return true; |
| 64 } | 43 } |
| 65 | 44 |
| 66 void APIAction::Record(sql::Connection* db) { | |
| 67 std::string sql_str = "INSERT INTO " + std::string(kTableName) | |
| 68 + " (extension_id, time, api_type, api_action_type, target_type," | |
| 69 " api_call, extra) VALUES (?,?,?,?,?,?,?)"; | |
| 70 sql::Statement statement(db->GetCachedStatement( | |
| 71 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | |
| 72 statement.BindString(0, extension_id_); | |
| 73 statement.BindInt64(1, time_.ToInternalValue()); | |
| 74 statement.BindString(2, TypeAsString()); | |
| 75 statement.BindString(3, VerbAsString()); | |
| 76 statement.BindString(4, TargetAsString()); | |
| 77 statement.BindString(5, api_call_); | |
| 78 statement.BindString(6, extra_); | |
| 79 | |
| 80 if (!statement.Run()) | |
| 81 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | |
| 82 } | |
| 83 | |
| 84 std::string APIAction::PrettyPrintFori18n() { | |
| 85 // TODO(felt): implement this for real when the UI is redesigned. | |
| 86 return PrettyPrintForDebug(); | |
| 87 } | |
| 88 | |
| 89 std::string APIAction::PrettyPrintForDebug() { | |
| 90 // TODO(felt): implement this for real when the UI is redesigned. | |
| 91 return "ID: " + extension_id_ + + ", CATEGORY: " + TypeAsString() + | |
| 92 ", VERB: " + VerbAsString() + ", TARGET: " + TargetAsString() + | |
| 93 ", API: " + api_call_; | |
| 94 } | |
| 95 | |
| 96 std::string APIAction::TypeAsString() const { | |
| 97 switch (type_) { | |
| 98 case CALL: | |
| 99 return "CALL"; | |
| 100 case EVENT_CALLBACK: | |
| 101 return "EVENT_CALLBACK"; | |
| 102 default: | |
| 103 return "UNKNOWN_TYPE"; | |
| 104 } | |
| 105 } | |
| 106 | |
| 107 std::string APIAction::VerbAsString() const { | |
| 108 switch (verb_) { | |
| 109 case READ: | |
| 110 return "READ"; | |
| 111 case MODIFIED: | |
| 112 return "MODIFIED"; | |
| 113 case DELETED: | |
| 114 return "DELETED"; | |
| 115 case ADDED: | |
| 116 return "ADDED"; | |
| 117 case ENABLED: | |
| 118 return "ENABLED"; | |
| 119 case DISABLED: | |
| 120 return "DISABLED"; | |
| 121 case CREATED: | |
| 122 return "CREATED"; | |
| 123 default: | |
| 124 return "UNKNOWN_VERB"; | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 std::string APIAction::TargetAsString() const { | |
| 129 switch (target_) { | |
| 130 case BOOKMARK: | |
| 131 return "BOOKMARK"; | |
| 132 case TABS: | |
| 133 return "TABS"; | |
| 134 case HISTORY: | |
| 135 return "HISTORY"; | |
| 136 case COOKIES: | |
| 137 return "COOKIES"; | |
| 138 case BROWSER_ACTION: | |
| 139 return "BROWSER_ACTION"; | |
| 140 case NOTIFICATION: | |
| 141 return "NOTIFICATION"; | |
| 142 case OMNIBOX: | |
| 143 return "OMNIBOX"; | |
| 144 default: | |
| 145 return "UNKNOWN_TARGET"; | |
| 146 } | |
| 147 } | |
| 148 | |
| 149 APIAction::Type APIAction::StringAsType( | |
| 150 const std::string& str) { | |
| 151 if (str == "CALL") { | |
| 152 return CALL; | |
| 153 } else if (str == "EVENT_CALLBACK") { | |
| 154 return EVENT_CALLBACK; | |
| 155 } else { | |
| 156 return UNKNOWN_TYPE; | |
| 157 } | |
| 158 } | |
| 159 | |
| 160 APIAction::Verb APIAction::StringAsVerb( | |
| 161 const std::string& str) { | |
| 162 if (str == "READ") { | |
| 163 return READ; | |
| 164 } else if (str == "MODIFIED") { | |
| 165 return MODIFIED; | |
| 166 } else if (str == "DELETED") { | |
| 167 return DELETED; | |
| 168 } else if (str == "ADDED") { | |
| 169 return ADDED; | |
| 170 } else if (str == "ENABLED") { | |
| 171 return ENABLED; | |
| 172 } else if (str == "DISABLED") { | |
| 173 return DISABLED; | |
| 174 } else if (str == "CREATED") { | |
| 175 return CREATED; | |
| 176 } else { | |
| 177 return UNKNOWN_VERB; | |
| 178 } | |
| 179 } | |
| 180 | |
| 181 // The all-caps strings match the enum names. The lowercase strings match the | |
| 182 // actual object names (e.g., cookies.remove(...);). | |
| 183 APIAction::Target APIAction::StringAsTarget( | |
| 184 const std::string& str) { | |
| 185 if (str == "BOOKMARK" || str == "bookmark") { | |
| 186 return BOOKMARK; | |
| 187 } else if (str == "TABS" || str == "tabs") { | |
| 188 return TABS; | |
| 189 } else if (str == "HISTORY" || str == "history") { | |
| 190 return HISTORY; | |
| 191 } else if (str == "COOKIES" || str == "cookies") { | |
| 192 return COOKIES; | |
| 193 } else if (str == "BROWSER_ACTION" || str == "browser_action") { | |
| 194 return BROWSER_ACTION; | |
| 195 } else if (str == "NOTIFICATION" || str == "notification") { | |
| 196 return NOTIFICATION; | |
| 197 } else if (str == "OMNIBOX" || str == "omnibox") { | |
| 198 return OMNIBOX; | |
| 199 } else { | |
| 200 return UNKNOWN_TARGET; | |
| 201 } | |
| 202 } | |
| 203 | |
| 204 } // namespace extensions | 45 } // namespace extensions |
| OLD | NEW |