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> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/observer_list_threadsafe.h" | 13 #include "base/observer_list_threadsafe.h" |
| 14 #include "base/scoped_observer.h" |
14 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
15 #include "base/threading/thread.h" | 16 #include "base/threading/thread.h" |
16 #include "chrome/browser/extensions/activity_log/activity_actions.h" | 17 #include "chrome/browser/extensions/activity_log/activity_actions.h" |
17 #include "chrome/browser/extensions/activity_log/activity_log_policy.h" | 18 #include "chrome/browser/extensions/activity_log/activity_log_policy.h" |
18 #include "chrome/browser/extensions/install_observer.h" | |
19 #include "chrome/browser/extensions/tab_helper.h" | 19 #include "chrome/browser/extensions/tab_helper.h" |
20 #include "extensions/browser/api_activity_monitor.h" | 20 #include "extensions/browser/api_activity_monitor.h" |
21 #include "extensions/browser/browser_context_keyed_api_factory.h" | 21 #include "extensions/browser/browser_context_keyed_api_factory.h" |
| 22 #include "extensions/browser/extension_registry_observer.h" |
22 #include "extensions/common/dom_action_types.h" | 23 #include "extensions/common/dom_action_types.h" |
23 | 24 |
24 class Profile; | 25 class Profile; |
25 | 26 |
26 namespace content { | 27 namespace content { |
27 class BrowserContext; | 28 class BrowserContext; |
28 } | 29 } |
29 | 30 |
30 namespace user_prefs { | 31 namespace user_prefs { |
31 class PrefRegistrySyncable; | 32 class PrefRegistrySyncable; |
32 } | 33 } |
33 | 34 |
34 namespace extensions { | 35 namespace extensions { |
35 class Extension; | 36 class Extension; |
36 class InstallTracker; | 37 class ExtensionRegistry; |
37 | 38 |
38 // A utility for tracing interesting activity for each extension. | 39 // A utility for tracing interesting activity for each extension. |
39 // It writes to an ActivityDatabase on a separate thread to record the activity. | 40 // It writes to an ActivityDatabase on a separate thread to record the activity. |
40 // Each profile has different extensions, so we keep a different database for | 41 // Each profile has different extensions, so we keep a different database for |
41 // each profile. | 42 // each profile. |
42 class ActivityLog : public BrowserContextKeyedAPI, | 43 class ActivityLog : public BrowserContextKeyedAPI, |
43 public ApiActivityMonitor, | 44 public ApiActivityMonitor, |
44 public TabHelper::ScriptExecutionObserver, | 45 public TabHelper::ScriptExecutionObserver, |
45 public InstallObserver { | 46 public ExtensionRegistryObserver { |
46 public: | 47 public: |
47 // Observers can listen for activity events. There is probably only one | 48 // Observers can listen for activity events. There is probably only one |
48 // observer: the activityLogPrivate API. | 49 // observer: the activityLogPrivate API. |
49 class Observer { | 50 class Observer { |
50 public: | 51 public: |
51 virtual void OnExtensionActivity(scoped_refptr<Action> activity) = 0; | 52 virtual void OnExtensionActivity(scoped_refptr<Action> activity) = 0; |
52 }; | 53 }; |
53 | 54 |
54 static BrowserContextKeyedAPIFactory<ActivityLog>* GetFactoryInstance(); | 55 static BrowserContextKeyedAPIFactory<ActivityLog>* GetFactoryInstance(); |
55 | 56 |
(...skipping 18 matching lines...) Expand all Loading... |
74 void GetFilteredActions( | 75 void GetFilteredActions( |
75 const std::string& extension_id, | 76 const std::string& extension_id, |
76 const Action::ActionType type, | 77 const Action::ActionType type, |
77 const std::string& api_name, | 78 const std::string& api_name, |
78 const std::string& page_url, | 79 const std::string& page_url, |
79 const std::string& arg_url, | 80 const std::string& arg_url, |
80 const int days_ago, | 81 const int days_ago, |
81 const base::Callback | 82 const base::Callback |
82 <void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& callback); | 83 <void(scoped_ptr<std::vector<scoped_refptr<Action> > >)>& callback); |
83 | 84 |
84 // Extension::InstallObserver | 85 // ExtensionRegistryObserver. |
85 // We keep track of whether the whitelisted extension is installed; if it is, | 86 // We keep track of whether the whitelisted extension is installed; if it is, |
86 // we want to recompute whether to have logging enabled. | 87 // we want to recompute whether to have logging enabled. |
87 virtual void OnExtensionLoaded(const Extension* extension) OVERRIDE; | 88 virtual void OnExtensionLoaded(content::BrowserContext* browser_context, |
88 virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE; | 89 const Extension* extension) OVERRIDE; |
89 virtual void OnExtensionUninstalled(const Extension* extension) OVERRIDE; | 90 virtual void OnExtensionUnloaded( |
| 91 content::BrowserContext* browser_context, |
| 92 const Extension* extension, |
| 93 UnloadedExtensionInfo::Reason reason) OVERRIDE; |
| 94 virtual void OnExtensionUninstalled(content::BrowserContext* browser_context, |
| 95 const Extension* extension) OVERRIDE; |
90 | 96 |
91 // ApiActivityMonitor | 97 // ApiActivityMonitor. |
92 virtual void OnApiEventDispatched( | 98 virtual void OnApiEventDispatched( |
93 const std::string& extension_id, | 99 const std::string& extension_id, |
94 const std::string& event_name, | 100 const std::string& event_name, |
95 scoped_ptr<base::ListValue> event_args) OVERRIDE; | 101 scoped_ptr<base::ListValue> event_args) OVERRIDE; |
96 virtual void OnApiFunctionCalled( | 102 virtual void OnApiFunctionCalled( |
97 const std::string& extension_id, | 103 const std::string& extension_id, |
98 const std::string& api_name, | 104 const std::string& api_name, |
99 scoped_ptr<base::ListValue> event_args) OVERRIDE; | 105 scoped_ptr<base::ListValue> event_args) OVERRIDE; |
100 | 106 |
101 // KeyedService | |
102 virtual void Shutdown() OVERRIDE; | |
103 | |
104 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 107 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
105 | 108 |
106 // Remove actions from the activity log database which IDs specified in the | 109 // Remove actions from the activity log database which IDs specified in the |
107 // action_ids array. | 110 // action_ids array. |
108 void RemoveActions(const std::vector<int64>& action_ids); | 111 void RemoveActions(const std::vector<int64>& action_ids); |
109 | 112 |
110 // Clean up URLs from the activity log database. | 113 // Clean up URLs from the activity log database. |
111 // If restrict_urls is empty then all URLs in the activity log database are | 114 // If restrict_urls is empty then all URLs in the activity log database are |
112 // removed, otherwise only those in restrict_urls are removed. | 115 // removed, otherwise only those in restrict_urls are removed. |
113 void RemoveURLs(const std::vector<GURL>& restrict_urls); | 116 void RemoveURLs(const std::vector<GURL>& restrict_urls); |
(...skipping 16 matching lines...) Expand all Loading... |
130 | 133 |
131 // Specifies if the Watchdog app is active (installed & enabled). | 134 // Specifies if the Watchdog app is active (installed & enabled). |
132 // If so, we need to log to the database and stream to the API. | 135 // If so, we need to log to the database and stream to the API. |
133 bool IsWatchdogAppActive(); | 136 bool IsWatchdogAppActive(); |
134 | 137 |
135 // Specifies if we need to record actions to the db. If so, we need to log to | 138 // Specifies if we need to record actions to the db. If so, we need to log to |
136 // the database. This is true if the Watchdog app is active *or* the | 139 // the database. This is true if the Watchdog app is active *or* the |
137 // --enable-extension-activity-logging flag is set. | 140 // --enable-extension-activity-logging flag is set. |
138 bool IsDatabaseEnabled(); | 141 bool IsDatabaseEnabled(); |
139 | 142 |
140 // Delayed initialization of Install Tracker which waits until after the | 143 // Delayed initialization of ExtensionRegistry which waits until after the |
141 // ExtensionSystem/ExtensionService are done with their own setup. | 144 // ExtensionSystem/ExtensionService are done with their own setup. |
142 void InitInstallTracker(); | 145 void StartObserving(); |
143 | 146 |
144 // TabHelper::ScriptExecutionObserver implementation. | 147 // TabHelper::ScriptExecutionObserver implementation. |
145 // Fires when a ContentScript is executed. | 148 // Fires when a ContentScript is executed. |
146 virtual void OnScriptsExecuted( | 149 virtual void OnScriptsExecuted( |
147 const content::WebContents* web_contents, | 150 const content::WebContents* web_contents, |
148 const ExecutingScriptsMap& extension_ids, | 151 const ExecutingScriptsMap& extension_ids, |
149 int32 page_id, | 152 int32 page_id, |
150 const GURL& on_url) OVERRIDE; | 153 const GURL& on_url) OVERRIDE; |
151 | 154 |
152 // At the moment, ActivityLog will use only one policy for summarization. | 155 // At the moment, ActivityLog will use only one policy for summarization. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 // testing_mode_ also causes us to print to the console. | 191 // testing_mode_ also causes us to print to the console. |
189 bool testing_mode_; | 192 bool testing_mode_; |
190 // We need the DB, FILE, and IO threads to write to the database. | 193 // We need the DB, FILE, and IO threads to write to the database. |
191 // In some cases (tests), these threads might not exist, so we avoid | 194 // In some cases (tests), these threads might not exist, so we avoid |
192 // dispatching anything to the policies/database to prevent things from | 195 // dispatching anything to the policies/database to prevent things from |
193 // exploding. | 196 // exploding. |
194 bool has_threads_; | 197 bool has_threads_; |
195 | 198 |
196 // Used to track whether the whitelisted extension is installed. If it's | 199 // Used to track whether the whitelisted extension is installed. If it's |
197 // added or removed, enabled_ may change. | 200 // added or removed, enabled_ may change. |
198 InstallTracker* tracker_; | 201 ScopedObserver<extensions::ExtensionRegistry, |
| 202 extensions::ExtensionRegistryObserver> |
| 203 extension_registry_observer_; |
199 | 204 |
200 // Set if the watchdog app is installed and enabled. Maintained by | 205 // Set if the watchdog app is installed and enabled. Maintained by |
201 // kWatchdogExtensionActive pref variable. Since there are multiple valid | 206 // kWatchdogExtensionActive pref variable. Since there are multiple valid |
202 // extension IDs, this needs to be an int to count how many are installed. | 207 // extension IDs, this needs to be an int to count how many are installed. |
203 int watchdog_apps_active_; | 208 int watchdog_apps_active_; |
204 | 209 |
205 FRIEND_TEST_ALL_PREFIXES(ActivityLogApiTest, TriggerEvent); | 210 FRIEND_TEST_ALL_PREFIXES(ActivityLogApiTest, TriggerEvent); |
206 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, AppAndCommandLine); | 211 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, AppAndCommandLine); |
207 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, CommandLineSwitch); | 212 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, CommandLineSwitch); |
208 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, NoSwitch); | 213 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, NoSwitch); |
209 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, PrefSwitch); | 214 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, PrefSwitch); |
210 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, WatchdogSwitch); | 215 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, WatchdogSwitch); |
211 DISALLOW_COPY_AND_ASSIGN(ActivityLog); | 216 DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
212 }; | 217 }; |
213 | 218 |
214 template <> | 219 template <> |
215 void BrowserContextKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies(); | 220 void BrowserContextKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies(); |
216 | 221 |
217 } // namespace extensions | 222 } // namespace extensions |
218 | 223 |
219 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ | 224 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ |
OLD | NEW |