Chromium Code Reviews| 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..eb806576e9fccf5a7e2590de358043c74a6866aa 100644 |
| --- a/components/content_settings/core/browser/content_settings_utils.cc |
| +++ b/components/content_settings/core/browser/content_settings_utils.cc |
| @@ -25,45 +25,50 @@ namespace { |
| const char kPatternSeparator[] = ","; |
| +struct ContentSettingsStringMapping { |
| + ContentSetting content_setting; |
| + const char* content_setting_str; |
| +}; |
| +const ContentSettingsStringMapping kContentSettingsStringMapping[] = { |
| + {CONTENT_SETTING_DEFAULT, "default"}, |
| + {CONTENT_SETTING_ALLOW, "allow"}, |
| + {CONTENT_SETTING_BLOCK, "block"}, |
| + {CONTENT_SETTING_ASK, "ask"}, |
| + {CONTENT_SETTING_SESSION_ONLY, "session_only"}, |
| + {CONTENT_SETTING_DETECT_IMPORTANT_CONTENT, "detect_important_content"}, |
| +}; |
| +static_assert(arraysize(kContentSettingsStringMapping) == |
| + CONTENT_SETTING_NUM_SETTINGS, |
| + "kContentSettingsToFromString should have " |
| + "CONTENT_SETTING_NUM_SETTINGS elements"); |
| } // namespace |
|
Bernhard Bauer
2015/10/07 11:38:21
Nit: empty line before closing a namespace
Deepak
2015/10/07 13:54:43
Done.
|
| 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(); |
| +bool ContentSettingToString(ContentSetting setting, |
| + std::string* setting_string) { |
| + if (setting >= CONTENT_SETTING_DEFAULT && |
| + setting < CONTENT_SETTING_NUM_SETTINGS && |
| + kContentSettingsStringMapping[setting].content_setting == setting) { |
|
Bernhard Bauer
2015/10/07 11:38:21
This is an invariant (it should always be true), s
Deepak
2015/10/07 13:54:43
Done.
|
| + *setting_string = |
| + kContentSettingsStringMapping[setting].content_setting_str; |
| + return true; |
| } |
| - |
| - return ResourceIdentifier(); |
| + return false; |
| } |
| -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; |
| - |
| - NOTREACHED() << name << " is not a recognized content setting."; |
| - return CONTENT_SETTING_DEFAULT; |
| +bool ContentSettingFromString(const std::string& name, |
| + ContentSetting* setting) { |
| + // We are starting the index from 1, as |CONTENT_SETTING_DEFAULT| is not |
| + // a recognized content setting. |
| + for (size_t i = 1; i < arraysize(kContentSettingsStringMapping); ++i) { |
| + if (name == kContentSettingsStringMapping[i].content_setting_str) { |
| + *setting = kContentSettingsStringMapping[i].content_setting; |
| + return true; |
| + } |
| + } |
| + *setting = CONTENT_SETTING_DEFAULT; |
| + return false; |
| } |
| std::string CreatePatternString( |