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/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
15 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
16 #include "chrome/browser/extensions/activity_log/activity_actions.h" | 16 #include "chrome/browser/extensions/activity_log/activity_actions.h" |
17 #include "chrome/browser/extensions/activity_log/activity_log_policy.h" | 17 #include "chrome/browser/extensions/activity_log/activity_log_policy.h" |
18 #include "chrome/browser/extensions/api/profile_keyed_api_factory.h" | |
19 #include "chrome/browser/extensions/install_observer.h" | 18 #include "chrome/browser/extensions/install_observer.h" |
20 #include "chrome/browser/extensions/tab_helper.h" | 19 #include "chrome/browser/extensions/tab_helper.h" |
21 #include "chrome/common/extensions/dom_action_types.h" | 20 #include "chrome/common/extensions/dom_action_types.h" |
22 #include "extensions/browser/api_activity_monitor.h" | 21 #include "extensions/browser/api_activity_monitor.h" |
| 22 #include "extensions/browser/browser_context_keyed_api_factory.h" |
23 | 23 |
24 class Profile; | 24 class Profile; |
25 | 25 |
26 namespace content { | 26 namespace content { |
27 class BrowserContext; | 27 class BrowserContext; |
28 } | 28 } |
29 | 29 |
30 namespace user_prefs { | 30 namespace user_prefs { |
31 class PrefRegistrySyncable; | 31 class PrefRegistrySyncable; |
32 } | 32 } |
33 | 33 |
34 namespace extensions { | 34 namespace extensions { |
35 class Extension; | 35 class Extension; |
36 class InstallTracker; | 36 class InstallTracker; |
37 | 37 |
38 // A utility for tracing interesting activity for each extension. | 38 // A utility for tracing interesting activity for each extension. |
39 // It writes to an ActivityDatabase on a separate thread to record the activity. | 39 // 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 | 40 // Each profile has different extensions, so we keep a different database for |
41 // each profile. | 41 // each profile. |
42 class ActivityLog : public ProfileKeyedAPI, | 42 class ActivityLog : public BrowserContextKeyedAPI, |
43 public ApiActivityMonitor, | 43 public ApiActivityMonitor, |
44 public TabHelper::ScriptExecutionObserver, | 44 public TabHelper::ScriptExecutionObserver, |
45 public InstallObserver { | 45 public InstallObserver { |
46 public: | 46 public: |
47 // Observers can listen for activity events. There is probably only one | 47 // Observers can listen for activity events. There is probably only one |
48 // observer: the activityLogPrivate API. | 48 // observer: the activityLogPrivate API. |
49 class Observer { | 49 class Observer { |
50 public: | 50 public: |
51 virtual void OnExtensionActivity(scoped_refptr<Action> activity) = 0; | 51 virtual void OnExtensionActivity(scoped_refptr<Action> activity) = 0; |
52 }; | 52 }; |
53 | 53 |
54 static ProfileKeyedAPIFactory<ActivityLog>* GetFactoryInstance(); | 54 static BrowserContextKeyedAPIFactory<ActivityLog>* GetFactoryInstance(); |
55 | 55 |
56 // ActivityLog is a BrowserContextKeyedService, so don't instantiate it with | 56 // ActivityLog is a BrowserContextKeyedService, so don't instantiate it with |
57 // the constructor; use GetInstance instead. | 57 // the constructor; use GetInstance instead. |
58 static ActivityLog* GetInstance(content::BrowserContext* context); | 58 static ActivityLog* GetInstance(content::BrowserContext* context); |
59 | 59 |
60 // Add/remove observer: the activityLogPrivate API only listens when the | 60 // Add/remove observer: the activityLogPrivate API only listens when the |
61 // ActivityLog extension is registered for an event. | 61 // ActivityLog extension is registered for an event. |
62 void AddObserver(Observer* observer); | 62 void AddObserver(Observer* observer); |
63 void RemoveObserver(Observer* observer); | 63 void RemoveObserver(Observer* observer); |
64 | 64 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 // removed, otherwise only those in restrict_urls are removed. | 112 // removed, otherwise only those in restrict_urls are removed. |
113 void RemoveURLs(const std::vector<GURL>& restrict_urls); | 113 void RemoveURLs(const std::vector<GURL>& restrict_urls); |
114 void RemoveURLs(const std::set<GURL>& restrict_urls); | 114 void RemoveURLs(const std::set<GURL>& restrict_urls); |
115 void RemoveURL(const GURL& url); | 115 void RemoveURL(const GURL& url); |
116 | 116 |
117 // Deletes the database associated with the policy that's currently in use. | 117 // Deletes the database associated with the policy that's currently in use. |
118 void DeleteDatabase(); | 118 void DeleteDatabase(); |
119 | 119 |
120 private: | 120 private: |
121 friend class ActivityLogTest; | 121 friend class ActivityLogTest; |
122 friend class ProfileKeyedAPIFactory<ActivityLog>; | 122 friend class BrowserContextKeyedAPIFactory<ActivityLog>; |
123 friend class RenderViewActivityLogTest; | 123 friend class RenderViewActivityLogTest; |
124 | 124 |
125 explicit ActivityLog(content::BrowserContext* context); | 125 explicit ActivityLog(content::BrowserContext* context); |
126 virtual ~ActivityLog(); | 126 virtual ~ActivityLog(); |
127 | 127 |
128 // Specifies if the Watchdog app is active (installed & enabled). | 128 // Specifies if the Watchdog app is active (installed & enabled). |
129 // If so, we need to log to the database and stream to the API. | 129 // If so, we need to log to the database and stream to the API. |
130 bool IsWatchdogAppActive(); | 130 bool IsWatchdogAppActive(); |
131 // If we're in a browser test, we need to pretend that the watchdog app is | 131 // If we're in a browser test, we need to pretend that the watchdog app is |
132 // active. | 132 // active. |
(...skipping 16 matching lines...) Expand all Loading... |
149 int32 page_id, | 149 int32 page_id, |
150 const GURL& on_url) OVERRIDE; | 150 const GURL& on_url) OVERRIDE; |
151 | 151 |
152 // At the moment, ActivityLog will use only one policy for summarization. | 152 // At the moment, ActivityLog will use only one policy for summarization. |
153 // These methods are used to choose and set the most appropriate policy. | 153 // These methods are used to choose and set the most appropriate policy. |
154 // Changing policies at runtime is not recommended, and likely only should be | 154 // Changing policies at runtime is not recommended, and likely only should be |
155 // done for unit tests. | 155 // done for unit tests. |
156 void ChooseDatabasePolicy(); | 156 void ChooseDatabasePolicy(); |
157 void SetDatabasePolicy(ActivityLogPolicy::PolicyType policy_type); | 157 void SetDatabasePolicy(ActivityLogPolicy::PolicyType policy_type); |
158 | 158 |
159 // ProfileKeyedAPI implementation. | 159 // BrowserContextKeyedAPI implementation. |
160 static const char* service_name() { return "ActivityLog"; } | 160 static const char* service_name() { return "ActivityLog"; } |
161 static const bool kServiceRedirectedInIncognito = true; | 161 static const bool kServiceRedirectedInIncognito = true; |
162 static const bool kServiceIsCreatedWithBrowserContext = false; | 162 static const bool kServiceIsCreatedWithBrowserContext = false; |
163 | 163 |
164 typedef ObserverListThreadSafe<Observer> ObserverList; | 164 typedef ObserverListThreadSafe<Observer> ObserverList; |
165 scoped_refptr<ObserverList> observers_; | 165 scoped_refptr<ObserverList> observers_; |
166 | 166 |
167 // Policy objects are owned by the ActivityLog, but cannot be scoped_ptrs | 167 // Policy objects are owned by the ActivityLog, but cannot be scoped_ptrs |
168 // since they may need to do some cleanup work on the database thread. | 168 // since they may need to do some cleanup work on the database thread. |
169 // Calling policy->Close() will free the object; see the comments on the | 169 // Calling policy->Close() will free the object; see the comments on the |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
205 FRIEND_TEST_ALL_PREFIXES(ActivityLogApiTest, TriggerEvent); | 205 FRIEND_TEST_ALL_PREFIXES(ActivityLogApiTest, TriggerEvent); |
206 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, AppAndCommandLine); | 206 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, AppAndCommandLine); |
207 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, CommandLineSwitch); | 207 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, CommandLineSwitch); |
208 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, NoSwitch); | 208 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, NoSwitch); |
209 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, PrefSwitch); | 209 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, PrefSwitch); |
210 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, WatchdogSwitch); | 210 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, WatchdogSwitch); |
211 DISALLOW_COPY_AND_ASSIGN(ActivityLog); | 211 DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
212 }; | 212 }; |
213 | 213 |
214 template <> | 214 template <> |
215 void ProfileKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies(); | 215 void BrowserContextKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies(); |
216 | 216 |
217 } // namespace extensions | 217 } // namespace extensions |
218 | 218 |
219 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ | 219 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ |
OLD | NEW |