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::string& message) = 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 // This will create an APIAction for storage in the database. |
51 Activity activity, | 65 void LogAPIAction(const Extension* extension, |
52 const std::string& message) const; | 66 const std::string& name, // e.g., chrome.tabs.get |
53 void Log(const Extension* extension, | 67 const ListValue* args, // the argument values e.g. 46 |
54 Activity activity, | 68 const std::string& extra); // any extra logging info |
55 const std::vector<std::string>& messages) const; | 69 |
| 70 // Log a blocked API call made by an extension. |
| 71 // This will create a BlockedAction for storage in the database. |
| 72 void LogBlockedAction(const Extension* extension, |
| 73 const std::string& blocked_call, // eg chrome.tabs.get |
| 74 const ListValue* args, // argument values |
| 75 const char* reason, // why it's blocked |
| 76 const std::string& extra); // extra logging info |
| 77 |
| 78 // Log an interaction between an extension and a URL. |
| 79 // This will create a UrlAction for storage in the database. |
| 80 // The technical message might be the list of content scripts that have been |
| 81 // injected, or the DOM API call; it's what's shown under "More". |
| 82 void LogUrlAction(const Extension* extension, |
| 83 const UrlAction::UrlActionType verb, // eg XHR |
| 84 const GURL& url, // target URL |
| 85 const string16& url_title, // title of the URL, |
| 86 // can be empty string |
| 87 const std::string& technical_message, // "More" |
| 88 const std::string& extra); // extra logging info |
| 89 |
| 90 // An error has happened; we want to rollback and close the db. |
| 91 // Needs to be public so the error delegate can call it. |
| 92 void KillActivityLogDatabase(); |
56 | 93 |
57 private: | 94 private: |
58 ActivityLog(); | 95 friend class ActivityLogFactory; |
59 friend struct DefaultSingletonTraits<ActivityLog>; | 96 |
| 97 explicit ActivityLog(Profile* profile); |
| 98 virtual ~ActivityLog(); |
60 | 99 |
61 // TabHelper::ScriptExecutionObserver implementation. | 100 // TabHelper::ScriptExecutionObserver implementation. |
| 101 // Fires when a ContentScript is executed. |
62 virtual void OnScriptsExecuted( | 102 virtual void OnScriptsExecuted( |
63 const content::WebContents* web_contents, | 103 const content::WebContents* web_contents, |
64 const ExecutingScriptsMap& extension_ids, | 104 const ExecutingScriptsMap& extension_ids, |
65 int32 page_id, | 105 int32 page_id, |
66 const GURL& on_url) OVERRIDE; | 106 const GURL& on_url) OVERRIDE; |
67 | 107 |
| 108 // The callback when initializing the database. |
| 109 void OnDBInitComplete(); |
| 110 |
68 static const char* ActivityToString(Activity activity); | 111 static const char* ActivityToString(Activity activity); |
69 | 112 |
70 // A lock used to synchronize access to member variables. | 113 // The Schedule methods dispatch the calls to the database on a |
71 mutable base::Lock lock_; | 114 // separate thread. |
| 115 template<typename DatabaseFunc> |
| 116 void ScheduleAndForget(DatabaseFunc func) { |
| 117 if (db_.get()) |
| 118 BrowserThread::PostTask(BrowserThread::DB, |
| 119 FROM_HERE, |
| 120 base::Bind(func, db_.get())); |
| 121 } |
72 | 122 |
73 // Whether to log activity to stdout. This is set by checking the | 123 template<typename DatabaseFunc, typename ArgA> |
74 // enable-extension-activity-logging switch. | 124 void ScheduleAndForget(DatabaseFunc func, ArgA a) { |
75 bool log_activity_to_stdout_; | 125 if (db_.get()) |
| 126 BrowserThread::PostTask(BrowserThread::DB, |
| 127 FROM_HERE, |
| 128 base::Bind(func, db_.get(), a)); |
| 129 } |
| 130 |
| 131 template<typename DatabaseFunc, typename ArgA, typename ArgB> |
| 132 void ScheduleAndForget(DatabaseFunc func, ArgA a, ArgB b) { |
| 133 if (db_.get()) |
| 134 BrowserThread::PostTask(BrowserThread::DB, |
| 135 FROM_HERE, |
| 136 base::Bind(func, db_.get(), a, b)); |
| 137 } |
76 | 138 |
77 typedef ObserverListThreadSafe<Observer> ObserverList; | 139 typedef ObserverListThreadSafe<Observer> ObserverList; |
78 typedef std::map<const Extension*, scoped_refptr<ObserverList> > | 140 typedef std::map<const Extension*, scoped_refptr<ObserverList> > |
79 ObserverMap; | 141 ObserverMap; |
80 // A map of extensions to activity observers for that extension. | 142 // A map of extensions to activity observers for that extension. |
81 ObserverMap observers_; | 143 ObserverMap observers_; |
82 | 144 |
| 145 // The database wrapper that does the actual database I/O. |
| 146 scoped_refptr<extensions::ActivityDatabase> db_; |
| 147 |
| 148 // Whether to log activity to stdout. This is set by checking the |
| 149 // enable-extension-activity-logging switch. |
| 150 bool log_activity_to_stdout_; |
| 151 |
83 DISALLOW_COPY_AND_ASSIGN(ActivityLog); | 152 DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
84 }; | 153 }; |
85 | 154 |
| 155 // Each profile has different extensions, so we keep a different database for |
| 156 // each profile. |
| 157 class ActivityLogFactory : public ProfileKeyedServiceFactory { |
| 158 public: |
| 159 static ActivityLog* GetForProfile(Profile* profile) { |
| 160 return static_cast<ActivityLog*>( |
| 161 GetInstance()->GetServiceForProfile(profile, true)); |
| 162 } |
| 163 |
| 164 static ActivityLogFactory* GetInstance(); |
| 165 |
| 166 private: |
| 167 friend struct DefaultSingletonTraits<ActivityLogFactory>; |
| 168 ActivityLogFactory() |
| 169 : ProfileKeyedServiceFactory("ActivityLog", |
| 170 ProfileDependencyManager::GetInstance()) {} |
| 171 virtual ~ActivityLogFactory() {} |
| 172 |
| 173 virtual ProfileKeyedService* BuildServiceInstanceFor( |
| 174 Profile* profile) const OVERRIDE; |
| 175 |
| 176 virtual bool ServiceRedirectedInIncognito() const OVERRIDE; |
| 177 |
| 178 DISALLOW_COPY_AND_ASSIGN(ActivityLogFactory); |
| 179 }; |
| 180 |
| 181 |
86 } // namespace extensions | 182 } // namespace extensions |
87 | 183 |
88 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ | 184 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ |
| 185 |
OLD | NEW |