OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 #include <map> | 9 #include <map> |
10 | 10 |
11 #include "base/gtest_prod_util.h" | 11 #include "base/gtest_prod_util.h" |
12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
13 #include "base/observer_list.h" | 13 #include "base/observer_list.h" |
| 14 #include "base/prefs/pref_change_registrar.h" |
14 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
15 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
16 #include "content/public/browser/notification_observer.h" | 17 #include "content/public/browser/notification_observer.h" |
17 #include "content/public/browser/notification_registrar.h" | 18 #include "content/public/browser/notification_registrar.h" |
18 #include "extensions/browser/extension_error.h" | 19 #include "extensions/browser/extension_error.h" |
19 | 20 |
20 namespace content { | 21 namespace content { |
21 class NotificationDetails; | 22 class NotificationDetails; |
22 class NotificationSource; | 23 class NotificationSource; |
23 class RenderViewHost; | 24 class RenderViewHost; |
24 } | 25 } |
25 | 26 |
| 27 class ExtensionService; |
26 class Profile; | 28 class Profile; |
27 | 29 |
28 namespace extensions { | 30 namespace extensions { |
29 class ErrorConsoleUnitTest; | 31 class ErrorConsoleUnitTest; |
| 32 class Extension; |
30 | 33 |
31 // The ErrorConsole is a central object to which all extension errors are | 34 // The ErrorConsole is a central object to which all extension errors are |
32 // reported. This includes errors detected in extensions core, as well as | 35 // reported. This includes errors detected in extensions core, as well as |
33 // runtime Javascript errors. | 36 // runtime Javascript errors. |
34 // This class is owned by ExtensionSystem, making it, in effect, a | 37 // This class is owned by ExtensionSystem, making it, in effect, a |
35 // BrowserContext-keyed service. | 38 // BrowserContext-keyed service. |
36 class ErrorConsole : public content::NotificationObserver { | 39 class ErrorConsole : public content::NotificationObserver { |
37 public: | 40 public: |
38 typedef std::deque<const ExtensionError*> ErrorList; | 41 typedef std::deque<const ExtensionError*> ErrorList; |
39 typedef std::map<std::string, ErrorList> ErrorMap; | 42 typedef std::map<std::string, ErrorList> ErrorMap; |
40 | 43 |
41 class Observer { | 44 class Observer { |
42 public: | 45 public: |
43 // Sent when a new error is reported to the error console. | 46 // Sent when a new error is reported to the error console. |
44 virtual void OnErrorAdded(const ExtensionError* error) = 0; | 47 virtual void OnErrorAdded(const ExtensionError* error) = 0; |
45 | 48 |
46 // Sent upon destruction to allow any observers to invalidate any references | 49 // Sent upon destruction to allow any observers to invalidate any references |
47 // they have to the error console. | 50 // they have to the error console. |
48 virtual void OnErrorConsoleDestroyed(); | 51 virtual void OnErrorConsoleDestroyed(); |
49 }; | 52 }; |
50 | 53 |
51 explicit ErrorConsole(Profile* profile); | 54 explicit ErrorConsole(Profile* profile, ExtensionService* extension_service); |
52 virtual ~ErrorConsole(); | 55 virtual ~ErrorConsole(); |
53 | 56 |
54 // Convenience method to return the ErrorConsole for a given profile. | 57 // Convenience method to return the ErrorConsole for a given profile. |
55 static ErrorConsole* Get(Profile* profile); | 58 static ErrorConsole* Get(Profile* profile); |
56 | 59 |
57 // Report an extension error, and add it to the list. | 60 // Report an extension error, and add it to the list. |
58 void ReportError(scoped_ptr<const ExtensionError> error); | 61 void ReportError(scoped_ptr<const ExtensionError> error); |
59 | 62 |
60 // Get a collection of weak pointers to all errors relating to the extension | 63 // Get a collection of weak pointers to all errors relating to the extension |
61 // with the given |extension_id|. | 64 // with the given |extension_id|. |
62 const ErrorList& GetErrorsForExtension(const std::string& extension_id) const; | 65 const ErrorList& GetErrorsForExtension(const std::string& extension_id) const; |
63 | 66 |
64 // Add or remove observers of the ErrorConsole to be notified of any errors | 67 // Add or remove observers of the ErrorConsole to be notified of any errors |
65 // added. | 68 // added. |
66 void AddObserver(Observer* observer); | 69 void AddObserver(Observer* observer); |
67 void RemoveObserver(Observer* observer); | 70 void RemoveObserver(Observer* observer); |
68 | 71 |
69 const ErrorMap& errors() { return errors_; } | 72 const ErrorMap& errors() { return errors_; } |
70 | 73 |
71 private: | 74 private: |
72 FRIEND_TEST_ALL_PREFIXES(ErrorConsoleUnitTest, AddAndRemoveErrors); | 75 FRIEND_TEST_ALL_PREFIXES(ErrorConsoleUnitTest, AddAndRemoveErrors); |
73 | 76 |
| 77 // Enable the error console for error collection and retention. This involves |
| 78 // subscribing to the appropriate notifications and fetching manifest errors. |
| 79 void Enable(ExtensionService* extension_service); |
| 80 // Disable the error console, removing the subscriptions to notifications and |
| 81 // removing all current errors. |
| 82 void Disable(); |
| 83 |
| 84 // Called when the Developer Mode preference is changed; this is important |
| 85 // since we use this as a heuristic to determine if the console is enabled or |
| 86 // not. |
| 87 void OnPrefChanged(); |
| 88 |
| 89 // Add manifest errors from an extension's install warnings. |
| 90 void AddManifestErrorsForExtension(const Extension* extension); |
| 91 |
74 // Remove all errors which happened while incognito; we have to do this once | 92 // Remove all errors which happened while incognito; we have to do this once |
75 // the incognito profile is destroyed. | 93 // the incognito profile is destroyed. |
76 void RemoveIncognitoErrors(); | 94 void RemoveIncognitoErrors(); |
77 | 95 |
78 // Remove all errors relating to a particular |extension_id|. | 96 // Remove all errors relating to a particular |extension_id|. |
79 void RemoveErrorsForExtension(const std::string& extension_id); | 97 void RemoveErrorsForExtension(const std::string& extension_id); |
80 | 98 |
81 // Remove all errors for all extensions. | 99 // Remove all errors for all extensions. |
82 void RemoveAllErrors(); | 100 void RemoveAllErrors(); |
83 | 101 |
84 // content::NotificationObserver implementation. | 102 // content::NotificationObserver implementation. |
85 virtual void Observe(int type, | 103 virtual void Observe(int type, |
86 const content::NotificationSource& source, | 104 const content::NotificationSource& source, |
87 const content::NotificationDetails& details) OVERRIDE; | 105 const content::NotificationDetails& details) OVERRIDE; |
88 | 106 |
| 107 // Whether or not the error console is enabled. Currently, this is set by |
| 108 // whether or not the user has Developer Mode enabled. |
| 109 bool enabled_; |
| 110 |
89 // Needed because ObserverList is not thread-safe. | 111 // Needed because ObserverList is not thread-safe. |
90 base::ThreadChecker thread_checker_; | 112 base::ThreadChecker thread_checker_; |
91 | 113 |
92 // The list of all observers for the ErrorConsole. | 114 // The list of all observers for the ErrorConsole. |
93 ObserverList<Observer> observers_; | 115 ObserverList<Observer> observers_; |
94 | 116 |
95 // The errors which we have received so far. | 117 // The errors which we have received so far. |
96 ErrorMap errors_; | 118 ErrorMap errors_; |
97 | 119 |
98 // The profile with which the ErrorConsole is associated. Only collect errors | 120 // The profile with which the ErrorConsole is associated. Only collect errors |
99 // from extensions and RenderViews associated with this Profile (and it's | 121 // from extensions and RenderViews associated with this Profile (and it's |
100 // incognito fellow). | 122 // incognito fellow). |
101 Profile* profile_; | 123 Profile* profile_; |
102 | 124 |
103 content::NotificationRegistrar registrar_; | 125 content::NotificationRegistrar notification_registrar_; |
| 126 PrefChangeRegistrar pref_registrar_; |
104 | 127 |
105 DISALLOW_COPY_AND_ASSIGN(ErrorConsole); | 128 DISALLOW_COPY_AND_ASSIGN(ErrorConsole); |
106 }; | 129 }; |
107 | 130 |
108 } // namespace extensions | 131 } // namespace extensions |
109 | 132 |
110 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ | 133 #endif // CHROME_BROWSER_EXTENSIONS_ERROR_CONSOLE_ERROR_CONSOLE_H_ |
OLD | NEW |