OLD | NEW |
---|---|
(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 "content/public/browser/browser_thread.h" | |
19 #include "chrome/browser/extensions/activity_log/activity_actions.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. | |
39 class ActivityLogPolicy { | |
felt
2013/05/24 18:43:38
Can you please specify that all of these methods c
dbabic
2013/05/28 21:11:49
Done.
| |
40 public: | |
41 enum PolicyType { | |
42 POLICY_FULLSTREAM, | |
43 POLICY_NOARGS, | |
44 }; | |
45 | |
46 enum ActionType { | |
47 ACTION_API, | |
48 ACTION_EVENT, | |
49 ACTION_BLOCKED, | |
50 ACTION_DOM, | |
51 ACTION_WEB_REQUEST, | |
52 }; | |
53 | |
54 // For all subclasses, add all the key types they might support here. | |
55 // The actual key is returned by calling GetKey(KeyType). The subclasses | |
56 // are free to return an empty string for keys they don't support. | |
57 // For every key added here, you should update the GetKey member function | |
58 // for at least one policy. | |
59 enum KeyType { | |
60 PARAM_KEY_REASON, // Why an action was blocked | |
61 PARAM_KEY_DOM_ACTION, // Getter, Setter, Method,... | |
62 PARAM_KEY_URL_TITLE, | |
63 PARAM_KEY_DETAILS_STRING, | |
64 }; | |
65 | |
66 explicit ActivityLogPolicy(Profile* profile, | |
67 content::BrowserThread::ID thread_id); | |
68 virtual ~ActivityLogPolicy(); | |
69 | |
70 // Updates the internal state of the model summarizing actions and possibly | |
71 // writes to the database. Implements the default policy storing internal | |
72 // state to memory every 5 min. | |
73 virtual void ProcessAction( | |
74 ActionType action_type, | |
75 const Extension& extension, | |
76 const std::string& name, // action name | |
77 const GURL* gurl, // target URL | |
78 const base::ListValue* args, // arguments | |
79 const base::DictionaryValue* details // details | |
80 ) = 0; | |
81 | |
82 // Saves the internal state in the memory into the database. Must be | |
83 // written so as to be thread-safe, as it can be called from a timer that | |
84 // saves state periodically and explicitly. | |
85 virtual void SaveState() { } | |
86 | |
87 // Pass the parameters as a set of key-value pairs and return data back via | |
88 // a callback passing results as a set of key-value pairs. The keys are | |
89 // policy-specific. | |
90 virtual void ReadData( | |
91 const base::DictionaryValue& parameters, | |
92 const base::Callback<void(scoped_ptr<base::DictionaryValue>)>& callback) | |
93 const {} | |
94 | |
95 // TODO(felt,dbabic) This is overly specific to the current implementation | |
96 // of the FullStreamUIPolicy. We should refractor it to use the above | |
felt
2013/05/24 18:43:38
"refractor" -> "refactor"
dbabic
2013/05/28 21:11:49
Done.
| |
97 // more general member function. | |
98 virtual void ReadData( | |
99 const std::string& extension_id, | |
100 const int day, | |
101 const base::Callback<void( | |
102 scoped_ptr<std::vector<scoped_refptr<Action> > >)>& callback) | |
103 const {} | |
104 | |
105 // For testing purposes --- disables periodic state saving, making the | |
106 // behavior reproducible. | |
107 void SetSaveStateOnRequestOnly(); | |
108 | |
109 virtual void GetKey(KeyType key_id, std::string& key_string) const; | |
110 | |
111 protected: | |
112 // The Schedule methods dispatch the calls to the database on a | |
113 // separate thread. We dispatch to the UI thread if the DB thread doesn't | |
114 // exist, which should only happen in tests where there is no DB thread. | |
115 template<typename DatabaseType, typename DatabaseFunc> | |
116 void ScheduleAndForget(DatabaseType db, DatabaseFunc func) { | |
117 content::BrowserThread::PostTask( | |
118 dispatch_thread_, | |
119 FROM_HERE, | |
120 base::Bind(func, base::Unretained(db))); | |
121 } | |
122 | |
123 template<typename DatabaseType, typename DatabaseFunc, typename ArgA> | |
124 void ScheduleAndForget(DatabaseType db, DatabaseFunc func, ArgA a) { | |
125 content::BrowserThread::PostTask( | |
126 dispatch_thread_, | |
127 FROM_HERE, | |
128 base::Bind(func, base::Unretained(db), a)); | |
129 } | |
130 | |
131 template<typename DatabaseType, typename DatabaseFunc, | |
132 typename ArgA, typename ArgB> | |
133 void ScheduleAndForget(DatabaseType db, DatabaseFunc func, ArgA a, ArgB b) { | |
134 content::BrowserThread::PostTask( | |
135 dispatch_thread_, | |
136 FROM_HERE, | |
137 base::Bind(func, base::Unretained(db), a, b)); | |
138 } | |
139 | |
140 base::FilePath profile_base_path_; | |
141 base::RepeatingTimer<ActivityLogPolicy> timer_; | |
142 // Normally the DB thread. In some cases (tests), it might not exist | |
143 // we dispatch to the UI thread. | |
144 content::BrowserThread::ID dispatch_thread_; | |
145 }; | |
146 | |
147 } // End of the extensions namespace | |
148 | |
149 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_POLICY_H_ | |
OLD | NEW |