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_category 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, |
27 const APICategory category, | |
25 const APIActionType verb, | 28 const APIActionType verb, |
26 const APITargetType target, | 29 const APITargetType 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 category_(category), | |
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_category")) { | |
51 // Old versions of the table lack the api_category column. Add it if | |
Eric Dingle
2013/01/24 16:16:34
This seems lousy to me. What if you want to add an
mvrable
2013/01/24 18:12:31
This is similar to some code in chrome/browser/his
Eric Dingle
2013/01/24 18:56:56
If it's already done elsewhere, then I'm okay with
| |
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_category LONGVARCHAR; " | |
58 "UPDATE %s SET api_category = '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_category, 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, CategoryAsString()); |
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: " + CategoryAsString() + |
64 ", TARGET: " + TargetAsString() + ", API: " + api_call_; | 92 ", VERB: " + VerbAsString() + ", TARGET: " + TargetAsString() + |
93 ", API: " + api_call_; | |
94 } | |
95 | |
96 std::string APIAction::CategoryAsString() const { | |
97 switch (category_) { | |
98 case CALL: | |
99 return "CALL"; | |
100 case EVENT_CALLBACK: | |
101 return "EVENT_CALLBACK"; | |
102 default: | |
103 return "UNKNOWN_CATEGORY"; | |
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"; |
(...skipping 24 matching lines...) Expand all Loading... | |
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 |
149 APIAction::APICategory APIAction::StringAsCategory( | |
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_CATEGORY; | |
157 } | |
158 } | |
159 | |
109 APIAction::APIActionType APIAction::StringAsActionType( | 160 APIAction::APIActionType APIAction::StringAsActionType( |
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; |
(...skipping 26 matching lines...) Expand all Loading... | |
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 | 205 |
OLD | NEW |