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/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/observer_list.h"
14 #include "base/prefs/pref_change_registrar.h"
15 #include "base/strings/string16.h"
16 #include "base/threading/thread_checker.h"
17 #include "content/public/browser/notification_observer.h"
18 #include "content/public/browser/notification_registrar.h"
19 #include "extensions/browser/error_map.h"
20 #include "extensions/browser/extension_error.h"
23 class NotificationDetails
;
24 class NotificationSource
;
28 class ExtensionService
;
31 namespace extensions
{
32 class ErrorConsoleUnitTest
;
35 // 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 // runtime Javascript errors.
38 // This class is owned by ExtensionSystem, making it, in effect, a
39 // BrowserContext-keyed service.
40 class ErrorConsole
: public content::NotificationObserver
{
44 // Sent when a new error is reported to the error console.
45 virtual void OnErrorAdded(const ExtensionError
* error
) = 0;
47 // Sent upon destruction to allow any observers to invalidate any references
48 // they have to the error console.
49 virtual void OnErrorConsoleDestroyed();
52 explicit ErrorConsole(Profile
* profile
, ExtensionService
* extension_service
);
53 virtual ~ErrorConsole();
55 // Convenience method to return the ErrorConsole for a given profile.
56 static ErrorConsole
* Get(Profile
* profile
);
58 // Set whether or not errors of the specified |type| are stored for the
59 // extension with the given |extension_id|. This will be stored in the
61 void SetReportingForExtension(const std::string
& extension_id
,
62 ExtensionError::Type type
,
65 // Restore default reporting to the given extension.
66 void UseDefaultReportingForExtension(const std::string
& extension_id
);
68 // Report an extension error, and add it to the list.
69 void ReportError(scoped_ptr
<ExtensionError
> error
);
71 // Get a collection of weak pointers to all errors relating to the extension
72 // with the given |extension_id|.
73 const ErrorList
& GetErrorsForExtension(const std::string
& extension_id
) const;
75 // Add or remove observers of the ErrorConsole to be notified of any errors
77 void AddObserver(Observer
* observer
);
78 void RemoveObserver(Observer
* observer
);
80 bool enabled() const { return enabled_
; }
82 // Return the number of entries (extensions) in the error map.
83 size_t get_num_entries_for_test() const { return errors_
.size(); }
85 // Set the default reporting for all extensions.
86 void set_default_reporting_for_test(ExtensionError::Type type
, bool enabled
) {
88 enabled
? default_mask_
| (1 << type
) : default_mask_
& ~(1 << type
);
92 // A map which stores the reporting preferences for each Extension. If there
93 // is no entry in the map, it signals that the |default_mask_| should be used.
94 typedef std::map
<std::string
, int32
> ErrorPreferenceMap
;
96 // Enable the error console for error collection and retention. This involves
97 // subscribing to the appropriate notifications and fetching manifest errors.
98 void Enable(ExtensionService
* extension_service
);
100 // Disable the error console, removing the subscriptions to notifications and
101 // removing all current errors.
104 // Called when the Developer Mode preference is changed; this is important
105 // since we use this as a heuristic to determine if the console is enabled or
107 void OnPrefChanged();
109 // Add manifest errors from an extension's install warnings.
110 void AddManifestErrorsForExtension(const Extension
* extension
);
112 // content::NotificationObserver implementation.
113 virtual void Observe(int type
,
114 const content::NotificationSource
& source
,
115 const content::NotificationDetails
& details
) OVERRIDE
;
117 // Whether or not the error console is enabled; it is enabled if the
118 // FeatureSwitch (FeatureSwitch::error_console) is enabled and the user is
119 // in Developer Mode.
122 // Needed because ObserverList is not thread-safe.
123 base::ThreadChecker thread_checker_
;
125 // The list of all observers for the ErrorConsole.
126 ObserverList
<Observer
> observers_
;
128 // The errors which we have received so far.
131 // The mapping of Extension's error-reporting preferences.
132 ErrorPreferenceMap pref_map_
;
134 // The default mask to use if an Extension does not have specific settings.
137 // The profile with which the ErrorConsole is associated. Only collect errors
138 // from extensions and RenderViews associated with this Profile (and it's
139 // incognito fellow).
142 content::NotificationRegistrar notification_registrar_
;
143 PrefChangeRegistrar pref_registrar_
;
145 DISALLOW_COPY_AND_ASSIGN(ErrorConsole
);
148 } // namespace extensions
150 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_