[email protected] | 71c10c5 | 2014-01-24 01:06:40 | [diff] [blame] | 1 | // Copyright 2014 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 EXTENSIONS_BROWSER_ERROR_MAP_H_ |
| 6 | #define EXTENSIONS_BROWSER_ERROR_MAP_H_ |
| 7 | |
| 8 | #include <deque> |
| 9 | #include <map> |
rdevlin.cronin | c799f9f | 2015-03-21 00:56:30 | [diff] [blame] | 10 | #include <set> |
[email protected] | 71c10c5 | 2014-01-24 01:06:40 | [diff] [blame] | 11 | #include <string> |
| 12 | |
| 13 | #include "base/basictypes.h" |
| 14 | #include "base/memory/scoped_ptr.h" |
| 15 | #include "extensions/browser/extension_error.h" |
| 16 | |
| 17 | namespace extensions { |
| 18 | |
| 19 | typedef std::deque<const ExtensionError*> ErrorList; |
| 20 | |
| 21 | // An ErrorMap is responsible for storing Extension-related errors, keyed by |
| 22 | // Extension ID. The errors are owned by the ErrorMap, and are deleted upon |
| 23 | // destruction. |
| 24 | class ErrorMap { |
| 25 | public: |
limasdf | d70dc5ae | 2014-09-13 00:02:22 | [diff] [blame] | 26 | ErrorMap(); |
[email protected] | 71c10c5 | 2014-01-24 01:06:40 | [diff] [blame] | 27 | ~ErrorMap(); |
| 28 | |
rdevlin.cronin | c799f9f | 2015-03-21 00:56:30 | [diff] [blame] | 29 | struct Filter { |
| 30 | Filter(const std::string& restrict_to_extension_id, |
| 31 | int restrict_to_type, |
| 32 | const std::set<int>& restrict_to_ids, |
| 33 | bool restrict_to_incognito); |
| 34 | ~Filter(); |
| 35 | |
| 36 | // Convenience methods to get a specific type of filter. Prefer these over |
| 37 | // the constructor when possible. |
| 38 | static Filter ErrorsForExtension(const std::string& extension_id); |
| 39 | static Filter ErrorsForExtensionWithType(const std::string& extension_id, |
| 40 | ExtensionError::Type type); |
| 41 | static Filter ErrorsForExtensionWithIds(const std::string& extension_id, |
| 42 | const std::set<int>& ids); |
| 43 | static Filter ErrorsForExtensionWithTypeAndIds( |
| 44 | const std::string& extension_id, |
| 45 | ExtensionError::Type type, |
| 46 | const std::set<int>& ids); |
| 47 | static Filter IncognitoErrors(); |
| 48 | |
| 49 | bool Matches(const ExtensionError* error) const; |
| 50 | |
| 51 | const std::string restrict_to_extension_id; |
| 52 | const int restrict_to_type; |
| 53 | const std::set<int> restrict_to_ids; |
| 54 | const bool restrict_to_incognito; |
| 55 | }; |
| 56 | |
[email protected] | 71c10c5 | 2014-01-24 01:06:40 | [diff] [blame] | 57 | // Return the list of all errors associated with the given extension. |
| 58 | const ErrorList& GetErrorsForExtension(const std::string& extension_id) const; |
| 59 | |
| 60 | // Add the |error| to the ErrorMap. |
| 61 | const ExtensionError* AddError(scoped_ptr<ExtensionError> error); |
| 62 | |
rdevlin.cronin | 20bf10b | 2015-04-29 16:51:43 | [diff] [blame] | 63 | // Removes errors that match the given |filter| from the map. If non-null, |
| 64 | // |affected_ids| will be populated with the set of extension ids that were |
| 65 | // affected by this removal. |
| 66 | void RemoveErrors(const Filter& filter, std::set<std::string>* affected_ids); |
rdevlin.cronin | c799f9f | 2015-03-21 00:56:30 | [diff] [blame] | 67 | |
[email protected] | 71c10c5 | 2014-01-24 01:06:40 | [diff] [blame] | 68 | // Remove all errors for all extensions, and clear the map. |
| 69 | void RemoveAllErrors(); |
| 70 | |
| 71 | size_t size() const { return map_.size(); } |
| 72 | |
| 73 | private: |
| 74 | // An Entry is created for each Extension ID, and stores the errors related to |
| 75 | // that Extension. |
rdevlin.cronin | c799f9f | 2015-03-21 00:56:30 | [diff] [blame] | 76 | class ExtensionEntry; |
| 77 | using EntryMap = std::map<std::string, ExtensionEntry*>; |
[email protected] | 71c10c5 | 2014-01-24 01:06:40 | [diff] [blame] | 78 | |
| 79 | // The mapping between Extension IDs and their corresponding Entries. |
| 80 | EntryMap map_; |
| 81 | |
| 82 | DISALLOW_COPY_AND_ASSIGN(ErrorMap); |
| 83 | }; |
| 84 | |
| 85 | } // namespace extensions |
| 86 | |
| 87 | #endif // EXTENSIONS_BROWSER_ERROR_MAP_H_ |