| 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> |
| (...skipping 20 matching lines...) Expand all Loading... |
| 31 using content::BrowserThread; | 31 using content::BrowserThread; |
| 32 | 32 |
| 33 namespace extensions { | 33 namespace extensions { |
| 34 class Extension; | 34 class Extension; |
| 35 | 35 |
| 36 // A utility for tracing interesting activity for each extension. | 36 // A utility for tracing interesting activity for each extension. |
| 37 // It writes to an ActivityDatabase on a separate thread to record the activity. | 37 // It writes to an ActivityDatabase on a separate thread to record the activity. |
| 38 class ActivityLog : public BrowserContextKeyedService, | 38 class ActivityLog : public BrowserContextKeyedService, |
| 39 public TabHelper::ScriptExecutionObserver { | 39 public TabHelper::ScriptExecutionObserver { |
| 40 public: | 40 public: |
| 41 enum Activity { | |
| 42 ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. | |
| 43 ACTIVITY_EXTENSION_API_BLOCK, // Extension API invocation is blocked. | |
| 44 ACTIVITY_CONTENT_SCRIPT, // Content script is executing. | |
| 45 ACTIVITY_EVENT_DISPATCH, // Event sent to listener in extension. | |
| 46 }; | |
| 47 | |
| 48 // Observers can listen for activity events. | 41 // Observers can listen for activity events. |
| 49 class Observer { | 42 class Observer { |
| 50 public: | 43 public: |
| 51 virtual void OnExtensionActivity( | 44 virtual void OnExtensionActivity(scoped_refptr<Action> activity) = 0; |
| 52 const Extension* extension, | |
| 53 Activity activity, | |
| 54 const std::string& message) = 0; | |
| 55 }; | 45 }; |
| 56 | 46 |
| 57 // ActivityLog is a singleton, so don't instantiate it with the constructor; | 47 // ActivityLog is a singleton, so don't instantiate it with the constructor; |
| 58 // use GetInstance instead. | 48 // use GetInstance instead. |
| 59 static ActivityLog* GetInstance(Profile* profile); | 49 static ActivityLog* GetInstance(Profile* profile); |
| 60 | 50 |
| 61 // Currently, we only want to record actions if the user has opted in to the | 51 // Currently, we only want to record actions if the user has opted in to the |
| 62 // ActivityLog feature. | 52 // ActivityLog feature. |
| 63 static bool IsLogEnabled(); | 53 static bool IsLogEnabled(); |
| 64 | 54 |
| 65 // Recompute whether logging should be enabled (the value of IsLogEnabled is | 55 // Recompute whether logging should be enabled (the value of IsLogEnabled is |
| 66 // normally cached). WARNING: This may not be thread-safe, and is only | 56 // normally cached). WARNING: This may not be thread-safe, and is only |
| 67 // really intended for use by unit tests. | 57 // really intended for use by unit tests. |
| 68 static void RecomputeLoggingIsEnabled(); | 58 static void RecomputeLoggingIsEnabled(); |
| 69 | 59 |
| 70 // Add/remove observer. | 60 // Add/remove observer. |
| 71 void AddObserver(const Extension* extension, Observer* observer); | 61 void AddObserver(Observer* observer); |
| 72 void RemoveObserver(const Extension* extension, | 62 void RemoveObserver(Observer* observer); |
| 73 Observer* observer); | |
| 74 | |
| 75 // Check for the existence observer list by extension_id. | |
| 76 bool HasObservers(const Extension* extension) const; | |
| 77 | 63 |
| 78 // Log a successful API call made by an extension. | 64 // Log a successful API call made by an extension. |
| 79 // This will create an APIAction for storage in the database. | 65 // This will create an APIAction for storage in the database. |
| 80 // (Note: implemented as a wrapper for LogAPIActionInternal.) | 66 // (Note: implemented as a wrapper for LogAPIActionInternal.) |
| 81 void LogAPIAction(const Extension* extension, | 67 void LogAPIAction(const std::string& extension_id, |
| 82 const std::string& name, // e.g., tabs.get | 68 const std::string& name, // e.g., tabs.get |
| 83 ListValue* args, // the argument values e.g. 46 | 69 ListValue* args, // the argument values e.g. 46 |
| 84 const std::string& extra); // any extra logging info | 70 const std::string& extra); // any extra logging info |
| 85 | 71 |
| 86 // Log an event notification delivered to an extension. | 72 // Log an event notification delivered to an extension. |
| 87 // This will create an APIAction for storage in the database. | 73 // This will create an APIAction for storage in the database. |
| 88 // (Note: implemented as a wrapper for LogAPIActionInternal.) | 74 // (Note: implemented as a wrapper for LogAPIActionInternal.) |
| 89 void LogEventAction(const Extension* extension, | 75 void LogEventAction(const std::string& extension_id, |
| 90 const std::string& name, // e.g., tabs.onUpdate | 76 const std::string& name, // e.g., tabs.onUpdate |
| 91 ListValue* args, // arguments to the callback | 77 ListValue* args, // arguments to the callback |
| 92 const std::string& extra); // any extra logging info | 78 const std::string& extra); // any extra logging info |
| 93 | 79 |
| 94 // Log a blocked API call made by an extension. | 80 // Log a blocked API call made by an extension. |
| 95 // This will create a BlockedAction for storage in the database. | 81 // This will create a BlockedAction for storage in the database. |
| 96 void LogBlockedAction(const Extension* extension, | 82 void LogBlockedAction(const std::string& extension_id, |
| 97 const std::string& blocked_call, // e.g., tabs.get | 83 const std::string& blocked_call, // e.g., tabs.get |
| 98 ListValue* args, // argument values | 84 ListValue* args, // argument values |
| 99 const BlockedAction::Reason reason, // why it's blocked | 85 const BlockedAction::Reason reason, // why it's blocked |
| 100 const std::string& extra); // extra logging info | 86 const std::string& extra); // extra logging info |
| 101 | 87 |
| 102 // Log an interaction between an extension and a URL. | 88 // Log an interaction between an extension and a URL. |
| 103 // This will create a DOMAction for storage in the database. | 89 // This will create a DOMAction for storage in the database. |
| 104 void LogDOMAction(const Extension* extension, | 90 void LogDOMAction(const std::string& extension_id, |
| 105 const GURL& url, // target URL | 91 const GURL& url, // target URL |
| 106 const string16& url_title, // title of the URL | 92 const string16& url_title, // title of the URL |
| 107 const std::string& api_call, // api call | 93 const std::string& api_call, // api call |
| 108 const ListValue* args, // arguments | 94 const ListValue* args, // arguments |
| 109 DomActionType::Type call_type, // type of the call | 95 DomActionType::Type call_type, // type of the call |
| 110 const std::string& extra); // extra logging info | 96 const std::string& extra); // extra logging info |
| 111 | 97 |
| 112 // Log a use of the WebRequest API to redirect, cancel, or modify page | 98 // Log a use of the WebRequest API to redirect, cancel, or modify page |
| 113 // headers. | 99 // headers. |
| 114 void LogWebRequestAction(const Extension* extension, | 100 void LogWebRequestAction(const std::string& extension_id, |
| 115 const GURL& url, | 101 const GURL& url, |
| 116 const std::string& api_call, | 102 const std::string& api_call, |
| 117 scoped_ptr<base::DictionaryValue> details, | 103 scoped_ptr<base::DictionaryValue> details, |
| 118 const std::string& extra); | 104 const std::string& extra); |
| 119 | 105 |
| 120 // Retrieves the list of actions for a given extension on a specific day. | 106 // 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. | 107 // Today is 0, yesterday is 1, etc. Returns one day at a time. |
| 122 // Response is sent to the method/function in the callback. | 108 // Response is sent to the method/function in the callback. |
| 123 // Use base::Bind to create the callback. | 109 // Use base::Bind to create the callback. |
| 124 void GetActions(const std::string& extension_id, | 110 void GetActions(const std::string& extension_id, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 135 | 121 |
| 136 explicit ActivityLog(Profile* profile); | 122 explicit ActivityLog(Profile* profile); |
| 137 virtual ~ActivityLog(); | 123 virtual ~ActivityLog(); |
| 138 | 124 |
| 139 // Reset the database in case of persistent catastrophic errors. | 125 // Reset the database in case of persistent catastrophic errors. |
| 140 void DatabaseErrorCallback(int error, sql::Statement* stmt); | 126 void DatabaseErrorCallback(int error, sql::Statement* stmt); |
| 141 | 127 |
| 142 // We log callbacks and API calls very similarly, so we handle them the same | 128 // We log callbacks and API calls very similarly, so we handle them the same |
| 143 // way internally. | 129 // way internally. |
| 144 void LogAPIActionInternal( | 130 void LogAPIActionInternal( |
| 145 const Extension* extension, | 131 const std::string& extension_id, |
| 146 const std::string& api_call, | 132 const std::string& api_call, |
| 147 ListValue* args, | 133 ListValue* args, |
| 148 const std::string& extra, | 134 const std::string& extra, |
| 149 const APIAction::Type type); | 135 const APIAction::Type type); |
| 150 | 136 |
| 151 // TabHelper::ScriptExecutionObserver implementation. | 137 // TabHelper::ScriptExecutionObserver implementation. |
| 152 // Fires when a ContentScript is executed. | 138 // Fires when a ContentScript is executed. |
| 153 virtual void OnScriptsExecuted( | 139 virtual void OnScriptsExecuted( |
| 154 const content::WebContents* web_contents, | 140 const content::WebContents* web_contents, |
| 155 const ExecutingScriptsMap& extension_ids, | 141 const ExecutingScriptsMap& extension_ids, |
| 156 int32 page_id, | 142 int32 page_id, |
| 157 const GURL& on_url) OVERRIDE; | 143 const GURL& on_url) OVERRIDE; |
| 158 | 144 |
| 159 // The callback when initializing the database. | 145 // The callback when initializing the database. |
| 160 void OnDBInitComplete(); | 146 void OnDBInitComplete(); |
| 161 | 147 |
| 162 static const char* ActivityToString(Activity activity); | |
| 163 | |
| 164 // The Schedule methods dispatch the calls to the database on a | 148 // The Schedule methods dispatch the calls to the database on a |
| 165 // separate thread. We dispatch to the UI thread if the DB thread doesn't | 149 // separate thread. We dispatch to the UI thread if the DB thread doesn't |
| 166 // exist, which should only happen in tests where there is no DB thread. | 150 // exist, which should only happen in tests where there is no DB thread. |
| 167 template<typename DatabaseFunc> | 151 template<typename DatabaseFunc> |
| 168 void ScheduleAndForget(DatabaseFunc func) { | 152 void ScheduleAndForget(DatabaseFunc func) { |
| 169 BrowserThread::PostTask(dispatch_thread_, | 153 BrowserThread::PostTask(dispatch_thread_, |
| 170 FROM_HERE, | 154 FROM_HERE, |
| 171 base::Bind(func, base::Unretained(db_))); | 155 base::Bind(func, base::Unretained(db_))); |
| 172 } | 156 } |
| 173 | 157 |
| 174 template<typename DatabaseFunc, typename ArgA> | 158 template<typename DatabaseFunc, typename ArgA> |
| 175 void ScheduleAndForget(DatabaseFunc func, ArgA a) { | 159 void ScheduleAndForget(DatabaseFunc func, ArgA a) { |
| 176 BrowserThread::PostTask(dispatch_thread_, | 160 BrowserThread::PostTask(dispatch_thread_, |
| 177 FROM_HERE, | 161 FROM_HERE, |
| 178 base::Bind(func, base::Unretained(db_), a)); | 162 base::Bind(func, base::Unretained(db_), a)); |
| 179 } | 163 } |
| 180 | 164 |
| 181 template<typename DatabaseFunc, typename ArgA, typename ArgB> | 165 template<typename DatabaseFunc, typename ArgA, typename ArgB> |
| 182 void ScheduleAndForget(DatabaseFunc func, ArgA a, ArgB b) { | 166 void ScheduleAndForget(DatabaseFunc func, ArgA a, ArgB b) { |
| 183 BrowserThread::PostTask(dispatch_thread_, | 167 BrowserThread::PostTask(dispatch_thread_, |
| 184 FROM_HERE, | 168 FROM_HERE, |
| 185 base::Bind(func, base::Unretained(db_), a, b)); | 169 base::Bind(func, base::Unretained(db_), a, b)); |
| 186 } | 170 } |
| 187 | 171 |
| 188 typedef ObserverListThreadSafe<Observer> ObserverList; | 172 typedef ObserverListThreadSafe<Observer> ObserverList; |
| 189 typedef std::map<const Extension*, scoped_refptr<ObserverList> > | |
| 190 ObserverMap; | |
| 191 // A map of extensions to activity observers for that extension. | |
| 192 ObserverMap observers_; | |
| 193 | 173 |
| 194 // The database wrapper that does the actual database I/O. | 174 // The database wrapper that does the actual database I/O. |
| 195 // We initialize this on the same thread as the ActivityLog, but then | 175 // We initialize this on the same thread as the ActivityLog, but then |
| 196 // subsequent operations occur on the DB thread. Instead of destructing the | 176 // subsequent operations occur on the DB thread. Instead of destructing the |
| 197 // ActivityDatabase, we call its Close() method on the DB thread and it | 177 // ActivityDatabase, we call its Close() method on the DB thread and it |
| 198 // commits suicide. | 178 // commits suicide. |
| 199 extensions::ActivityDatabase* db_; | 179 extensions::ActivityDatabase* db_; |
| 200 | 180 |
| 201 // Normally the DB thread. In some cases (tests), it might not exist | 181 // Normally the DB thread. In some cases (tests), it might not exist |
| 202 // we dispatch to the UI thread. | 182 // we dispatch to the UI thread. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 243 virtual content::BrowserContext* GetBrowserContextToUse( | 223 virtual content::BrowserContext* GetBrowserContextToUse( |
| 244 content::BrowserContext* context) const OVERRIDE; | 224 content::BrowserContext* context) const OVERRIDE; |
| 245 | 225 |
| 246 DISALLOW_COPY_AND_ASSIGN(ActivityLogFactory); | 226 DISALLOW_COPY_AND_ASSIGN(ActivityLogFactory); |
| 247 }; | 227 }; |
| 248 | 228 |
| 249 | 229 |
| 250 } // namespace extensions | 230 } // namespace extensions |
| 251 | 231 |
| 252 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ | 232 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ |
| OLD | NEW |