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_ |
| 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 ExtensionService; | |
| 15 class Profile; | 20 class Profile; |
| 16 | 21 |
| 17 // A set of warnings caused by extensions. These warnings (e.g. conflicting | 22 // This class is used to represent warnings if extensions misbehave. |
| 18 // modifications of network requests by extensions, slow extensions, etc.) | 23 class ExtensionWarning { |
|
Aaron Boodman
2012/09/05 08:11:11
Move all this into the extensions namespace and re
battre
2012/09/06 14:25:48
I have moved it into an extensions namespace. As r
Aaron Boodman
2012/09/06 15:33:31
ok
| |
| 19 // trigger a warning badge in the UI and and provide means to resolve them. | |
| 20 class ExtensionWarningSet { | |
| 21 public: | 24 public: |
| 22 enum WarningType { | 25 enum WarningType { |
| 23 // Don't use this, it is only intended for the default constructor and | 26 // Don't use this, it is only intended for the default constructor and |
| 24 // does not have localized warning messages for the UI. | 27 // does not have localized warning messages for the UI. |
| 25 kInvalid = 0, | 28 kInvalid = 0, |
| 26 // An extension caused excessive network delays. | 29 // An extension caused excessive network delays. |
| 27 kNetworkDelay, | 30 kNetworkDelay, |
| 28 // This extension failed to modify a network request because the | 31 // This extension failed to modify a network request because the |
| 29 // modification conflicted with a modification of another extension. | 32 // modification conflicted with a modification of another extension. |
| 30 kNetworkConflict, | 33 kNetworkConflict, |
| 34 // This extension failed to redirect a network request because another | |
| 35 // extension with higher precedence redirected to a different target. | |
| 36 kRedirectConflict, | |
| 31 // The extension repeatedly flushed WebKit's in-memory cache, which slows | 37 // The extension repeatedly flushed WebKit's in-memory cache, which slows |
| 32 // down the overall performance. | 38 // down the overall performance. |
| 33 kRepeatedCacheFlushes, | 39 kRepeatedCacheFlushes, |
| 34 kMaxWarningType | 40 kMaxWarningType |
| 35 }; | 41 }; |
| 36 | 42 |
| 37 // Returns a localized string describing |warning_type|. | 43 ~ExtensionWarning(); |
| 38 static string16 GetLocalizedWarning(WarningType warning_type); | |
| 39 | 44 |
| 45 // Factory methods for various warning types. | |
| 46 static scoped_ptr<ExtensionWarning> CreateNetworkDelayWarning( | |
| 47 const std::string& extension_id); | |
| 48 static scoped_ptr<ExtensionWarning> CreateNetworkConflictWarning( | |
| 49 const std::string& extension_id); | |
| 50 static scoped_ptr<ExtensionWarning> CreateRedirectConflictWarning( | |
| 51 const std::string& extension_id, | |
| 52 const std::string& winning_extension_id, | |
| 53 const GURL& attempted_redirect_url, | |
| 54 const GURL& winning_redirect_url); | |
| 55 static scoped_ptr<ExtensionWarning> CreateRequestHeaderConflictWarning( | |
| 56 const std::string& extension_id, | |
| 57 const std::string& winning_extension_id, | |
| 58 const std::string& conflicting_header); | |
| 59 static scoped_ptr<ExtensionWarning> CreateResponseHeaderConflictWarning( | |
| 60 const std::string& extension_id, | |
| 61 const std::string& winning_extension_id, | |
| 62 const std::string& conflicting_header); | |
| 63 static scoped_ptr<ExtensionWarning> CreateCredentialsConflictWarning( | |
| 64 const std::string& extension_id, | |
| 65 const std::string& winning_extension_id); | |
| 66 static scoped_ptr<ExtensionWarning> CreateRepeatedCacheFlushesWarning( | |
| 67 const std::string& extension_id); | |
| 68 | |
| 69 // Returns the specific warning type. | |
| 70 WarningType warning_type() const { return type_; } | |
| 71 | |
| 72 // Returns the id of the extension for which this warning is valid. | |
| 73 const std::string& extension_id() const { return extension_id_; } | |
| 74 | |
| 75 // Returns a localized warning message. | |
| 76 const std::string GetMessage(ExtensionService* extension_service) const; | |
|
Aaron Boodman
2012/09/05 08:11:11
Hm, odd that this takes |extension_service|.
battre
2012/09/06 14:25:48
I need to translate extension_ids to extension nam
Aaron Boodman
2012/09/06 15:33:31
Can you pass ExtensionSet instead?
battre
2012/09/10 17:21:38
Done.
| |
| 77 | |
| 78 private: | |
| 79 // Constructs a warning of type |type| for extension |extension_id|. This | |
| 80 // could indicate for example the fact that an extension conflicted with | |
| 81 // others. The |message_id| refers to an IDS_ string ID. The | |
| 82 // |message_parameters| are filled into the message template. | |
| 83 ExtensionWarning(WarningType type, | |
| 84 const std::string& extension_id, | |
| 85 int message_id, | |
| 86 const std::vector<std::string>& message_parameters); | |
| 87 | |
| 88 WarningType type_; | |
| 89 std::string extension_id_; | |
| 90 // IDS_* resource ID. | |
| 91 int message_id_; | |
| 92 // Parameters to be filled into the string identified by |message_id_|. | |
| 93 std::vector<std::string> message_parameters_; | |
| 94 | |
| 95 DISALLOW_COPY_AND_ASSIGN(ExtensionWarning); | |
| 96 }; | |
| 97 | |
| 98 // Compare ExtensionWarnings based on the tuple of (extension_id, type). | |
| 99 // The message associated with ExtensionWarnings is purely informational | |
| 100 // and does not contribute to distinguishing extensions. | |
| 101 bool operator<(const linked_ptr<ExtensionWarning>& a, | |
| 102 const linked_ptr<ExtensionWarning>& b); | |
| 103 | |
| 104 // Container for ExtensionWarnings intended to be passed between threads. | |
| 105 // This exists to work around the lack of thread-safety of linke_ptrs. | |
| 106 // Only one thread should have access to an ExtensionWarningSet at any time. | |
| 107 class ExtensionWarningSet { | |
|
Aaron Boodman
2012/09/05 08:11:11
Can ExtensionWarningSet and ExtensionWarningServic
battre
2012/09/06 14:25:48
The reason for this class is that I need to pass a
Aaron Boodman
2012/09/06 15:33:31
Did you consider making this just std::set<Extensi
battre
2012/09/10 17:21:38
Done.
| |
| 108 public: | |
| 109 ExtensionWarningSet(); | |
|
Aaron Boodman
2012/09/05 08:11:11
It seems like a map is a better fit for this class
battre
2012/09/06 14:25:48
We need set logic. For each pair of (extension_id,
Aaron Boodman
2012/09/06 15:33:31
I see. OK.
| |
| 110 ~ExtensionWarningSet(); | |
| 111 | |
| 112 // Inserts |warning| into the warning set if it is unique under operator<(): | |
| 113 // If |warnings_| contains an entry of the same type and extension_id, the | |
| 114 // insertion is ignored. | |
| 115 void Insert(scoped_ptr<ExtensionWarning> warning); | |
| 116 | |
| 117 // Returns and resets |warnings_|. | |
| 118 std::set<linked_ptr<ExtensionWarning> > Release(); | |
| 119 | |
| 120 bool IsEmpty() const; | |
| 121 | |
| 122 private: | |
| 123 std::set<linked_ptr<ExtensionWarning> > warnings_; | |
| 124 | |
| 125 DISALLOW_COPY_AND_ASSIGN(ExtensionWarningSet); | |
| 126 }; | |
| 127 | |
| 128 // Manages a set of warnings caused by extensions. These warnings (e.g. | |
| 129 // conflicting modifications of network requests by extensions, slow extensions, | |
| 130 // etc.) trigger a warning badge in the UI and and provide means to resolve | |
| 131 // them. This class must be used on the UI thread only. | |
| 132 class ExtensionWarningService : public base::NonThreadSafe { | |
| 133 public: | |
| 40 // |profile| may be NULL for testing. In this case, be sure to not insert | 134 // |profile| may be NULL for testing. In this case, be sure to not insert |
| 41 // any warnings. | 135 // any warnings. |
| 42 explicit ExtensionWarningSet(Profile* profile); | 136 explicit ExtensionWarningService(Profile* profile); |
| 43 virtual ~ExtensionWarningSet(); | 137 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 | 138 |
| 51 // Clears all warnings of types contained in |types| and triggers a | 139 // Clears all warnings of types contained in |types| and triggers a |
| 52 // chrome::NOTIFICATION_EXTENSION_WARNING message if such warnings existed. | 140 // chrome::NOTIFICATION_EXTENSION_WARNING message if such warnings existed. |
|
Aaron Boodman
2012/09/05 08:11:11
We've been trying to reduce usage of notifications
battre
2012/09/06 14:25:48
I have created https://chromiumcodereview.appspot.
| |
| 53 // If no warning remains that is not suppressed, this may deactivate a | 141 // If no warning remains that is not suppressed, this may deactivate a |
| 54 // warning badge on the wrench mennu. | 142 // warning badge on the wrench mennu. |
| 55 void ClearWarnings(const std::set<WarningType>& types); | 143 void ClearWarnings(const std::set<ExtensionWarning::WarningType>& types); |
| 56 | 144 |
| 57 // Suppresses showing a badge for all currently existing warnings in the | 145 // Suppresses showing a badge for all currently existing warnings in the |
| 58 // future. | 146 // future. |
| 59 void SuppressBadgeForCurrentWarnings(); | 147 void SuppressBadgeForCurrentWarnings(); |
| 60 | 148 |
| 149 // Stores all types of warnings effecting extension |extension_id| in | |
| 150 // |result|. The previous content of |result| is erased. | |
| 151 void GetWarningTypesAffectingExtension( | |
| 152 const std::string& extension_id, | |
| 153 std::set<ExtensionWarning::WarningType>* result) const; | |
| 154 | |
| 61 // Stores all warnings for extension |extension_id| in |result|. The previous | 155 // Stores all warnings for extension |extension_id| in |result|. The previous |
| 62 // content of |result| is erased. | 156 // content of |result| is erased. |
| 63 void GetWarningsAffectingExtension( | 157 void GetWarningsAffectingExtension( |
| 64 const std::string& extension_id, | 158 const std::string& extension_id, |
| 65 std::set<WarningType>* result) const; | 159 std::set<linked_ptr<ExtensionWarning> >* result) const; |
| 66 | 160 |
| 67 // Notifies the ExtensionWarningSet of profile |profile_id| that | 161 // Adds a set of warnings and triggers a |
| 68 // |extension_ids| caused warning |warning_type|. This function must only be | 162 // chrome::NOTIFICATION_EXTENSION_WARNING message if any warning is new. |
| 69 // called on the UI thread. | 163 // If the warning is new and has not been suppressed, this may activate a |
| 70 static void NotifyWarningsOnUI(void* profile_id, | 164 // badge on the wrench menu. |
| 71 std::set<std::string> extension_ids, | 165 void AddWarnings(scoped_ptr<ExtensionWarningSet> warnings); |
| 72 WarningType warning_type); | 166 |
| 167 // Notifies the ExtensionWarningService of profile |profile_id| that new | |
| 168 // |warnings| occurred and triggers a warning badge. | |
| 169 static void NotifyWarningsOnUI( | |
| 170 void* profile_id, | |
| 171 scoped_ptr<ExtensionWarningSet> warnings); | |
| 73 | 172 |
| 74 protected: | 173 protected: |
| 75 // Virtual for testing. | 174 // Virtual for testing. |
| 76 virtual void NotifyWarningsChanged(); | 175 virtual void NotifyWarningsChanged(); |
| 77 | 176 |
| 78 private: | 177 private: |
| 79 typedef std::set<ExtensionWarning>::const_iterator const_iterator; | 178 typedef std::set<linked_ptr<ExtensionWarning> >::const_iterator |
| 80 typedef std::set<ExtensionWarning>::iterator iterator; | 179 const_iterator; |
| 180 typedef std::set<linked_ptr<ExtensionWarning> >::iterator iterator; | |
| 81 | 181 |
| 82 // Shows or hides the warning badge on the wrench menu depending on whether | 182 // Shows or hides the warning badge on the wrench menu depending on whether |
| 83 // any non-suppressed warnings exist. | 183 // any non-suppressed warnings exist. |
| 84 void UpdateWarningBadge(); | 184 void UpdateWarningBadge(); |
| 85 | 185 |
| 86 // Currently existing warnings. | 186 // Currently existing warnings. |
| 87 std::set<ExtensionWarning> warnings_; | 187 std::set<linked_ptr<ExtensionWarning> > warnings_; |
|
Aaron Boodman
2012/09/05 08:11:11
We have been discouraged from using linked_ptr and
battre
2012/09/06 14:25:48
I have created https://chromiumcodereview.appspot.
| |
| 88 | 188 |
| 89 // Warnings that do not trigger a badge on the wrench menu. | 189 // Warnings that do not trigger a badge on the wrench menu. |
| 90 std::set<ExtensionWarning> badge_suppressions_; | 190 std::set<linked_ptr<ExtensionWarning> > badge_suppressions_; |
| 91 | 191 |
| 92 Profile* profile_; | 192 Profile* profile_; |
| 93 }; | 193 }; |
| 94 | 194 |
| 95 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WARNING_SET_H_ | 195 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WARNING_SET_H_ |
| OLD | NEW |