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_provider.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "chrome/common/chrome_switches.h" | |
| 10 #include "googleurl/src/gurl.h" | |
| 11 #include "net/base/net_util.h" | |
| 12 | |
| 13 namespace { | |
| 14 | |
| 15 // True if a given content settings type requires additional resource | |
| 16 // identifiers. | |
| 17 const bool kRequiresResourceIdentifier[CONTENT_SETTINGS_NUM_TYPES] = { | |
| 18 false, // CONTENT_SETTINGS_TYPE_COOKIES | |
| 19 false, // CONTENT_SETTINGS_TYPE_IMAGES | |
| 20 false, // CONTENT_SETTINGS_TYPE_JAVASCRIPT | |
| 21 true, // CONTENT_SETTINGS_TYPE_PLUGINS | |
| 22 false, // CONTENT_SETTINGS_TYPE_POPUPS | |
| 23 false, // Not used for Geolocation | |
| 24 false, // Not used for Notifications | |
| 25 }; | |
| 26 | |
| 27 } // namespace | |
| 28 | |
| 29 namespace content_settings { | |
| 30 | |
| 31 // //////////////////////////////////////////////////////////////////////////// | |
| 32 // BaseProvider | |
| 33 // | |
| 34 bool BaseProvider::RequiresResourceIdentifier( | |
| 35 ContentSettingsType content_type) const { | |
| 36 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 37 switches::kEnableResourceContentSettings)) { | |
| 38 return kRequiresResourceIdentifier[content_type]; | |
| 39 } else { | |
| 40 return false; | |
| 41 } | |
| 42 } | |
| 43 | |
| 44 bool BaseProvider::AllDefault( | |
| 45 const ExtendedContentSettings& settings) const { | |
| 46 for (size_t i = 0; i < arraysize(settings.content_settings.settings); ++i) { | |
| 47 if (settings.content_settings.settings[i] != CONTENT_SETTING_DEFAULT) | |
| 48 return false; | |
| 49 } | |
| 50 return settings.content_settings_for_resources.empty(); | |
| 51 } | |
| 52 | |
| 53 ContentSetting BaseProvider::GetContentSetting( | |
| 54 const GURL& requesting_url, | |
| 55 const GURL& embedding_url, | |
| 56 ContentSettingsType content_type, | |
| 57 const ResourceIdentifier& resource_identifier) const { | |
| 58 // Support for embedding_patterns is not implemented yet. | |
| 59 DCHECK(requesting_url == embedding_url); | |
| 60 | |
| 61 if (!RequiresResourceIdentifier(content_type)) | |
| 62 return GetNonDefaultContentSettings(requesting_url).settings[content_type]; | |
| 63 | |
| 64 if (RequiresResourceIdentifier(content_type) && resource_identifier.empty()) | |
| 65 return CONTENT_SETTING_DEFAULT; | |
| 66 | |
| 67 // TODO(markuseheintz) Remove this DCHECK. | |
| 
 
Bernhard Bauer
2011/02/14 16:58:36
Your attempt to keep people from grepping for your
 
markusheintz_
2011/02/14 18:25:28
Damn I have to try harder. Oh ... I mean I didn't
 
 | |
| 68 if (CommandLine::ForCurrentProcess()->HasSwitch( | |
| 69 switches::kEnableResourceContentSettings)) { | |
| 70 DCHECK(!resource_identifier.empty()); | |
| 71 } | |
| 72 | |
| 73 // Resolve content settings with resource identifier. | |
| 74 // 1. Check for pattern that exactly match the url/host | |
| 75 // 1.1 In the content-settings-map | |
| 76 // 1.2 In the off_the_record content-settings-map | |
| 77 // 3. Shorten the url subdomain by subdomain and try to find a pattern in | |
| 78 // 3.1 OTR content-settings-map | |
| 79 // 3.2 content-settings-map | |
| 80 base::AutoLock auto_lock(lock_); | |
| 81 const std::string host(net::GetHostOrSpecFromURL(requesting_url)); | |
| 82 ContentSettingsTypeResourceIdentifierPair | |
| 83 requested_setting(content_type, resource_identifier); | |
| 84 | |
| 85 // Check for exact matches first. | |
| 86 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | |
| 87 if (i != host_content_settings_.end() && | |
| 88 i->second.content_settings_for_resources.find(requested_setting) != | |
| 89 i->second.content_settings_for_resources.end()) { | |
| 90 return i->second.content_settings_for_resources.find( | |
| 91 requested_setting)->second; | |
| 92 } | |
| 93 | |
| 94 // If this map is not for an off-the-record profile, these searches will never | |
| 95 // match. The additional off-the-record exceptions always overwrite the | |
| 96 // regular ones. | |
| 97 i = off_the_record_settings_.find(host); | |
| 98 if (i != off_the_record_settings_.end() && | |
| 99 i->second.content_settings_for_resources.find(requested_setting) != | |
| 100 i->second.content_settings_for_resources.end()) { | |
| 101 return i->second.content_settings_for_resources.find( | |
| 102 requested_setting)->second; | |
| 103 } | |
| 104 | |
| 105 // Match patterns starting with the most concrete pattern match. | |
| 106 for (std::string key = | |
| 107 std::string(ContentSettingsPattern::kDomainWildcard) + host; ; ) { | |
| 108 HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); | |
| 109 if (i != off_the_record_settings_.end() && | |
| 110 i->second.content_settings_for_resources.find(requested_setting) != | |
| 111 i->second.content_settings_for_resources.end()) { | |
| 112 return i->second.content_settings_for_resources.find( | |
| 113 requested_setting)->second; | |
| 114 } | |
| 115 | |
| 116 i = host_content_settings_.find(key); | |
| 117 if (i != host_content_settings_.end() && | |
| 118 i->second.content_settings_for_resources.find(requested_setting) != | |
| 119 i->second.content_settings_for_resources.end()) { | |
| 120 return i->second.content_settings_for_resources.find( | |
| 121 requested_setting)->second; | |
| 122 } | |
| 123 | |
| 124 const size_t next_dot = | |
| 125 key.find('.', ContentSettingsPattern::kDomainWildcardLength); | |
| 126 if (next_dot == std::string::npos) | |
| 127 break; | |
| 128 key.erase(ContentSettingsPattern::kDomainWildcardLength, | |
| 129 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); | |
| 130 } | |
| 131 | |
| 132 return CONTENT_SETTING_DEFAULT; | |
| 133 } | |
| 134 | |
| 135 void BaseProvider::GetAllContentSettingsRules( | |
| 136 ContentSettingsType content_type, | |
| 137 const ResourceIdentifier& resource_identifier, | |
| 138 Rules* content_setting_rules) const { | |
| 139 DCHECK(RequiresResourceIdentifier(content_type) != | |
| 140 resource_identifier.empty()); | |
| 141 DCHECK(content_setting_rules); | |
| 142 content_setting_rules->clear(); | |
| 143 | |
| 144 const HostContentSettings* map_to_return = | |
| 145 is_off_the_record_ ? &off_the_record_settings_ : &host_content_settings_; | |
| 146 ContentSettingsTypeResourceIdentifierPair requested_setting( | |
| 147 content_type, resource_identifier); | |
| 148 | |
| 149 base::AutoLock auto_lock(lock_); | |
| 150 for (HostContentSettings::const_iterator i(map_to_return->begin()); | |
| 151 i != map_to_return->end(); ++i) { | |
| 152 ContentSetting setting; | |
| 153 if (RequiresResourceIdentifier(content_type)) { | |
| 154 if (i->second.content_settings_for_resources.find(requested_setting) != | |
| 155 i->second.content_settings_for_resources.end()) { | |
| 156 setting = i->second.content_settings_for_resources.find( | |
| 157 requested_setting)->second; | |
| 158 } else { | |
| 159 setting = CONTENT_SETTING_DEFAULT; | |
| 160 } | |
| 161 } else { | |
| 162 setting = i->second.content_settings.settings[content_type]; | |
| 163 } | |
| 164 if (setting != CONTENT_SETTING_DEFAULT) { | |
| 165 // Use of push_back() relies on the map iterator traversing in order of | |
| 166 // ascending keys. | |
| 167 content_setting_rules->push_back(Rule(ContentSettingsPattern(i->first), | |
| 168 ContentSettingsPattern(i->first), | |
| 169 setting)); | |
| 170 } | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 // LEGACY: TBR | |
| 
 
Bernhard Bauer
2011/02/14 16:58:36
I think repeating the comment from the header is u
 
markusheintz_
2011/02/14 18:25:28
:-) that's fair. Removed
 
 | |
| 175 ContentSettings BaseProvider::GetNonDefaultContentSettings( | |
| 176 const GURL& url) const { | |
| 177 base::AutoLock auto_lock(lock_); | |
| 178 | |
| 179 const std::string host(net::GetHostOrSpecFromURL(url)); | |
| 180 ContentSettings output; | |
| 181 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) | |
| 182 output.settings[j] = CONTENT_SETTING_DEFAULT; | |
| 183 | |
| 184 // Check for exact matches first. | |
| 185 HostContentSettings::const_iterator i(host_content_settings_.find(host)); | |
| 186 if (i != host_content_settings_.end()) | |
| 187 output = i->second.content_settings; | |
| 188 | |
| 189 // If this map is not for an off-the-record profile, these searches will never | |
| 190 // match. The additional off-the-record exceptions always overwrite the | |
| 191 // regular ones. | |
| 192 i = off_the_record_settings_.find(host); | |
| 193 if (i != off_the_record_settings_.end()) { | |
| 194 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) | |
| 195 if (i->second.content_settings.settings[j] != CONTENT_SETTING_DEFAULT) | |
| 196 output.settings[j] = i->second.content_settings.settings[j]; | |
| 197 } | |
| 198 | |
| 199 // Match patterns starting with the most concrete pattern match. | |
| 200 for (std::string key = | |
| 201 std::string(ContentSettingsPattern::kDomainWildcard) + host; ; ) { | |
| 202 HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); | |
| 203 if (i != off_the_record_settings_.end()) { | |
| 204 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | |
| 205 if (output.settings[j] == CONTENT_SETTING_DEFAULT) | |
| 206 output.settings[j] = i->second.content_settings.settings[j]; | |
| 207 } | |
| 208 } | |
| 209 i = host_content_settings_.find(key); | |
| 210 if (i != host_content_settings_.end()) { | |
| 211 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { | |
| 212 if (output.settings[j] == CONTENT_SETTING_DEFAULT) | |
| 213 output.settings[j] = i->second.content_settings.settings[j]; | |
| 214 } | |
| 215 } | |
| 216 const size_t next_dot = | |
| 217 key.find('.', ContentSettingsPattern::kDomainWildcardLength); | |
| 218 if (next_dot == std::string::npos) | |
| 219 break; | |
| 220 key.erase(ContentSettingsPattern::kDomainWildcardLength, | |
| 221 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); | |
| 222 } | |
| 223 | |
| 224 return output; | |
| 225 } | |
| 226 | |
| 227 // //////////////////////////////////////////////////////////////////////////// | |
| 228 // ProviderUtil | |
| 229 // | |
| 230 | |
| 231 // static | |
| 232 ContentSetting ProviderUtil::ClickToPlayFixup(ContentSettingsType content_type, | |
| 233 ContentSetting setting) { | |
| 234 if (setting == CONTENT_SETTING_ASK && | |
| 235 content_type == CONTENT_SETTINGS_TYPE_PLUGINS && | |
| 236 !CommandLine::ForCurrentProcess()->HasSwitch( | |
| 237 switches::kEnableClickToPlay)) { | |
| 238 return CONTENT_SETTING_BLOCK; | |
| 239 } | |
| 240 return setting; | |
| 241 } | |
| 242 | |
| 243 } // namespace content_settings | |
| OLD | NEW |