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