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 <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <map> | 10 #include <map> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/callback.h" | 14 #include "base/callback.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/macros.h" | 16 #include "base/macros.h" |
17 #include "base/observer_list_threadsafe.h" | 17 #include "base/observer_list_threadsafe.h" |
18 #include "base/scoped_observer.h" | 18 #include "base/scoped_observer.h" |
19 #include "base/synchronization/lock.h" | |
20 #include "base/threading/thread.h" | 19 #include "base/threading/thread.h" |
21 #include "chrome/browser/extensions/activity_log/activity_actions.h" | 20 #include "chrome/browser/extensions/activity_log/activity_actions.h" |
22 #include "chrome/browser/extensions/activity_log/activity_log_policy.h" | 21 #include "chrome/browser/extensions/activity_log/activity_log_policy.h" |
23 #include "extensions/browser/api_activity_monitor.h" | 22 #include "content/public/browser/notification_observer.h" |
| 23 #include "content/public/browser/notification_registrar.h" |
24 #include "extensions/browser/browser_context_keyed_api_factory.h" | 24 #include "extensions/browser/browser_context_keyed_api_factory.h" |
25 #include "extensions/browser/extension_registry_observer.h" | 25 #include "extensions/browser/extension_registry_observer.h" |
26 #include "extensions/browser/script_execution_observer.h" | 26 #include "extensions/browser/script_execution_observer.h" |
27 #include "extensions/common/dom_action_types.h" | 27 #include "extensions/common/dom_action_types.h" |
28 | 28 |
29 class Profile; | 29 class Profile; |
30 | 30 |
31 namespace content { | 31 namespace content { |
32 class BrowserContext; | 32 class BrowserContext; |
33 } | 33 } |
34 | 34 |
35 namespace user_prefs { | 35 namespace user_prefs { |
36 class PrefRegistrySyncable; | 36 class PrefRegistrySyncable; |
37 } | 37 } |
38 | 38 |
39 namespace extensions { | 39 namespace extensions { |
40 class Extension; | 40 class Extension; |
41 class ExtensionRegistry; | 41 class ExtensionRegistry; |
42 | 42 |
43 // A utility for tracing interesting activity for each extension. | 43 // A utility for tracing interesting activity for each extension. |
44 // It writes to an ActivityDatabase on a separate thread to record the activity. | 44 // It writes to an ActivityDatabase on a separate thread to record the activity. |
45 // Each profile has different extensions, so we keep a different database for | 45 // Each profile has different extensions, so we keep a different database for |
46 // each profile. | 46 // each profile. |
47 // | 47 // |
48 class ActivityLog : public BrowserContextKeyedAPI, | 48 class ActivityLog : public BrowserContextKeyedAPI, |
49 public ApiActivityMonitor, | |
50 public ScriptExecutionObserver, | 49 public ScriptExecutionObserver, |
51 public ExtensionRegistryObserver { | 50 public ExtensionRegistryObserver, |
| 51 public content::NotificationObserver { |
52 public: | 52 public: |
53 // Observers can listen for activity events. There is probably only one | 53 // Observers can listen for activity events. There is probably only one |
54 // observer: the activityLogPrivate API. | 54 // observer: the activityLogPrivate API. |
55 class Observer { | 55 class Observer { |
56 public: | 56 public: |
57 virtual void OnExtensionActivity(scoped_refptr<Action> activity) = 0; | 57 virtual void OnExtensionActivity(scoped_refptr<Action> activity) = 0; |
58 }; | 58 }; |
59 | 59 |
60 static BrowserContextKeyedAPIFactory<ActivityLog>* GetFactoryInstance(); | 60 static BrowserContextKeyedAPIFactory<ActivityLog>* GetFactoryInstance(); |
61 | 61 |
62 // ActivityLog is a KeyedService, so don't instantiate it with | 62 // ActivityLog is a KeyedService, so don't instantiate it with |
63 // the constructor; use GetInstance instead. | 63 // the constructor; use GetInstance instead. |
64 static ActivityLog* GetInstance(content::BrowserContext* context); | 64 static ActivityLog* GetInstance(content::BrowserContext* context); |
65 | 65 |
66 // Add/remove observer: the activityLogPrivate API only listens when the | 66 // Add/remove observer: the activityLogPrivate API only listens when the |
67 // ActivityLog extension is registered for an event. | 67 // ActivityLog extension is registered for an event. |
68 void AddObserver(Observer* observer); | 68 void AddObserver(Observer* observer); |
69 void RemoveObserver(Observer* observer); | 69 void RemoveObserver(Observer* observer); |
70 | 70 |
71 // Logs an extension action: passes it to any installed policy to be logged | 71 // Logs an extension action: passes it to any installed policy to be logged |
72 // to the database, to any observers, and logs to the console if in testing | 72 // to the database, to any observers, and logs to the console if in testing |
73 // mode. | 73 // mode. |
74 void LogAction(scoped_refptr<Action> action); | 74 void LogAction(scoped_refptr<Action> action); |
75 | 75 |
| 76 // Returns true if an event for the given extension should be logged. |
| 77 bool ShouldLog(const std::string& extension_id) const; |
| 78 |
76 // Gets all actions that match the specified fields. URLs are treated like | 79 // Gets all actions that match the specified fields. URLs are treated like |
77 // prefixes; other fields are exact matches. Empty strings are not matched to | 80 // prefixes; other fields are exact matches. Empty strings are not matched to |
78 // anything. For daysAgo, today is 0, yesterday is 1, etc.; a negative number | 81 // anything. For daysAgo, today is 0, yesterday is 1, etc.; a negative number |
79 // of days is treated as a missing parameter. | 82 // of days is treated as a missing parameter. |
80 void GetFilteredActions( | 83 void GetFilteredActions( |
81 const std::string& extension_id, | 84 const std::string& extension_id, |
82 const Action::ActionType type, | 85 const Action::ActionType type, |
83 const std::string& api_name, | 86 const std::string& api_name, |
84 const std::string& page_url, | 87 const std::string& page_url, |
85 const std::string& arg_url, | 88 const std::string& arg_url, |
86 const int days_ago, | 89 const int days_ago, |
87 const base::Callback< | 90 const base::Callback< |
88 void(std::unique_ptr<std::vector<scoped_refptr<Action>>>)>& callback); | 91 void(std::unique_ptr<std::vector<scoped_refptr<Action>>>)>& callback); |
89 | 92 |
90 // ExtensionRegistryObserver. | 93 // ExtensionRegistryObserver. |
91 // We keep track of whether the whitelisted extension is installed; if it is, | 94 // We keep track of whether the whitelisted extension is installed; if it is, |
92 // we want to recompute whether to have logging enabled. | 95 // we want to recompute whether to have logging enabled. |
93 void OnExtensionLoaded(content::BrowserContext* browser_context, | 96 void OnExtensionLoaded(content::BrowserContext* browser_context, |
94 const Extension* extension) override; | 97 const Extension* extension) override; |
95 void OnExtensionUnloaded(content::BrowserContext* browser_context, | 98 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
96 const Extension* extension, | 99 const Extension* extension, |
97 UnloadedExtensionInfo::Reason reason) override; | 100 UnloadedExtensionInfo::Reason reason) override; |
98 void OnExtensionUninstalled(content::BrowserContext* browser_context, | 101 void OnExtensionUninstalled(content::BrowserContext* browser_context, |
99 const Extension* extension, | 102 const Extension* extension, |
100 extensions::UninstallReason reason) override; | 103 extensions::UninstallReason reason) override; |
101 | 104 |
102 // ApiActivityMonitor. | |
103 void OnApiEventDispatched( | |
104 const std::string& extension_id, | |
105 const std::string& event_name, | |
106 std::unique_ptr<base::ListValue> event_args) override; | |
107 void OnApiFunctionCalled( | |
108 const std::string& extension_id, | |
109 const std::string& api_name, | |
110 std::unique_ptr<base::ListValue> event_args) override; | |
111 | |
112 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); | 105 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); |
113 | 106 |
114 // Remove actions from the activity log database which IDs specified in the | 107 // Remove actions from the activity log database which IDs specified in the |
115 // action_ids array. | 108 // action_ids array. |
116 void RemoveActions(const std::vector<int64_t>& action_ids); | 109 void RemoveActions(const std::vector<int64_t>& action_ids); |
117 | 110 |
118 // Clean up URLs from the activity log database. | 111 // Clean up URLs from the activity log database. |
119 // If restrict_urls is empty then all URLs in the activity log database are | 112 // If restrict_urls is empty then all URLs in the activity log database are |
120 // removed, otherwise only those in restrict_urls are removed. | 113 // removed, otherwise only those in restrict_urls are removed. |
121 void RemoveURLs(const std::vector<GURL>& restrict_urls); | 114 void RemoveURLs(const std::vector<GURL>& restrict_urls); |
(...skipping 29 matching lines...) Expand all Loading... |
151 const ExecutingScriptsMap& extension_ids, | 144 const ExecutingScriptsMap& extension_ids, |
152 const GURL& on_url) override; | 145 const GURL& on_url) override; |
153 | 146 |
154 // At the moment, ActivityLog will use only one policy for summarization. | 147 // At the moment, ActivityLog will use only one policy for summarization. |
155 // These methods are used to choose and set the most appropriate policy. | 148 // These methods are used to choose and set the most appropriate policy. |
156 // Changing policies at runtime is not recommended, and likely only should be | 149 // Changing policies at runtime is not recommended, and likely only should be |
157 // done for unit tests. | 150 // done for unit tests. |
158 void ChooseDatabasePolicy(); | 151 void ChooseDatabasePolicy(); |
159 void SetDatabasePolicy(ActivityLogPolicy::PolicyType policy_type); | 152 void SetDatabasePolicy(ActivityLogPolicy::PolicyType policy_type); |
160 | 153 |
| 154 // Checks the current |is_active_| state and modifies it if appropriate. |
| 155 void CheckActive(); |
| 156 |
| 157 // content::NotificationObserver: |
| 158 void Observe(int type, |
| 159 const content::NotificationSource& source, |
| 160 const content::NotificationDetails& details) override; |
| 161 |
161 // BrowserContextKeyedAPI implementation. | 162 // BrowserContextKeyedAPI implementation. |
162 static const char* service_name() { return "ActivityLog"; } | 163 static const char* service_name() { return "ActivityLog"; } |
163 static const bool kServiceRedirectedInIncognito = true; | 164 static const bool kServiceRedirectedInIncognito = true; |
164 static const bool kServiceIsCreatedWithBrowserContext = false; | 165 static const bool kServiceIsCreatedWithBrowserContext = false; |
165 | 166 |
166 typedef base::ObserverListThreadSafe<Observer> ObserverList; | 167 typedef base::ObserverListThreadSafe<Observer> ObserverList; |
167 scoped_refptr<ObserverList> observers_; | 168 scoped_refptr<ObserverList> observers_; |
168 | 169 |
169 // Policy objects are owned by the ActivityLog, but cannot be scoped_ptrs | 170 // Policy objects are owned by the ActivityLog, but cannot be scoped_ptrs |
170 // since they may need to do some cleanup work on the database thread. | 171 // since they may need to do some cleanup work on the database thread. |
(...skipping 24 matching lines...) Expand all Loading... |
195 // added or removed, enabled_ may change. | 196 // added or removed, enabled_ may change. |
196 ScopedObserver<extensions::ExtensionRegistry, | 197 ScopedObserver<extensions::ExtensionRegistry, |
197 extensions::ExtensionRegistryObserver> | 198 extensions::ExtensionRegistryObserver> |
198 extension_registry_observer_; | 199 extension_registry_observer_; |
199 | 200 |
200 // Set if the watchdog app is installed and enabled. Maintained by | 201 // Set if the watchdog app is installed and enabled. Maintained by |
201 // kWatchdogExtensionActive pref variable. Since there are multiple valid | 202 // kWatchdogExtensionActive pref variable. Since there are multiple valid |
202 // extension IDs, this needs to be an int to count how many are installed. | 203 // extension IDs, this needs to be an int to count how many are installed. |
203 int watchdog_apps_active_; | 204 int watchdog_apps_active_; |
204 | 205 |
| 206 // True if the activity log is currently active. |
| 207 bool is_active_; |
| 208 |
| 209 content::NotificationRegistrar registrar_; |
| 210 |
205 FRIEND_TEST_ALL_PREFIXES(ActivityLogApiTest, TriggerEvent); | 211 FRIEND_TEST_ALL_PREFIXES(ActivityLogApiTest, TriggerEvent); |
206 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, AppAndCommandLine); | 212 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, AppAndCommandLine); |
207 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, CommandLineSwitch); | 213 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, CommandLineSwitch); |
208 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, NoSwitch); | 214 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, NoSwitch); |
209 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, PrefSwitch); | 215 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, PrefSwitch); |
210 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, WatchdogSwitch); | 216 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, WatchdogSwitch); |
211 DISALLOW_COPY_AND_ASSIGN(ActivityLog); | 217 DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
212 }; | 218 }; |
213 | 219 |
214 template <> | 220 template <> |
215 void BrowserContextKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies(); | 221 void BrowserContextKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies(); |
216 | 222 |
217 } // namespace extensions | 223 } // namespace extensions |
218 | 224 |
219 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ | 225 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ |
OLD | NEW |