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 ~ErrorConsole() override
;
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 void OnExtensionUnloaded(content::BrowserContext
* browser_context
,
130 const Extension
* extension
,
131 UnloadedExtensionInfo::Reason reason
) override
;
132 void OnExtensionLoaded(content::BrowserContext
* browser_context
,
133 const Extension
* extension
) override
;
134 void OnExtensionInstalled(content::BrowserContext
* browser_context
,
135 const Extension
* extension
,
136 bool is_update
) override
;
137 void OnExtensionUninstalled(content::BrowserContext
* browser_context
,
138 const Extension
* extension
,
139 extensions::UninstallReason reason
) override
;
141 // Add manifest errors from an extension's install warnings.
142 void AddManifestErrorsForExtension(const Extension
* extension
);
144 // content::NotificationObserver implementation.
145 void Observe(int type
,
146 const content::NotificationSource
& source
,
147 const content::NotificationDetails
& details
) override
;
149 // Returns the applicable bit mask of reporting preferences for the extension.
150 int GetMaskForExtension(const std::string
& extension_id
) const;
152 // Whether or not the error console should record errors. This is true if
153 // the user is in developer mode, and at least one of the following is true:
154 // - The Chrome Apps Developer Tools are installed.
155 // - FeatureSwitch::error_console() is enabled.
156 // - This is a Dev Channel release.
159 // Needed because ObserverList is not thread-safe.
160 base::ThreadChecker thread_checker_
;
162 // The list of all observers for the ErrorConsole.
163 ObserverList
<Observer
> observers_
;
165 // The errors which we have received so far.
168 // The default mask to use if an Extension does not have specific settings.
171 // The profile with which the ErrorConsole is associated. Only collect errors
172 // from extensions and RenderViews associated with this Profile (and it's
173 // incognito fellow).
176 // The ExtensionPrefs with which the ErrorConsole is associated. This weak
177 // pointer is safe because ErrorConsole is owned by ExtensionSystem, which
178 // is dependent on ExtensionPrefs.
179 ExtensionPrefs
* prefs_
;
181 content::NotificationRegistrar notification_registrar_
;
182 PrefChangeRegistrar pref_registrar_
;
184 ScopedObserver
<ExtensionRegistry
, ExtensionRegistryObserver
>
187 DISALLOW_COPY_AND_ASSIGN(ErrorConsole
);
190 } // namespace extensions
192 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_