Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Unified Diff: components/content_settings/core/browser/content_settings_utils.cc

Issue 1372353004: Making structure for ContentSettings and its corresponding strings. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: updating variable name. Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/content_settings/core/browser/content_settings_utils.cc
diff --git a/components/content_settings/core/browser/content_settings_utils.cc b/components/content_settings/core/browser/content_settings_utils.cc
index 8a629b0fa5c20a53a40d759a46c1ad5a3faae9aa..0073c528cc433f05e71dde414140546533212ad9 100644
--- a/components/content_settings/core/browser/content_settings_utils.cc
+++ b/components/content_settings/core/browser/content_settings_utils.cc
@@ -25,42 +25,42 @@ namespace {
const char kPatternSeparator[] = ",";
+struct ContentSettingsToFromString {
Bernhard Bauer 2015/10/01 14:36:09 Nit: ContentSettingsStringMapping might be better.
Deepak 2015/10/03 09:02:11 Done.
+ ContentSetting content_setting;
+ const char* content_setting_str;
+};
+const ContentSettingsToFromString kContentSettingsToFromString[] = {
+ {CONTENT_SETTING_DEFAULT, "default"},
+ {CONTENT_SETTING_ALLOW, "allow"},
+ {CONTENT_SETTING_BLOCK, "block"},
+ {CONTENT_SETTING_ASK, "ask"},
+ {CONTENT_SETTING_SESSION_ONLY, "session"},
+ {CONTENT_SETTING_DETECT_IMPORTANT_CONTENT, "detect"},
+};
+static_assert(arraysize(kContentSettingsToFromString) ==
+ CONTENT_SETTING_NUM_SETTINGS,
+ "kContentSettingsToFromString should have "
+ "CONTENT_SETTING_NUM_SETTINGS elements");
} // namespace
namespace content_settings {
std::string ContentSettingToString(ContentSetting setting) {
- switch (setting) {
- case CONTENT_SETTING_ALLOW:
- return "allow";
- case CONTENT_SETTING_ASK:
- return "ask";
- case CONTENT_SETTING_BLOCK:
- return "block";
- case CONTENT_SETTING_SESSION_ONLY:
- return "session";
- case CONTENT_SETTING_DETECT_IMPORTANT_CONTENT:
- return "detect";
- case CONTENT_SETTING_DEFAULT:
- return "default";
- case CONTENT_SETTING_NUM_SETTINGS:
- NOTREACHED();
- }
-
- return ResourceIdentifier();
+ 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.
+ setting < CONTENT_SETTING_NUM_SETTINGS);
+ 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.
}
ContentSetting ContentSettingFromString(const std::string& name) {
- if (name == "allow")
- return CONTENT_SETTING_ALLOW;
- if (name == "ask")
- return CONTENT_SETTING_ASK;
- if (name == "block")
- return CONTENT_SETTING_BLOCK;
- if (name == "session")
- return CONTENT_SETTING_SESSION_ONLY;
- if (name == "detect")
- return CONTENT_SETTING_DETECT_IMPORTANT_CONTENT;
+ 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.
+ for (i = 0; i < arraysize(kContentSettingsToFromString); ++i) {
+ if (name == kContentSettingsToFromString[i].content_setting_str)
+ 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.
+ }
+ ContentSetting content_setting = static_cast<ContentSetting>(i);
+ if (content_setting != CONTENT_SETTING_NUM_SETTINGS &&
+ content_setting != CONTENT_SETTING_DEFAULT)
+ return kContentSettingsToFromString[i].content_setting;
NOTREACHED() << name << " is not a recognized content setting.";
return CONTENT_SETTING_DEFAULT;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698