| 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/memory/singleton.h" | |
| 14 #include "base/observer_list_threadsafe.h" | 13 #include "base/observer_list_threadsafe.h" |
| 15 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 16 #include "base/threading/thread.h" | 15 #include "base/threading/thread.h" |
| 17 #include "chrome/browser/extensions/activity_log/activity_actions.h" | 16 #include "chrome/browser/extensions/activity_log/activity_actions.h" |
| 18 #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" | 19 #include "chrome/browser/extensions/install_observer.h" |
| 20 #include "chrome/browser/extensions/tab_helper.h" | 20 #include "chrome/browser/extensions/tab_helper.h" |
| 21 #include "chrome/common/extensions/dom_action_types.h" | 21 #include "chrome/common/extensions/dom_action_types.h" |
| 22 #include "components/browser_context_keyed_service/browser_context_keyed_service
.h" | |
| 23 #include "components/browser_context_keyed_service/browser_context_keyed_service
_factory.h" | |
| 24 #include "extensions/browser/api_activity_monitor.h" | 22 #include "extensions/browser/api_activity_monitor.h" |
| 25 | 23 |
| 26 class Profile; | 24 class Profile; |
| 27 | 25 |
| 28 namespace content { | 26 namespace content { |
| 29 class BrowserContext; | 27 class BrowserContext; |
| 30 } | 28 } |
| 31 | 29 |
| 32 namespace user_prefs { | 30 namespace user_prefs { |
| 33 class PrefRegistrySyncable; | 31 class PrefRegistrySyncable; |
| 34 } | 32 } |
| 35 | 33 |
| 36 namespace extensions { | 34 namespace extensions { |
| 37 class Extension; | 35 class Extension; |
| 38 class InstallTracker; | 36 class InstallTracker; |
| 39 | 37 |
| 40 // A utility for tracing interesting activity for each extension. | 38 // A utility for tracing interesting activity for each extension. |
| 41 // 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. |
| 42 class ActivityLog : public BrowserContextKeyedService, | 40 // Each profile has different extensions, so we keep a different database for |
| 41 // each profile. |
| 42 class ActivityLog : public ProfileKeyedAPI, |
| 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 // ActivityLog is a singleton, so don't instantiate it with the constructor; | 54 static ProfileKeyedAPIFactory<ActivityLog>* GetFactoryInstance(); |
| 55 // use GetInstance instead. | 55 |
| 56 // ActivityLog is a BrowserContextKeyedService, so don't instantiate it with |
| 57 // the constructor; use GetInstance instead. |
| 56 static ActivityLog* GetInstance(content::BrowserContext* context); | 58 static ActivityLog* GetInstance(content::BrowserContext* context); |
| 57 | 59 |
| 58 // Add/remove observer: the activityLogPrivate API only listens when the | 60 // Add/remove observer: the activityLogPrivate API only listens when the |
| 59 // ActivityLog extension is registered for an event. | 61 // ActivityLog extension is registered for an event. |
| 60 void AddObserver(Observer* observer); | 62 void AddObserver(Observer* observer); |
| 61 void RemoveObserver(Observer* observer); | 63 void RemoveObserver(Observer* observer); |
| 62 | 64 |
| 63 // Logs an extension action: passes it to any installed policy to be logged | 65 // Logs an extension action: passes it to any installed policy to be logged |
| 64 // to the database, to any observers, and logs to the console if in testing | 66 // to the database, to any observers, and logs to the console if in testing |
| 65 // mode. | 67 // mode. |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 // If restrict_urls is empty then all URLs in the activity log database are | 122 // If restrict_urls is empty then all URLs in the activity log database are |
| 121 // removed, otherwise only those in restrict_urls are removed. | 123 // removed, otherwise only those in restrict_urls are removed. |
| 122 void RemoveURLs(const std::vector<GURL>& restrict_urls); | 124 void RemoveURLs(const std::vector<GURL>& restrict_urls); |
| 123 void RemoveURLs(const std::set<GURL>& restrict_urls); | 125 void RemoveURLs(const std::set<GURL>& restrict_urls); |
| 124 void RemoveURL(const GURL& url); | 126 void RemoveURL(const GURL& url); |
| 125 | 127 |
| 126 // Deletes the database associated with the policy that's currently in use. | 128 // Deletes the database associated with the policy that's currently in use. |
| 127 void DeleteDatabase(); | 129 void DeleteDatabase(); |
| 128 | 130 |
| 129 private: | 131 private: |
| 130 friend class ActivityLogFactory; | |
| 131 friend class ActivityLogTest; | 132 friend class ActivityLogTest; |
| 133 friend class ProfileKeyedAPIFactory<ActivityLog>; |
| 132 friend class RenderViewActivityLogTest; | 134 friend class RenderViewActivityLogTest; |
| 133 | 135 |
| 134 explicit ActivityLog(Profile* profile); | 136 explicit ActivityLog(content::BrowserContext* context); |
| 135 virtual ~ActivityLog(); | 137 virtual ~ActivityLog(); |
| 136 | 138 |
| 137 // Specifies if the Watchdog app is active (installed & enabled). | 139 // Specifies if the Watchdog app is active (installed & enabled). |
| 138 // If so, we need to log to the database and stream to the API. | 140 // If so, we need to log to the database and stream to the API. |
| 139 bool IsWatchdogAppActive(); | 141 bool IsWatchdogAppActive(); |
| 140 // If we're in a browser test, we need to pretend that the watchdog app is | 142 // If we're in a browser test, we need to pretend that the watchdog app is |
| 141 // active. | 143 // active. |
| 142 void SetWatchdogAppActive(bool active); | 144 void SetWatchdogAppActive(bool active); |
| 143 | 145 |
| 144 // Specifies if we need to record actions to the db. If so, we need to log to | 146 // Specifies if we need to record actions to the db. If so, we need to log to |
| (...skipping 13 matching lines...) Expand all Loading... |
| 158 int32 page_id, | 160 int32 page_id, |
| 159 const GURL& on_url) OVERRIDE; | 161 const GURL& on_url) OVERRIDE; |
| 160 | 162 |
| 161 // At the moment, ActivityLog will use only one policy for summarization. | 163 // At the moment, ActivityLog will use only one policy for summarization. |
| 162 // These methods are used to choose and set the most appropriate policy. | 164 // These methods are used to choose and set the most appropriate policy. |
| 163 // Changing policies at runtime is not recommended, and likely only should be | 165 // Changing policies at runtime is not recommended, and likely only should be |
| 164 // done for unit tests. | 166 // done for unit tests. |
| 165 void ChooseDatabasePolicy(); | 167 void ChooseDatabasePolicy(); |
| 166 void SetDatabasePolicy(ActivityLogPolicy::PolicyType policy_type); | 168 void SetDatabasePolicy(ActivityLogPolicy::PolicyType policy_type); |
| 167 | 169 |
| 170 // ProfileKeyedAPI implementation. |
| 171 static const char* service_name() { return "ActivityLog"; } |
| 172 static const bool kServiceRedirectedInIncognito = true; |
| 173 static const bool kServiceIsCreatedWithBrowserContext = false; |
| 174 |
| 168 typedef ObserverListThreadSafe<Observer> ObserverList; | 175 typedef ObserverListThreadSafe<Observer> ObserverList; |
| 169 scoped_refptr<ObserverList> observers_; | 176 scoped_refptr<ObserverList> observers_; |
| 170 | 177 |
| 171 // Policy objects are owned by the ActivityLog, but cannot be scoped_ptrs | 178 // Policy objects are owned by the ActivityLog, but cannot be scoped_ptrs |
| 172 // since they may need to do some cleanup work on the database thread. | 179 // since they may need to do some cleanup work on the database thread. |
| 173 // Calling policy->Close() will free the object; see the comments on the | 180 // Calling policy->Close() will free the object; see the comments on the |
| 174 // ActivityDatabase class for full details. | 181 // ActivityDatabase class for full details. |
| 175 | 182 |
| 176 // The database policy object takes care of recording & looking up data: | 183 // The database policy object takes care of recording & looking up data: |
| 177 // data summarization, compression, and logging. There should only be a | 184 // data summarization, compression, and logging. There should only be a |
| (...skipping 30 matching lines...) Expand all Loading... |
| 208 | 215 |
| 209 FRIEND_TEST_ALL_PREFIXES(ActivityLogApiTest, TriggerEvent); | 216 FRIEND_TEST_ALL_PREFIXES(ActivityLogApiTest, TriggerEvent); |
| 210 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, AppAndCommandLine); | 217 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, AppAndCommandLine); |
| 211 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, CommandLineSwitch); | 218 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, CommandLineSwitch); |
| 212 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, NoSwitch); | 219 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, NoSwitch); |
| 213 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, PrefSwitch); | 220 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, PrefSwitch); |
| 214 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, WatchdogSwitch); | 221 FRIEND_TEST_ALL_PREFIXES(ActivityLogEnabledTest, WatchdogSwitch); |
| 215 DISALLOW_COPY_AND_ASSIGN(ActivityLog); | 222 DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
| 216 }; | 223 }; |
| 217 | 224 |
| 218 // Each profile has different extensions, so we keep a different database for | 225 template <> |
| 219 // each profile. | 226 void ProfileKeyedAPIFactory<ActivityLog>::DeclareFactoryDependencies(); |
| 220 class ActivityLogFactory : public BrowserContextKeyedServiceFactory { | |
| 221 public: | |
| 222 static ActivityLog* GetForBrowserContext(content::BrowserContext* context) { | |
| 223 return static_cast<ActivityLog*>( | |
| 224 GetInstance()->GetServiceForBrowserContext(context, true)); | |
| 225 } | |
| 226 | |
| 227 static ActivityLogFactory* GetInstance(); | |
| 228 | |
| 229 private: | |
| 230 friend struct DefaultSingletonTraits<ActivityLogFactory>; | |
| 231 ActivityLogFactory(); | |
| 232 virtual ~ActivityLogFactory(); | |
| 233 | |
| 234 virtual BrowserContextKeyedService* BuildServiceInstanceFor( | |
| 235 content::BrowserContext* profile) const OVERRIDE; | |
| 236 | |
| 237 virtual content::BrowserContext* GetBrowserContextToUse( | |
| 238 content::BrowserContext* context) const OVERRIDE; | |
| 239 | |
| 240 DISALLOW_COPY_AND_ASSIGN(ActivityLogFactory); | |
| 241 }; | |
| 242 | |
| 243 | 227 |
| 244 } // namespace extensions | 228 } // namespace extensions |
| 245 | 229 |
| 246 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ | 230 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_ACTIVITY_LOG_H_ |
| OLD | NEW |