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 ContentSettingsToFromString { | |
Bernhard Bauer
2015/10/01 14:36:09
Nit: ContentSettingsStringMapping might be better.
Deepak
2015/10/03 09:02:11
Done.
| |
29 ContentSetting content_setting; | |
30 const char* content_setting_str; | |
31 }; | |
32 const ContentSettingsToFromString kContentSettingsToFromString[] = { | |
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(kContentSettingsToFromString) == | |
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(setting >= CONTENT_SETTING_DEFAULT && |
Bernhard Bauer
2015/10/01 14:36:09
Split this up into two checks, and use DCHECK_GE /
Deepak
2015/10/03 09:02:11
Done.
| |
34 case CONTENT_SETTING_ALLOW: | 50 setting < CONTENT_SETTING_NUM_SETTINGS); |
35 return "allow"; | 51 return kContentSettingsToFromString[setting].content_setting_str; |
Bernhard Bauer
2015/10/01 14:36:09
There is an implicit assumption here that `kConten
Deepak
2015/10/03 09:02:12
Done.
| |
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 } | |
49 | |
50 return ResourceIdentifier(); | |
51 } | 52 } |
52 | 53 |
53 ContentSetting ContentSettingFromString(const std::string& name) { | 54 ContentSetting ContentSettingFromString(const std::string& name) { |
54 if (name == "allow") | 55 size_t i = 0; |
Bernhard Bauer
2015/10/01 14:36:09
Inline this into the loop?
Deepak
2015/10/03 09:02:11
Done.
| |
55 return CONTENT_SETTING_ALLOW; | 56 for (i = 0; i < arraysize(kContentSettingsToFromString); ++i) { |
56 if (name == "ask") | 57 if (name == kContentSettingsToFromString[i].content_setting_str) |
57 return CONTENT_SETTING_ASK; | 58 break; |
Bernhard Bauer
2015/10/01 14:36:09
You could directly return here, then you only need
Deepak
2015/10/03 09:02:11
Done.
| |
58 if (name == "block") | 59 } |
59 return CONTENT_SETTING_BLOCK; | 60 ContentSetting content_setting = static_cast<ContentSetting>(i); |
60 if (name == "session") | 61 if (content_setting != CONTENT_SETTING_NUM_SETTINGS && |
61 return CONTENT_SETTING_SESSION_ONLY; | 62 content_setting != CONTENT_SETTING_DEFAULT) |
62 if (name == "detect") | 63 return kContentSettingsToFromString[i].content_setting; |
63 return CONTENT_SETTING_DETECT_IMPORTANT_CONTENT; | |
64 | 64 |
65 NOTREACHED() << name << " is not a recognized content setting."; | 65 NOTREACHED() << name << " is not a recognized content setting."; |
66 return CONTENT_SETTING_DEFAULT; | 66 return CONTENT_SETTING_DEFAULT; |
67 } | 67 } |
68 | 68 |
69 std::string CreatePatternString( | 69 std::string CreatePatternString( |
70 const ContentSettingsPattern& item_pattern, | 70 const ContentSettingsPattern& item_pattern, |
71 const ContentSettingsPattern& top_level_frame_pattern) { | 71 const ContentSettingsPattern& top_level_frame_pattern) { |
72 return item_pattern.ToString() | 72 return item_pattern.ToString() |
73 + std::string(kPatternSeparator) | 73 + std::string(kPatternSeparator) |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 CONTENT_SETTINGS_TYPE_IMAGES, | 189 CONTENT_SETTINGS_TYPE_IMAGES, |
190 ResourceIdentifier(), | 190 ResourceIdentifier(), |
191 &(rules->image_rules)); | 191 &(rules->image_rules)); |
192 map->GetSettingsForOneType( | 192 map->GetSettingsForOneType( |
193 CONTENT_SETTINGS_TYPE_JAVASCRIPT, | 193 CONTENT_SETTINGS_TYPE_JAVASCRIPT, |
194 ResourceIdentifier(), | 194 ResourceIdentifier(), |
195 &(rules->script_rules)); | 195 &(rules->script_rules)); |
196 } | 196 } |
197 | 197 |
198 } // namespace content_settings | 198 } // namespace content_settings |
OLD | NEW |