OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 // A policy for storing activity log data to a database that performs | 5 // A policy for storing activity log data to a database that performs |
6 // aggregation to reduce the size of the database. The database layout is | 6 // aggregation to reduce the size of the database. The database layout is |
7 // nearly the same as FullStreamUIPolicy, which stores a complete log, with a | 7 // nearly the same as FullStreamUIPolicy, which stores a complete log, with a |
8 // few changes: | 8 // few changes: |
9 // - a "count" column is added to track how many log records were merged | 9 // - a "count" column is added to track how many log records were merged |
10 // together into this row | 10 // together into this row |
(...skipping 424 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 where_next = " AND "; | 435 where_next = " AND "; |
436 } | 436 } |
437 if (!page_url.empty()) { | 437 if (!page_url.empty()) { |
438 where_str += where_next + "page_url LIKE ?"; | 438 where_str += where_next + "page_url LIKE ?"; |
439 where_next = " AND "; | 439 where_next = " AND "; |
440 } | 440 } |
441 if (!arg_url.empty()) | 441 if (!arg_url.empty()) |
442 where_str += where_next + "arg_url LIKE ?"; | 442 where_str += where_next + "arg_url LIKE ?"; |
443 std::string query_str = base::StringPrintf( | 443 std::string query_str = base::StringPrintf( |
444 "SELECT extension_id,time, action_type, api_name, args, page_url," | 444 "SELECT extension_id,time, action_type, api_name, args, page_url," |
445 "page_title, arg_url, other, count FROM %s WHERE %s ORDER BY count DESC " | 445 "page_title, arg_url, other, count FROM %s %s %s ORDER BY time DESC " |
446 "LIMIT 300", | 446 "LIMIT 300", |
447 kReadViewName, | 447 kReadViewName, |
| 448 where_str.empty() ? "" : "WHERE", |
448 where_str.c_str()); | 449 where_str.c_str()); |
449 sql::Statement query(db->GetUniqueStatement(query_str.c_str())); | 450 sql::Statement query(db->GetUniqueStatement(query_str.c_str())); |
450 int i = -1; | 451 int i = -1; |
451 if (!extension_id.empty()) | 452 if (!extension_id.empty()) |
452 query.BindString(++i, extension_id); | 453 query.BindString(++i, extension_id); |
453 if (!api_name.empty()) | 454 if (!api_name.empty()) |
454 query.BindString(++i, api_name); | 455 query.BindString(++i, api_name); |
455 if (type != Action::ACTION_ANY) | 456 if (type != Action::ACTION_ANY) |
456 query.BindInt(++i, static_cast<int>(type)); | 457 query.BindInt(++i, static_cast<int>(type)); |
457 if (!page_url.empty()) | 458 if (!page_url.empty()) |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
633 return; | 634 return; |
634 } | 635 } |
635 } | 636 } |
636 | 637 |
637 // Clean up unused strings from the strings and urls table to really delete | 638 // Clean up unused strings from the strings and urls table to really delete |
638 // the urls and page titles. Should be called even if an error occured when | 639 // the urls and page titles. Should be called even if an error occured when |
639 // removing a URL as there may some things to clean up. | 640 // removing a URL as there may some things to clean up. |
640 CleanStringTables(db); | 641 CleanStringTables(db); |
641 } | 642 } |
642 | 643 |
| 644 void CountingPolicy::DoDeleteDatabase() { |
| 645 sql::Connection* db = GetDatabaseConnection(); |
| 646 if (!db) { |
| 647 LOG(ERROR) << "Unable to connect to database"; |
| 648 return; |
| 649 } |
| 650 |
| 651 queued_actions_.clear(); |
| 652 |
| 653 // Not wrapped in a transaction because a late failure shouldn't undo a |
| 654 // previous deletion. |
| 655 std::string sql_str = base::StringPrintf("DELETE FROM %s", kTableName); |
| 656 sql::Statement statement(db->GetCachedStatement( |
| 657 sql::StatementID(SQL_FROM_HERE), |
| 658 sql_str.c_str())); |
| 659 if (!statement.Run()) { |
| 660 LOG(ERROR) << "Deleting the database failed: " |
| 661 << statement.GetSQLStatement(); |
| 662 return; |
| 663 } |
| 664 statement.Clear(); |
| 665 statement.Assign(db->GetCachedStatement(sql::StatementID(SQL_FROM_HERE), |
| 666 "DELETE FROM string_ids")); |
| 667 if (!statement.Run()) { |
| 668 LOG(ERROR) << "Deleting the database failed: " |
| 669 << statement.GetSQLStatement(); |
| 670 return; |
| 671 } |
| 672 statement.Clear(); |
| 673 statement.Assign(db->GetCachedStatement(sql::StatementID(SQL_FROM_HERE), |
| 674 "DELETE FROM url_ids")); |
| 675 if (!statement.Run()) { |
| 676 LOG(ERROR) << "Deleting the database failed: " |
| 677 << statement.GetSQLStatement(); |
| 678 return; |
| 679 } |
| 680 statement.Clear(); |
| 681 statement.Assign(db->GetCachedStatement(sql::StatementID(SQL_FROM_HERE), |
| 682 "VACUUM")); |
| 683 if (!statement.Run()) { |
| 684 LOG(ERROR) << "Vacuuming the database failed: " |
| 685 << statement.GetSQLStatement(); |
| 686 } |
| 687 } |
| 688 |
643 void CountingPolicy::ReadData( | 689 void CountingPolicy::ReadData( |
644 const std::string& extension_id, | 690 const std::string& extension_id, |
645 const int day, | 691 const int day, |
646 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& callback) { | 692 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& callback) { |
647 BrowserThread::PostTaskAndReplyWithResult( | 693 BrowserThread::PostTaskAndReplyWithResult( |
648 BrowserThread::DB, | 694 BrowserThread::DB, |
649 FROM_HERE, | 695 FROM_HERE, |
650 base::Bind(&CountingPolicy::DoReadData, | 696 base::Bind(&CountingPolicy::DoReadData, |
651 base::Unretained(this), | 697 base::Unretained(this), |
652 extension_id, | 698 extension_id, |
(...skipping 19 matching lines...) Expand all Loading... |
672 api_name, | 718 api_name, |
673 page_url, | 719 page_url, |
674 arg_url), | 720 arg_url), |
675 callback); | 721 callback); |
676 } | 722 } |
677 | 723 |
678 void CountingPolicy::RemoveURLs(const std::vector<GURL>& restrict_urls) { | 724 void CountingPolicy::RemoveURLs(const std::vector<GURL>& restrict_urls) { |
679 ScheduleAndForget(this, &CountingPolicy::DoRemoveURLs, restrict_urls); | 725 ScheduleAndForget(this, &CountingPolicy::DoRemoveURLs, restrict_urls); |
680 } | 726 } |
681 | 727 |
| 728 void CountingPolicy::DeleteDatabase() { |
| 729 ScheduleAndForget(this, &CountingPolicy::DoDeleteDatabase); |
| 730 } |
| 731 |
682 void CountingPolicy::OnDatabaseFailure() { | 732 void CountingPolicy::OnDatabaseFailure() { |
683 queued_actions_.clear(); | 733 queued_actions_.clear(); |
684 } | 734 } |
685 | 735 |
686 void CountingPolicy::OnDatabaseClose() { | 736 void CountingPolicy::OnDatabaseClose() { |
687 delete this; | 737 delete this; |
688 } | 738 } |
689 | 739 |
690 // Cleans old records from the activity log database. | 740 // Cleans old records from the activity log database. |
691 bool CountingPolicy::CleanOlderThan(sql::Connection* db, | 741 bool CountingPolicy::CleanOlderThan(sql::Connection* db, |
(...skipping 28 matching lines...) Expand all Loading... |
720 return true; | 770 return true; |
721 } | 771 } |
722 | 772 |
723 void CountingPolicy::Close() { | 773 void CountingPolicy::Close() { |
724 // The policy object should have never been created if there's no DB thread. | 774 // The policy object should have never been created if there's no DB thread. |
725 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); | 775 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); |
726 ScheduleAndForget(activity_database(), &ActivityDatabase::Close); | 776 ScheduleAndForget(activity_database(), &ActivityDatabase::Close); |
727 } | 777 } |
728 | 778 |
729 } // namespace extensions | 779 } // namespace extensions |
OLD | NEW |