| 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_EXTENSION_ACTIVITY_LOG_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_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 | 10 |
| 11 #include "base/memory/singleton.h" | 11 #include "base/memory/singleton.h" |
| 12 #include "base/observer_list_threadsafe.h" | 12 #include "base/observer_list_threadsafe.h" |
| 13 #include "base/synchronization/lock.h" | 13 #include "base/synchronization/lock.h" |
| 14 | 14 |
| 15 namespace extensions { | 15 namespace extensions { |
| 16 class Extension; | 16 class Extension; |
| 17 } | |
| 18 | 17 |
| 19 // A utility for tracing interesting activity for each extension. | 18 // A utility for tracing interesting activity for each extension. |
| 20 class ExtensionActivityLog { | 19 class ActivityLog { |
| 21 public: | 20 public: |
| 22 enum Activity { | 21 enum Activity { |
| 23 ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. | 22 ACTIVITY_EXTENSION_API_CALL, // Extension API invocation is called. |
| 24 ACTIVITY_EXTENSION_API_BLOCK // Extension API invocation is blocked. | 23 ACTIVITY_EXTENSION_API_BLOCK // Extension API invocation is blocked. |
| 25 }; | 24 }; |
| 26 | 25 |
| 27 // Observers can listen for activity events. | 26 // Observers can listen for activity events. |
| 28 class Observer { | 27 class Observer { |
| 29 public: | 28 public: |
| 30 virtual void OnExtensionActivity(const extensions::Extension* extension, | 29 virtual void OnExtensionActivity(const Extension* extension, |
| 31 Activity activity, | 30 Activity activity, |
| 32 const std::string& msg) = 0; | 31 const std::string& msg) = 0; |
| 33 }; | 32 }; |
| 34 | 33 |
| 35 ~ExtensionActivityLog(); | 34 ~ActivityLog(); |
| 36 static ExtensionActivityLog* GetInstance(); | 35 static ActivityLog* GetInstance(); |
| 37 | 36 |
| 38 // Add/remove observer. | 37 // Add/remove observer. |
| 39 void AddObserver(const extensions::Extension* extension, Observer* observer); | 38 void AddObserver(const Extension* extension, Observer* observer); |
| 40 void RemoveObserver(const extensions::Extension* extension, | 39 void RemoveObserver(const Extension* extension, |
| 41 Observer* observer); | 40 Observer* observer); |
| 42 | 41 |
| 43 // Check for the existence observer list by extension_id. | 42 // Check for the existence observer list by extension_id. |
| 44 bool HasObservers(const extensions::Extension* extension) const; | 43 bool HasObservers(const Extension* extension) const; |
| 45 | 44 |
| 46 // Log |activity| for |extension|. | 45 // Log |activity| for |extension|. |
| 47 void Log(const extensions::Extension* extension, | 46 void Log(const Extension* extension, |
| 48 Activity activity, | 47 Activity activity, |
| 49 const std::string& msg) const; | 48 const std::string& msg) const; |
| 50 | 49 |
| 51 private: | 50 private: |
| 52 ExtensionActivityLog(); | 51 ActivityLog(); |
| 53 friend struct DefaultSingletonTraits<ExtensionActivityLog>; | 52 friend struct DefaultSingletonTraits<ActivityLog>; |
| 54 | 53 |
| 55 static const char* ActivityToString(Activity activity); | 54 static const char* ActivityToString(Activity activity); |
| 56 | 55 |
| 57 // A lock used to synchronize access to member variables. | 56 // A lock used to synchronize access to member variables. |
| 58 mutable base::Lock lock_; | 57 mutable base::Lock lock_; |
| 59 | 58 |
| 60 // Whether to log activity to stdout. This is set by checking the | 59 // Whether to log activity to stdout. This is set by checking the |
| 61 // enable-extension-activity-logging switch. | 60 // enable-extension-activity-logging switch. |
| 62 bool log_activity_to_stdout_; | 61 bool log_activity_to_stdout_; |
| 63 | 62 |
| 64 typedef ObserverListThreadSafe<Observer> ObserverList; | 63 typedef ObserverListThreadSafe<Observer> ObserverList; |
| 65 typedef std::map<const extensions::Extension*, scoped_refptr<ObserverList> > | 64 typedef std::map<const Extension*, scoped_refptr<ObserverList> > |
| 66 ObserverMap; | 65 ObserverMap; |
| 67 // A map of extensions to activity observers for that extension. | 66 // A map of extensions to activity observers for that extension. |
| 68 ObserverMap observers_; | 67 ObserverMap observers_; |
| 69 | 68 |
| 70 DISALLOW_COPY_AND_ASSIGN(ExtensionActivityLog); | 69 DISALLOW_COPY_AND_ASSIGN(ActivityLog); |
| 71 }; | 70 }; |
| 72 | 71 |
| 73 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_ACTIVITY_LOG_H_ | 72 } // namespace extensions |
| 73 |
| 74 #endif // CHROME_BROWSER_EXTENSIONS_ACTIVITY_LOG_H_ |
| OLD | NEW |