summaryrefslogtreecommitdiffstats
path: root/extensions/browser/error_map.h
blob: 2dcff220cb56ee4abc3b7b92a80c4d0e99d93b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef EXTENSIONS_BROWSER_ERROR_MAP_H_
#define EXTENSIONS_BROWSER_ERROR_MAP_H_

#include <deque>
#include <map>
#include <string>

#include "base/basictypes.h"
#include "base/memory/scoped_ptr.h"
#include "extensions/browser/extension_error.h"

namespace extensions {

typedef std::deque<const ExtensionError*> ErrorList;

// An ErrorMap is responsible for storing Extension-related errors, keyed by
// Extension ID. The errors are owned by the ErrorMap, and are deleted upon
// destruction.
class ErrorMap {
 public:
  explicit ErrorMap();
  ~ErrorMap();

  // Return the list of all errors associated with the given extension.
  const ErrorList& GetErrorsForExtension(const std::string& extension_id) const;

  // Add the |error| to the ErrorMap.
  const ExtensionError* AddError(scoped_ptr<ExtensionError> error);

  // Remove an extension from the ErrorMap, deleting all associated errors.
  void Remove(const std::string& extension_id);
  // Remove all errors of a given type for an extension.
  void RemoveErrorsForExtensionOfType(const std::string& extension_id,
                                      ExtensionError::Type type);
  // Remove all incognito errors for all extensions.
  void RemoveIncognitoErrors();
  // Remove all errors for all extensions, and clear the map.
  void RemoveAllErrors();

  size_t size() const { return map_.size(); }

 private:
  // An Entry is created for each Extension ID, and stores the errors related to
  // that Extension.
  class ExtensionEntry {
   public:
    explicit ExtensionEntry();
    ~ExtensionEntry();

    // Delete all errors associated with this extension.
    void DeleteAllErrors();
    // Delete all incognito errors associated with this extension.
    void DeleteIncognitoErrors();
    // Delete all errors of the given |type| associated with this extension.
    void DeleteErrorsOfType(ExtensionError::Type type);

    // Add the error to the list, and return a weak reference.
    const ExtensionError* AddError(scoped_ptr<ExtensionError> error);

    const ErrorList* list() const { return &list_; }

   private:
    // The list of all errors associated with the extension. The errors are
    // owned by the Entry (in turn owned by the ErrorMap) and are deleted upon
    // destruction.
    ErrorList list_;

    DISALLOW_COPY_AND_ASSIGN(ExtensionEntry);
  };
  typedef std::map<std::string, ExtensionEntry*> EntryMap;

  // The mapping between Extension IDs and their corresponding Entries.
  EntryMap map_;

  DISALLOW_COPY_AND_ASSIGN(ErrorMap);
};

}  // namespace extensions

#endif  // EXTENSIONS_BROWSER_ERROR_MAP_H_