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( | |
45 Profile* profile, | |
46 content::BrowserThread::ID thread_id) | |
47 : ActivityLogPolicy(profile, thread_id) { | |
48 // We normally dispatch DB requests to the DB thread, but the thread might | |
49 // not exist if we are under test conditions. Substitute the UI thread for | |
50 // this case. | |
51 if (BrowserThread::IsMessageLoopValid(BrowserThread::DB)) { | |
52 dispatch_thread_ = BrowserThread::DB; | |
53 } else { | |
54 LOG(ERROR) << "BrowserThread::DB does not exist, running on UI thread!"; | |
55 dispatch_thread_ = BrowserThread::UI; | |
felt
2013/06/13 20:24:16
Hey, during my last bug scrub I got rid of this di
dbabic
2013/06/13 23:10:08
Done.
| |
56 } | |
57 | |
58 db_ = new ActivityDatabase(); | |
59 FilePath database_name = profile_base_path_.Append( | |
60 chrome::kExtensionActivityLogFilename); | |
61 ScheduleAndForget(db_, &ActivityDatabase::Init, database_name); | |
62 } | |
63 | |
64 FullStreamUIPolicy::~FullStreamUIPolicy() { | |
65 ScheduleAndForget(db_, &ActivityDatabase::Close); | |
felt
2013/06/13 20:24:16
If the IO, DB, ir FILE threads are missing you nee
dbabic
2013/06/13 23:10:08
Done. If the DB thread is missing, the policy will
| |
66 } | |
67 | |
68 // Get data as a set of key-value pairs. The keys are policy-specific. | |
69 void FullStreamUIPolicy::ReadData( | |
70 const std::string& extension_id, | |
71 const int day, | |
72 const Callback | |
73 <void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& callback) | |
74 const { | |
75 BrowserThread::PostTaskAndReplyWithResult( | |
76 dispatch_thread_, | |
77 FROM_HERE, | |
78 base::Bind(&ActivityDatabase::GetActions, Unretained(db_), | |
79 extension_id, day), | |
80 callback); | |
81 } | |
82 | |
83 void FullStreamUIPolicy::SetSaveStateOnRequestOnly() { | |
84 db_->SetBatchModeForTesting(false); | |
felt
2013/06/13 20:24:16
Hey, I just noticed that you're doing this from th
dbabic
2013/06/13 23:10:08
This should all be running on the DB thread. If t
felt
2013/06/14 04:29:02
I'm confused. Where is SetSaveStateOnRequestOnly d
| |
85 ActivityLogPolicy::SetSaveStateOnRequestOnly(); | |
86 } | |
87 | |
88 void FullStreamUIPolicy::GetKey(ActivityLogPolicy::KeyType key_ty, | |
89 std::string* key) const { | |
90 DCHECK(key && "Unexpected NULL pointer."); | |
91 switch (key_ty) { | |
92 case PARAM_KEY_REASON: | |
93 *key = kKeyReason; | |
94 break; | |
95 case PARAM_KEY_DOM_ACTION: | |
96 *key = kKeyDomainAction; | |
97 break; | |
98 case PARAM_KEY_URL_TITLE: | |
99 *key = kKeyURLTitle; | |
100 break; | |
101 case PARAM_KEY_DETAILS_STRING: | |
102 *key = kKeyDetailsString; | |
103 break; | |
104 default: | |
105 *key = ""; | |
106 } | |
107 } | |
108 | |
109 void FullStreamUIPolicy::ProcessArguments( | |
110 ActionType action_type, | |
111 const std::string& name, | |
112 const ListValue* args, | |
113 std::string* processed_args) const { | |
114 if (args) { | |
115 ListValue::const_iterator it = args->begin(); | |
116 // TODO(felt,dbabic) Think about replacing the loop with a single | |
117 // call to SerializeAndOmitBinaryValues. | |
118 for (; it != args->end(); ++it) { | |
119 std::string arg; | |
120 JSONStringValueSerializer serializer(&arg); | |
121 if (serializer.SerializeAndOmitBinaryValues(**it)) { | |
122 if (it != args->begin()) { | |
123 processed_args->append(", "); | |
124 } | |
125 processed_args->append(arg); | |
126 } | |
127 } | |
128 } | |
129 } | |
130 | |
131 void FullStreamUIPolicy::ProcessWebRequestModifications( | |
132 DictionaryValue& details, | |
133 std::string& details_string) const { | |
134 JSONStringValueSerializer serializer(&details_string); | |
135 serializer.Serialize(details); | |
136 } | |
137 | |
138 void FullStreamUIPolicy::ProcessAction( | |
139 ActionType action_type, | |
140 const std::string& extension_id, | |
141 const std::string& name, | |
142 const GURL* url_param, | |
143 const ListValue* args, | |
144 const DictionaryValue* details) { | |
145 std::string concatenated_args; | |
146 ProcessArguments(action_type, name, args, &concatenated_args); | |
147 const Time now = Time::Now(); | |
148 // TODO(dbabic,felt) Drop the dummy string in the next revision | |
felt
2013/06/13 20:24:16
What's the dummy string? Is that the extra? Can yo
dbabic
2013/06/13 23:10:08
Done. It's now the extra string.
| |
149 const std::string dummy; | |
150 GURL url_obj; | |
151 if (url_param) { | |
152 url_obj = *url_param; | |
153 } | |
154 scoped_refptr<Action> action; | |
155 | |
156 switch (action_type) { | |
157 case ACTION_API: { | |
158 action = new APIAction( | |
159 extension_id, | |
160 now, | |
161 APIAction::CALL, | |
162 name, | |
163 concatenated_args, | |
164 dummy); // TODO(dbabic,felt) Drop in the next revision | |
165 break; | |
166 } | |
167 case ACTION_EVENT: { | |
168 action = new APIAction( | |
169 extension_id, | |
170 now, | |
171 APIAction::EVENT_CALLBACK, | |
172 name, | |
173 concatenated_args, | |
174 dummy); // TODO(dbabic,felt) Drop in the next revision | |
175 break; | |
176 } | |
177 case ACTION_BLOCKED: { | |
178 std::string key; | |
179 int reason = 0; | |
180 if (details) { | |
181 GetKey(PARAM_KEY_REASON, &key); | |
182 details->GetInteger(key, &reason); | |
183 } | |
184 | |
185 action = new BlockedAction( | |
186 extension_id, | |
187 now, | |
188 name, | |
189 concatenated_args, | |
190 static_cast<BlockedAction::Reason>(reason), | |
191 dummy); // TODO(dbabic,felt) Drop in the next revision | |
192 break; | |
193 } | |
194 case ACTION_DOM: { | |
195 std::string key; | |
196 string16 value; | |
197 DomActionType::Type action_type = DomActionType::MODIFIED; | |
198 | |
199 if (details) { | |
200 int action_id = 0; | |
201 GetKey(PARAM_KEY_DOM_ACTION, &key); | |
202 details->GetInteger(key, &action_id); | |
203 action_type = static_cast<DomActionType::Type>(action_id); | |
204 GetKey(PARAM_KEY_URL_TITLE, &key); | |
205 details->GetString(key, &value); | |
206 } | |
207 | |
208 action = new DOMAction( | |
209 extension_id, | |
210 now, | |
211 action_type, | |
212 url_obj, | |
213 value, | |
214 name, | |
215 concatenated_args, | |
216 dummy); // TODO(dbabic,felt) Drop in the next revision | |
217 break; | |
218 } | |
219 case ACTION_WEB_REQUEST: { | |
220 std::string key; | |
221 std::string details_string; | |
222 if (details) { | |
223 scoped_ptr<DictionaryValue> copy_of_details(details->DeepCopy()); | |
224 GetKey(PARAM_KEY_DETAILS_STRING, &key); | |
225 ProcessWebRequestModifications(*copy_of_details.get(), details_string); | |
226 } | |
227 | |
228 action = new DOMAction( | |
229 extension_id, | |
230 now, | |
231 DomActionType::WEBREQUEST, | |
232 url_obj, | |
233 string16(), | |
234 name, | |
235 details_string, | |
236 dummy); // TODO(dbabic,felt) Drop in the next revision | |
237 break; | |
238 } | |
239 default: | |
240 NOTREACHED(); | |
241 } | |
242 | |
243 ScheduleAndForget(db_, &ActivityDatabase::RecordAction, action); | |
244 } | |
245 | |
246 } // namespace extensions | |
OLD | NEW |