Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / extensions / browser / error_map.h
bloba636b717c1d079dfabbec052d71bc55eb4b7a187
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.
5 #ifndef EXTENSIONS_BROWSER_ERROR_MAP_H_
6 #define EXTENSIONS_BROWSER_ERROR_MAP_H_
8 #include <deque>
9 #include <map>
10 #include <set>
11 #include <string>
13 #include "base/basictypes.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "extensions/browser/extension_error.h"
17 namespace extensions {
19 typedef std::deque<const ExtensionError*> ErrorList;
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:
26 ErrorMap();
27 ~ErrorMap();
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();
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();
49 bool Matches(const ExtensionError* error) const;
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;
57 // Return the list of all errors associated with the given extension.
58 const ErrorList& GetErrorsForExtension(const std::string& extension_id) const;
60 // Add the |error| to the ErrorMap.
61 const ExtensionError* AddError(scoped_ptr<ExtensionError> error);
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);
68 // Remove all errors for all extensions, and clear the map.
69 void RemoveAllErrors();
71 size_t size() const { return map_.size(); }
73 private:
74 // An Entry is created for each Extension ID, and stores the errors related to
75 // that Extension.
76 class ExtensionEntry;
77 using EntryMap = std::map<std::string, ExtensionEntry*>;
79 // The mapping between Extension IDs and their corresponding Entries.
80 EntryMap map_;
82 DISALLOW_COPY_AND_ASSIGN(ErrorMap);
85 } // namespace extensions
87 #endif // EXTENSIONS_BROWSER_ERROR_MAP_H_