Index: chrome/browser/extensions/activity_log.h |
=================================================================== |
--- chrome/browser/extensions/activity_log.h (revision 172624) |
+++ chrome/browser/extensions/activity_log.h (working copy) |
@@ -9,16 +9,24 @@ |
#include <string> |
#include <vector> |
+#include "base/bind.h" |
#include "base/memory/singleton.h" |
#include "base/observer_list_threadsafe.h" |
#include "base/synchronization/lock.h" |
+#include "base/threading/thread.h" |
+#include "chrome/browser/extensions/activity_database.h" |
#include "chrome/browser/extensions/tab_helper.h" |
+#include "chrome/browser/profiles/profile_keyed_service.h" |
+class Profile; |
+ |
namespace extensions { |
class Extension; |
// A utility for tracing interesting activity for each extension. |
-class ActivityLog : public TabHelper::ScriptExecutionObserver { |
+// It writes to an ActivityDatabase on a separate thread to record the activity. |
+class ActivityLog : public ProfileKeyedService, |
+ public TabHelper::ScriptExecutionObserver { |
public: |
enum Activity { |
ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. |
@@ -35,8 +43,12 @@ |
const std::vector<std::string>& messages) = 0; |
}; |
+ // ActivityLog is a singleton, so don't instantiate it with the constructor; |
+ // use GetInstance instead. |
+ explicit ActivityLog(Profile* profile); |
Eric Dingle
2012/12/13 20:37:34
It's lousy that you have to make the constructor p
felt
2012/12/15 02:51:52
Good idea.
|
+ ActivityLog(); |
Eric Dingle
2012/12/13 20:37:34
You can omit the default constructor if there's no
felt
2012/12/15 02:51:52
Done.
|
virtual ~ActivityLog(); |
- static ActivityLog* GetInstance(); |
+ static ActivityLog* GetInstance(Profile* profile); |
// Add/remove observer. |
void AddObserver(const Extension* extension, Observer* observer); |
@@ -46,19 +58,34 @@ |
// Check for the existence observer list by extension_id. |
bool HasObservers(const Extension* extension) const; |
- // Log |activity| for |extension|. |
- void Log(const Extension* extension, |
- Activity activity, |
- const std::string& message) const; |
- void Log(const Extension* extension, |
- Activity activity, |
- const std::vector<std::string>& messages) const; |
+ // Log an API call made by an extension. |
+ void LogManagerAction(const Extension* extension, |
Eric Dingle
2012/12/13 20:37:34
Maybe this is just me, but the name of the method
felt
2012/12/15 02:51:52
OK, I renamed it to be LogAPIAction.
|
+ const std::string& name, |
+ const ListValue& args); |
+ // Log a blocked API call made by an extension. |
+ void LogBlockedAction(const Extension* extension, |
+ const std::string& blocked_call, |
+ const ListValue& args, |
+ const char* reason); |
+ |
+ // Log an interaction between an extension and a URL. |
+ // The message might be the list of content scripts that have been injected, |
+ // or the list of DOM nodes that have been touched. Either way, the message |
+ // is not intended to be shown to average users. |
+ void LogUrlAction(const Extension* extension, |
+ const GURL& url, |
+ const string16& url_title, |
+ std::string& message, |
+ const UrlAction::UrlActionType verb); |
+ |
+ // An error has happened; we want to rollback and close the db. |
+ // Needs to be public so the error delegate can call it. |
+ void KillActivityLogDatabase(); |
+ |
private: |
- ActivityLog(); |
- friend struct DefaultSingletonTraits<ActivityLog>; |
Eric Dingle
2012/12/13 20:37:34
Is this not required anymore?
felt
2012/12/15 02:51:52
It's in the ActivityLogFactory now.
|
- |
// TabHelper::ScriptExecutionObserver implementation. |
+ // Fires when a ContentScript is executed. |
virtual void OnScriptsExecuted( |
const content::WebContents* web_contents, |
const ExecutingScriptsMap& extension_ids, |
@@ -67,12 +94,24 @@ |
static const char* ActivityToString(Activity activity); |
- // A lock used to synchronize access to member variables. |
- mutable base::Lock lock_; |
+ // The ScheduleAndForget methods dispatch the calls to the database on a |
+ // separate thread. |
+ template<typename DatabaseFunc> |
+ void ScheduleAndForget(DatabaseFunc func) { |
+ CHECK(thread_); |
+ CHECK(thread_->message_loop()); |
+ if (db_.get()) |
+ thread_->message_loop()->PostTask(FROM_HERE, base::Bind(func, db_.get())); |
+ } |
- // Whether to log activity to stdout. This is set by checking the |
- // enable-extension-activity-logging switch. |
- bool log_activity_to_stdout_; |
+ template<typename DatabaseFunc, typename ArgA> |
+ void ScheduleAndForget(DatabaseFunc func, ArgA a) { |
+ CHECK(thread_); |
+ CHECK(thread_->message_loop()); |
+ if (db_.get()) |
+ thread_->message_loop()->PostTask(FROM_HERE, |
+ base::Bind(func, db_.get(), a)); |
+ } |
typedef ObserverListThreadSafe<Observer> ObserverList; |
typedef std::map<const Extension*, scoped_refptr<ObserverList> > |
@@ -80,6 +119,19 @@ |
// A map of extensions to activity observers for that extension. |
ObserverMap observers_; |
+ // We use another thread for db operations (no I/O on main thread). |
+ base::Thread* thread_; |
Eric Dingle
2012/12/13 20:37:34
You should use a scoped_ptr for this.
felt
2012/12/15 02:51:52
Done.
|
+ |
+ // The database wrapper that does the actual database I/O. |
+ scoped_refptr<extensions::ActivityDatabase> db_; |
+ |
+ // A lock used to synchronize access to member variables. |
+ mutable base::Lock lock_; |
+ |
+ // Whether to log activity to stdout. This is set by checking the |
+ // enable-extension-activity-logging switch. |
+ bool log_activity_to_stdout_; |
+ |
DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
}; |