Rename GetIconID to GetIconId
[chromium-blink-merge.git] / chrome / browser / extensions / activity_log / counting_policy.h
blob8c224c684df47e151fb72c1aa83dd3e3dd97ab2b
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.
5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_COUNTING_POLICY_H_
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_COUNTING_POLICY_H_
8 #include <string>
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"
15 namespace extensions {
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 ~CountingPolicy() override;
28 void ProcessAction(scoped_refptr<Action> action) override;
30 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<void(scoped_ptr<Action::ActionVector>)>& callback)
38 override;
40 void Close() override;
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;
48 // Remove actions (rows) which IDs are specified in the action_ids array.
49 void RemoveActions(const std::vector<int64>& action_ids) override;
51 // Clean the URL data stored for this policy.
52 void RemoveURLs(const std::vector<GURL>&) override;
54 // Clean the data related to this extension for this policy.
55 void RemoveExtensionData(const std::string& extension_id) override;
57 // Delete everything in the database.
58 void DeleteDatabase() override;
60 // The main database table, and the name for a read-only view that
61 // decompresses string values for easier parsing.
62 static const char kTableName[];
63 static const char kReadViewName[];
65 protected:
66 // The ActivityDatabase::Delegate interface. These are always called from
67 // the database thread.
68 bool InitDatabase(sql::Connection* db) override;
69 bool FlushDatabase(sql::Connection* db) override;
70 void OnDatabaseFailure() override;
71 void OnDatabaseClose() override;
73 private:
74 // A type used to track pending writes to the database. The key is an action
75 // to write; the value is the amount by which the count field should be
76 // incremented in the database.
77 typedef std::map<
78 scoped_refptr<Action>,
79 int,
80 ActionComparatorExcludingTimeAndActionId>
81 ActionQueue;
83 // Adds an Action to those to be written out; this is an internal method used
84 // by ProcessAction and is called on the database thread.
85 void QueueAction(scoped_refptr<Action> action);
87 // Internal method to read data from the database; called on the database
88 // thread.
89 scoped_ptr<Action::ActionVector> DoReadFilteredData(
90 const std::string& extension_id,
91 const Action::ActionType type,
92 const std::string& api_name,
93 const std::string& page_url,
94 const std::string& arg_url,
95 const int days_ago);
97 // The implementation of RemoveActions; this must only run on the database
98 // thread.
99 void DoRemoveActions(const std::vector<int64>& action_ids);
101 // The implementation of RemoveURLs; this must only run on the database
102 // thread.
103 void DoRemoveURLs(const std::vector<GURL>& restrict_urls);
105 // The implementation of RemoveExtensionData; this must only run on the
106 // database thread.
107 void DoRemoveExtensionData(const std::string& extension_id);
109 // The implementation of DeleteDatabase; called on the database thread.
110 void DoDeleteDatabase();
112 // Cleans old records from the activity log database.
113 bool CleanOlderThan(sql::Connection* db, const base::Time& cutoff);
115 // Cleans unused interned strings from the database. This should be run
116 // after deleting rows from the main log table to clean out stale values.
117 bool CleanStringTables(sql::Connection* db);
119 // API calls for which complete arguments should be logged.
120 Util::ApiSet api_arg_whitelist_;
122 // Tables for mapping strings to integers for shrinking database storage
123 // requirements. URLs are kept in a separate table from other strings to
124 // make history clearing simpler.
125 DatabaseStringTable string_table_;
126 DatabaseStringTable url_table_;
128 // Tracks any pending updates to be written to the database, if write
129 // batching is turned on. Should only be accessed from the database thread.
130 ActionQueue queued_actions_;
132 // All queued actions must fall on the same day, so that we do not
133 // accidentally aggregate actions that should be kept separate.
134 // queued_actions_date_ is the date (timestamp at local midnight) of all the
135 // actions in queued_actions_.
136 base::Time queued_actions_date_;
138 // The amount of time old activity log records should be kept in the
139 // database. This time is subtracted from the current time, rounded down to
140 // midnight, and rows older than this are deleted from the database when
141 // cleaning runs.
142 base::TimeDelta retention_time_;
144 // The time at which old activity log records were last cleaned out of the
145 // database (only tracked for this browser session). Old records are deleted
146 // on the first database flush, and then every 12 hours subsequently.
147 base::Time last_database_cleaning_time_;
149 friend class CountingPolicyTest;
150 FRIEND_TEST_ALL_PREFIXES(CountingPolicyTest, EarlyFlush);
151 FRIEND_TEST_ALL_PREFIXES(CountingPolicyTest, MergingAndExpiring);
152 FRIEND_TEST_ALL_PREFIXES(CountingPolicyTest, StringTableCleaning);
155 } // namespace extensions
157 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_COUNTING_POLICY_H_