Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / error_console / error_console.h
blob9f2fc7aad19c9c673d8085e34c8541973db03d61
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 "components/keyed_service/core/keyed_service.h"
14 #include "content/public/browser/notification_observer.h"
15 #include "content/public/browser/notification_registrar.h"
16 #include "extensions/browser/error_map.h"
17 #include "extensions/browser/extension_error.h"
18 #include "extensions/browser/extension_registry_observer.h"
20 namespace content {
21 class BrowserContext;
22 class NotificationDetails;
23 class NotificationSource;
24 class RenderViewHost;
27 class Profile;
29 namespace extensions {
30 class Extension;
31 class ExtensionPrefs;
32 class ExtensionRegistry;
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. If FeatureSwitch::error_console() is enabled these
37 // errors can be viewed at chrome://extensions in developer mode.
38 // This class is owned by ExtensionSystem, making it, in effect, a
39 // BrowserContext-keyed service.
40 class ErrorConsole : public KeyedService,
41 public content::NotificationObserver,
42 public ExtensionRegistryObserver {
43 public:
44 class Observer {
45 public:
46 // Sent when a new error is reported to the error console.
47 virtual void OnErrorAdded(const ExtensionError* error);
49 // Sent when errors are removed from the error console. |extension_ids| is
50 // the set of ids that were affected.
51 // Note: This is not sent when an extension is uninstalled, or when a
52 // profile is destroyed.
53 virtual void OnErrorsRemoved(const std::set<std::string>& extension_ids);
55 // Sent upon destruction to allow any observers to invalidate any references
56 // they have to the error console.
57 virtual void OnErrorConsoleDestroyed();
60 explicit ErrorConsole(Profile* profile);
61 ~ErrorConsole() override;
63 // Convenience method to return the ErrorConsole for a given |context|.
64 static ErrorConsole* Get(content::BrowserContext* context);
66 // Set whether or not errors of the specified |type| are stored for the
67 // extension with the given |extension_id|. This will be stored in the
68 // preferences.
69 void SetReportingForExtension(const std::string& extension_id,
70 ExtensionError::Type type,
71 bool enabled);
73 // Set whether or not errors of all types are stored for the extension with
74 // the given |extension_id|.
75 void SetReportingAllForExtension(const std::string& extension_id,
76 bool enabled);
78 // Returns true if reporting for either manifest or runtime errors is enabled
79 // for the extension with the given |extension_id|.
80 bool IsReportingEnabledForExtension(const std::string& extension_id) const;
82 // Restore default reporting to the given extension.
83 void UseDefaultReportingForExtension(const std::string& extension_id);
85 // Report an extension error, and add it to the list.
86 void ReportError(scoped_ptr<ExtensionError> error);
88 // Removes errors from the map according to the given |filter|.
89 void RemoveErrors(const ErrorMap::Filter& filter);
91 // Get a collection of weak pointers to all errors relating to the extension
92 // with the given |extension_id|.
93 const ErrorList& GetErrorsForExtension(const std::string& extension_id) const;
95 // Add or remove observers of the ErrorConsole to be notified of any errors
96 // added.
97 void AddObserver(Observer* observer);
98 void RemoveObserver(Observer* observer);
100 // Returns whether or not the ErrorConsole is enabled for the
101 // chrome:extensions page or the Chrome Apps Developer Tools.
103 // TODO(rdevlin.cronin): These have different answers - ErrorConsole is
104 // enabled by default in ADT, but only Dev Channel for chrome:extensions (or
105 // with the commandline switch). Once we do a full launch, clean all this up.
106 bool IsEnabledForChromeExtensionsPage() const;
107 bool IsEnabledForAppsDeveloperTools() const;
109 // Return whether or not the ErrorConsole is enabled.
110 bool enabled() const { return enabled_; }
112 // Return the number of entries (extensions) in the error map.
113 size_t get_num_entries_for_test() const { return errors_.size(); }
115 // Set the default reporting for all extensions.
116 void set_default_reporting_for_test(ExtensionError::Type type, bool enabled) {
117 default_mask_ =
118 enabled ? default_mask_ | (1 << type) : default_mask_ & ~(1 << type);
121 private:
122 // Checks whether or not the ErrorConsole should be enabled or disabled. If it
123 // is in the wrong state, enables or disables it appropriately.
124 void CheckEnabled();
126 // Enable the error console for error collection and retention. This involves
127 // subscribing to the appropriate notifications and fetching manifest errors.
128 void Enable();
130 // Disable the error console, removing the subscriptions to notifications and
131 // removing all current errors.
132 void Disable();
134 // Called when the Developer Mode preference is changed; this is important
135 // since we use this as a heuristic to determine if the console is enabled or
136 // not.
137 void OnPrefChanged();
139 // ExtensionRegistry implementation. If the Apps Developer Tools app is
140 // installed or uninstalled, we may need to turn the ErrorConsole on/off.
141 void OnExtensionUnloaded(content::BrowserContext* browser_context,
142 const Extension* extension,
143 UnloadedExtensionInfo::Reason reason) override;
144 void OnExtensionLoaded(content::BrowserContext* browser_context,
145 const Extension* extension) override;
146 void OnExtensionInstalled(content::BrowserContext* browser_context,
147 const Extension* extension,
148 bool is_update) override;
149 void OnExtensionUninstalled(content::BrowserContext* browser_context,
150 const Extension* extension,
151 extensions::UninstallReason reason) override;
153 // Add manifest errors from an extension's install warnings.
154 void AddManifestErrorsForExtension(const Extension* extension);
156 // content::NotificationObserver implementation.
157 void Observe(int type,
158 const content::NotificationSource& source,
159 const content::NotificationDetails& details) override;
161 // Returns the applicable bit mask of reporting preferences for the extension.
162 int GetMaskForExtension(const std::string& extension_id) const;
164 // Whether or not the error console should record errors. This is true if
165 // the user is in developer mode, and at least one of the following is true:
166 // - The Chrome Apps Developer Tools are installed.
167 // - FeatureSwitch::error_console() is enabled.
168 // - This is a Dev Channel release.
169 bool enabled_;
171 // Needed because base::ObserverList is not thread-safe.
172 base::ThreadChecker thread_checker_;
174 // The list of all observers for the ErrorConsole.
175 base::ObserverList<Observer> observers_;
177 // The errors which we have received so far.
178 ErrorMap errors_;
180 // The default mask to use if an Extension does not have specific settings.
181 int32 default_mask_;
183 // The profile with which the ErrorConsole is associated. Only collect errors
184 // from extensions and RenderViews associated with this Profile (and it's
185 // incognito fellow).
186 Profile* profile_;
188 // The ExtensionPrefs with which the ErrorConsole is associated. This weak
189 // pointer is safe because ErrorConsole is owned by ExtensionSystem, which
190 // is dependent on ExtensionPrefs.
191 ExtensionPrefs* prefs_;
193 content::NotificationRegistrar notification_registrar_;
194 PrefChangeRegistrar pref_registrar_;
196 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
197 registry_observer_;
199 DISALLOW_COPY_AND_ASSIGN(ErrorConsole);
202 } // namespace extensions
204 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_