Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/bind.h" | |
| 13 #include "base/bind_helpers.h" | |
| 14 #include "base/callback.h" | 12 #include "base/callback.h" |
| 15 #include "base/hash_tables.h" | |
| 16 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 17 #include "base/observer_list_threadsafe.h" | 14 #include "base/observer_list_threadsafe.h" |
| 18 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 19 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
| 20 #include "chrome/browser/extensions/activity_log/activity_actions.h" | 17 #include "chrome/browser/extensions/activity_log/activity_actions.h" |
| 21 #include "chrome/browser/extensions/activity_log/activity_database.h" | 18 #include "chrome/browser/extensions/activity_log/activity_database.h" |
| 19 #include "chrome/browser/extensions/activity_log/activity_log_policy.h" | |
| 22 #include "chrome/browser/extensions/tab_helper.h" | 20 #include "chrome/browser/extensions/tab_helper.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 21 #include "chrome/browser/profiles/profile.h" |
| 24 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" | 22 #include "components/browser_context_keyed_service/browser_context_dependency_ma nager.h" |
| 25 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" | 23 #include "components/browser_context_keyed_service/browser_context_keyed_service .h" |
| 26 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h" | 24 #include "components/browser_context_keyed_service/browser_context_keyed_service _factory.h" |
| 27 #include "content/public/browser/browser_thread.h" | |
| 28 | 25 |
| 29 class Profile; | 26 class Profile; |
| 30 using content::BrowserThread; | 27 using content::BrowserThread; |
| 31 | 28 |
| 32 namespace extensions { | 29 namespace extensions { |
| 33 class Extension; | 30 class Extension; |
| 31 class ActivityLogPolicy; | |
| 34 | 32 |
| 35 // A utility for tracing interesting activity for each extension. | 33 // A utility for tracing interesting activity for each extension. |
| 36 // It writes to an ActivityDatabase on a separate thread to record the activity. | 34 // It writes to an ActivityDatabase on a separate thread to record the activity. |
| 37 class ActivityLog : public ProfileKeyedService, | 35 class ActivityLog : public ProfileKeyedService, |
| 38 public TabHelper::ScriptExecutionObserver { | 36 public TabHelper::ScriptExecutionObserver { |
| 39 public: | 37 public: |
| 40 enum Activity { | 38 enum Activity { |
| 41 ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. | 39 ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. |
| 42 ACTIVITY_EXTENSION_API_BLOCK, // Extension API invocation is blocked. | 40 ACTIVITY_EXTENSION_API_BLOCK, // Extension API invocation is blocked. |
| 43 ACTIVITY_CONTENT_SCRIPT, // Content script is executing. | 41 ACTIVITY_CONTENT_SCRIPT, // Content script is executing. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 88 void LogEventAction(const Extension* extension, | 86 void LogEventAction(const Extension* extension, |
| 89 const std::string& name, // e.g., tabs.onUpdate | 87 const std::string& name, // e.g., tabs.onUpdate |
| 90 ListValue* args, // arguments to the callback | 88 ListValue* args, // arguments to the callback |
| 91 const std::string& extra); // any extra logging info | 89 const std::string& extra); // any extra logging info |
| 92 | 90 |
| 93 // Log a blocked API call made by an extension. | 91 // Log a blocked API call made by an extension. |
| 94 // This will create a BlockedAction for storage in the database. | 92 // This will create a BlockedAction for storage in the database. |
| 95 void LogBlockedAction(const Extension* extension, | 93 void LogBlockedAction(const Extension* extension, |
| 96 const std::string& blocked_call, // e.g., tabs.get | 94 const std::string& blocked_call, // e.g., tabs.get |
| 97 ListValue* args, // argument values | 95 ListValue* args, // argument values |
| 98 const BlockedAction::Reason reason, // why it's blocked | 96 BlockedAction::Reason reason, // why it's blocked |
| 99 const std::string& extra); // extra logging info | 97 const std::string& extra); // extra logging info |
| 100 | 98 |
| 101 // Log an interaction between an extension and a URL. | 99 // Log an interaction between an extension and a URL. |
| 102 // This will create a DOMAction for storage in the database. | 100 // This will create a DOMAction for storage in the database. |
| 103 // The technical message might be the list of content scripts that have been | 101 // The technical message might be the list of content scripts that have been |
| 104 // injected, or the DOM API call; it's what's shown under "More". | 102 // injected, or the DOM API call; it's what's shown under "More". |
| 105 void LogDOMAction(const Extension* extension, | 103 void LogDOMAction(const Extension* extension, |
| 106 const GURL& url, // target URL | 104 const GURL& url, // target URL |
| 107 const string16& url_title, // title of the URL | 105 const string16& url_title, // title of the URL |
| 108 const std::string& api_call, // api call | 106 const std::string& api_call, // api call |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 120 // Retrieves the list of actions for a given extension on a specific day. | 118 // Retrieves the list of actions for a given extension on a specific day. |
| 121 // Today is 0, yesterday is 1, etc. Returns one day at a time. | 119 // Today is 0, yesterday is 1, etc. Returns one day at a time. |
| 122 // Response is sent to the method/function in the callback. | 120 // Response is sent to the method/function in the callback. |
| 123 // Use base::Bind to create the callback. | 121 // Use base::Bind to create the callback. |
| 124 void GetActions(const std::string& extension_id, | 122 void GetActions(const std::string& extension_id, |
| 125 const int day, | 123 const int day, |
| 126 const base::Callback | 124 const base::Callback |
| 127 <void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& | 125 <void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& |
| 128 callback); | 126 callback); |
| 129 | 127 |
| 130 // An error has happened; we want to rollback and close the db. | 128 static void SetDefaultPolicy(ActivityLogPolicy::PolicyType policy_type) { |
|
felt
2013/05/24 18:43:38
Can you add a comment describing what this is used
dbabic
2013/05/28 21:11:49
Done.
| |
| 131 // Needs to be public so the error delegate can call it. | 129 policy_type_ = policy_type; |
| 132 void KillActivityLogDatabase(); | 130 } |
| 133 | |
| 134 // For unit tests only. | |
| 135 void SetArgumentLoggingForTesting(bool log_arguments); | |
| 136 | 131 |
| 137 private: | 132 private: |
| 138 friend class ActivityLogFactory; | 133 friend class ActivityLogFactory; |
| 139 | 134 |
| 140 explicit ActivityLog(Profile* profile); | 135 explicit ActivityLog(Profile* profile); |
| 141 virtual ~ActivityLog(); | 136 virtual ~ActivityLog(); |
| 142 | 137 |
| 143 // We log callbacks and API calls very similarly, so we handle them the same | 138 // We log callbacks and API calls very similarly, so we handle them the same |
| 144 // way internally. | 139 // way internally. |
| 145 void LogAPIActionInternal( | 140 void LogAPIActionInternal( |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 160 DOMAction::DOMActionType verb); | 155 DOMAction::DOMActionType verb); |
| 161 | 156 |
| 162 // TabHelper::ScriptExecutionObserver implementation. | 157 // TabHelper::ScriptExecutionObserver implementation. |
| 163 // Fires when a ContentScript is executed. | 158 // Fires when a ContentScript is executed. |
| 164 virtual void OnScriptsExecuted( | 159 virtual void OnScriptsExecuted( |
| 165 const content::WebContents* web_contents, | 160 const content::WebContents* web_contents, |
| 166 const ExecutingScriptsMap& extension_ids, | 161 const ExecutingScriptsMap& extension_ids, |
| 167 int32 page_id, | 162 int32 page_id, |
| 168 const GURL& on_url) OVERRIDE; | 163 const GURL& on_url) OVERRIDE; |
| 169 | 164 |
| 170 // The callback when initializing the database. | |
| 171 void OnDBInitComplete(); | |
| 172 | |
| 173 static const char* ActivityToString(Activity activity); | 165 static const char* ActivityToString(Activity activity); |
| 174 | 166 |
| 175 // The Schedule methods dispatch the calls to the database on a | |
| 176 // separate thread. We dispatch to the UI thread if the DB thread doesn't | |
| 177 // exist, which should only happen in tests where there is no DB thread. | |
| 178 template<typename DatabaseFunc> | |
| 179 void ScheduleAndForget(DatabaseFunc func) { | |
| 180 BrowserThread::PostTask(dispatch_thread_, | |
| 181 FROM_HERE, | |
| 182 base::Bind(func, base::Unretained(db_))); | |
| 183 } | |
| 184 | |
| 185 template<typename DatabaseFunc, typename ArgA> | |
| 186 void ScheduleAndForget(DatabaseFunc func, ArgA a) { | |
| 187 BrowserThread::PostTask(dispatch_thread_, | |
| 188 FROM_HERE, | |
| 189 base::Bind(func, base::Unretained(db_), a)); | |
| 190 } | |
| 191 | |
| 192 template<typename DatabaseFunc, typename ArgA, typename ArgB> | |
| 193 void ScheduleAndForget(DatabaseFunc func, ArgA a, ArgB b) { | |
| 194 BrowserThread::PostTask(dispatch_thread_, | |
| 195 FROM_HERE, | |
| 196 base::Bind(func, base::Unretained(db_), a, b)); | |
| 197 } | |
| 198 | |
| 199 typedef ObserverListThreadSafe<Observer> ObserverList; | 167 typedef ObserverListThreadSafe<Observer> ObserverList; |
| 200 typedef std::map<const Extension*, scoped_refptr<ObserverList> > | 168 typedef std::map<const Extension*, scoped_refptr<ObserverList> > |
| 201 ObserverMap; | 169 ObserverMap; |
| 202 // A map of extensions to activity observers for that extension. | 170 // A map of extensions to activity observers for that extension. |
| 203 ObserverMap observers_; | 171 ObserverMap observers_; |
| 204 | 172 |
| 205 // The database wrapper that does the actual database I/O. | 173 // The policy object takes care of data summarization, compression, and |
| 206 // We initialize this on the same thread as the ActivityLog, but then | 174 // logging |
| 207 // subsequent operations occur on the DB thread. Instead of destructing the | 175 extensions::ActivityLogPolicy* policy_; |
| 208 // ActivityDatabase, we call its Close() method on the DB thread and it | |
| 209 // commits suicide. | |
| 210 extensions::ActivityDatabase* db_; | |
| 211 | |
| 212 // Normally the DB thread. In some cases (tests), it might not exist | |
| 213 // we dispatch to the UI thread. | |
| 214 BrowserThread::ID dispatch_thread_; | |
| 215 | 176 |
| 216 // Whether to log activity to stdout or the UI. These are set by switches. | 177 // Whether to log activity to stdout or the UI. These are set by switches. |
| 217 bool log_activity_to_stdout_; | 178 bool log_activity_to_stdout_; |
| 218 bool log_activity_to_ui_; | 179 bool log_activity_to_ui_; |
| 219 | 180 |
| 220 // testing_mode_ controls whether to log API call arguments. By default, we | 181 // TODO(dbabic,felt) change this into a list of policy types later. |
| 221 // don't log most arguments to avoid saving too much data. In testing mode, | 182 static ActivityLogPolicy::PolicyType policy_type_; |
| 222 // argument collection is enabled. We also whitelist some arguments for | |
| 223 // collection regardless of whether this bool is true. | |
| 224 bool testing_mode_; | |
| 225 base::hash_set<std::string> arg_whitelist_api_; | |
| 226 | 183 |
| 227 Profile* profile_; | 184 Profile* profile_; |
| 228 | 185 |
| 229 DISALLOW_COPY_AND_ASSIGN(ActivityLog); | 186 DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
| 230 }; | 187 }; |
| 231 | 188 |
| 232 // Each profile has different extensions, so we keep a different database for | 189 // Each profile has different extensions, so we keep a different database for |
| 233 // each profile. | 190 // each profile. |
| 234 class ActivityLogFactory : public ProfileKeyedServiceFactory { | 191 class ActivityLogFactory : public ProfileKeyedServiceFactory { |
| 235 public: | 192 public: |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 253 virtual content::BrowserContext* GetBrowserContextToUse( | 210 virtual content::BrowserContext* GetBrowserContextToUse( |
| 254 content::BrowserContext* context) const OVERRIDE; | 211 content::BrowserContext* context) const OVERRIDE; |
| 255 | 212 |
| 256 DISALLOW_COPY_AND_ASSIGN(ActivityLogFactory); | 213 DISALLOW_COPY_AND_ASSIGN(ActivityLogFactory); |
| 257 }; | 214 }; |
| 258 | 215 |
| 259 | 216 |
| 260 } // namespace extensions | 217 } // namespace extensions |
| 261 | 218 |
| 262 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ | 219 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ |
| OLD | NEW |