error_map.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef EXTENSIONS_BROWSER_ERROR_MAP_H_
  5. #define EXTENSIONS_BROWSER_ERROR_MAP_H_
  6. #include <stddef.h>
  7. #include <map>
  8. #include <memory>
  9. #include <set>
  10. #include <string>
  11. #include "base/containers/circular_deque.h"
  12. #include "extensions/browser/extension_error.h"
  13. namespace extensions {
  14. using ErrorList = base::circular_deque<std::unique_ptr<ExtensionError>>;
  15. // An ErrorMap is responsible for storing Extension-related errors, keyed by
  16. // Extension ID.
  17. class ErrorMap {
  18. public:
  19. ErrorMap();
  20. ErrorMap(const ErrorMap&) = delete;
  21. ErrorMap& operator=(const ErrorMap&) = delete;
  22. ~ErrorMap();
  23. struct Filter {
  24. Filter(const std::string& restrict_to_extension_id,
  25. int restrict_to_type,
  26. const std::set<int>& restrict_to_ids,
  27. bool restrict_to_incognito);
  28. Filter(const Filter& other);
  29. ~Filter();
  30. // Convenience methods to get a specific type of filter. Prefer these over
  31. // the constructor when possible.
  32. static Filter ErrorsForExtension(const std::string& extension_id);
  33. static Filter ErrorsForExtensionWithType(const std::string& extension_id,
  34. ExtensionError::Type type);
  35. static Filter ErrorsForExtensionWithIds(const std::string& extension_id,
  36. const std::set<int>& ids);
  37. static Filter ErrorsForExtensionWithTypeAndIds(
  38. const std::string& extension_id,
  39. ExtensionError::Type type,
  40. const std::set<int>& ids);
  41. static Filter IncognitoErrors();
  42. bool Matches(const ExtensionError* error) const;
  43. const std::string restrict_to_extension_id;
  44. const int restrict_to_type;
  45. const std::set<int> restrict_to_ids;
  46. const bool restrict_to_incognito;
  47. };
  48. // Return the list of all errors associated with the given extension.
  49. const ErrorList& GetErrorsForExtension(const std::string& extension_id) const;
  50. // Add the |error| to the ErrorMap.
  51. const ExtensionError* AddError(std::unique_ptr<ExtensionError> error);
  52. // Removes errors that match the given |filter| from the map. If non-null,
  53. // |affected_ids| will be populated with the set of extension ids that were
  54. // affected by this removal.
  55. void RemoveErrors(const Filter& filter, std::set<std::string>* affected_ids);
  56. // Remove all errors for all extensions, and clear the map.
  57. void RemoveAllErrors();
  58. size_t size() const { return map_.size(); }
  59. private:
  60. // An Entry is created for each Extension ID, and stores the errors related to
  61. // that Extension.
  62. class ExtensionEntry;
  63. // The mapping between Extension IDs and their corresponding Entries.
  64. std::map<std::string, std::unique_ptr<ExtensionEntry>> map_;
  65. };
  66. } // namespace extensions
  67. #endif // EXTENSIONS_BROWSER_ERROR_MAP_H_