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/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" 20 21 namespace content { 22 class NotificationDetails; 23 class NotificationSource; 24 class RenderViewHost; 25 } 26 27 class ExtensionService; 28 class Profile; 29 30 namespace extensions { 31 class ErrorConsoleUnitTest; 32 class Extension; 33 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 { 40 public: 41 typedef std::deque<const ExtensionError*> ErrorList; 42 typedef std::map<std::string, ErrorList> ErrorMap; 43 44 class Observer { 45 public: 46 // Sent when a new error is reported to the error console. 47 virtual void OnErrorAdded(const ExtensionError* error) = 0; 48 49 // Sent upon destruction to allow any observers to invalidate any references 50 // they have to the error console. 51 virtual void OnErrorConsoleDestroyed(); 52 }; 53 54 explicit ErrorConsole(Profile* profile, ExtensionService* extension_service); 55 virtual ~ErrorConsole(); 56 57 // Convenience method to return the ErrorConsole for a given profile. 58 static ErrorConsole* Get(Profile* profile); 59 60 // Report an extension error, and add it to the list. 61 void ReportError(scoped_ptr<ExtensionError> error); 62 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; 66 67 // Add or remove observers of the ErrorConsole to be notified of any errors 68 // added. 69 void AddObserver(Observer* observer); 70 void RemoveObserver(Observer* observer); 71 enabled()72 bool enabled() const { return enabled_; } errors()73 const ErrorMap& errors() const { return errors_; } 74 75 private: 76 FRIEND_TEST_ALL_PREFIXES(ErrorConsoleUnitTest, AddAndRemoveErrors); 77 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. 83 void Disable(); 84 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 87 // not. 88 void OnPrefChanged(); 89 90 // Add manifest errors from an extension's install warnings. 91 void AddManifestErrorsForExtension(const Extension* extension); 92 93 // Remove all errors which happened while incognito; we have to do this once 94 // the incognito profile is destroyed. 95 void RemoveIncognitoErrors(); 96 97 // Remove all errors relating to a particular |extension_id|. 98 void RemoveErrorsForExtension(const std::string& extension_id); 99 100 // Remove all errors for all extensions. 101 void RemoveAllErrors(); 102 103 // content::NotificationObserver implementation. 104 virtual void Observe(int type, 105 const content::NotificationSource& source, 106 const content::NotificationDetails& details) OVERRIDE; 107 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. 111 bool enabled_; 112 113 // Needed because ObserverList is not thread-safe. 114 base::ThreadChecker thread_checker_; 115 116 // The list of all observers for the ErrorConsole. 117 ObserverList<Observer> observers_; 118 119 // The errors which we have received so far. 120 ErrorMap errors_; 121 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). 125 Profile* profile_; 126 127 content::NotificationRegistrar notification_registrar_; 128 PrefChangeRegistrar pref_registrar_; 129 130 DISALLOW_COPY_AND_ASSIGN(ErrorConsole); 131 }; 132 133 } // namespace extensions 134 135 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ 136