Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/content_settings/core/browser/content_settings_utils.h" | 5 #include "components/content_settings/core/browser/content_settings_utils.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/prefs/pref_registry.h" | 12 #include "base/prefs/pref_registry.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "components/content_settings/core/browser/content_settings_provider.h" | 15 #include "components/content_settings/core/browser/content_settings_provider.h" |
| 16 #include "components/content_settings/core/browser/content_settings_rule.h" | 16 #include "components/content_settings/core/browser/content_settings_rule.h" |
| 17 #include "components/content_settings/core/browser/host_content_settings_map.h" | 17 #include "components/content_settings/core/browser/host_content_settings_map.h" |
| 18 #include "components/content_settings/core/browser/website_settings_info.h" | 18 #include "components/content_settings/core/browser/website_settings_info.h" |
| 19 #include "components/content_settings/core/browser/website_settings_registry.h" | 19 #include "components/content_settings/core/browser/website_settings_registry.h" |
| 20 #include "components/content_settings/core/common/content_settings_pattern.h" | 20 #include "components/content_settings/core/common/content_settings_pattern.h" |
| 21 #include "components/pref_registry/pref_registry_syncable.h" | 21 #include "components/pref_registry/pref_registry_syncable.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 namespace { | 24 namespace { |
| 25 | 25 |
| 26 const char kPatternSeparator[] = ","; | 26 const char kPatternSeparator[] = ","; |
| 27 | 27 |
| 28 struct ContentSettingsStringMapping { | |
| 29 ContentSetting content_setting; | |
| 30 const char* content_setting_str; | |
| 31 }; | |
| 32 const ContentSettingsStringMapping kContentSettingsStringMapping[] = { | |
| 33 {CONTENT_SETTING_DEFAULT, "default"}, | |
| 34 {CONTENT_SETTING_ALLOW, "allow"}, | |
| 35 {CONTENT_SETTING_BLOCK, "block"}, | |
| 36 {CONTENT_SETTING_ASK, "ask"}, | |
| 37 {CONTENT_SETTING_SESSION_ONLY, "session"}, | |
| 38 {CONTENT_SETTING_DETECT_IMPORTANT_CONTENT, "detect"}, | |
| 39 }; | |
| 40 static_assert(arraysize(kContentSettingsStringMapping) == | |
| 41 CONTENT_SETTING_NUM_SETTINGS, | |
| 42 "kContentSettingsToFromString should have " | |
| 43 "CONTENT_SETTING_NUM_SETTINGS elements"); | |
| 28 } // namespace | 44 } // namespace |
| 29 | 45 |
| 30 namespace content_settings { | 46 namespace content_settings { |
| 31 | 47 |
| 32 std::string ContentSettingToString(ContentSetting setting) { | 48 std::string ContentSettingToString(ContentSetting setting) { |
| 33 switch (setting) { | 49 DCHECK_GE(setting, CONTENT_SETTING_DEFAULT); |
| 34 case CONTENT_SETTING_ALLOW: | 50 DCHECK_LT(setting, CONTENT_SETTING_NUM_SETTINGS); |
| 35 return "allow"; | 51 DCHECK_EQ(kContentSettingsStringMapping[setting].content_setting, setting); |
| 36 case CONTENT_SETTING_ASK: | 52 return kContentSettingsStringMapping[setting].content_setting_str; |
| 37 return "ask"; | |
| 38 case CONTENT_SETTING_BLOCK: | |
| 39 return "block"; | |
| 40 case CONTENT_SETTING_SESSION_ONLY: | |
| 41 return "session"; | |
| 42 case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT: | |
| 43 return "detect"; | |
| 44 case CONTENT_SETTING_DEFAULT: | |
| 45 return "default"; | |
| 46 case CONTENT_SETTING_NUM_SETTINGS: | |
| 47 NOTREACHED(); | |
| 48 } | |
| 49 | |
| 50 return ResourceIdentifier(); | |
| 51 } | 53 } |
| 52 | 54 |
| 53 ContentSetting ContentSettingFromString(const std::string& name) { | 55 ContentSetting ContentSettingFromString(const std::string& name) { |
| 54 if (name == "allow") | 56 for (size_t i = 1; i < arraysize(kContentSettingsStringMapping); ++i) { |
|
Bernhard Bauer
2015/10/05 08:15:09
Please add a comment explaining why we start with
Deepak
2015/10/05 09:51:30
Done.
| |
| 55 return CONTENT_SETTING_ALLOW; | 57 if (name == kContentSettingsStringMapping[i].content_setting_str) |
| 56 if (name == "ask") | 58 return kContentSettingsStringMapping[i].content_setting; |
| 57 return CONTENT_SETTING_ASK; | 59 } |
| 58 if (name == "block") | |
| 59 return CONTENT_SETTING_BLOCK; | |
| 60 if (name == "session") | |
| 61 return CONTENT_SETTING_SESSION_ONLY; | |
| 62 if (name == "detect") | |
| 63 return CONTENT_SETTING_DETECT_IMPORTANT_CONTENT; | |
| 64 | |
| 65 NOTREACHED() << name << " is not a recognized content setting."; | 60 NOTREACHED() << name << " is not a recognized content setting."; |
| 66 return CONTENT_SETTING_DEFAULT; | 61 return CONTENT_SETTING_DEFAULT; |
| 67 } | 62 } |
| 68 | 63 |
| 69 std::string CreatePatternString( | 64 std::string CreatePatternString( |
| 70 const ContentSettingsPattern& item_pattern, | 65 const ContentSettingsPattern& item_pattern, |
| 71 const ContentSettingsPattern& top_level_frame_pattern) { | 66 const ContentSettingsPattern& top_level_frame_pattern) { |
| 72 return item_pattern.ToString() | 67 return item_pattern.ToString() |
| 73 + std::string(kPatternSeparator) | 68 + std::string(kPatternSeparator) |
| 74 + top_level_frame_pattern.ToString(); | 69 + top_level_frame_pattern.ToString(); |
| (...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 189 CONTENT_SETTINGS_TYPE_IMAGES, | 184 CONTENT_SETTINGS_TYPE_IMAGES, |
| 190 ResourceIdentifier(), | 185 ResourceIdentifier(), |
| 191 &(rules->image_rules)); | 186 &(rules->image_rules)); |
| 192 map->GetSettingsForOneType( | 187 map->GetSettingsForOneType( |
| 193 CONTENT_SETTINGS_TYPE_JAVASCRIPT, | 188 CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
| 194 ResourceIdentifier(), | 189 ResourceIdentifier(), |
| 195 &(rules->script_rules)); | 190 &(rules->script_rules)); |
| 196 } | 191 } |
| 197 | 192 |
| 198 } // namespace content_settings | 193 } // namespace content_settings |
| OLD | NEW |