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