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