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_only"}, |
| 38 {CONTENT_SETTING_DETECT_IMPORTANT_CONTENT, "detect_important_content"}, |
| 39 }; |
| 40 static_assert(arraysize(kContentSettingsStringMapping) == |
| 41 CONTENT_SETTING_NUM_SETTINGS, |
| 42 "kContentSettingsToFromString should have " |
| 43 "CONTENT_SETTING_NUM_SETTINGS elements"); |
| 44 |
28 } // namespace | 45 } // namespace |
29 | 46 |
30 namespace content_settings { | 47 namespace content_settings { |
31 | 48 |
32 std::string ContentSettingToString(ContentSetting setting) { | 49 std::string ContentSettingToString(ContentSetting setting) { |
33 switch (setting) { | 50 if (setting >= CONTENT_SETTING_DEFAULT && |
34 case CONTENT_SETTING_ALLOW: | 51 setting < CONTENT_SETTING_NUM_SETTINGS) { |
35 return "allow"; | 52 return kContentSettingsStringMapping[setting].content_setting_str; |
36 case CONTENT_SETTING_ASK: | |
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 } | 53 } |
49 | 54 return std::string(); |
50 return ResourceIdentifier(); | |
51 } | 55 } |
52 | 56 |
53 ContentSetting ContentSettingFromString(const std::string& name) { | 57 bool ContentSettingFromString(const std::string& name, |
54 if (name == "allow") | 58 ContentSetting* setting) { |
55 return CONTENT_SETTING_ALLOW; | 59 // We are starting the index from 1, as |CONTENT_SETTING_DEFAULT| is not |
56 if (name == "ask") | 60 // a recognized content setting. |
57 return CONTENT_SETTING_ASK; | 61 for (size_t i = 1; i < arraysize(kContentSettingsStringMapping); ++i) { |
58 if (name == "block") | 62 if (name == kContentSettingsStringMapping[i].content_setting_str) { |
59 return CONTENT_SETTING_BLOCK; | 63 *setting = kContentSettingsStringMapping[i].content_setting; |
60 if (name == "session") | 64 return true; |
61 return CONTENT_SETTING_SESSION_ONLY; | 65 } |
62 if (name == "detect") | 66 } |
63 return CONTENT_SETTING_DETECT_IMPORTANT_CONTENT; | 67 *setting = CONTENT_SETTING_DEFAULT; |
64 | 68 return false; |
65 NOTREACHED() << name << " is not a recognized content setting."; | |
66 return CONTENT_SETTING_DEFAULT; | |
67 } | 69 } |
68 | 70 |
69 std::string CreatePatternString( | 71 std::string CreatePatternString( |
70 const ContentSettingsPattern& item_pattern, | 72 const ContentSettingsPattern& item_pattern, |
71 const ContentSettingsPattern& top_level_frame_pattern) { | 73 const ContentSettingsPattern& top_level_frame_pattern) { |
72 return item_pattern.ToString() | 74 return item_pattern.ToString() |
73 + std::string(kPatternSeparator) | 75 + std::string(kPatternSeparator) |
74 + top_level_frame_pattern.ToString(); | 76 + top_level_frame_pattern.ToString(); |
75 } | 77 } |
76 | 78 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 CONTENT_SETTINGS_TYPE_IMAGES, | 191 CONTENT_SETTINGS_TYPE_IMAGES, |
190 ResourceIdentifier(), | 192 ResourceIdentifier(), |
191 &(rules->image_rules)); | 193 &(rules->image_rules)); |
192 map->GetSettingsForOneType( | 194 map->GetSettingsForOneType( |
193 CONTENT_SETTINGS_TYPE_JAVASCRIPT, | 195 CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
194 ResourceIdentifier(), | 196 ResourceIdentifier(), |
195 &(rules->script_rules)); | 197 &(rules->script_rules)); |
196 } | 198 } |
197 | 199 |
198 } // namespace content_settings | 200 } // namespace content_settings |
OLD | NEW |