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