- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / activity_log / counting_policy.h
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 with most arguments stripped
18 // out (to improve privacy and reduce database size) and with multiple
19 // identical rows combined together using a count column to track the total
20 // number of repetitions.  Identical rows within the same day are merged, but
21 // actions on separate days are kept distinct.  Data is kept for up to a few
22 // days then deleted.
23 class CountingPolicy : public ActivityLogDatabasePolicy {
24  public:
25   explicit CountingPolicy(Profile* profile);
26   virtual ~CountingPolicy();
27
28   virtual void ProcessAction(scoped_refptr<Action> action) OVERRIDE;
29
30   virtual void ReadFilteredData(
31       const std::string& extension_id,
32       const Action::ActionType type,
33       const std::string& api_name,
34       const std::string& page_url,
35       const std::string& arg_url,
36       const int days_ago,
37       const base::Callback
38           <void(scoped_ptr<Action::ActionVector>)>& callback) OVERRIDE;
39
40   virtual void Close() OVERRIDE;
41
42   // Gets or sets the amount of time that old records are kept in the database.
43   const base::TimeDelta& retention_time() const { return retention_time_; }
44   void set_retention_time(const base::TimeDelta& delta) {
45     retention_time_ = delta;
46   }
47
48   // Clean the URL data stored for this policy.
49   virtual void RemoveURLs(const std::vector<GURL>&) OVERRIDE;
50
51   // Clean the data related to this extension for this policy.
52   virtual void RemoveExtensionData(const std::string& extension_id) OVERRIDE;
53
54   // Delete everything in the database.
55   virtual void DeleteDatabase() OVERRIDE;
56
57   // The main database table, and the name for a read-only view that
58   // decompresses string values for easier parsing.
59   static const char* kTableName;
60   static const char* kReadViewName;
61
62  protected:
63   // The ActivityDatabase::Delegate interface.  These are always called from
64   // the database thread.
65   virtual bool InitDatabase(sql::Connection* db) OVERRIDE;
66   virtual bool FlushDatabase(sql::Connection* db) OVERRIDE;
67   virtual void OnDatabaseFailure() OVERRIDE;
68   virtual void OnDatabaseClose() OVERRIDE;
69
70  private:
71   // A type used to track pending writes to the database.  The key is an action
72   // to write; the value is the amount by which the count field should be
73   // incremented in the database.
74   typedef std::map<scoped_refptr<Action>, int, ActionComparatorExcludingTime>
75       ActionQueue;
76
77   // Adds an Action to those to be written out; this is an internal method used
78   // by ProcessAction and is called on the database thread.
79   void QueueAction(scoped_refptr<Action> action);
80
81   // Internal method to read data from the database; called on the database
82   // thread.
83   scoped_ptr<Action::ActionVector> DoReadFilteredData(
84       const std::string& extension_id,
85       const Action::ActionType type,
86       const std::string& api_name,
87       const std::string& page_url,
88       const std::string& arg_url,
89       const int days_ago);
90
91   // The implementation of RemoveURLs; this must only run on the database
92   // thread.
93   void DoRemoveURLs(const std::vector<GURL>& restrict_urls);
94
95   // The implementation of RemoveExtensionData; this must only run on the
96   // database thread.
97   void DoRemoveExtensionData(const std::string& extension_id);
98
99   // The implementation of DeleteDatabase; called on the database thread.
100   void DoDeleteDatabase();
101
102   // Cleans old records from the activity log database.
103   bool CleanOlderThan(sql::Connection* db, const base::Time& cutoff);
104
105   // Cleans unused interned strings from the database.  This should be run
106   // after deleting rows from the main log table to clean out stale values.
107   bool CleanStringTables(sql::Connection* db);
108
109   // API calls for which complete arguments should be logged.
110   Util::ApiSet api_arg_whitelist_;
111
112   // Tables for mapping strings to integers for shrinking database storage
113   // requirements.  URLs are kept in a separate table from other strings to
114   // make history clearing simpler.
115   DatabaseStringTable string_table_;
116   DatabaseStringTable url_table_;
117
118   // Tracks any pending updates to be written to the database, if write
119   // batching is turned on.  Should only be accessed from the database thread.
120   ActionQueue queued_actions_;
121
122   // All queued actions must fall on the same day, so that we do not
123   // accidentally aggregate actions that should be kept separate.
124   // queued_actions_date_ is the date (timestamp at local midnight) of all the
125   // actions in queued_actions_.
126   base::Time queued_actions_date_;
127
128   // The amount of time old activity log records should be kept in the
129   // database.  This time is subtracted from the current time, rounded down to
130   // midnight, and rows older than this are deleted from the database when
131   // cleaning runs.
132   base::TimeDelta retention_time_;
133
134   // The time at which old activity log records were last cleaned out of the
135   // database (only tracked for this browser session).  Old records are deleted
136   // on the first database flush, and then every 12 hours subsequently.
137   base::Time last_database_cleaning_time_;
138
139   friend class CountingPolicyTest;
140   FRIEND_TEST_ALL_PREFIXES(CountingPolicyTest, EarlyFlush);
141   FRIEND_TEST_ALL_PREFIXES(CountingPolicyTest, MergingAndExpiring);
142   FRIEND_TEST_ALL_PREFIXES(CountingPolicyTest, StringTableCleaning);
143 };
144
145 }  // namespace extensions
146
147 #endif  // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_COUNTING_POLICY_H_