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_
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
23 class CountingPolicy
: public ActivityLogDatabasePolicy
{
25 explicit CountingPolicy(Profile
* profile
);
26 virtual ~CountingPolicy();
28 virtual void ProcessAction(scoped_refptr
<Action
> action
) OVERRIDE
;
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
,
38 <void(scoped_ptr
<Action::ActionVector
>)>& callback
) OVERRIDE
;
40 virtual 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 // Clean the URL data stored for this policy.
49 virtual void RemoveURLs(const std::vector
<GURL
>&) OVERRIDE
;
51 // Clean the data related to this extension for this policy.
52 virtual void RemoveExtensionData(const std::string
& extension_id
) OVERRIDE
;
54 // Delete everything in the database.
55 virtual void DeleteDatabase() OVERRIDE
;
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
;
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
;
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
>
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
);
81 // Internal method to read data from the database; called on the database
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
,
91 // The implementation of RemoveURLs; this must only run on the database
93 void DoRemoveURLs(const std::vector
<GURL
>& restrict_urls
);
95 // The implementation of RemoveExtensionData; this must only run on the
97 void DoRemoveExtensionData(const std::string
& extension_id
);
99 // The implementation of DeleteDatabase; called on the database thread.
100 void DoDeleteDatabase();
102 // Cleans old records from the activity log database.
103 bool CleanOlderThan(sql::Connection
* db
, const base::Time
& cutoff
);
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
);
109 // API calls for which complete arguments should be logged.
110 Util::ApiSet api_arg_whitelist_
;
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_
;
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_
;
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_
;
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
132 base::TimeDelta retention_time_
;
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_
;
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
);
145 } // namespace extensions
147 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_COUNTING_POLICY_H_