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

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

Issue 2938163002: Store base::Value in ContentSettingPatternSource instead of an enum (Closed)
Patch Set: rebased Created 3 years, 6 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
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..3f86905a69b0c11e206c0edffe5d0ad471d182e5
--- /dev/null
+++ b/components/content_settings/core/common/content_settings_utils.cc
@@ -0,0 +1,47 @@
+// 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"
+
+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

Powered by Google App Engine
This is Rietveld 408576698