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 549 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
560 } | 560 } |
561 | 561 |
562 action->set_count(query.ColumnInt(8)); | 562 action->set_count(query.ColumnInt(8)); |
563 | 563 |
564 actions->push_back(action); | 564 actions->push_back(action); |
565 } | 565 } |
566 | 566 |
567 return actions.Pass(); | 567 return actions.Pass(); |
568 } | 568 } |
569 | 569 |
| 570 void CountingPolicy::DoRemoveURLs(const std::vector<GURL>& restrict_urls) { |
| 571 sql::Connection* db = GetDatabaseConnection(); |
| 572 if (!db) { |
| 573 LOG(ERROR) << "Unable to connect to database"; |
| 574 return; |
| 575 } |
| 576 |
| 577 // Flush data first so the URL clearing affects queued-up data as well. |
| 578 activity_database()->AdviseFlush(ActivityDatabase::kFlushImmediately); |
| 579 |
| 580 // If no restrictions then then all URLs need to be removed. |
| 581 if (restrict_urls.empty()) { |
| 582 std::string sql_str = base::StringPrintf( |
| 583 "UPDATE %s SET page_url_x=NULL,page_title_x=NULL,arg_url_x=NULL", |
| 584 kTableName); |
| 585 |
| 586 sql::Statement statement; |
| 587 statement.Assign(db->GetCachedStatement( |
| 588 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 589 |
| 590 if (!statement.Run()) { |
| 591 LOG(ERROR) << "Removing all URLs from database failed: " |
| 592 << statement.GetSQLStatement(); |
| 593 return; |
| 594 } |
| 595 } |
| 596 |
| 597 // If URLs are specified then restrict to only those URLs. |
| 598 for (size_t i = 0; i < restrict_urls.size(); ++i) { |
| 599 int64 url_id; |
| 600 if (!restrict_urls[i].is_valid() || |
| 601 !url_table_.StringToInt(db, restrict_urls[i].spec(), &url_id)) { |
| 602 continue; |
| 603 } |
| 604 |
| 605 // Remove any that match the page_url. |
| 606 std::string sql_str = base::StringPrintf( |
| 607 "UPDATE %s SET page_url_x=NULL,page_title_x=NULL WHERE page_url_x IS ?", |
| 608 kTableName); |
| 609 |
| 610 sql::Statement statement; |
| 611 statement.Assign(db->GetCachedStatement( |
| 612 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 613 statement.BindInt64(0, url_id); |
| 614 |
| 615 if (!statement.Run()) { |
| 616 LOG(ERROR) << "Removing page URL from database failed: " |
| 617 << statement.GetSQLStatement(); |
| 618 return; |
| 619 } |
| 620 |
| 621 // Remove any that match the arg_url. |
| 622 sql_str = base::StringPrintf( |
| 623 "UPDATE %s SET arg_url_x=NULL WHERE arg_url_x IS ?", kTableName); |
| 624 |
| 625 statement.Assign(db->GetCachedStatement( |
| 626 sql::StatementID(SQL_FROM_HERE), sql_str.c_str())); |
| 627 statement.BindInt64(0, url_id); |
| 628 |
| 629 if (!statement.Run()) { |
| 630 LOG(ERROR) << "Removing arg URL from database failed: " |
| 631 << statement.GetSQLStatement(); |
| 632 return; |
| 633 } |
| 634 } |
| 635 |
| 636 // Clean up unused strings from the strings and urls table to really delete |
| 637 // the urls and page titles. Should be called even if an error occured when |
| 638 // removing a URL as there may some things to clean up. |
| 639 CleanStringTables(db); |
| 640 } |
| 641 |
570 void CountingPolicy::ReadData( | 642 void CountingPolicy::ReadData( |
571 const std::string& extension_id, | 643 const std::string& extension_id, |
572 const int day, | 644 const int day, |
573 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& callback) { | 645 const base::Callback<void(scoped_ptr<Action::ActionVector>)>& callback) { |
574 BrowserThread::PostTaskAndReplyWithResult( | 646 BrowserThread::PostTaskAndReplyWithResult( |
575 BrowserThread::DB, | 647 BrowserThread::DB, |
576 FROM_HERE, | 648 FROM_HERE, |
577 base::Bind(&CountingPolicy::DoReadData, | 649 base::Bind(&CountingPolicy::DoReadData, |
578 base::Unretained(this), | 650 base::Unretained(this), |
579 extension_id, | 651 extension_id, |
(...skipping 15 matching lines...) Expand all Loading... |
595 base::Bind(&CountingPolicy::DoReadFilteredData, | 667 base::Bind(&CountingPolicy::DoReadFilteredData, |
596 base::Unretained(this), | 668 base::Unretained(this), |
597 extension_id, | 669 extension_id, |
598 type, | 670 type, |
599 api_name, | 671 api_name, |
600 page_url, | 672 page_url, |
601 arg_url), | 673 arg_url), |
602 callback); | 674 callback); |
603 } | 675 } |
604 | 676 |
| 677 void CountingPolicy::RemoveURLs(const std::vector<GURL>& restrict_urls) { |
| 678 ScheduleAndForget(this, &CountingPolicy::DoRemoveURLs, restrict_urls); |
| 679 } |
| 680 |
605 void CountingPolicy::OnDatabaseFailure() { | 681 void CountingPolicy::OnDatabaseFailure() { |
606 queued_actions_.clear(); | 682 queued_actions_.clear(); |
607 } | 683 } |
608 | 684 |
609 void CountingPolicy::OnDatabaseClose() { | 685 void CountingPolicy::OnDatabaseClose() { |
610 delete this; | 686 delete this; |
611 } | 687 } |
612 | 688 |
613 // Cleans old records from the activity log database. | 689 // Cleans old records from the activity log database. |
614 bool CountingPolicy::CleanOlderThan(sql::Connection* db, | 690 bool CountingPolicy::CleanOlderThan(sql::Connection* db, |
(...skipping 28 matching lines...) Expand all Loading... |
643 return true; | 719 return true; |
644 } | 720 } |
645 | 721 |
646 void CountingPolicy::Close() { | 722 void CountingPolicy::Close() { |
647 // The policy object should have never been created if there's no DB thread. | 723 // The policy object should have never been created if there's no DB thread. |
648 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); | 724 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); |
649 ScheduleAndForget(activity_database(), &ActivityDatabase::Close); | 725 ScheduleAndForget(activity_database(), &ActivityDatabase::Close); |
650 } | 726 } |
651 | 727 |
652 } // namespace extensions | 728 } // namespace extensions |
OLD | NEW |