OLD | NEW |
(Empty) | |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/content_settings/core/common/content_settings_utils.h" |
| 6 |
| 7 #include "base/memory/ptr_util.h" |
| 8 #include "base/values.h" |
| 9 |
| 10 namespace content_settings { |
| 11 |
| 12 namespace { |
| 13 |
| 14 // Converts a |Value| to a |ContentSetting|. Returns true if |value| encodes |
| 15 // a valid content setting, false otherwise. Note that |CONTENT_SETTING_DEFAULT| |
| 16 // is encoded as a NULL value, so it is not allowed as an integer value. |
| 17 bool ParseContentSettingValue(const base::Value* value, |
| 18 ContentSetting* setting) { |
| 19 if (!value) { |
| 20 *setting = CONTENT_SETTING_DEFAULT; |
| 21 return true; |
| 22 } |
| 23 int int_value = -1; |
| 24 if (!value->GetAsInteger(&int_value)) |
| 25 return false; |
| 26 *setting = IntToContentSetting(int_value); |
| 27 return *setting != CONTENT_SETTING_DEFAULT; |
| 28 } |
| 29 |
| 30 } // namespace |
| 31 |
| 32 ContentSetting ValueToContentSetting(const base::Value* value) { |
| 33 ContentSetting setting = CONTENT_SETTING_DEFAULT; |
| 34 bool valid = ParseContentSettingValue(value, &setting); |
| 35 DCHECK(valid); |
| 36 return setting; |
| 37 } |
| 38 |
| 39 std::unique_ptr<base::Value> ContentSettingToValue(ContentSetting setting) { |
| 40 if (setting <= CONTENT_SETTING_DEFAULT || |
| 41 setting >= CONTENT_SETTING_NUM_SETTINGS) { |
| 42 return nullptr; |
| 43 } |
| 44 return base::MakeUnique<base::Value>(setting); |
| 45 } |
| 46 |
| 47 } // namespace content_settings |
OLD | NEW |