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. |
|
Eric Dingle
2012/12/13 20:37:34
In general, I feel like you've introduced a discon
felt
2012/12/15 02:51:52
I'm planning on getting rid of the enum and modify
| |
| 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_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_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" | |
| 12 #include "base/memory/singleton.h" | 13 #include "base/memory/singleton.h" |
| 13 #include "base/observer_list_threadsafe.h" | 14 #include "base/observer_list_threadsafe.h" |
| 14 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/thread.h" | |
| 17 #include "chrome/browser/extensions/activity_database.h" | |
| 15 #include "chrome/browser/extensions/tab_helper.h" | 18 #include "chrome/browser/extensions/tab_helper.h" |
| 19 #include "chrome/browser/profiles/profile_keyed_service.h" | |
| 20 | |
| 21 class Profile; | |
| 16 | 22 |
| 17 namespace extensions { | 23 namespace extensions { |
| 18 class Extension; | 24 class Extension; |
| 19 | 25 |
| 20 // A utility for tracing interesting activity for each extension. | 26 // A utility for tracing interesting activity for each extension. |
| 21 class ActivityLog : public TabHelper::ScriptExecutionObserver { | 27 // It writes to an ActivityDatabase on a separate thread to record the activity. |
| 28 class ActivityLog : public ProfileKeyedService, | |
| 29 public TabHelper::ScriptExecutionObserver { | |
| 22 public: | 30 public: |
| 23 enum Activity { | 31 enum Activity { |
| 24 ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. | 32 ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. |
| 25 ACTIVITY_EXTENSION_API_BLOCK, // Extension API invocation is blocked. | 33 ACTIVITY_EXTENSION_API_BLOCK, // Extension API invocation is blocked. |
| 26 ACTIVITY_CONTENT_SCRIPT // Content script is executing. | 34 ACTIVITY_CONTENT_SCRIPT // Content script is executing. |
| 27 }; | 35 }; |
| 28 | 36 |
| 29 // Observers can listen for activity events. | 37 // Observers can listen for activity events. |
| 30 class Observer { | 38 class Observer { |
| 31 public: | 39 public: |
| 32 virtual void OnExtensionActivity( | 40 virtual void OnExtensionActivity( |
| 33 const Extension* extension, | 41 const Extension* extension, |
| 34 Activity activity, | 42 Activity activity, |
| 35 const std::vector<std::string>& messages) = 0; | 43 const std::vector<std::string>& messages) = 0; |
| 36 }; | 44 }; |
| 37 | 45 |
| 46 // ActivityLog is a singleton, so don't instantiate it with the constructor; | |
| 47 // use GetInstance instead. | |
| 48 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.
| |
| 49 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.
| |
| 38 virtual ~ActivityLog(); | 50 virtual ~ActivityLog(); |
| 39 static ActivityLog* GetInstance(); | 51 static ActivityLog* GetInstance(Profile* profile); |
| 40 | 52 |
| 41 // Add/remove observer. | 53 // Add/remove observer. |
| 42 void AddObserver(const Extension* extension, Observer* observer); | 54 void AddObserver(const Extension* extension, Observer* observer); |
| 43 void RemoveObserver(const Extension* extension, | 55 void RemoveObserver(const Extension* extension, |
| 44 Observer* observer); | 56 Observer* observer); |
| 45 | 57 |
| 46 // Check for the existence observer list by extension_id. | 58 // Check for the existence observer list by extension_id. |
| 47 bool HasObservers(const Extension* extension) const; | 59 bool HasObservers(const Extension* extension) const; |
| 48 | 60 |
| 49 // Log |activity| for |extension|. | 61 // Log an API call made by an extension. |
| 50 void Log(const Extension* extension, | 62 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.
| |
| 51 Activity activity, | 63 const std::string& name, |
| 52 const std::string& message) const; | 64 const ListValue& args); |
| 53 void Log(const Extension* extension, | 65 |
| 54 Activity activity, | 66 // Log a blocked API call made by an extension. |
| 55 const std::vector<std::string>& messages) const; | 67 void LogBlockedAction(const Extension* extension, |
| 68 const std::string& blocked_call, | |
| 69 const ListValue& args, | |
| 70 const char* reason); | |
| 71 | |
| 72 // Log an interaction between an extension and a URL. | |
| 73 // The message might be the list of content scripts that have been injected, | |
| 74 // or the list of DOM nodes that have been touched. Either way, the message | |
| 75 // is not intended to be shown to average users. | |
| 76 void LogUrlAction(const Extension* extension, | |
| 77 const GURL& url, | |
| 78 const string16& url_title, | |
| 79 std::string& message, | |
| 80 const UrlAction::UrlActionType verb); | |
| 81 | |
| 82 // An error has happened; we want to rollback and close the db. | |
| 83 // Needs to be public so the error delegate can call it. | |
| 84 void KillActivityLogDatabase(); | |
| 56 | 85 |
| 57 private: | 86 private: |
| 58 ActivityLog(); | |
| 59 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.
| |
| 60 | |
| 61 // TabHelper::ScriptExecutionObserver implementation. | 87 // TabHelper::ScriptExecutionObserver implementation. |
| 88 // Fires when a ContentScript is executed. | |
| 62 virtual void OnScriptsExecuted( | 89 virtual void OnScriptsExecuted( |
| 63 const content::WebContents* web_contents, | 90 const content::WebContents* web_contents, |
| 64 const ExecutingScriptsMap& extension_ids, | 91 const ExecutingScriptsMap& extension_ids, |
| 65 int32 page_id, | 92 int32 page_id, |
| 66 const GURL& on_url) OVERRIDE; | 93 const GURL& on_url) OVERRIDE; |
| 67 | 94 |
| 68 static const char* ActivityToString(Activity activity); | 95 static const char* ActivityToString(Activity activity); |
| 69 | 96 |
| 70 // A lock used to synchronize access to member variables. | 97 // The ScheduleAndForget methods dispatch the calls to the database on a |
| 71 mutable base::Lock lock_; | 98 // separate thread. |
| 99 template<typename DatabaseFunc> | |
| 100 void ScheduleAndForget(DatabaseFunc func) { | |
| 101 CHECK(thread_); | |
| 102 CHECK(thread_->message_loop()); | |
| 103 if (db_.get()) | |
| 104 thread_->message_loop()->PostTask(FROM_HERE, base::Bind(func, db_.get())); | |
| 105 } | |
| 72 | 106 |
| 73 // Whether to log activity to stdout. This is set by checking the | 107 template<typename DatabaseFunc, typename ArgA> |
| 74 // enable-extension-activity-logging switch. | 108 void ScheduleAndForget(DatabaseFunc func, ArgA a) { |
| 75 bool log_activity_to_stdout_; | 109 CHECK(thread_); |
| 110 CHECK(thread_->message_loop()); | |
| 111 if (db_.get()) | |
| 112 thread_->message_loop()->PostTask(FROM_HERE, | |
| 113 base::Bind(func, db_.get(), a)); | |
| 114 } | |
| 76 | 115 |
| 77 typedef ObserverListThreadSafe<Observer> ObserverList; | 116 typedef ObserverListThreadSafe<Observer> ObserverList; |
| 78 typedef std::map<const Extension*, scoped_refptr<ObserverList> > | 117 typedef std::map<const Extension*, scoped_refptr<ObserverList> > |
| 79 ObserverMap; | 118 ObserverMap; |
| 80 // A map of extensions to activity observers for that extension. | 119 // A map of extensions to activity observers for that extension. |
| 81 ObserverMap observers_; | 120 ObserverMap observers_; |
| 82 | 121 |
| 122 // We use another thread for db operations (no I/O on main thread). | |
| 123 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.
| |
| 124 | |
| 125 // The database wrapper that does the actual database I/O. | |
| 126 scoped_refptr<extensions::ActivityDatabase> db_; | |
| 127 | |
| 128 // A lock used to synchronize access to member variables. | |
| 129 mutable base::Lock lock_; | |
| 130 | |
| 131 // Whether to log activity to stdout. This is set by checking the | |
| 132 // enable-extension-activity-logging switch. | |
| 133 bool log_activity_to_stdout_; | |
| 134 | |
| 83 DISALLOW_COPY_AND_ASSIGN(ActivityLog); | 135 DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
| 84 }; | 136 }; |
| 85 | 137 |
| 86 } // namespace extensions | 138 } // namespace extensions |
| 87 | 139 |
| 88 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ | 140 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ |
| OLD | NEW |