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

Unified Diff: mojo/common/values_struct_traits.cc

Issue 2577563002: Add struct traits for base::Value. (Closed)
Patch Set: rebase Created 4 years 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 | « mojo/common/values_struct_traits.h ('k') | services/preferences/public/cpp/pref_observer_store.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/common/values_struct_traits.cc
diff --git a/mojo/common/values_struct_traits.cc b/mojo/common/values_struct_traits.cc
new file mode 100644
index 0000000000000000000000000000000000000000..831698858b237a719059f0e0e6b7a357bcc2290a
--- /dev/null
+++ b/mojo/common/values_struct_traits.cc
@@ -0,0 +1,101 @@
+// Copyright 2016 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 "mojo/common/values_struct_traits.h"
+
+namespace mojo {
+
+bool StructTraits<common::mojom::ListValueDataView,
+ std::unique_ptr<base::ListValue>>::
+ Read(common::mojom::ListValueDataView data,
+ std::unique_ptr<base::ListValue>* value_out) {
+ mojo::ArrayDataView<common::mojom::ValueDataView> view;
+ data.GetValuesDataView(&view);
+
+ auto list_value = base::MakeUnique<base::ListValue>();
+ for (size_t i = 0; i < view.size(); ++i) {
+ std::unique_ptr<base::Value> value;
+ if (!view.Read(i, &value))
+ return false;
+
+ list_value->Append(std::move(value));
+ }
+ *value_out = std::move(list_value);
+ return true;
+}
+
+bool StructTraits<common::mojom::DictionaryValueDataView,
+ std::unique_ptr<base::DictionaryValue>>::
+ Read(common::mojom::DictionaryValueDataView data,
+ std::unique_ptr<base::DictionaryValue>* value_out) {
+ mojo::MapDataView<mojo::StringDataView, common::mojom::ValueDataView> view;
+ data.GetValuesDataView(&view);
+ auto dictionary_value = base::MakeUnique<base::DictionaryValue>();
+ for (size_t i = 0; i < view.size(); ++i) {
+ base::StringPiece key;
+ std::unique_ptr<base::Value> value;
+ if (!view.keys().Read(i, &key) || !view.values().Read(i, &value))
+ return false;
+
+ dictionary_value->Set(key, std::move(value));
+ }
+ *value_out = std::move(dictionary_value);
+ return true;
+}
+
+bool UnionTraits<common::mojom::ValueDataView, std::unique_ptr<base::Value>>::
+ Read(common::mojom::ValueDataView data,
+ std::unique_ptr<base::Value>* value_out) {
+ switch (data.tag()) {
+ case common::mojom::ValueDataView::Tag::NULL_VALUE: {
+ *value_out = base::Value::CreateNullValue();
+ return true;
+ }
+ case common::mojom::ValueDataView::Tag::BOOL_VALUE: {
+ *value_out = base::MakeUnique<base::FundamentalValue>(data.bool_value());
+ return true;
+ }
+ case common::mojom::ValueDataView::Tag::INT_VALUE: {
+ *value_out = base::MakeUnique<base::FundamentalValue>(data.int_value());
+ return true;
+ }
+ case common::mojom::ValueDataView::Tag::DOUBLE_VALUE: {
+ *value_out =
+ base::MakeUnique<base::FundamentalValue>(data.double_value());
+ return true;
+ }
+ case common::mojom::ValueDataView::Tag::STRING_VALUE: {
+ base::StringPiece string_value;
+ if (!data.ReadStringValue(&string_value))
+ return false;
+ *value_out = base::MakeUnique<base::StringValue>(string_value);
+ return true;
+ }
+ case common::mojom::ValueDataView::Tag::BINARY_VALUE: {
+ mojo::ArrayDataView<uint8_t> binary_data;
+ data.GetBinaryValueDataView(&binary_data);
+ *value_out = base::BinaryValue::CreateWithCopiedBuffer(
+ reinterpret_cast<const char*>(binary_data.data()),
+ binary_data.size());
+ return true;
+ }
+ case common::mojom::ValueDataView::Tag::DICTIONARY_VALUE: {
+ std::unique_ptr<base::DictionaryValue> dictionary_value;
+ if (!data.ReadDictionaryValue(&dictionary_value))
+ return false;
+ *value_out = std::move(dictionary_value);
+ return true;
+ }
+ case common::mojom::ValueDataView::Tag::LIST_VALUE: {
+ std::unique_ptr<base::ListValue> list_value;
+ if (!data.ReadListValue(&list_value))
+ return false;
+ *value_out = std::move(list_value);
+ return true;
+ }
+ }
+ return false;
+}
+
+} // namespace mojo
« no previous file with comments | « mojo/common/values_struct_traits.h ('k') | services/preferences/public/cpp/pref_observer_store.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698