1// Copyright (c) 2012 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 COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_ORIGIN_IDENTIFIER_VALUE_MAP_H_
6#define COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_ORIGIN_IDENTIFIER_VALUE_MAP_H_
7
8#include <map>
9#include <string>
10
11#include "base/memory/linked_ptr.h"
12#include "components/content_settings/core/common/content_settings_pattern.h"
13#include "components/content_settings/core/common/content_settings_types.h"
14
15class GURL;
16
17namespace base {
18class Lock;
19class Value;
20}
21
22namespace content_settings {
23
24class RuleIterator;
25
26class OriginIdentifierValueMap {
27 public:
28  typedef std::string ResourceIdentifier;
29  struct EntryMapKey {
30    ContentSettingsType content_type;
31    ResourceIdentifier resource_identifier;
32    EntryMapKey(ContentSettingsType content_type,
33                const ResourceIdentifier& resource_identifier);
34    bool operator<(const OriginIdentifierValueMap::EntryMapKey& other) const;
35  };
36
37  struct PatternPair {
38    ContentSettingsPattern primary_pattern;
39    ContentSettingsPattern secondary_pattern;
40    PatternPair(const ContentSettingsPattern& primary_pattern,
41                const ContentSettingsPattern& secondary_pattern);
42    bool operator<(const OriginIdentifierValueMap::PatternPair& other) const;
43  };
44
45  typedef std::map<PatternPair, linked_ptr<base::Value> > Rules;
46  typedef std::map<EntryMapKey, Rules> EntryMap;
47
48  EntryMap::iterator begin() {
49    return entries_.begin();
50  }
51
52  EntryMap::iterator end() {
53    return entries_.end();
54  }
55
56  EntryMap::const_iterator begin() const {
57    return entries_.begin();
58  }
59
60  EntryMap::const_iterator end() const {
61    return entries_.end();
62  }
63
64  bool empty() const {
65    return size() == 0u;
66  }
67
68  size_t size() const;
69
70  // Returns an iterator for reading the rules for |content_type| and
71  // |resource_identifier|. The caller takes the ownership of the iterator. It
72  // is not allowed to call functions of |OriginIdentifierValueMap| (also
73  // |GetRuleIterator|) before the iterator has been destroyed. If |lock| is
74  // non-NULL, the returned |RuleIterator| locks it and releases it when it is
75  // destroyed.
76  RuleIterator* GetRuleIterator(ContentSettingsType content_type,
77                                const ResourceIdentifier& resource_identifier,
78                                base::Lock* lock) const;
79
80  OriginIdentifierValueMap();
81  ~OriginIdentifierValueMap();
82
83  // Returns a weak pointer to the value for the given |primary_pattern|,
84  // |secondary_pattern|, |content_type|, |resource_identifier| tuple. If
85  // no value is stored for the passed parameter |NULL| is returned.
86  base::Value* GetValue(
87      const GURL& primary_url,
88      const GURL& secondary_url,
89      ContentSettingsType content_type,
90      const ResourceIdentifier& resource_identifier) const;
91
92  // Sets the |value| for the given |primary_pattern|, |secondary_pattern|,
93  // |content_type|, |resource_identifier| tuple. The method takes the ownership
94  // of the passed |value|.
95  void SetValue(
96      const ContentSettingsPattern& primary_pattern,
97      const ContentSettingsPattern& secondary_pattern,
98      ContentSettingsType content_type,
99      const ResourceIdentifier& resource_identifier,
100      base::Value* value);
101
102  // Deletes the map entry for the given |primary_pattern|,
103  // |secondary_pattern|, |content_type|, |resource_identifier| tuple.
104  void DeleteValue(
105      const ContentSettingsPattern& primary_pattern,
106      const ContentSettingsPattern& secondary_pattern,
107      ContentSettingsType content_type,
108      const ResourceIdentifier& resource_identifier);
109
110  // Deletes all map entries for the given |content_type| and
111  // |resource_identifier|.
112  void DeleteValues(
113      ContentSettingsType content_type,
114      const ResourceIdentifier& resource_identifier);
115
116  // Clears all map entries.
117  void clear();
118
119 private:
120  EntryMap entries_;
121
122  DISALLOW_COPY_AND_ASSIGN(OriginIdentifierValueMap);
123};
124
125}  // namespace content_settings
126
127#endif  // COMPONENTS_CONTENT_SETTINGS_CORE_BROWSER_CONTENT_SETTINGS_ORIGIN_IDENTIFIER_VALUE_MAP_H_
128