| 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 26 matching lines...) Expand all Loading... |
| 37 #include <vector> | 37 #include <vector> |
| 38 | 38 |
| 39 #include "base/callback.h" | 39 #include "base/callback.h" |
| 40 #include "base/files/file_path.h" | 40 #include "base/files/file_path.h" |
| 41 #include "base/json/json_reader.h" | 41 #include "base/json/json_reader.h" |
| 42 #include "base/json/json_string_value_serializer.h" | 42 #include "base/json/json_string_value_serializer.h" |
| 43 #include "base/macros.h" | 43 #include "base/macros.h" |
| 44 #include "base/memory/ptr_util.h" | 44 #include "base/memory/ptr_util.h" |
| 45 #include "base/strings/string_util.h" | 45 #include "base/strings/string_util.h" |
| 46 #include "base/strings/stringprintf.h" | 46 #include "base/strings/stringprintf.h" |
| 47 #include "base/task_runner_util.h" |
| 48 #include "chrome/browser/extensions/activity_log/activity_log_task_runner.h" |
| 47 #include "chrome/common/chrome_constants.h" | 49 #include "chrome/common/chrome_constants.h" |
| 48 #include "sql/statement.h" | 50 #include "sql/statement.h" |
| 49 #include "sql/transaction.h" | 51 #include "sql/transaction.h" |
| 50 | 52 |
| 51 using content::BrowserThread; | |
| 52 | |
| 53 namespace { | 53 namespace { |
| 54 | 54 |
| 55 using extensions::Action; | 55 using extensions::Action; |
| 56 | 56 |
| 57 // Delay between cleaning passes (to delete old action records) through the | 57 // Delay between cleaning passes (to delete old action records) through the |
| 58 // database. | 58 // database. |
| 59 const int kCleaningDelayInHours = 12; | 59 const int kCleaningDelayInHours = 12; |
| 60 | 60 |
| 61 // We should log the arguments to these API calls. Be careful when | 61 // We should log the arguments to these API calls. Be careful when |
| 62 // constructing this whitelist to not keep arguments that might compromise | 62 // constructing this whitelist to not keep arguments that might compromise |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 419 return true; | 419 return true; |
| 420 } | 420 } |
| 421 | 421 |
| 422 std::unique_ptr<Action::ActionVector> CountingPolicy::DoReadFilteredData( | 422 std::unique_ptr<Action::ActionVector> CountingPolicy::DoReadFilteredData( |
| 423 const std::string& extension_id, | 423 const std::string& extension_id, |
| 424 const Action::ActionType type, | 424 const Action::ActionType type, |
| 425 const std::string& api_name, | 425 const std::string& api_name, |
| 426 const std::string& page_url, | 426 const std::string& page_url, |
| 427 const std::string& arg_url, | 427 const std::string& arg_url, |
| 428 const int days_ago) { | 428 const int days_ago) { |
| 429 DCHECK(GetActivityLogTaskRunner()->RunsTasksInCurrentSequence()); |
| 429 // Ensure data is flushed to the database first so that we query over all | 430 // Ensure data is flushed to the database first so that we query over all |
| 430 // data. | 431 // data. |
| 431 activity_database()->AdviseFlush(ActivityDatabase::kFlushImmediately); | 432 activity_database()->AdviseFlush(ActivityDatabase::kFlushImmediately); |
| 432 std::unique_ptr<Action::ActionVector> actions(new Action::ActionVector()); | 433 std::unique_ptr<Action::ActionVector> actions(new Action::ActionVector()); |
| 433 | 434 |
| 434 sql::Connection* db = GetDatabaseConnection(); | 435 sql::Connection* db = GetDatabaseConnection(); |
| 435 if (!db) | 436 if (!db) |
| 436 return actions; | 437 return actions; |
| 437 | 438 |
| 438 // Build up the query based on which parameters were specified. | 439 // Build up the query based on which parameters were specified. |
| (...skipping 278 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 717 | 718 |
| 718 void CountingPolicy::ReadFilteredData( | 719 void CountingPolicy::ReadFilteredData( |
| 719 const std::string& extension_id, | 720 const std::string& extension_id, |
| 720 const Action::ActionType type, | 721 const Action::ActionType type, |
| 721 const std::string& api_name, | 722 const std::string& api_name, |
| 722 const std::string& page_url, | 723 const std::string& page_url, |
| 723 const std::string& arg_url, | 724 const std::string& arg_url, |
| 724 const int days_ago, | 725 const int days_ago, |
| 725 const base::Callback<void(std::unique_ptr<Action::ActionVector>)>& | 726 const base::Callback<void(std::unique_ptr<Action::ActionVector>)>& |
| 726 callback) { | 727 callback) { |
| 727 BrowserThread::PostTaskAndReplyWithResult( | 728 base::PostTaskAndReplyWithResult( |
| 728 BrowserThread::DB, | 729 GetActivityLogTaskRunner().get(), FROM_HERE, |
| 729 FROM_HERE, | 730 base::Bind(&CountingPolicy::DoReadFilteredData, base::Unretained(this), |
| 730 base::Bind(&CountingPolicy::DoReadFilteredData, | 731 extension_id, type, api_name, page_url, arg_url, days_ago), |
| 731 base::Unretained(this), | |
| 732 extension_id, | |
| 733 type, | |
| 734 api_name, | |
| 735 page_url, | |
| 736 arg_url, | |
| 737 days_ago), | |
| 738 callback); | 732 callback); |
| 739 } | 733 } |
| 740 | 734 |
| 741 void CountingPolicy::RemoveActions(const std::vector<int64_t>& action_ids) { | 735 void CountingPolicy::RemoveActions(const std::vector<int64_t>& action_ids) { |
| 742 ScheduleAndForget(this, &CountingPolicy::DoRemoveActions, action_ids); | 736 ScheduleAndForget(this, &CountingPolicy::DoRemoveActions, action_ids); |
| 743 } | 737 } |
| 744 | 738 |
| 745 void CountingPolicy::RemoveURLs(const std::vector<GURL>& restrict_urls) { | 739 void CountingPolicy::RemoveURLs(const std::vector<GURL>& restrict_urls) { |
| 746 ScheduleAndForget(this, &CountingPolicy::DoRemoveURLs, restrict_urls); | 740 ScheduleAndForget(this, &CountingPolicy::DoRemoveURLs, restrict_urls); |
| 747 } | 741 } |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 789 sql::StatementID(SQL_FROM_HERE), kUrlTableCleanup)); | 783 sql::StatementID(SQL_FROM_HERE), kUrlTableCleanup)); |
| 790 if (!cleaner2.Run()) | 784 if (!cleaner2.Run()) |
| 791 return false; | 785 return false; |
| 792 if (db->GetLastChangeCount() > 0) | 786 if (db->GetLastChangeCount() > 0) |
| 793 url_table_.ClearCache(); | 787 url_table_.ClearCache(); |
| 794 | 788 |
| 795 return true; | 789 return true; |
| 796 } | 790 } |
| 797 | 791 |
| 798 void CountingPolicy::Close() { | 792 void CountingPolicy::Close() { |
| 799 // The policy object should have never been created if there's no DB thread. | |
| 800 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::DB)); | |
| 801 ScheduleAndForget(activity_database(), &ActivityDatabase::Close); | 793 ScheduleAndForget(activity_database(), &ActivityDatabase::Close); |
| 802 } | 794 } |
| 803 | 795 |
| 804 } // namespace extensions | 796 } // namespace extensions |
| OLD | NEW |