| 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(markusheintz) Remove this DCHECK. |
| 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 ContentSettings BaseProvider::GetNonDefaultContentSettings( |
| 175 const GURL& url) const { |
| 176 base::AutoLock auto_lock(lock_); |
| 177 |
| 178 const std::string host(net::GetHostOrSpecFromURL(url)); |
| 179 ContentSettings output; |
| 180 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) |
| 181 output.settings[j] = CONTENT_SETTING_DEFAULT; |
| 182 |
| 183 // Check for exact matches first. |
| 184 HostContentSettings::const_iterator i(host_content_settings_.find(host)); |
| 185 if (i != host_content_settings_.end()) |
| 186 output = i->second.content_settings; |
| 187 |
| 188 // If this map is not for an off-the-record profile, these searches will never |
| 189 // match. The additional off-the-record exceptions always overwrite the |
| 190 // regular ones. |
| 191 i = off_the_record_settings_.find(host); |
| 192 if (i != off_the_record_settings_.end()) { |
| 193 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) |
| 194 if (i->second.content_settings.settings[j] != CONTENT_SETTING_DEFAULT) |
| 195 output.settings[j] = i->second.content_settings.settings[j]; |
| 196 } |
| 197 |
| 198 // Match patterns starting with the most concrete pattern match. |
| 199 for (std::string key = |
| 200 std::string(ContentSettingsPattern::kDomainWildcard) + host; ; ) { |
| 201 HostContentSettings::const_iterator i(off_the_record_settings_.find(key)); |
| 202 if (i != off_the_record_settings_.end()) { |
| 203 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 204 if (output.settings[j] == CONTENT_SETTING_DEFAULT) |
| 205 output.settings[j] = i->second.content_settings.settings[j]; |
| 206 } |
| 207 } |
| 208 i = host_content_settings_.find(key); |
| 209 if (i != host_content_settings_.end()) { |
| 210 for (int j = 0; j < CONTENT_SETTINGS_NUM_TYPES; ++j) { |
| 211 if (output.settings[j] == CONTENT_SETTING_DEFAULT) |
| 212 output.settings[j] = i->second.content_settings.settings[j]; |
| 213 } |
| 214 } |
| 215 const size_t next_dot = |
| 216 key.find('.', ContentSettingsPattern::kDomainWildcardLength); |
| 217 if (next_dot == std::string::npos) |
| 218 break; |
| 219 key.erase(ContentSettingsPattern::kDomainWildcardLength, |
| 220 next_dot - ContentSettingsPattern::kDomainWildcardLength + 1); |
| 221 } |
| 222 |
| 223 return output; |
| 224 } |
| 225 |
| 226 // //////////////////////////////////////////////////////////////////////////// |
| 227 // ProviderUtil |
| 228 // |
| 229 |
| 230 // static |
| 231 ContentSetting ProviderUtil::ClickToPlayFixup(ContentSettingsType content_type, |
| 232 ContentSetting setting) { |
| 233 if (setting == CONTENT_SETTING_ASK && |
| 234 content_type == CONTENT_SETTINGS_TYPE_PLUGINS && |
| 235 !CommandLine::ForCurrentProcess()->HasSwitch( |
| 236 switches::kEnableClickToPlay)) { |
| 237 return CONTENT_SETTING_BLOCK; |
| 238 } |
| 239 return setting; |
| 240 } |
| 241 |
| 242 } // namespace content_settings |
| OLD | NEW |