Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(867)

Side by Side Diff: chrome/browser/extensions/activity_log/activity_log_policy.h

Issue 15573003: New architecture of the activity logging: Policies for summarization (and compression) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addressed Adrienne's final comments. Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright $YEAR 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_ACTIVITY_LOG_POLICY_H_
6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_POLICY_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/bind.h"
12 #include "base/bind_helpers.h"
13 #include "base/callback.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/timer.h"
17 #include "base/values.h"
18 #include "chrome/browser/extensions/activity_log/activity_actions.h"
19 #include "content/public/browser/browser_thread.h"
20 #include "googleurl/src/gurl.h"
21
22 class Profile;
23 class GURL;
24
25 namespace extensions {
26
27 class Extension;
28
29 // Abstract class for summarizing data and storing it into the database. Any
30 // subclass should implement the following functionality:
31 //
32 // (1) Summarization (and possibly) compression of data
33 // (2) Periodical saving of the in-memory state with the database
34 // (3) Periodical database cleanup
35 //
36 // Since every policy implementation might summarize data differently, the
37 // database implementation is policy-specific and therefore completely
38 // encapsulated in the policy class. All the member functions can be called
39 // on the UI thread, because all DB operations are dispatched via the
40 // ActivityDatabase.
41 class ActivityLogPolicy {
42 public:
43 enum PolicyType {
44 POLICY_FULLSTREAM,
45 POLICY_NOARGS,
46 };
47
48 enum ActionType {
49 ACTION_API,
50 ACTION_EVENT,
51 ACTION_BLOCKED,
52 ACTION_DOM,
53 ACTION_WEB_REQUEST,
54 };
55
56 // For all subclasses, add all the key types they might support here.
57 // The actual key is returned by calling GetKey(KeyType). The subclasses
58 // are free to return an empty string for keys they don't support.
59 // For every key added here, you should update the GetKey member function
60 // for at least one policy.
61 enum KeyType {
62 PARAM_KEY_REASON, // Why an action was blocked
63 PARAM_KEY_DOM_ACTION, // Getter, Setter, Method,...
64 PARAM_KEY_URL_TITLE,
65 PARAM_KEY_DETAILS_STRING,
66 };
67
68 // Parameters are the profile and the thread that will be used to execute
69 // the callback when ReadData is called.
70 // TODO(felt,dbabic) Since only ReadData uses thread_id, it would be
71 // cleaner to pass thread_id as a param of ReadData directly.
72 explicit ActivityLogPolicy(Profile* profile,
73 content::BrowserThread::ID thread_id);
74 virtual ~ActivityLogPolicy();
75
76 // Updates the internal state of the model summarizing actions and possibly
77 // writes to the database. Implements the default policy storing internal
78 // state to memory every 5 min.
79 virtual void ProcessAction(
80 ActionType action_type,
81 const Extension& extension,
82 const std::string& name, // action name
83 const GURL* gurl, // target URL
84 const base::ListValue* args, // arguments
85 const base::DictionaryValue* details) = 0; // details
86
87 // Saves the internal state in the memory into the database. Must be
88 // written so as to be thread-safe, as it can be called from a timer that
89 // saves state periodically and explicitly.
90 virtual void SaveState() { }
91
92 // Pass the parameters as a set of key-value pairs and return data back via
93 // a callback passing results as a set of key-value pairs. The keys are
94 // policy-specific.
95 virtual void ReadData(
96 const base::DictionaryValue& parameters,
97 const base::Callback
98 <void(scoped_ptr<base::DictionaryValue>)>& callback) const {}
99
100 // TODO(felt,dbabic) This is overly specific to the current implementation
101 // of the FullStreamUIPolicy. We should refactor it to use the above
102 // more general member function.
103 virtual void ReadData(
104 const std::string& extension_id,
105 const int day,
106 const base::Callback
107 <void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& callback)
108 const {}
109
110 // For testing purposes --- disables periodic state saving, making the
111 // behavior reproducible.
112 virtual void SetSaveStateOnRequestOnly();
113
114 virtual void GetKey(KeyType key_id, std::string* key_string) const;
115
116 protected:
117 // The Schedule methods dispatch the calls to the database on a
118 // separate thread. We dispatch to the UI thread if the DB thread doesn't
119 // exist, which should only happen in tests where there is no DB thread.
120 template<typename DatabaseType, typename DatabaseFunc>
121 void ScheduleAndForget(DatabaseType db, DatabaseFunc func) {
122 content::BrowserThread::PostTask(
123 dispatch_thread_,
124 FROM_HERE,
125 base::Bind(func, base::Unretained(db)));
126 }
127
128 template<typename DatabaseType, typename DatabaseFunc, typename ArgA>
129 void ScheduleAndForget(DatabaseType db, DatabaseFunc func, ArgA a) {
130 content::BrowserThread::PostTask(
131 dispatch_thread_,
132 FROM_HERE,
133 base::Bind(func, base::Unretained(db), a));
134 }
135
136 template<typename DatabaseType, typename DatabaseFunc,
137 typename ArgA, typename ArgB>
138 void ScheduleAndForget(DatabaseType db, DatabaseFunc func, ArgA a, ArgB b) {
139 content::BrowserThread::PostTask(
140 dispatch_thread_,
141 FROM_HERE,
142 base::Bind(func, base::Unretained(db), a, b));
143 }
144
145 base::FilePath profile_base_path_;
146 base::RepeatingTimer<ActivityLogPolicy> timer_;
147 // Normally the DB thread. In some cases (tests), it might not exist
148 // we dispatch to the UI thread.
149 content::BrowserThread::ID dispatch_thread_;
150 };
151
152 } // End of the extensions namespace
153
154 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_POLICY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698