Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / error_console / error_console.h
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.
4
5 #ifndef CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_
6 #define CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_
7
8 #include <deque>
9 #include <map>
10
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"
22
23 namespace content {
24 class NotificationDetails;
25 class NotificationSource;
26 class RenderViewHost;
27 }
28
29 class ExtensionService;
30 class Profile;
31
32 namespace extensions {
33 class Extension;
34 class ExtensionPrefs;
35
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 {
44  public:
45   class Observer {
46    public:
47     // Sent when a new error is reported to the error console.
48     virtual void OnErrorAdded(const ExtensionError* error) = 0;
49
50     // Sent upon destruction to allow any observers to invalidate any references
51     // they have to the error console.
52     virtual void OnErrorConsoleDestroyed();
53   };
54
55   explicit ErrorConsole(Profile* profile);
56   virtual ~ErrorConsole();
57
58   // Convenience method to return the ErrorConsole for a given profile.
59   static ErrorConsole* Get(Profile* profile);
60
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
63   // preferences.
64   void SetReportingForExtension(const std::string& extension_id,
65                                 ExtensionError::Type type,
66                                 bool enabled);
67
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,
71                                            bool enabled);
72
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;
76
77   // Restore default reporting to the given extension.
78   void UseDefaultReportingForExtension(const std::string& extension_id);
79
80   // Report an extension error, and add it to the list.
81   void ReportError(scoped_ptr<ExtensionError> error);
82
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;
86
87   // Add or remove observers of the ErrorConsole to be notified of any errors
88   // added.
89   void AddObserver(Observer* observer);
90   void RemoveObserver(Observer* observer);
91
92   // Returns whether or not the ErrorConsole is enabled for the
93   // chrome:extensions page or the Chrome Apps Developer Tools.
94   //
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;
100
101   // Return whether or not the ErrorConsole is enabled.
102   bool enabled() const { return enabled_; }
103
104   // Return the number of entries (extensions) in the error map.
105   size_t get_num_entries_for_test() const { return errors_.size(); }
106
107   // Set the default reporting for all extensions.
108   void set_default_reporting_for_test(ExtensionError::Type type, bool enabled) {
109     default_mask_ =
110         enabled ? default_mask_ | (1 << type) : default_mask_ & ~(1 << type);
111   }
112
113  private:
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.
116   void CheckEnabled();
117
118   // Enable the error console for error collection and retention. This involves
119   // subscribing to the appropriate notifications and fetching manifest errors.
120   void Enable();
121
122   // Disable the error console, removing the subscriptions to notifications and
123   // removing all current errors.
124   void Disable();
125
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
128   // not.
129   void OnPrefChanged();
130
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)
136       OVERRIDE;
137   virtual void OnExtensionLoaded(content::BrowserContext* browser_context,
138                                  const Extension* extension) OVERRIDE;
139
140   // Add manifest errors from an extension's install warnings.
141   void AddManifestErrorsForExtension(const Extension* extension);
142
143   // content::NotificationObserver implementation.
144   virtual void Observe(int type,
145                        const content::NotificationSource& source,
146                        const content::NotificationDetails& details) OVERRIDE;
147
148   // Returns the applicable bit mask of reporting preferences for the extension.
149   int GetMaskForExtension(const std::string& extension_id) const;
150
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.
156   bool enabled_;
157
158   // Needed because ObserverList is not thread-safe.
159   base::ThreadChecker thread_checker_;
160
161   // The list of all observers for the ErrorConsole.
162   ObserverList<Observer> observers_;
163
164   // The errors which we have received so far.
165   ErrorMap errors_;
166
167   // The default mask to use if an Extension does not have specific settings.
168   int32 default_mask_;
169
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).
173   Profile* profile_;
174
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_;
179
180   content::NotificationRegistrar notification_registrar_;
181   PrefChangeRegistrar pref_registrar_;
182
183   ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
184       registry_observer_;
185
186   DISALLOW_COPY_AND_ASSIGN(ErrorConsole);
187 };
188
189 }  // namespace extensions
190
191 #endif  // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_