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