OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/command_line.h" | 6 #include "base/command_line.h" |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/strings/string_util.h" | 8 #include "base/strings/string_util.h" |
9 #include "base/strings/stringprintf.h" | 9 #include "base/strings/stringprintf.h" |
10 #include "base/threading/thread.h" | 10 #include "base/threading/thread.h" |
(...skipping 16 matching lines...) Expand all Loading... | |
27 | 27 |
28 bool SortActionsByTime(const scoped_refptr<extensions::Action> a, | 28 bool SortActionsByTime(const scoped_refptr<extensions::Action> a, |
29 const scoped_refptr<extensions::Action> b) { | 29 const scoped_refptr<extensions::Action> b) { |
30 return a->time() > b->time(); | 30 return a->time() > b->time(); |
31 } | 31 } |
32 | 32 |
33 } // namespace | 33 } // namespace |
34 | 34 |
35 namespace extensions { | 35 namespace extensions { |
36 | 36 |
37 const char* ActivityDatabase::kURLFields[] = | |
38 {"url_tld", "url_title", "url_path"}; | |
39 | |
37 ActivityDatabase::ActivityDatabase(ActivityDatabase::Delegate* delegate) | 40 ActivityDatabase::ActivityDatabase(ActivityDatabase::Delegate* delegate) |
38 : delegate_(delegate), | 41 : delegate_(delegate), |
39 testing_clock_(NULL), | 42 testing_clock_(NULL), |
40 valid_db_(false), | 43 valid_db_(false), |
41 already_closed_(false), | 44 already_closed_(false), |
42 did_init_(false) { | 45 did_init_(false) { |
43 // We don't batch commits when in testing mode. | 46 // We don't batch commits when in testing mode. |
44 batch_mode_ = !(CommandLine::ForCurrentProcess()-> | 47 batch_mode_ = !(CommandLine::ForCurrentProcess()-> |
45 HasSwitch(switches::kEnableExtensionActivityLogTesting)); | 48 HasSwitch(switches::kEnableExtensionActivityLogTesting)); |
46 } | 49 } |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
254 } | 257 } |
255 | 258 |
256 void ActivityDatabase::SetTimerForTesting(int ms) { | 259 void ActivityDatabase::SetTimerForTesting(int ms) { |
257 timer_.Stop(); | 260 timer_.Stop(); |
258 timer_.Start(FROM_HERE, | 261 timer_.Start(FROM_HERE, |
259 base::TimeDelta::FromMilliseconds(ms), | 262 base::TimeDelta::FromMilliseconds(ms), |
260 this, | 263 this, |
261 &ActivityDatabase::RecordBatchedActionsWhileTesting); | 264 &ActivityDatabase::RecordBatchedActionsWhileTesting); |
262 } | 265 } |
263 | 266 |
267 std::string ActivityDatabase::ExtractTLD(const GURL& gurl) { | |
mvrable
2013/07/17 16:41:23
I know you just took the name from the DOMAction c
karenlees
2013/08/08 23:36:37
No longer need this method.
karenlees
2013/08/08 23:36:37
Method no longer needed, removed.
| |
268 std::string url_tld = gurl.GetOrigin().spec(); | |
269 if ((url_tld.size() > 0) && (url_tld[url_tld.size()-1] == '/')) { | |
270 url_tld.erase(url_tld.size()-1); | |
271 } | |
272 return url_tld; | |
273 } | |
274 | |
275 void ActivityDatabase::ConstructRemoveURLQuery(const std::string& url, | |
276 std::string* query) { | |
277 if (arraysize(kURLFields) < 1) { | |
278 DLOG(INFO) << "No fields to clean"; | |
279 return; | |
280 } | |
281 | |
282 std::string set_fields_str = base::StringPrintf("%s=''", kURLFields[0]); | |
283 for (uint32_t i = 1; i < arraysize(kURLFields); i++) { | |
284 set_fields_str = base::StringPrintf("%s,%s=''", set_fields_str.c_str(), | |
mvrable
2013/07/17 16:41:23
Personal preference: I would use NULL rather than
karenlees
2013/08/08 23:36:37
I like NULL, updated.
| |
285 kURLFields[i]); | |
286 } | |
287 | |
288 if (url.empty()) { | |
289 // Set fields to empty string for all rows. | |
290 *query = base::StringPrintf("UPDATE %s SET %s", | |
291 DOMAction::kTableName, | |
292 set_fields_str.c_str()); | |
293 } else { | |
294 // Set fields to empty string if the row has the url. | |
295 // TODO(karenlees): for the moment this matches anything with the same top | |
296 // level domain. After the database refactoring it should match the extact | |
297 // URLs in the column. For now it's better to clean too much than too | |
298 // little. | |
299 *query = base::StringPrintf("UPDATE %s SET %s " | |
300 "WHERE url_tld='%s'", | |
mvrable
2013/07/17 16:41:23
You shouldn't insert the URL directly into the que
karenlees
2013/08/08 23:36:37
I like safe, updated.
| |
301 DOMAction::kTableName, | |
302 set_fields_str.c_str(), | |
303 ExtractTLD(GURL(url)).c_str()); | |
304 } | |
305 } | |
306 | |
307 void ActivityDatabase::RemoveURLs(const std::vector<GURL>& gurls) { | |
308 DLOG(INFO) << "Removing URLs from the activity log"; | |
309 if (!gurls.empty()) { | |
310 for (uint32_t i = 0; i < gurls.size(); ++i) { | |
311 RemoveURL(gurls[i]); | |
312 } | |
313 } else { | |
314 RemoveAllURLs(); | |
315 } | |
316 } | |
317 | |
318 void ActivityDatabase::RemoveURL(const GURL& gurl) { | |
319 std::string tld = ExtractTLD(gurl); | |
320 if (tld.empty()) { | |
321 LOG(ERROR) << "Top level domain was empty, cannot delete."; | |
322 return; | |
323 } | |
324 | |
325 std::string remove_urls_str; | |
326 ConstructRemoveURLQuery(tld, &remove_urls_str); | |
327 sql::Statement remove_statement( | |
328 db_.GetCachedStatement(SQL_FROM_HERE, remove_urls_str.c_str())); | |
329 if (!remove_statement.is_valid()) { | |
330 DLOG(ERROR) << "Invalid sql statement from: " << remove_urls_str.c_str(); | |
331 return; | |
332 } | |
333 DLOG(INFO) << "Running: " << remove_urls_str.c_str(); | |
334 remove_statement.Run(); | |
335 } | |
336 | |
337 void ActivityDatabase::RemoveAllURLs() { | |
338 DLOG(INFO) << "Removing all URLs from the activity log"; | |
339 std::string remove_urls_str; | |
340 ConstructRemoveURLQuery("", &remove_urls_str); | |
341 sql::Statement remove_statement( | |
342 db_.GetCachedStatement(SQL_FROM_HERE, remove_urls_str.c_str())); | |
343 if (!remove_statement.is_valid()) { | |
344 DLOG(ERROR) << "Invalid sql statement from: " << remove_urls_str.c_str(); | |
345 return; | |
346 } | |
347 DLOG(INFO) << "Running: " << remove_urls_str.c_str(); | |
348 remove_statement.Run(); | |
349 } | |
350 | |
264 } // namespace extensions | 351 } // namespace extensions |
OLD | NEW |