| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_ERROR_CONSOLE_ERROR_CONSOLE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ |
| 7 | 7 |
| 8 #include <deque> | 8 #include <deque> |
| 9 #include <map> | 9 #include <map> |
| 10 | 10 |
| 11 #include "base/gtest_prod_util.h" | |
| 12 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/observer_list.h" | 12 #include "base/observer_list.h" |
| 14 #include "base/prefs/pref_change_registrar.h" | 13 #include "base/prefs/pref_change_registrar.h" |
| 15 #include "base/strings/string16.h" | 14 #include "base/scoped_observer.h" |
| 16 #include "base/threading/thread_checker.h" | 15 #include "base/threading/thread_checker.h" |
| 17 #include "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
| 19 #include "extensions/browser/error_map.h" | 18 #include "extensions/browser/error_map.h" |
| 20 #include "extensions/browser/extension_error.h" | 19 #include "extensions/browser/extension_error.h" |
| 20 #include "extensions/browser/extension_registry.h" |
| 21 #include "extensions/browser/extension_registry_observer.h" |
| 21 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 class NotificationDetails; | 24 class NotificationDetails; |
| 24 class NotificationSource; | 25 class NotificationSource; |
| 25 class RenderViewHost; | 26 class RenderViewHost; |
| 26 } | 27 } |
| 27 | 28 |
| 28 class ExtensionService; | 29 class ExtensionService; |
| 29 class Profile; | 30 class Profile; |
| 30 | 31 |
| 31 namespace extensions { | 32 namespace extensions { |
| 32 class ErrorConsoleUnitTest; | 33 class ErrorConsoleUnitTest; |
| 33 class Extension; | 34 class Extension; |
| 34 | 35 |
| 35 // The ErrorConsole is a central object to which all extension errors are | 36 // The ErrorConsole is a central object to which all extension errors are |
| 36 // reported. This includes errors detected in extensions core, as well as | 37 // reported. This includes errors detected in extensions core, as well as |
| 37 // runtime Javascript errors. If FeatureSwitch::error_console() is enabled these | 38 // runtime Javascript errors. If FeatureSwitch::error_console() is enabled these |
| 38 // errors can be viewed at chrome://extensions in developer mode. | 39 // errors can be viewed at chrome://extensions in developer mode. |
| 39 // This class is owned by ExtensionSystem, making it, in effect, a | 40 // This class is owned by ExtensionSystem, making it, in effect, a |
| 40 // BrowserContext-keyed service. | 41 // BrowserContext-keyed service. |
| 41 class ErrorConsole : public content::NotificationObserver { | 42 class ErrorConsole : public content::NotificationObserver, |
| 43 public ExtensionRegistryObserver { |
| 42 public: | 44 public: |
| 43 class Observer { | 45 class Observer { |
| 44 public: | 46 public: |
| 45 // Sent when a new error is reported to the error console. | 47 // Sent when a new error is reported to the error console. |
| 46 virtual void OnErrorAdded(const ExtensionError* error) = 0; | 48 virtual void OnErrorAdded(const ExtensionError* error) = 0; |
| 47 | 49 |
| 48 // Sent upon destruction to allow any observers to invalidate any references | 50 // Sent upon destruction to allow any observers to invalidate any references |
| 49 // they have to the error console. | 51 // they have to the error console. |
| 50 virtual void OnErrorConsoleDestroyed(); | 52 virtual void OnErrorConsoleDestroyed(); |
| 51 }; | 53 }; |
| 52 | 54 |
| 53 explicit ErrorConsole(Profile* profile, ExtensionService* extension_service); | 55 explicit ErrorConsole(Profile* profile); |
| 54 virtual ~ErrorConsole(); | 56 virtual ~ErrorConsole(); |
| 55 | 57 |
| 56 // Convenience method to return the ErrorConsole for a given profile. | 58 // Convenience method to return the ErrorConsole for a given profile. |
| 57 static ErrorConsole* Get(Profile* profile); | 59 static ErrorConsole* Get(Profile* profile); |
| 58 | 60 |
| 59 // Set whether or not errors of the specified |type| are stored for the | 61 // Set whether or not errors of the specified |type| are stored for the |
| 60 // extension with the given |extension_id|. This will be stored in the | 62 // extension with the given |extension_id|. This will be stored in the |
| 61 // preferences. | 63 // preferences. |
| 62 void SetReportingForExtension(const std::string& extension_id, | 64 void SetReportingForExtension(const std::string& extension_id, |
| 63 ExtensionError::Type type, | 65 ExtensionError::Type type, |
| 64 bool enabled); | 66 bool enabled); |
| 65 | 67 |
| 66 // Restore default reporting to the given extension. | 68 // Restore default reporting to the given extension. |
| 67 void UseDefaultReportingForExtension(const std::string& extension_id); | 69 void UseDefaultReportingForExtension(const std::string& extension_id); |
| 68 | 70 |
| 69 // Report an extension error, and add it to the list. | 71 // Report an extension error, and add it to the list. |
| 70 void ReportError(scoped_ptr<ExtensionError> error); | 72 void ReportError(scoped_ptr<ExtensionError> error); |
| 71 | 73 |
| 72 // Get a collection of weak pointers to all errors relating to the extension | 74 // Get a collection of weak pointers to all errors relating to the extension |
| 73 // with the given |extension_id|. | 75 // with the given |extension_id|. |
| 74 const ErrorList& GetErrorsForExtension(const std::string& extension_id) const; | 76 const ErrorList& GetErrorsForExtension(const std::string& extension_id) const; |
| 75 | 77 |
| 76 // Add or remove observers of the ErrorConsole to be notified of any errors | 78 // Add or remove observers of the ErrorConsole to be notified of any errors |
| 77 // added. | 79 // added. |
| 78 void AddObserver(Observer* observer); | 80 void AddObserver(Observer* observer); |
| 79 void RemoveObserver(Observer* observer); | 81 void RemoveObserver(Observer* observer); |
| 80 | 82 |
| 83 // Returns whether or not the ErrorConsole is enabled for the |
| 84 // chrome:extensions page or the Chrome Apps Developer Tools. |
| 85 // |
| 86 // TODO(rdevlin.cronin): These have different answers - ErrorConsole is |
| 87 // enabled by default in ADT, but only Dev Channel for chrome:extensions (or |
| 88 // with the commandline switch). Once we do a full launch, clean all this up. |
| 89 bool IsEnabledForChromeExtensionsPage() const; |
| 90 bool IsEnabledForAppsDeveloperTools() const; |
| 91 |
| 92 // Return whether or not the ErrorConsole is enabled. |
| 81 bool enabled() const { return enabled_; } | 93 bool enabled() const { return enabled_; } |
| 82 | 94 |
| 83 // Return the number of entries (extensions) in the error map. | 95 // Return the number of entries (extensions) in the error map. |
| 84 size_t get_num_entries_for_test() const { return errors_.size(); } | 96 size_t get_num_entries_for_test() const { return errors_.size(); } |
| 85 | 97 |
| 86 // Set the default reporting for all extensions. | 98 // Set the default reporting for all extensions. |
| 87 void set_default_reporting_for_test(ExtensionError::Type type, bool enabled) { | 99 void set_default_reporting_for_test(ExtensionError::Type type, bool enabled) { |
| 88 default_mask_ = | 100 default_mask_ = |
| 89 enabled ? default_mask_ | (1 << type) : default_mask_ & ~(1 << type); | 101 enabled ? default_mask_ | (1 << type) : default_mask_ & ~(1 << type); |
| 90 } | 102 } |
| 91 | 103 |
| 92 private: | 104 private: |
| 93 // A map which stores the reporting preferences for each Extension. If there | 105 // A map which stores the reporting preferences for each Extension. If there |
| 94 // is no entry in the map, it signals that the |default_mask_| should be used. | 106 // is no entry in the map, it signals that the |default_mask_| should be used. |
| 95 typedef std::map<std::string, int32> ErrorPreferenceMap; | 107 typedef std::map<std::string, int32> ErrorPreferenceMap; |
| 96 | 108 |
| 109 // Checks whether or not the ErrorConsole should be enabled or disabled. If it |
| 110 // is in the wrong state, enables or disables it appropriately. |
| 111 void CheckEnabled(); |
| 112 |
| 97 // Enable the error console for error collection and retention. This involves | 113 // Enable the error console for error collection and retention. This involves |
| 98 // subscribing to the appropriate notifications and fetching manifest errors. | 114 // subscribing to the appropriate notifications and fetching manifest errors. |
| 99 void Enable(ExtensionService* extension_service); | 115 void Enable(); |
| 100 | 116 |
| 101 // Disable the error console, removing the subscriptions to notifications and | 117 // Disable the error console, removing the subscriptions to notifications and |
| 102 // removing all current errors. | 118 // removing all current errors. |
| 103 void Disable(); | 119 void Disable(); |
| 104 | 120 |
| 105 // Called when the Developer Mode preference is changed; this is important | 121 // Called when the Developer Mode preference is changed; this is important |
| 106 // since we use this as a heuristic to determine if the console is enabled or | 122 // since we use this as a heuristic to determine if the console is enabled or |
| 107 // not. | 123 // not. |
| 108 void OnPrefChanged(); | 124 void OnPrefChanged(); |
| 109 | 125 |
| 126 // ExtensionRegistry implementation. If the Apps Developer Tools app is |
| 127 // installed or uninstalled, we may need to turn the ErrorConsole on/off. |
| 128 virtual void OnExtensionUnloaded(const Extension* extension) OVERRIDE; |
| 129 virtual void OnExtensionLoaded(const Extension* extension) OVERRIDE; |
| 130 |
| 110 // Add manifest errors from an extension's install warnings. | 131 // Add manifest errors from an extension's install warnings. |
| 111 void AddManifestErrorsForExtension(const Extension* extension); | 132 void AddManifestErrorsForExtension(const Extension* extension); |
| 112 | 133 |
| 113 // content::NotificationObserver implementation. | 134 // content::NotificationObserver implementation. |
| 114 virtual void Observe(int type, | 135 virtual void Observe(int type, |
| 115 const content::NotificationSource& source, | 136 const content::NotificationSource& source, |
| 116 const content::NotificationDetails& details) OVERRIDE; | 137 const content::NotificationDetails& details) OVERRIDE; |
| 117 | 138 |
| 118 // Whether or not the error console is enabled; it is enabled if the | 139 // Whether or not the error console should record errors. This is true if |
| 119 // FeatureSwitch (FeatureSwitch::error_console) is enabled and the user is | 140 // the user is in developer mode, and at least one of the following is true: |
| 120 // in Developer Mode. | 141 // - The Chrome Apps Developer Tools are installed. |
| 142 // - FeatureSwitch::error_console() is enabled. |
| 143 // - This is a Dev Channel release. |
| 121 bool enabled_; | 144 bool enabled_; |
| 122 | 145 |
| 123 // Needed because ObserverList is not thread-safe. | 146 // Needed because ObserverList is not thread-safe. |
| 124 base::ThreadChecker thread_checker_; | 147 base::ThreadChecker thread_checker_; |
| 125 | 148 |
| 126 // The list of all observers for the ErrorConsole. | 149 // The list of all observers for the ErrorConsole. |
| 127 ObserverList<Observer> observers_; | 150 ObserverList<Observer> observers_; |
| 128 | 151 |
| 129 // The errors which we have received so far. | 152 // The errors which we have received so far. |
| 130 ErrorMap errors_; | 153 ErrorMap errors_; |
| 131 | 154 |
| 132 // The mapping of Extension's error-reporting preferences. | 155 // The mapping of Extension's error-reporting preferences. |
| 133 ErrorPreferenceMap pref_map_; | 156 ErrorPreferenceMap pref_map_; |
| 134 | 157 |
| 135 // The default mask to use if an Extension does not have specific settings. | 158 // The default mask to use if an Extension does not have specific settings. |
| 136 int32 default_mask_; | 159 int32 default_mask_; |
| 137 | 160 |
| 138 // The profile with which the ErrorConsole is associated. Only collect errors | 161 // The profile with which the ErrorConsole is associated. Only collect errors |
| 139 // from extensions and RenderViews associated with this Profile (and it's | 162 // from extensions and RenderViews associated with this Profile (and it's |
| 140 // incognito fellow). | 163 // incognito fellow). |
| 141 Profile* profile_; | 164 Profile* profile_; |
| 142 | 165 |
| 143 content::NotificationRegistrar notification_registrar_; | 166 content::NotificationRegistrar notification_registrar_; |
| 144 PrefChangeRegistrar pref_registrar_; | 167 PrefChangeRegistrar pref_registrar_; |
| 145 | 168 |
| 169 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 170 registry_observer_; |
| 171 |
| 146 DISALLOW_COPY_AND_ASSIGN(ErrorConsole); | 172 DISALLOW_COPY_AND_ASSIGN(ErrorConsole); |
| 147 }; | 173 }; |
| 148 | 174 |
| 149 } // namespace extensions | 175 } // namespace extensions |
| 150 | 176 |
| 151 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ | 177 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ |
| OLD | NEW |