| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_EXTENSION_WARNING_SET_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WARNING_SET_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WARNING_SET_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WARNING_SET_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> |
| 10 | 11 |
| 12 #include "base/memory/linked_ptr.h" |
| 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/observer_list.h" |
| 11 #include "base/string16.h" | 15 #include "base/string16.h" |
| 16 #include "base/threading/non_thread_safe.h" |
| 17 #include "content/public/browser/notification_observer.h" |
| 18 #include "content/public/browser/notification_registrar.h" |
| 19 #include "googleurl/src/gurl.h" |
| 12 | 20 |
| 13 class ExtensionWarning; | |
| 14 class ExtensionGlobalErrorBadge; | 21 class ExtensionGlobalErrorBadge; |
| 22 class ExtensionSet; |
| 15 class Profile; | 23 class Profile; |
| 16 | 24 |
| 17 // A set of warnings caused by extensions. These warnings (e.g. conflicting | 25 // TODO(battre) Remove the Extension prefix. |
| 18 // modifications of network requests by extensions, slow extensions, etc.) | 26 |
| 19 // trigger a warning badge in the UI and and provide means to resolve them. | 27 namespace content { |
| 20 class ExtensionWarningSet { | 28 class NotificationDetails; |
| 29 class NotificationSource; |
| 30 } |
| 31 |
| 32 namespace extensions { |
| 33 |
| 34 // This class is used to represent warnings if extensions misbehave. |
| 35 class ExtensionWarning { |
| 21 public: | 36 public: |
| 22 enum WarningType { | 37 enum WarningType { |
| 23 // Don't use this, it is only intended for the default constructor and | 38 // Don't use this, it is only intended for the default constructor and |
| 24 // does not have localized warning messages for the UI. | 39 // does not have localized warning messages for the UI. |
| 25 kInvalid = 0, | 40 kInvalid = 0, |
| 26 // An extension caused excessive network delays. | 41 // An extension caused excessive network delays. |
| 27 kNetworkDelay, | 42 kNetworkDelay, |
| 28 // This extension failed to modify a network request because the | 43 // This extension failed to modify a network request because the |
| 29 // modification conflicted with a modification of another extension. | 44 // modification conflicted with a modification of another extension. |
| 30 kNetworkConflict, | 45 kNetworkConflict, |
| 46 // This extension failed to redirect a network request because another |
| 47 // extension with higher precedence redirected to a different target. |
| 48 kRedirectConflict, |
| 31 // The extension repeatedly flushed WebKit's in-memory cache, which slows | 49 // The extension repeatedly flushed WebKit's in-memory cache, which slows |
| 32 // down the overall performance. | 50 // down the overall performance. |
| 33 kRepeatedCacheFlushes, | 51 kRepeatedCacheFlushes, |
| 34 kMaxWarningType | 52 kMaxWarningType |
| 35 }; | 53 }; |
| 36 | 54 |
| 37 // Returns a localized string describing |warning_type|. | 55 ExtensionWarning(const ExtensionWarning& other); |
| 38 static string16 GetLocalizedWarning(WarningType warning_type); | 56 ~ExtensionWarning(); |
| 57 ExtensionWarning& operator=(const ExtensionWarning& other); |
| 58 |
| 59 // Factory methods for various warning types. |
| 60 static ExtensionWarning CreateNetworkDelayWarning( |
| 61 const std::string& extension_id); |
| 62 static ExtensionWarning CreateNetworkConflictWarning( |
| 63 const std::string& extension_id); |
| 64 static ExtensionWarning CreateRedirectConflictWarning( |
| 65 const std::string& extension_id, |
| 66 const std::string& winning_extension_id, |
| 67 const GURL& attempted_redirect_url, |
| 68 const GURL& winning_redirect_url); |
| 69 static ExtensionWarning CreateRequestHeaderConflictWarning( |
| 70 const std::string& extension_id, |
| 71 const std::string& winning_extension_id, |
| 72 const std::string& conflicting_header); |
| 73 static ExtensionWarning CreateResponseHeaderConflictWarning( |
| 74 const std::string& extension_id, |
| 75 const std::string& winning_extension_id, |
| 76 const std::string& conflicting_header); |
| 77 static ExtensionWarning CreateCredentialsConflictWarning( |
| 78 const std::string& extension_id, |
| 79 const std::string& winning_extension_id); |
| 80 static ExtensionWarning CreateRepeatedCacheFlushesWarning( |
| 81 const std::string& extension_id); |
| 82 |
| 83 // Returns the specific warning type. |
| 84 WarningType warning_type() const { return type_; } |
| 85 |
| 86 // Returns the id of the extension for which this warning is valid. |
| 87 const std::string& extension_id() const { return extension_id_; } |
| 88 |
| 89 // Returns a localized warning message. |
| 90 const std::string GetMessage(const ExtensionSet* extensions) const; |
| 91 |
| 92 private: |
| 93 // Constructs a warning of type |type| for extension |extension_id|. This |
| 94 // could indicate for example the fact that an extension conflicted with |
| 95 // others. The |message_id| refers to an IDS_ string ID. The |
| 96 // |message_parameters| are filled into the message template. |
| 97 ExtensionWarning(WarningType type, |
| 98 const std::string& extension_id, |
| 99 int message_id, |
| 100 const std::vector<std::string>& message_parameters); |
| 101 |
| 102 WarningType type_; |
| 103 std::string extension_id_; |
| 104 // IDS_* resource ID. |
| 105 int message_id_; |
| 106 // Parameters to be filled into the string identified by |message_id_|. |
| 107 std::vector<std::string> message_parameters_; |
| 108 }; |
| 109 |
| 110 // Compare ExtensionWarnings based on the tuple of (extension_id, type). |
| 111 // The message associated with ExtensionWarnings is purely informational |
| 112 // and does not contribute to distinguishing extensions. |
| 113 bool operator<(const ExtensionWarning& a, const ExtensionWarning& b); |
| 114 |
| 115 // Manages a set of warnings caused by extensions. These warnings (e.g. |
| 116 // conflicting modifications of network requests by extensions, slow extensions, |
| 117 // etc.) may be used for example to trigger a warning badge in the UI and and |
| 118 // provide means to resolve them. This class must be used on the UI thread only. |
| 119 class ExtensionWarningSet : public content::NotificationObserver, |
| 120 public base::NonThreadSafe { |
| 121 public: |
| 122 class Observer { |
| 123 public: |
| 124 virtual void ExtensionWarningsChanged() = 0; |
| 125 }; |
| 39 | 126 |
| 40 // |profile| may be NULL for testing. In this case, be sure to not insert | 127 // |profile| may be NULL for testing. In this case, be sure to not insert |
| 41 // any warnings. | 128 // any warnings. |
| 42 explicit ExtensionWarningSet(Profile* profile); | 129 explicit ExtensionWarningSet(Profile* profile); |
| 43 virtual ~ExtensionWarningSet(); | 130 virtual ~ExtensionWarningSet(); |
| 44 | 131 |
| 45 // Adds a warning and triggers a chrome::NOTIFICATION_EXTENSION_WARNING | 132 // Clears all warnings of types contained in |types| and notifies observers |
| 46 // message if this warning is is new. If the warning is new and has not | 133 // of the changed warnings. |
| 47 // been suppressed, this may activate a badge on the wrench menu. | 134 void ClearWarnings(const std::set<ExtensionWarning::WarningType>& types); |
| 48 void SetWarning(ExtensionWarningSet::WarningType type, | |
| 49 const std::string& extension_id); | |
| 50 | 135 |
| 51 // Clears all warnings of types contained in |types| and triggers a | 136 // Stores all types of warnings effecting extension |extension_id| in |
| 52 // chrome::NOTIFICATION_EXTENSION_WARNING message if such warnings existed. | 137 // |result|. The previous content of |result| is erased. |
| 53 // If no warning remains that is not suppressed, this may deactivate a | 138 void GetWarningTypesAffectingExtension( |
| 54 // warning badge on the wrench mennu. | 139 const std::string& extension_id, |
| 55 void ClearWarnings(const std::set<WarningType>& types); | 140 std::set<ExtensionWarning::WarningType>* result) const; |
| 56 | 141 |
| 57 // Suppresses showing a badge for all currently existing warnings in the | 142 // Stores all localized warnings for extension |extension_id| in |result|. |
| 58 // future. | 143 // The previous content of |result| is erased. |
| 59 void SuppressBadgeForCurrentWarnings(); | 144 void GetWarningMessagesForExtension( |
| 145 const std::string& extension_id, |
| 146 std::vector<std::string>* result) const; |
| 60 | 147 |
| 61 // Stores all warnings for extension |extension_id| in |result|. The previous | 148 const std::set<ExtensionWarning>& warnings() const { return warnings_; } |
| 62 // content of |result| is erased. | |
| 63 void GetWarningsAffectingExtension( | |
| 64 const std::string& extension_id, | |
| 65 std::set<WarningType>* result) const; | |
| 66 | 149 |
| 67 // Notifies the ExtensionWarningSet of profile |profile_id| that | 150 // Adds a set of warnings and notifies observers if any warning is new. |
| 68 // |extension_ids| caused warning |warning_type|. This function must only be | 151 void AddWarnings(const std::set<ExtensionWarning>& warnings); |
| 69 // called on the UI thread. | 152 |
| 153 // Notifies the ExtensionWarningSet of profile |profile_id| that new |
| 154 // |warnings| occurred and triggers a warning badge. |
| 70 static void NotifyWarningsOnUI(void* profile_id, | 155 static void NotifyWarningsOnUI(void* profile_id, |
| 71 std::set<std::string> extension_ids, | 156 std::set<ExtensionWarning> warnings); |
| 72 WarningType warning_type); | |
| 73 | 157 |
| 74 protected: | 158 void AddObserver(Observer* observer); |
| 75 // Virtual for testing. | 159 void RemoveObserver(Observer* observer); |
| 76 virtual void NotifyWarningsChanged(); | |
| 77 | 160 |
| 78 private: | 161 private: |
| 79 typedef std::set<ExtensionWarning>::const_iterator const_iterator; | 162 typedef std::set<ExtensionWarning>::const_iterator const_iterator; |
| 80 typedef std::set<ExtensionWarning>::iterator iterator; | 163 typedef std::set<ExtensionWarning>::iterator iterator; |
| 81 | 164 |
| 82 // Shows or hides the warning badge on the wrench menu depending on whether | 165 void NotifyWarningsChanged(); |
| 83 // any non-suppressed warnings exist. | 166 |
| 84 void UpdateWarningBadge(); | 167 // Implementation for content::NotificationObserver. |
| 168 virtual void Observe(int type, |
| 169 const content::NotificationSource& source, |
| 170 const content::NotificationDetails& details) OVERRIDE; |
| 85 | 171 |
| 86 // Currently existing warnings. | 172 // Currently existing warnings. |
| 87 std::set<ExtensionWarning> warnings_; | 173 std::set<ExtensionWarning> warnings_; |
| 88 | 174 |
| 89 // Warnings that do not trigger a badge on the wrench menu. | 175 content::NotificationRegistrar registrar_; |
| 90 std::set<ExtensionWarning> badge_suppressions_; | |
| 91 | 176 |
| 92 Profile* profile_; | 177 Profile* profile_; |
| 178 |
| 179 ObserverList<Observer> observer_list_; |
| 93 }; | 180 }; |
| 94 | 181 |
| 182 } // namespace extensions |
| 183 |
| 95 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WARNING_SET_H_ | 184 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WARNING_SET_H_ |
| OLD | NEW |