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