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"); | |
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 bool ContentSettingFromString(const std::string& name, |
54 if (name == "allow") | 56 ContentSetting* setting) { |
55 return CONTENT_SETTING_ALLOW; | 57 // We are starting the index from 1, as |CONTENT_SETTING_DEFAULT| is not |
56 if (name == "ask") | 58 // a recognized content setting. |
57 return CONTENT_SETTING_ASK; | 59 for (size_t i = 1; i < arraysize(kContentSettingsStringMapping); ++i) { |
58 if (name == "block") | 60 if (name == kContentSettingsStringMapping[i].content_setting_str) { |
59 return CONTENT_SETTING_BLOCK; | 61 *setting = kContentSettingsStringMapping[i].content_setting; |
60 if (name == "session") | 62 return true; |
61 return CONTENT_SETTING_SESSION_ONLY; | 63 } |
62 if (name == "detect") | 64 } |
63 return CONTENT_SETTING_DETECT_IMPORTANT_CONTENT; | |
64 | |
65 NOTREACHED() << name << " is not a recognized content setting."; | 65 NOTREACHED() << name << " is not a recognized content setting."; |
Bernhard Bauer
2015/10/07 09:32:04
Remove this. Passing an invalid string is now acce
Deepak
2015/10/07 11:12:24
Done.
| |
66 return CONTENT_SETTING_DEFAULT; | 66 *setting = CONTENT_SETTING_DEFAULT; |
67 return false; | |
67 } | 68 } |
68 | 69 |
69 std::string CreatePatternString( | 70 std::string CreatePatternString( |
70 const ContentSettingsPattern& item_pattern, | 71 const ContentSettingsPattern& item_pattern, |
71 const ContentSettingsPattern& top_level_frame_pattern) { | 72 const ContentSettingsPattern& top_level_frame_pattern) { |
72 return item_pattern.ToString() | 73 return item_pattern.ToString() |
73 + std::string(kPatternSeparator) | 74 + std::string(kPatternSeparator) |
74 + top_level_frame_pattern.ToString(); | 75 + top_level_frame_pattern.ToString(); |
75 } | 76 } |
76 | 77 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 CONTENT_SETTINGS_TYPE_IMAGES, | 190 CONTENT_SETTINGS_TYPE_IMAGES, |
190 ResourceIdentifier(), | 191 ResourceIdentifier(), |
191 &(rules->image_rules)); | 192 &(rules->image_rules)); |
192 map->GetSettingsForOneType( | 193 map->GetSettingsForOneType( |
193 CONTENT_SETTINGS_TYPE_JAVASCRIPT, | 194 CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
194 ResourceIdentifier(), | 195 ResourceIdentifier(), |
195 &(rules->script_rules)); | 196 &(rules->script_rules)); |
196 } | 197 } |
197 | 198 |
198 } // namespace content_settings | 199 } // namespace content_settings |
OLD | NEW |