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/extensions/extension_content_settings_store.h" |
| 6 |
| 7 #include <set> |
| 8 |
| 9 #include "base/logging.h" |
| 10 |
| 11 struct ExtensionContentSettingsStore::ExtensionEntry { |
| 12 // Installation time of the extension. |
| 13 base::Time install_time; |
| 14 // Whether extension is enabled in the profile. |
| 15 bool enabled; |
| 16 // Content settings. |
| 17 ContentSettingSpecList settings; |
| 18 // Incognito content settings |
| 19 ContentSettingSpecList incognito_settings; |
| 20 }; |
| 21 |
| 22 ExtensionContentSettingsStore::ExtensionContentSettingsStore() { |
| 23 } |
| 24 |
| 25 ExtensionContentSettingsStore::~ExtensionContentSettingsStore() { |
| 26 NotifyOfDestruction(); |
| 27 } |
| 28 |
| 29 void ExtensionContentSettingsStore::SetExtensionDefaultContentSetting( |
| 30 ContentSettingsType content_type, |
| 31 ContentSetting setting) { |
| 32 // TODO(markusheintz): Implement. |
| 33 } |
| 34 |
| 35 ContentSetting |
| 36 ExtensionContentSettingsStore::GetEffectiveDefaultContentSetting( |
| 37 ContentSettingsType content_type) { |
| 38 // TODO(markusheintz): Implement. |
| 39 return CONTENT_SETTING_DEFAULT; |
| 40 } |
| 41 |
| 42 void ExtensionContentSettingsStore::SetExtensionContentSetting( |
| 43 const std::string& ext_id, |
| 44 const ContentSettingsPattern& pattern, |
| 45 const ContentSettingsPattern& embedder_pattern, |
| 46 const ContentSettingsType& type, |
| 47 const content_settings::ResourceIdentifier& identifier, |
| 48 ContentSetting setting, |
| 49 bool incognito) { |
| 50 ContentSettingSpecList* setting_spec_list = |
| 51 GetContentSettingSpecList(ext_id, incognito); |
| 52 |
| 53 // Find |ContentSettingSpec| |
| 54 ContentSettingSpecList::iterator setting_spec = setting_spec_list->begin(); |
| 55 while (setting_spec != setting_spec_list->end()) { |
| 56 if (setting_spec->a == pattern && |
| 57 setting_spec->b == embedder_pattern && |
| 58 setting_spec->c == type && |
| 59 setting_spec->d == identifier) { |
| 60 break; |
| 61 } |
| 62 ++setting_spec; |
| 63 } |
| 64 if (setting_spec == setting_spec_list->end()) { |
| 65 setting_spec = setting_spec_list->insert( |
| 66 setting_spec_list->end(), |
| 67 MakeTuple(pattern, embedder_pattern, type, identifier, setting)); |
| 68 } |
| 69 |
| 70 // Update setting |
| 71 if (setting != CONTENT_SETTING_DEFAULT) { |
| 72 setting_spec->e = setting; |
| 73 } else { |
| 74 setting_spec_list->erase(setting_spec); |
| 75 } |
| 76 |
| 77 // Send notification that content settings changed |
| 78 // TODO(markusheintz): Notifications should only be sent if the set content |
| 79 // setting is effective and not hidden by an other setting of an other |
| 80 // extensions installed more recently. |
| 81 NotifyOfContentSettingChanged( |
| 82 pattern, |
| 83 embedder_pattern, |
| 84 type, |
| 85 identifier, |
| 86 setting, |
| 87 incognito); |
| 88 } |
| 89 |
| 90 void ExtensionContentSettingsStore::RegisterExtension( |
| 91 const std::string& ext_id, |
| 92 const base::Time& install_time, |
| 93 bool is_enabled) { |
| 94 if (entries_.find(ext_id) != entries_.end()) |
| 95 UnregisterExtension(ext_id); |
| 96 entries_[ext_id] = new ExtensionEntry; |
| 97 entries_[ext_id]->install_time = install_time; |
| 98 entries_[ext_id]->enabled = is_enabled; |
| 99 } |
| 100 void ExtensionContentSettingsStore::UnregisterExtension( |
| 101 const std::string& ext_id) { |
| 102 ExtensionEntryMap::iterator i = entries_.find(ext_id); |
| 103 if (i == entries_.end()) |
| 104 return; |
| 105 |
| 106 delete i->second; |
| 107 entries_.erase(i); |
| 108 } |
| 109 |
| 110 void ExtensionContentSettingsStore::SetExtensionState( |
| 111 const std::string& ext_id, bool is_enabled) { |
| 112 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
| 113 CHECK(i != entries_.end()); |
| 114 if (i->second->enabled == is_enabled) |
| 115 return; |
| 116 i->second->enabled = is_enabled; |
| 117 } |
| 118 |
| 119 ExtensionContentSettingsStore::ContentSettingSpecList* |
| 120 ExtensionContentSettingsStore::GetContentSettingSpecList( |
| 121 const std::string& ext_id, |
| 122 bool incognito) { |
| 123 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
| 124 CHECK(i != entries_.end()); |
| 125 return incognito ? &(i->second->incognito_settings) |
| 126 : &(i->second->settings); |
| 127 } |
| 128 |
| 129 const ExtensionContentSettingsStore::ContentSettingSpecList* |
| 130 ExtensionContentSettingsStore::GetContentSettingSpecList( |
| 131 const std::string& ext_id, |
| 132 bool incognito) const { |
| 133 ExtensionEntryMap::const_iterator i = entries_.find(ext_id); |
| 134 CHECK(i != entries_.end()); |
| 135 return incognito ? &(i->second->incognito_settings) |
| 136 : &(i->second->settings); |
| 137 } |
| 138 |
| 139 ContentSetting ExtensionContentSettingsStore::GetContentSettingFrom( |
| 140 const GURL& url, |
| 141 const GURL& embedder_url, |
| 142 const ContentSettingsType& type, |
| 143 const content_settings::ResourceIdentifier& identifier, |
| 144 const ContentSettingSpecList* setting_spec_list) const { |
| 145 ContentSettingSpecList::const_iterator winner_spec = |
| 146 setting_spec_list->end(); |
| 147 |
| 148 for (ContentSettingSpecList::const_iterator spec = |
| 149 setting_spec_list->begin(); |
| 150 spec!= setting_spec_list->end(); |
| 151 ++spec) { |
| 152 if (!spec->a.Matches(url) || |
| 153 !spec->b.Matches(embedder_url) || |
| 154 spec->c != type || |
| 155 spec->d != identifier) { |
| 156 continue; |
| 157 } |
| 158 |
| 159 if (winner_spec == setting_spec_list->end() || |
| 160 spec->a.Compare(winner_spec->a) == ContentSettingsPattern::SUCCESSOR) { |
| 161 winner_spec = spec; |
| 162 } |
| 163 } |
| 164 |
| 165 return (winner_spec != setting_spec_list->end()) ? winner_spec->e |
| 166 : CONTENT_SETTING_DEFAULT; |
| 167 } |
| 168 |
| 169 ContentSetting ExtensionContentSettingsStore::GetEffectiveContentSetting( |
| 170 const GURL& url, |
| 171 const GURL& embedder_url, |
| 172 const ContentSettingsType& type, |
| 173 const content_settings::ResourceIdentifier& identifier, |
| 174 bool incognito) const { |
| 175 ExtensionEntryMap::const_iterator winner = entries_.end(); |
| 176 base::Time winners_install_time; |
| 177 ContentSetting winner_setting = CONTENT_SETTING_DEFAULT; |
| 178 |
| 179 ExtensionEntryMap::const_iterator i; |
| 180 for (i = entries_.begin(); i != entries_.end(); ++i) { |
| 181 const std::string& ext_id = i->first; |
| 182 const base::Time& install_time = i->second->install_time; |
| 183 const bool enabled = i->second->enabled; |
| 184 |
| 185 if (!enabled) |
| 186 continue; |
| 187 if (install_time < winners_install_time) |
| 188 continue; |
| 189 |
| 190 const ContentSettingSpecList* setting_spec_list = |
| 191 GetContentSettingSpecList(ext_id, false); |
| 192 // Get non incognito setting |
| 193 ContentSetting setting = GetContentSettingFrom( |
| 194 url, |
| 195 embedder_url, |
| 196 type, |
| 197 identifier, |
| 198 setting_spec_list); |
| 199 |
| 200 if (setting != CONTENT_SETTING_DEFAULT) { |
| 201 winner = i; |
| 202 winners_install_time = install_time; |
| 203 winner_setting = setting; |
| 204 } |
| 205 |
| 206 if (!incognito) |
| 207 continue; |
| 208 // Get incognito setting |
| 209 setting_spec_list = |
| 210 GetContentSettingSpecList(ext_id, true); |
| 211 setting = GetContentSettingFrom( |
| 212 url, |
| 213 embedder_url, |
| 214 type, |
| 215 identifier, |
| 216 setting_spec_list); |
| 217 |
| 218 if (setting != CONTENT_SETTING_DEFAULT) { |
| 219 winner = i; |
| 220 winners_install_time = install_time; |
| 221 winner_setting = setting; |
| 222 } |
| 223 } |
| 224 |
| 225 return winner_setting; |
| 226 } |
| 227 |
| 228 void ExtensionContentSettingsStore::AddObserver(Observer* observer) { |
| 229 observers_.AddObserver(observer); |
| 230 } |
| 231 |
| 232 void ExtensionContentSettingsStore::RemoveObserver(Observer* observer) { |
| 233 observers_.RemoveObserver(observer); |
| 234 } |
| 235 |
| 236 void ExtensionContentSettingsStore::NotifyOfDestruction() { |
| 237 FOR_EACH_OBSERVER(ExtensionContentSettingsStore::Observer, |
| 238 observers_, |
| 239 OnDestruction()); |
| 240 } |
| 241 |
| 242 void ExtensionContentSettingsStore::NotifyOfContentSettingChanged( |
| 243 const ContentSettingsPattern& pattern, |
| 244 const ContentSettingsPattern& embedder_pattern, |
| 245 const ContentSettingsType& type, |
| 246 const content_settings::ResourceIdentifier& identifier, |
| 247 ContentSetting setting, |
| 248 bool incognito) { |
| 249 FOR_EACH_OBSERVER(ExtensionContentSettingsStore::Observer, |
| 250 observers_, |
| 251 OnContentSettingChanged( |
| 252 pattern, |
| 253 embedder_pattern, |
| 254 type, |
| 255 identifier, |
| 256 setting, |
| 257 incognito)); |
| 258 } |
OLD | NEW |