OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/files/file_path.h" | |
6 #include "base/json/json_string_value_serializer.h" | |
7 #include "base/logging.h" | |
8 #include "base/string16.h" | |
9 #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/blocked_actions.h" | |
12 #include "chrome/browser/extensions/activity_log/dom_actions.h" | |
13 #include "chrome/browser/extensions/activity_log/fullstream_ui_policy.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/common/chrome_constants.h" | |
16 #include "chrome/common/extensions/dom_action_types.h" | |
17 #include "chrome/common/extensions/extension.h" | |
18 #include "googleurl/src/gurl.h" | |
19 #include "sql/error_delegate_util.h" | |
20 | |
21 using base::Callback; | |
22 using base::FilePath; | |
23 using base::Time; | |
24 using base::Unretained; | |
25 using content::BrowserThread; | |
26 | |
27 namespace { | |
28 | |
29 // Key strings for passing parameters to the ProcessAction member function. | |
30 const char kKeyReason[] = "fsuip.reason"; | |
31 const char kKeyDomainAction[] = "fsuip.domact"; | |
32 const char kKeyURLTitle[] = "fsuip.urltitle"; | |
33 const char kKeyDetailsString[] = "fsuip.details"; | |
34 | |
35 } // namespace | |
36 | |
37 namespace extensions { | |
38 | |
39 // TODO(dbabic) This would be a fine error handler for all sql-based policies, | |
40 // so it would make sense to introduce another class in the hierarchy, | |
41 // SQLiteBasedPolicy as a super class of FullStreamUIPolicy and move this | |
42 // error handler (as well as other SQLite-related functionality) there. | |
43 | |
44 FullStreamUIPolicy::FullStreamUIPolicy(Profile* profile) | |
45 : ActivityLogPolicy(profile) { | |
46 db_ = new ActivityDatabase(); | |
47 FilePath database_name = profile_base_path_.Append( | |
48 chrome::kExtensionActivityLogFilename); | |
49 ScheduleAndForget(db_, &ActivityDatabase::Init, database_name); | |
50 } | |
51 | |
52 FullStreamUIPolicy::~FullStreamUIPolicy() { | |
53 // The policy object should have never been created if there's no DB thread. | |
54 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); | |
55 ScheduleAndForget(db_, &ActivityDatabase::Close); | |
56 } | |
57 | |
58 // Get data as a set of key-value pairs. The keys are policy-specific. | |
59 void FullStreamUIPolicy::ReadData( | |
60 const std::string& extension_id, | |
61 const int day, | |
62 const Callback | |
63 <void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& callback) | |
64 const { | |
65 BrowserThread::PostTaskAndReplyWithResult( | |
66 BrowserThread::DB, | |
67 FROM_HERE, | |
68 base::Bind(&ActivityDatabase::GetActions, Unretained(db_), | |
69 extension_id, day), | |
70 callback); | |
71 } | |
72 | |
73 void FullStreamUIPolicy::SetSaveStateOnRequestOnly() { | |
74 db_->SetBatchModeForTesting(false); | |
75 ActivityLogPolicy::SetSaveStateOnRequestOnly(); | |
76 } | |
77 | |
78 std::string FullStreamUIPolicy::GetKey(ActivityLogPolicy::KeyType key_ty) const | |
79 { | |
80 switch (key_ty) { | |
81 case PARAM_KEY_REASON: | |
82 return std::string(kKeyReason); | |
83 case PARAM_KEY_DOM_ACTION: | |
84 return std::string(kKeyDomainAction); | |
85 case PARAM_KEY_URL_TITLE: | |
86 return std::string(kKeyURLTitle); | |
87 case PARAM_KEY_DETAILS_STRING: | |
88 return std::string(kKeyDetailsString); | |
89 default: | |
90 return std::string(); | |
91 } | |
92 } | |
93 | |
94 void FullStreamUIPolicy::ProcessArguments( | |
95 ActionType action_type, | |
96 const std::string& name, | |
97 const ListValue* args, | |
98 std::string* processed_args) const { | |
Matt Perry
2013/06/13 23:16:14
Sorry, this is the method I was thinking of when I
dbabic
2013/06/14 00:30:38
Done.
| |
99 if (args) { | |
100 ListValue::const_iterator it = args->begin(); | |
101 // TODO(felt,dbabic) Think about replacing the loop with a single | |
102 // call to SerializeAndOmitBinaryValues. | |
103 for (; it != args->end(); ++it) { | |
104 std::string arg; | |
105 JSONStringValueSerializer serializer(&arg); | |
106 if (serializer.SerializeAndOmitBinaryValues(**it)) { | |
107 if (it != args->begin()) { | |
108 processed_args->append(", "); | |
109 } | |
110 processed_args->append(arg); | |
111 } | |
112 } | |
113 } | |
114 } | |
115 | |
116 void FullStreamUIPolicy::ProcessWebRequestModifications( | |
117 DictionaryValue& details, | |
118 std::string& details_string) const { | |
119 JSONStringValueSerializer serializer(&details_string); | |
120 serializer.Serialize(details); | |
121 } | |
122 | |
123 void FullStreamUIPolicy::ProcessAction( | |
124 ActionType action_type, | |
125 const std::string& extension_id, | |
126 const std::string& name, | |
127 const GURL& url_param, | |
128 const ListValue* args, | |
129 const DictionaryValue* details) { | |
130 std::string concatenated_args; | |
131 ProcessArguments(action_type, name, args, &concatenated_args); | |
132 const Time now = Time::Now(); | |
133 scoped_refptr<Action> action; | |
134 std::string extra; | |
135 if (details) { | |
136 std::string key; | |
137 key = GetKey(PARAM_KEY_EXTRA); | |
138 details->GetString(key, &extra); | |
Matt Perry
2013/06/13 23:16:14
nit: could just do details->GetString(GetKey(PARAM
dbabic
2013/06/14 00:30:38
Done.
| |
139 } | |
140 | |
141 switch (action_type) { | |
142 case ACTION_API: { | |
143 action = new APIAction( | |
144 extension_id, | |
145 now, | |
146 APIAction::CALL, | |
147 name, | |
148 concatenated_args, | |
149 extra); | |
150 break; | |
151 } | |
152 case ACTION_EVENT: { | |
153 action = new APIAction( | |
154 extension_id, | |
155 now, | |
156 APIAction::EVENT_CALLBACK, | |
157 name, | |
158 concatenated_args, | |
159 extra); | |
160 break; | |
161 } | |
162 case ACTION_BLOCKED: { | |
163 std::string key; | |
164 int reason = 0; | |
165 if (details) { | |
166 key = GetKey(PARAM_KEY_REASON); | |
167 details->GetInteger(key, &reason); | |
168 } | |
169 | |
170 action = new BlockedAction( | |
171 extension_id, | |
172 now, | |
173 name, | |
174 concatenated_args, | |
175 static_cast<BlockedAction::Reason>(reason), | |
176 extra); | |
177 break; | |
178 } | |
179 case ACTION_DOM: { | |
180 std::string key; | |
181 string16 value; | |
182 DomActionType::Type action_type = DomActionType::MODIFIED; | |
183 | |
184 if (details) { | |
185 int action_id = 0; | |
186 key = GetKey(PARAM_KEY_DOM_ACTION); | |
187 details->GetInteger(key, &action_id); | |
188 action_type = static_cast<DomActionType::Type>(action_id); | |
189 key = GetKey(PARAM_KEY_URL_TITLE); | |
190 details->GetString(key, &value); | |
191 } | |
192 | |
193 action = new DOMAction( | |
194 extension_id, | |
195 now, | |
196 action_type, | |
197 url_param, | |
198 value, | |
199 name, | |
200 concatenated_args, | |
201 extra); | |
202 break; | |
203 } | |
204 case ACTION_WEB_REQUEST: { | |
205 std::string key; | |
206 std::string details_string; | |
207 if (details) { | |
208 scoped_ptr<DictionaryValue> copy_of_details(details->DeepCopy()); | |
209 key = GetKey(PARAM_KEY_DETAILS_STRING); | |
210 ProcessWebRequestModifications(*copy_of_details.get(), details_string); | |
211 } | |
212 | |
213 action = new DOMAction( | |
214 extension_id, | |
215 now, | |
216 DomActionType::WEBREQUEST, | |
217 url_param, | |
218 string16(), | |
219 name, | |
220 details_string, | |
221 extra); | |
222 break; | |
223 } | |
224 default: | |
225 NOTREACHED(); | |
226 } | |
227 | |
228 ScheduleAndForget(db_, &ActivityDatabase::RecordAction, action); | |
229 } | |
230 | |
231 } // namespace extensions | |
OLD | NEW |