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_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.h" |
| 20 #include "chrome/browser/profiles/profile_dependency_manager.h" |
| 21 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 22 #include "chrome/browser/profiles/profile_keyed_service_factory.h" |
| 23 #include "content/public/browser/browser_thread.h" |
| 24 |
| 25 class Profile; |
| 26 using content::BrowserThread; |
16 | 27 |
17 namespace extensions { | 28 namespace extensions { |
18 class Extension; | 29 class Extension; |
19 | 30 |
20 // A utility for tracing interesting activity for each extension. | 31 // A utility for tracing interesting activity for each extension. |
21 class ActivityLog : public TabHelper::ScriptExecutionObserver { | 32 // It writes to an ActivityDatabase on a separate thread to record the activity. |
| 33 class ActivityLog : public ProfileKeyedService, |
| 34 public TabHelper::ScriptExecutionObserver { |
22 public: | 35 public: |
23 enum Activity { | 36 enum Activity { |
24 ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. | 37 ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. |
25 ACTIVITY_EXTENSION_API_BLOCK, // Extension API invocation is blocked. | 38 ACTIVITY_EXTENSION_API_BLOCK, // Extension API invocation is blocked. |
26 ACTIVITY_CONTENT_SCRIPT // Content script is executing. | 39 ACTIVITY_CONTENT_SCRIPT // Content script is executing. |
27 }; | 40 }; |
28 | 41 |
29 // Observers can listen for activity events. | 42 // Observers can listen for activity events. |
30 class Observer { | 43 class Observer { |
31 public: | 44 public: |
32 virtual void OnExtensionActivity( | 45 virtual void OnExtensionActivity( |
33 const Extension* extension, | 46 const Extension* extension, |
34 Activity activity, | 47 Activity activity, |
35 const std::vector<std::string>& messages) = 0; | 48 const std::vector<std::string>& messages) = 0; |
36 }; | 49 }; |
37 | 50 |
38 virtual ~ActivityLog(); | 51 // ActivityLog is a singleton, so don't instantiate it with the constructor; |
39 static ActivityLog* GetInstance(); | 52 // use GetInstance instead. |
| 53 static ActivityLog* GetInstance(Profile* profile); |
40 | 54 |
41 // Add/remove observer. | 55 // Add/remove observer. |
42 void AddObserver(const Extension* extension, Observer* observer); | 56 void AddObserver(const Extension* extension, Observer* observer); |
43 void RemoveObserver(const Extension* extension, | 57 void RemoveObserver(const Extension* extension, |
44 Observer* observer); | 58 Observer* observer); |
45 | 59 |
46 // Check for the existence observer list by extension_id. | 60 // Check for the existence observer list by extension_id. |
47 bool HasObservers(const Extension* extension) const; | 61 bool HasObservers(const Extension* extension) const; |
48 | 62 |
49 // Log |activity| for |extension|. | 63 // Log a successful API call made by an extension. |
50 void Log(const Extension* extension, | 64 void LogAPIAction(const Extension* extension, |
51 Activity activity, | 65 const std::string& name, |
52 const std::string& message) const; | 66 const ListValue* args); |
53 void Log(const Extension* extension, | 67 |
54 Activity activity, | 68 // Log a blocked API call made by an extension. |
55 const std::vector<std::string>& messages) const; | 69 void LogBlockedAction(const Extension* extension, |
| 70 const std::string& blocked_call, |
| 71 const ListValue* args, |
| 72 const char* reason); |
| 73 |
| 74 // Log an interaction between an extension and a URL. |
| 75 // The message might be the list of content scripts that have been injected, |
| 76 // or the list of DOM nodes that have been touched. Either way, the message |
| 77 // is not intended to be shown to average users. |
| 78 void LogUrlAction(const Extension* extension, |
| 79 const GURL& url, |
| 80 const string16& url_title, |
| 81 std::string& message, |
| 82 const UrlAction::UrlActionType verb); |
| 83 |
| 84 // An error has happened; we want to rollback and close the db. |
| 85 // Needs to be public so the error delegate can call it. |
| 86 void KillActivityLogDatabase(); |
56 | 87 |
57 private: | 88 private: |
58 ActivityLog(); | 89 friend class ActivityLogFactory; |
59 friend struct DefaultSingletonTraits<ActivityLog>; | 90 |
| 91 explicit ActivityLog(Profile* profile); |
| 92 virtual ~ActivityLog(); |
60 | 93 |
61 // TabHelper::ScriptExecutionObserver implementation. | 94 // TabHelper::ScriptExecutionObserver implementation. |
| 95 // Fires when a ContentScript is executed. |
62 virtual void OnScriptsExecuted( | 96 virtual void OnScriptsExecuted( |
63 const content::WebContents* web_contents, | 97 const content::WebContents* web_contents, |
64 const ExecutingScriptsMap& extension_ids, | 98 const ExecutingScriptsMap& extension_ids, |
65 int32 page_id, | 99 int32 page_id, |
66 const GURL& on_url) OVERRIDE; | 100 const GURL& on_url) OVERRIDE; |
67 | 101 |
68 static const char* ActivityToString(Activity activity); | 102 static const char* ActivityToString(Activity activity); |
69 | 103 |
70 // A lock used to synchronize access to member variables. | 104 // The ScheduleAndForget methods dispatch the calls to the database on a |
71 mutable base::Lock lock_; | 105 // separate thread. |
| 106 template<typename DatabaseFunc> |
| 107 void ScheduleAndForget(DatabaseFunc func) { |
| 108 if (db_.get()) |
| 109 BrowserThread::PostTask(BrowserThread::DB, |
| 110 FROM_HERE, |
| 111 base::Bind(func, db_.get())); |
| 112 } |
72 | 113 |
73 // Whether to log activity to stdout. This is set by checking the | 114 template<typename DatabaseFunc, typename ArgA> |
74 // enable-extension-activity-logging switch. | 115 void ScheduleAndForget(DatabaseFunc func, ArgA a) { |
75 bool log_activity_to_stdout_; | 116 if (db_.get()) |
| 117 BrowserThread::PostTask(BrowserThread::DB, |
| 118 FROM_HERE, |
| 119 base::Bind(func, db_.get(), a)); |
| 120 } |
76 | 121 |
77 typedef ObserverListThreadSafe<Observer> ObserverList; | 122 typedef ObserverListThreadSafe<Observer> ObserverList; |
78 typedef std::map<const Extension*, scoped_refptr<ObserverList> > | 123 typedef std::map<const Extension*, scoped_refptr<ObserverList> > |
79 ObserverMap; | 124 ObserverMap; |
80 // A map of extensions to activity observers for that extension. | 125 // A map of extensions to activity observers for that extension. |
81 ObserverMap observers_; | 126 ObserverMap observers_; |
82 | 127 |
| 128 // The database wrapper that does the actual database I/O. |
| 129 scoped_refptr<extensions::ActivityDatabase> db_; |
| 130 |
| 131 // A lock used to synchronize access to member variables. |
| 132 mutable base::Lock lock_; |
| 133 |
| 134 // Whether to log activity to stdout. This is set by checking the |
| 135 // enable-extension-activity-logging switch. |
| 136 bool log_activity_to_stdout_; |
| 137 |
83 DISALLOW_COPY_AND_ASSIGN(ActivityLog); | 138 DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
84 }; | 139 }; |
85 | 140 |
| 141 // Each profile has different extensions, so we keep a different database for |
| 142 // each profile. |
| 143 class ActivityLogFactory : public ProfileKeyedServiceFactory { |
| 144 public: |
| 145 static ActivityLog* GetForProfile(Profile* profile) { |
| 146 return static_cast<ActivityLog*>( |
| 147 GetInstance()->GetServiceForProfile(profile, true)); |
| 148 } |
| 149 |
| 150 static ActivityLogFactory* GetInstance(); |
| 151 |
| 152 private: |
| 153 friend struct DefaultSingletonTraits<ActivityLogFactory>; |
| 154 ActivityLogFactory() |
| 155 : ProfileKeyedServiceFactory("ActivityLog", |
| 156 ProfileDependencyManager::GetInstance()) {} |
| 157 virtual ~ActivityLogFactory() {} |
| 158 |
| 159 virtual ProfileKeyedService* BuildServiceInstanceFor( |
| 160 Profile* profile) const OVERRIDE; |
| 161 |
| 162 virtual bool ServiceRedirectedInIncognito() const OVERRIDE; |
| 163 |
| 164 DISALLOW_COPY_AND_ASSIGN(ActivityLogFactory); |
| 165 }; |
| 166 |
| 167 |
86 } // namespace extensions | 168 } // namespace extensions |
87 | 169 |
88 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ | 170 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ |
OLD | NEW |