Home | History | Annotate | Download | only in activity_log
      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 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_COUNTING_POLICY_H_
      6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_COUNTING_POLICY_H_
      7 
      8 #include <string>
      9 
     10 #include "base/containers/hash_tables.h"
     11 #include "chrome/browser/extensions/activity_log/activity_database.h"
     12 #include "chrome/browser/extensions/activity_log/activity_log_policy.h"
     13 #include "chrome/browser/extensions/activity_log/database_string_table.h"
     14 
     15 namespace extensions {
     16 
     17 // A policy for logging the stream of actions, but without arguments.
     18 class CountingPolicy : public ActivityLogDatabasePolicy {
     19  public:
     20   explicit CountingPolicy(Profile* profile);
     21   virtual ~CountingPolicy();
     22 
     23   virtual void ProcessAction(scoped_refptr<Action> action) OVERRIDE;
     24 
     25   virtual void ReadData(
     26       const std::string& extension_id,
     27       const int day,
     28       const base::Callback
     29           <void(scoped_ptr<Action::ActionVector>)>& callback) OVERRIDE;
     30 
     31   virtual void Close() OVERRIDE;
     32 
     33   // Gets or sets the amount of time that old records are kept in the database.
     34   const base::TimeDelta& retention_time() const { return retention_time_; }
     35   void set_retention_time(const base::TimeDelta& delta) {
     36     retention_time_ = delta;
     37   }
     38 
     39   // The main database table, and the name for a read-only view that
     40   // decompresses string values for easier parsing.
     41   static const char* kTableName;
     42   static const char* kReadViewName;
     43 
     44  protected:
     45   // The ActivityDatabase::Delegate interface.  These are always called from
     46   // the database thread.
     47   virtual bool InitDatabase(sql::Connection* db) OVERRIDE;
     48   virtual bool FlushDatabase(sql::Connection* db) OVERRIDE;
     49   virtual void OnDatabaseFailure() OVERRIDE;
     50   virtual void OnDatabaseClose() OVERRIDE;
     51 
     52  private:
     53   // Adds an Action to those to be written out; this is an internal method used
     54   // by ProcessAction and is called on the database thread.
     55   void QueueAction(scoped_refptr<Action> action);
     56 
     57   // Internal method to read data from the database; called on the database
     58   // thread.
     59   scoped_ptr<Action::ActionVector> DoReadData(
     60       const std::string& extension_id,
     61       const int days_ago);
     62 
     63   // Cleans old records from the activity log database.
     64   bool CleanOlderThan(sql::Connection* db, const base::Time& cutoff);
     65 
     66   // Cleans unused interned strings from the database.  This should be run
     67   // after deleting rows from the main log table to clean out stale values.
     68   bool CleanStringTables(sql::Connection* db);
     69 
     70   // API calls for which complete arguments should be logged.
     71   std::set<std::string> api_arg_whitelist_;
     72 
     73   // Tables for mapping strings to integers for shrinking database storage
     74   // requirements.  URLs are kept in a separate table from other strings to
     75   // make history clearing simpler.
     76   DatabaseStringTable string_table_;
     77   DatabaseStringTable url_table_;
     78 
     79   // Tracks any pending updates to be written to the database, if write
     80   // batching is turned on.  Should only be accessed from the database thread.
     81   // TODO(mvrable): Do in-memory aggregation as well.
     82   Action::ActionVector queued_actions_;
     83 
     84   // The amount of time old activity log records should be kept in the
     85   // database.  This time is subtracted from the current time, rounded down to
     86   // midnight, and rows older than this are deleted from the database when
     87   // cleaning runs.
     88   base::TimeDelta retention_time_;
     89 
     90   // The time at which old activity log records were last cleaned out of the
     91   // database (only tracked for this browser session).  Old records are deleted
     92   // on the first database flush, and then every 12 hours subsequently.
     93   base::Time last_database_cleaning_time_;
     94 
     95   FRIEND_TEST_ALL_PREFIXES(CountingPolicyTest, MergingAndExpiring);
     96 };
     97 
     98 }  // namespace extensions
     99 
    100 #endif  // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_COUNTING_POLICY_H_
    101