OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include <string> | |
6 #include "base/logging.h" | |
7 #include "chrome/browser/extensions/api_actions.h" | |
8 #include "content/public/browser/browser_thread.h" | |
9 | |
10 using content::BrowserThread; | |
11 | |
12 namespace extensions { | |
13 | |
14 const char* APIAction::kTableName = "activitylog_apis"; | |
15 const char* APIAction::kTableStructure = "(" | |
16 "extension_id LONGVARCHAR NOT NULL, " | |
17 "time INTEGER NOT NULL, " | |
18 "api_action_type LONGVARCHAR NOT NULL, " | |
19 "target_type LONGVARCHAR NOT NULL, " | |
20 "api_call LONGVARCHAR NOT NULL, " | |
21 "extra LONGVARCHAR NOT NULL)"; | |
22 | |
23 APIAction::APIAction(const std::string& extension_id, | |
24 const base::Time& time, | |
25 const APIActionType verb, | |
26 const APITargetType target, | |
27 const std::string& api_call, | |
28 const std::string& extra) | |
29 : extension_id_(extension_id), | |
30 time_(time), | |
31 verb_(verb), | |
32 target_(target), | |
33 api_call_(api_call), | |
34 extra_(extra) { } | |
35 | |
36 APIAction::~APIAction() { | |
37 } | |
38 | |
39 void APIAction::Record(sql::Connection* db) { | |
40 std::string sql_str = "INSERT INTO " + std::string(kTableName) | |
41 + " (extension_id, time, api_action_type, target_type, api_call, extra)" | |
42 " VALUES (?,?,?,?,?,?)"; | |
43 sql::Statement statement(db->GetCachedStatement( | |
44 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); | |
45 statement.BindString(0, extension_id_); | |
46 statement.BindInt64(1, time_.ToInternalValue()); | |
47 statement.BindString(2, VerbAsString()); | |
48 statement.BindString(3, TargetAsString()); | |
49 statement.BindString(4, api_call_); | |
50 statement.BindString(5, extra_); | |
51 | |
52 if (!statement.Run()) | |
53 LOG(ERROR) << "Activity log database I/O failed: " << sql_str; | |
54 } | |
55 | |
56 std::string APIAction::PrettyPrintFori18n() { | |
57 // TODO(felt): implement this for real when the UI is redesigned. | |
58 return PrettyPrintForDebug(); | |
59 } | |
60 | |
61 std::string APIAction::PrettyPrintForDebug() { | |
62 // TODO(felt): implement this for real when the UI is redesigned. | |
63 return "ID: " + extension_id_ + ", VERB: " + VerbAsString() + | |
64 ", TARGET: " + TargetAsString() + ", API: " + api_call_; | |
65 } | |
66 | |
67 std::string APIAction::VerbAsString() const { | |
68 switch (verb_) { | |
69 case READ: | |
70 return "READ"; | |
71 case MODIFIED: | |
72 return "MODIFIED"; | |
73 case DELETED: | |
74 return "DELETED"; | |
75 case ADDED: | |
76 return "ADDED"; | |
77 case ENABLED: | |
78 return "ENABLED"; | |
79 case DISABLED: | |
80 return "DISABLED"; | |
81 case CREATED: | |
82 return "CREATED"; | |
83 default: | |
84 return "UNKNOWN_ACTION"; | |
85 } | |
86 } | |
87 | |
88 std::string APIAction::TargetAsString() const { | |
89 switch (target_) { | |
90 case BOOKMARK: | |
91 return "BOOKMARK"; | |
92 case TABS: | |
93 return "TABS"; | |
94 case HISTORY: | |
95 return "HISTORY"; | |
96 case COOKIES: | |
97 return "COOKIES"; | |
98 case BROWSER_ACTION: | |
99 return "BROWSER_ACTION"; | |
100 case NOTIFICATION: | |
101 return "NOTIFICATION"; | |
102 case OMNIBOX: | |
103 return "OMNIBOX"; | |
104 default: | |
105 return "UNKNOWN_TARGET"; | |
106 } | |
107 } | |
108 | |
109 APIAction::APIActionType APIAction::StringAsActionType( | |
110 const std::string& str) { | |
111 if (str == "READ") { | |
112 return READ; | |
113 } else if (str == "MODIFIED") { | |
114 return MODIFIED; | |
115 } else if (str == "DELETED") { | |
116 return DELETED; | |
117 } else if (str == "ADDED") { | |
118 return ADDED; | |
119 } else if (str == "ENABLED") { | |
120 return ENABLED; | |
121 } else if (str == "DISABLED") { | |
122 return DISABLED; | |
123 } else if (str == "CREATED") { | |
124 return CREATED; | |
125 } else { | |
126 return UNKNOWN_ACTION; | |
127 } | |
128 } | |
129 | |
130 // The all-caps strings match the enum names. The lowercase strings match the | |
131 // actual object names (e.g., cookies.remove(...);). | |
132 APIAction::APITargetType APIAction::StringAsTargetType( | |
133 const std::string& str) { | |
134 if (str == "BOOKMARK" || str == "bookmark") { | |
135 return BOOKMARK; | |
136 } else if (str == "TABS" || str == "tabs") { | |
137 return TABS; | |
138 } else if (str == "HISTORY" || str == "history") { | |
139 return HISTORY; | |
140 } else if (str == "COOKIES" || str == "cookies") { | |
141 return COOKIES; | |
142 } else if (str == "BROWSER_ACTION" || str == "browser_action") { | |
143 return BROWSER_ACTION; | |
144 } else if (str == "NOTIFICATION" || str == "notification") { | |
145 return NOTIFICATION; | |
146 } else if (str == "OMNIBOX" || str == "omnibox") { | |
147 return OMNIBOX; | |
148 } else { | |
149 return UNKNOWN_TARGET; | |
150 } | |
151 } | |
152 | |
153 } // namespace extensions | |
154 | |
OLD | NEW |