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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_
6 #define CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_
8 #include "base/memory/scoped_ptr.h"
9 #include "base/observer_list.h"
10 #include "base/prefs/pref_change_registrar.h"
11 #include "base/scoped_observer.h"
12 #include "base/threading/thread_checker.h"
13 #include "content/public/browser/notification_observer.h"
14 #include "content/public/browser/notification_registrar.h"
15 #include "extensions/browser/error_map.h"
16 #include "extensions/browser/extension_error.h"
17 #include "extensions/browser/extension_registry_observer.h"
20 class NotificationDetails;
21 class NotificationSource;
27 namespace extensions {
30 class ExtensionRegistry;
32 // The ErrorConsole is a central object to which all extension errors are
33 // reported. This includes errors detected in extensions core, as well as
34 // runtime Javascript errors. If FeatureSwitch::error_console() is enabled these
35 // errors can be viewed at chrome://extensions in developer mode.
36 // This class is owned by ExtensionSystem, making it, in effect, a
37 // BrowserContext-keyed service.
38 class ErrorConsole : public content::NotificationObserver,
39 public ExtensionRegistryObserver {
43 // Sent when a new error is reported to the error console.
44 virtual void OnErrorAdded(const ExtensionError* error) = 0;
46 // Sent upon destruction to allow any observers to invalidate any references
47 // they have to the error console.
48 virtual void OnErrorConsoleDestroyed();
51 explicit ErrorConsole(Profile* profile);
52 virtual ~ErrorConsole();
54 // Convenience method to return the ErrorConsole for a given profile.
55 static ErrorConsole* Get(Profile* profile);
57 // Set whether or not errors of the specified |type| are stored for the
58 // extension with the given |extension_id|. This will be stored in the
60 void SetReportingForExtension(const std::string& extension_id,
61 ExtensionError::Type type,
64 // Set whether or not errors of all types are stored for the extension with
65 // the given |extension_id|.
66 void SetReportingAllForExtension(const std::string& extension_id,
69 // Returns true if reporting for either manifest or runtime errors is enabled
70 // for the extension with the given |extension_id|.
71 bool IsReportingEnabledForExtension(const std::string& extension_id) const;
73 // Restore default reporting to the given extension.
74 void UseDefaultReportingForExtension(const std::string& extension_id);
76 // Report an extension error, and add it to the list.
77 void ReportError(scoped_ptr<ExtensionError> error);
79 // Get a collection of weak pointers to all errors relating to the extension
80 // with the given |extension_id|.
81 const ErrorList& GetErrorsForExtension(const std::string& extension_id) const;
83 // Add or remove observers of the ErrorConsole to be notified of any errors
85 void AddObserver(Observer* observer);
86 void RemoveObserver(Observer* observer);
88 // Returns whether or not the ErrorConsole is enabled for the
89 // chrome:extensions page or the Chrome Apps Developer Tools.
91 // TODO(rdevlin.cronin): These have different answers - ErrorConsole is
92 // enabled by default in ADT, but only Dev Channel for chrome:extensions (or
93 // with the commandline switch). Once we do a full launch, clean all this up.
94 bool IsEnabledForChromeExtensionsPage() const;
95 bool IsEnabledForAppsDeveloperTools() const;
97 // Return whether or not the ErrorConsole is enabled.
98 bool enabled() const { return enabled_; }
100 // Return the number of entries (extensions) in the error map.
101 size_t get_num_entries_for_test() const { return errors_.size(); }
103 // Set the default reporting for all extensions.
104 void set_default_reporting_for_test(ExtensionError::Type type, bool enabled) {
106 enabled ? default_mask_ | (1 << type) : default_mask_ & ~(1 << type);
110 // Checks whether or not the ErrorConsole should be enabled or disabled. If it
111 // is in the wrong state, enables or disables it appropriately.
114 // Enable the error console for error collection and retention. This involves
115 // subscribing to the appropriate notifications and fetching manifest errors.
118 // Disable the error console, removing the subscriptions to notifications and
119 // removing all current errors.
122 // Called when the Developer Mode preference is changed; this is important
123 // since we use this as a heuristic to determine if the console is enabled or
125 void OnPrefChanged();
127 // ExtensionRegistry implementation. If the Apps Developer Tools app is
128 // installed or uninstalled, we may need to turn the ErrorConsole on/off.
129 virtual void OnExtensionUnloaded(
130 content::BrowserContext* browser_context,
131 const Extension* extension,
132 UnloadedExtensionInfo::Reason reason) OVERRIDE;
133 virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
134 const Extension* extension) OVERRIDE;
135 virtual void OnExtensionInstalled(content::BrowserContext* browser_context,
136 const Extension* extension,
137 bool is_update) OVERRIDE;
138 virtual void OnExtensionUninstalled(
139 content::BrowserContext* browser_context,
140 const Extension* extension,
141 extensions::UninstallReason reason) OVERRIDE;
143 // Add manifest errors from an extension's install warnings.
144 void AddManifestErrorsForExtension(const Extension* extension);
146 // content::NotificationObserver implementation.
147 virtual void Observe(int type,
148 const content::NotificationSource& source,
149 const content::NotificationDetails& details) OVERRIDE;
151 // Returns the applicable bit mask of reporting preferences for the extension.
152 int GetMaskForExtension(const std::string& extension_id) const;
154 // Whether or not the error console should record errors. This is true if
155 // the user is in developer mode, and at least one of the following is true:
156 // - The Chrome Apps Developer Tools are installed.
157 // - FeatureSwitch::error_console() is enabled.
158 // - This is a Dev Channel release.
161 // Needed because ObserverList is not thread-safe.
162 base::ThreadChecker thread_checker_;
164 // The list of all observers for the ErrorConsole.
165 ObserverList<Observer> observers_;
167 // The errors which we have received so far.
170 // The default mask to use if an Extension does not have specific settings.
173 // The profile with which the ErrorConsole is associated. Only collect errors
174 // from extensions and RenderViews associated with this Profile (and it's
175 // incognito fellow).
178 // The ExtensionPrefs with which the ErrorConsole is associated. This weak
179 // pointer is safe because ErrorConsole is owned by ExtensionSystem, which
180 // is dependent on ExtensionPrefs.
181 ExtensionPrefs* prefs_;
183 content::NotificationRegistrar notification_registrar_;
184 PrefChangeRegistrar pref_registrar_;
186 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
189 DISALLOW_COPY_AND_ASSIGN(ErrorConsole);
192 } // namespace extensions
194 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_