• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
10 #include <string>
11 
12 #include "base/basictypes.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "extensions/browser/extension_error.h"
15 
16 namespace extensions {
17 
18 typedef std::deque<const ExtensionError*> ErrorList;
19 
20 // An ErrorMap is responsible for storing Extension-related errors, keyed by
21 // Extension ID. The errors are owned by the ErrorMap, and are deleted upon
22 // destruction.
23 class ErrorMap {
24  public:
25   ErrorMap();
26   ~ErrorMap();
27 
28   // Return the list of all errors associated with the given extension.
29   const ErrorList& GetErrorsForExtension(const std::string& extension_id) const;
30 
31   // Add the |error| to the ErrorMap.
32   const ExtensionError* AddError(scoped_ptr<ExtensionError> error);
33 
34   // Remove an extension from the ErrorMap, deleting all associated errors.
35   void Remove(const std::string& extension_id);
36   // Remove all errors of a given type for an extension.
37   void RemoveErrorsForExtensionOfType(const std::string& extension_id,
38                                       ExtensionError::Type type);
39   // Remove all incognito errors for all extensions.
40   void RemoveIncognitoErrors();
41   // Remove all errors for all extensions, and clear the map.
42   void RemoveAllErrors();
43 
size()44   size_t size() const { return map_.size(); }
45 
46  private:
47   // An Entry is created for each Extension ID, and stores the errors related to
48   // that Extension.
49   class ExtensionEntry {
50    public:
51     ExtensionEntry();
52     ~ExtensionEntry();
53 
54     // Delete all errors associated with this extension.
55     void DeleteAllErrors();
56     // Delete all incognito errors associated with this extension.
57     void DeleteIncognitoErrors();
58     // Delete all errors of the given |type| associated with this extension.
59     void DeleteErrorsOfType(ExtensionError::Type type);
60 
61     // Add the error to the list, and return a weak reference.
62     const ExtensionError* AddError(scoped_ptr<ExtensionError> error);
63 
list()64     const ErrorList* list() const { return &list_; }
65 
66    private:
67     // The list of all errors associated with the extension. The errors are
68     // owned by the Entry (in turn owned by the ErrorMap) and are deleted upon
69     // destruction.
70     ErrorList list_;
71 
72     DISALLOW_COPY_AND_ASSIGN(ExtensionEntry);
73   };
74   typedef std::map<std::string, ExtensionEntry*> EntryMap;
75 
76   // The mapping between Extension IDs and their corresponding Entries.
77   EntryMap map_;
78 
79   DISALLOW_COPY_AND_ASSIGN(ErrorMap);
80 };
81 
82 }  // namespace extensions
83 
84 #endif  // EXTENSIONS_BROWSER_ERROR_MAP_H_
85