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/extension_error.h"
22 class NotificationDetails
;
23 class NotificationSource
;
27 class ExtensionService
;
30 namespace extensions
{
31 class ErrorConsoleUnitTest
;
34 // The ErrorConsole is a central object to which all extension errors are
35 // reported. This includes errors detected in extensions core, as well as
36 // runtime Javascript errors.
37 // This class is owned by ExtensionSystem, making it, in effect, a
38 // BrowserContext-keyed service.
39 class ErrorConsole
: public content::NotificationObserver
{
41 typedef std::deque
<const ExtensionError
*> ErrorList
;
42 typedef std::map
<std::string
, ErrorList
> ErrorMap
;
46 // Sent when a new error is reported to the error console.
47 virtual void OnErrorAdded(const ExtensionError
* error
) = 0;
49 // Sent upon destruction to allow any observers to invalidate any references
50 // they have to the error console.
51 virtual void OnErrorConsoleDestroyed();
54 explicit ErrorConsole(Profile
* profile
, ExtensionService
* extension_service
);
55 virtual ~ErrorConsole();
57 // Convenience method to return the ErrorConsole for a given profile.
58 static ErrorConsole
* Get(Profile
* profile
);
60 // Report an extension error, and add it to the list.
61 void ReportError(scoped_ptr
<ExtensionError
> error
);
63 // Get a collection of weak pointers to all errors relating to the extension
64 // with the given |extension_id|.
65 const ErrorList
& GetErrorsForExtension(const std::string
& extension_id
) const;
67 // Add or remove observers of the ErrorConsole to be notified of any errors
69 void AddObserver(Observer
* observer
);
70 void RemoveObserver(Observer
* observer
);
72 bool enabled() const { return enabled_
; }
73 const ErrorMap
& errors() const { return errors_
; }
76 FRIEND_TEST_ALL_PREFIXES(ErrorConsoleUnitTest
, AddAndRemoveErrors
);
78 // Enable the error console for error collection and retention. This involves
79 // subscribing to the appropriate notifications and fetching manifest errors.
80 void Enable(ExtensionService
* extension_service
);
81 // Disable the error console, removing the subscriptions to notifications and
82 // removing all current errors.
85 // Called when the Developer Mode preference is changed; this is important
86 // since we use this as a heuristic to determine if the console is enabled or
90 // Add manifest errors from an extension's install warnings.
91 void AddManifestErrorsForExtension(const Extension
* extension
);
93 // Remove all errors which happened while incognito; we have to do this once
94 // the incognito profile is destroyed.
95 void RemoveIncognitoErrors();
97 // Remove all errors relating to a particular |extension_id|.
98 void RemoveErrorsForExtension(const std::string
& extension_id
);
100 // Remove all errors for all extensions.
101 void RemoveAllErrors();
103 // content::NotificationObserver implementation.
104 virtual void Observe(int type
,
105 const content::NotificationSource
& source
,
106 const content::NotificationDetails
& details
) OVERRIDE
;
108 // Whether or not the error console is enabled; it is enabled if the
109 // FeatureSwitch (FeatureSwitch::error_console) is enabled and the user is
110 // in Developer Mode.
113 // Needed because ObserverList is not thread-safe.
114 base::ThreadChecker thread_checker_
;
116 // The list of all observers for the ErrorConsole.
117 ObserverList
<Observer
> observers_
;
119 // The errors which we have received so far.
122 // The profile with which the ErrorConsole is associated. Only collect errors
123 // from extensions and RenderViews associated with this Profile (and it's
124 // incognito fellow).
127 content::NotificationRegistrar notification_registrar_
;
128 PrefChangeRegistrar pref_registrar_
;
130 DISALLOW_COPY_AND_ASSIGN(ErrorConsole
);
133 } // namespace extensions
135 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_