Chromium Code Reviews| Index: components/content_settings/core/common/content_settings_utils.cc |
| diff --git a/components/content_settings/core/common/content_settings_utils.cc b/components/content_settings/core/common/content_settings_utils.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..06ec865c1c375b52ed8aeabc1573b15979735fed |
| --- /dev/null |
| +++ b/components/content_settings/core/common/content_settings_utils.cc |
| @@ -0,0 +1,48 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/content_settings/core/common/content_settings_utils.h" |
| + |
| +#include "base/memory/ptr_util.h" |
| +#include "base/values.h" |
| +#include "components/content_settings/core/common/content_settings_types.h" |
|
raymes
2017/06/27 03:49:33
nit: is this needed?
raymes
2017/06/28 01:15:43
Please also remove this if it's not needed.
tbansal1
2017/06/28 14:16:40
Done.
|
| + |
| +namespace content_settings { |
| + |
| +namespace { |
| + |
| +// Converts a |Value| to a |ContentSetting|. Returns true if |value| encodes |
| +// a valid content setting, false otherwise. Note that |CONTENT_SETTING_DEFAULT| |
| +// is encoded as a NULL value, so it is not allowed as an integer value. |
| +bool ParseContentSettingValue(const base::Value* value, |
| + ContentSetting* setting) { |
| + if (!value) { |
| + *setting = CONTENT_SETTING_DEFAULT; |
| + return true; |
| + } |
| + int int_value = -1; |
| + if (!value->GetAsInteger(&int_value)) |
| + return false; |
| + *setting = IntToContentSetting(int_value); |
| + return *setting != CONTENT_SETTING_DEFAULT; |
| +} |
| + |
| +} // namespace |
| + |
| +ContentSetting ValueToContentSetting(const base::Value* value) { |
| + ContentSetting setting = CONTENT_SETTING_DEFAULT; |
| + bool valid = ParseContentSettingValue(value, &setting); |
| + DCHECK(valid); |
| + return setting; |
| +} |
| + |
| +std::unique_ptr<base::Value> ContentSettingToValue(ContentSetting setting) { |
| + if (setting <= CONTENT_SETTING_DEFAULT || |
| + setting >= CONTENT_SETTING_NUM_SETTINGS) { |
| + return nullptr; |
| + } |
| + return base::MakeUnique<base::Value>(setting); |
| +} |
| + |
| +} // namespace content_settings |