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