Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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 #include "chrome/browser/content_settings/content_settings_origin_identifier_val ue_map.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "base/values.h" | |
| 9 #include "googleurl/src/gurl.h" | |
| 10 | |
| 11 namespace content_settings { | |
| 12 | |
| 13 typedef OriginIdentifierValueMap::EntryList::const_iterator ConstEntryIterator; | |
|
Bernhard Bauer
2011/05/24 14:21:29
This is already defined in the header?
markusheintz_
2011/05/26 13:22:13
Done.
| |
| 14 | |
| 15 typedef OriginIdentifierValueMap::EntryList::iterator EntryIterator; | |
| 16 | |
| 17 OriginIdentifierValueMap::OriginIdentifierValueMap() {} | |
| 18 | |
| 19 OriginIdentifierValueMap::~OriginIdentifierValueMap() { | |
| 20 Clear(); | |
| 21 } | |
| 22 | |
| 23 ConstEntryIterator OriginIdentifierValueMap::ReturnEntryWithHighestPrecedence( | |
| 24 ConstEntryIterator first, | |
| 25 ConstEntryIterator second) const { | |
| 26 // Compare requesting patterns. | |
| 27 if (first->a.Compare(second->a) == ContentSettingsPattern::PREDECESSOR) | |
| 28 return first; | |
| 29 if (second->a.Compare(first->a) == ContentSettingsPattern::PREDECESSOR) | |
| 30 return second; | |
| 31 DCHECK(first->a == second->a); | |
| 32 | |
| 33 // Compare embedding patterns. | |
| 34 if (first->a.Compare(second->a) == ContentSettingsPattern::PREDECESSOR) | |
| 35 return first; | |
| 36 DCHECK(first->b.Compare(second->b) == ContentSettingsPattern::PREDECESSOR); | |
| 37 return second; | |
| 38 } | |
| 39 | |
| 40 Value* OriginIdentifierValueMap::GetValue( | |
| 41 const GURL& item_url, | |
| 42 const GURL& top_level_frame_url, | |
| 43 ContentSettingsType content_type, | |
| 44 const ResourceIdentifier& resource_identifier) const { | |
| 45 // Find best matching list entry. | |
| 46 ConstEntryIterator best_match = entries_.end(); | |
| 47 for (ConstEntryIterator entry = entries_.begin(); | |
| 48 entry != entries_.end(); | |
| 49 ++entry) { | |
| 50 if (entry->a.Matches(item_url) && | |
| 51 entry->b.Matches(top_level_frame_url) && | |
| 52 entry->c == content_type && | |
| 53 entry->d == resource_identifier) { | |
| 54 if (best_match == entries_.end()) { | |
| 55 best_match = entry; | |
| 56 } else { | |
| 57 best_match = ReturnEntryWithHighestPrecedence(best_match, entry); | |
| 58 } | |
| 59 } | |
| 60 } | |
| 61 if (best_match != entries_.end()) | |
| 62 return best_match->e; | |
| 63 return NULL; | |
| 64 } | |
| 65 | |
| 66 void OriginIdentifierValueMap::SetValue( | |
| 67 const ContentSettingsPattern& item_pattern, | |
| 68 const ContentSettingsPattern& top_level_frame_pattern, | |
| 69 ContentSettingsType content_type, | |
| 70 const ResourceIdentifier& resource_identifier, | |
| 71 Value* value) { | |
| 72 EntryIterator list_entry = FindEntry(item_pattern, | |
| 73 top_level_frame_pattern, | |
| 74 content_type, | |
| 75 resource_identifier); | |
| 76 if (list_entry == entries_.end()) { | |
| 77 // No matching list entry found. Add a new entry to the list. | |
| 78 list_entry = entries_.insert(list_entry, MakeTuple(item_pattern, | |
|
Bernhard Bauer
2011/05/24 14:21:29
aaaand the assignment to list_entry is not necessa
markusheintz_
2011/05/26 13:22:13
Done
| |
| 79 top_level_frame_pattern, | |
| 80 content_type, | |
| 81 resource_identifier, | |
| 82 value)); | |
| 83 } else { | |
| 84 // Update the list entry. | |
| 85 list_entry->e = value; | |
| 86 } | |
| 87 } | |
| 88 | |
| 89 void OriginIdentifierValueMap::DeleteValue( | |
| 90 const ContentSettingsPattern& item_pattern, | |
| 91 const ContentSettingsPattern& top_level_frame_pattern, | |
| 92 ContentSettingsType content_type, | |
| 93 const ResourceIdentifier& resource_identifier) { | |
| 94 EntryIterator entry_to_delete = FindEntry(item_pattern, | |
| 95 top_level_frame_pattern, | |
| 96 content_type, | |
| 97 resource_identifier); | |
| 98 if (entry_to_delete != entries_.end()) { | |
| 99 delete entry_to_delete->e; | |
| 100 entries_.erase(entry_to_delete); | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 void OriginIdentifierValueMap::Clear() { | |
| 105 // Delete all owned value objects. | |
| 106 for (EntryIterator entry = entries_.begin(); | |
| 107 entry != entries_.end(); | |
| 108 ++entry) { | |
| 109 delete entry->e; | |
| 110 } | |
| 111 entries_.clear(); | |
| 112 } | |
| 113 | |
| 114 EntryIterator OriginIdentifierValueMap::DeleteValue(EntryIterator entry) { | |
| 115 return entries_.erase(entry); | |
| 116 } | |
| 117 | |
| 118 EntryIterator OriginIdentifierValueMap::FindEntry( | |
| 119 const ContentSettingsPattern& item_pattern, | |
| 120 const ContentSettingsPattern& top_level_frame_pattern, | |
| 121 ContentSettingsType content_type, | |
| 122 const ResourceIdentifier& resource_identifier) { | |
| 123 for (EntryIterator entry = entries_.begin(); | |
| 124 entry != entries_.end(); | |
| 125 ++entry) { | |
| 126 if (item_pattern == entry->a && | |
| 127 top_level_frame_pattern == entry->b && | |
| 128 content_type == entry->c && | |
| 129 resource_identifier == entry->d) { | |
| 130 return entry; | |
| 131 } | |
| 132 } | |
| 133 return entries_.end(); | |
| 134 } | |
| 135 | |
| 136 } // namespace content_settings | |
| OLD | NEW |