15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dbus/values_util.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/json/json_writer.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/values.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "dbus/message.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace dbus {
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Returns whether |value| is exactly representable by double or not.
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template<typename T>
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool IsExactlyRepresentableByDouble(T value) {
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return value == static_cast<T>(static_cast<double>(value));
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Pops values from |reader| and appends them to |list_value|.
242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)bool PopListElements(MessageReader* reader, base::ListValue* list_value) {
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (reader->HasMoreData()) {
262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::Value* element_value = PopDataAsValue(reader);
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!element_value)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    list_value->Append(element_value);
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Pops dict-entries from |reader| and sets them to |dictionary_value|
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)bool PopDictionaryEntries(MessageReader* reader,
362a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                          base::DictionaryValue* dictionary_value) {
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  while (reader->HasMoreData()) {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    DCHECK_EQ(Message::DICT_ENTRY, reader->GetDataType());
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    MessageReader entry_reader(NULL);
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!reader->PopDictEntry(&entry_reader))
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Get key as a string.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    std::string key_string;
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (entry_reader.GetDataType() == Message::STRING) {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // If the type of keys is STRING, pop it directly.
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!entry_reader.PopString(&key_string))
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return false;
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    } else {
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // If the type of keys is not STRING, convert it to string.
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      scoped_ptr<base::Value> key(PopDataAsValue(&entry_reader));
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (!key.get())
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        return false;
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Use JSONWriter to convert an arbitrary value to a string.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::JSONWriter::Write(key.get(), &key_string);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    // Get the value and set the key-value pair.
572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)    base::Value* value = PopDataAsValue(&entry_reader);
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    if (!value)
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return false;
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    dictionary_value->SetWithoutPathExpansion(key_string, value);
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return true;
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets the D-Bus type signature for the value.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)std::string GetTypeSignature(const base::Value& value) {
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (value.GetType()) {
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_BOOLEAN:
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "b";
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_INTEGER:
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "i";
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_DOUBLE:
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "d";
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_STRING:
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "s";
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_BINARY:
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "ay";
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_DICTIONARY:
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      return "a{sv}";
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DLOG(ERROR) << "Unexpected type " << value.GetType();
82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      return std::string();
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)base::Value* PopDataAsValue(MessageReader* reader) {
892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::Value* result = NULL;
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (reader->GetDataType()) {
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::INVALID_DATA:
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Do nothing.
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::BYTE: {
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      uint8 value = 0;
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopByte(&value))
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::FundamentalValue(value);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::BOOL: {
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool value = false;
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopBool(&value))
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::FundamentalValue(value);
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::INT16: {
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int16 value = 0;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopInt16(&value))
1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::FundamentalValue(value);
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::UINT16: {
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      uint16 value = 0;
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopUint16(&value))
1152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::FundamentalValue(value);
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::INT32: {
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int32 value = 0;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopInt32(&value))
1212a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::FundamentalValue(value);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::UINT32: {
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      uint32 value = 0;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopUint32(&value))
1272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::FundamentalValue(static_cast<double>(value));
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::INT64: {
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int64 value = 0;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopInt64(&value)) {
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) <<
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            value << " is not exactly representable by double";
1352a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::FundamentalValue(static_cast<double>(value));
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::UINT64: {
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      uint64 value = 0;
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopUint64(&value)) {
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        DLOG_IF(WARNING, !IsExactlyRepresentableByDouble(value)) <<
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            value << " is not exactly representable by double";
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::FundamentalValue(static_cast<double>(value));
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::DOUBLE: {
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      double value = 0;
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopDouble(&value))
1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::FundamentalValue(value);
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::STRING: {
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string value;
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopString(&value))
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::StringValue(value);
1585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::OBJECT_PATH: {
1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      ObjectPath value;
1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopObjectPath(&value))
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        result = new base::StringValue(value.value());
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::UNIX_FD: {
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // Cannot distinguish a file descriptor from an int
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::ARRAY: {
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MessageReader sub_reader(NULL);
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopArray(&sub_reader)) {
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // If the type of the array's element is DICT_ENTRY, create a
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        // DictionaryValue, otherwise create a ListValue.
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (sub_reader.GetDataType() == Message::DICT_ENTRY) {
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          scoped_ptr<base::DictionaryValue> dictionary_value(
1782a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)              new base::DictionaryValue);
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (PopDictionaryEntries(&sub_reader, dictionary_value.get()))
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            result = dictionary_value.release();
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        } else {
1822a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)          scoped_ptr<base::ListValue> list_value(new base::ListValue);
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          if (PopListElements(&sub_reader, list_value.get()))
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)            result = list_value.release();
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        }
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::STRUCT: {
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MessageReader sub_reader(NULL);
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopStruct(&sub_reader)) {
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)        scoped_ptr<base::ListValue> list_value(new base::ListValue);
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        if (PopListElements(&sub_reader, list_value.get()))
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)          result = list_value.release();
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      }
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::DICT_ENTRY:
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      // DICT_ENTRY must be popped as an element of an array.
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      NOTREACHED();
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case Message::VARIANT: {
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MessageReader sub_reader(NULL);
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      if (reader->PopVariant(&sub_reader))
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        result = PopDataAsValue(&sub_reader);
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return result;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AppendBasicTypeValueData(MessageWriter* writer, const base::Value& value) {
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  switch (value.GetType()) {
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_BOOLEAN: {
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool bool_value = false;
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool success = value.GetAsBoolean(&bool_value);
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK(success);
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      writer->AppendBool(bool_value);
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_INTEGER: {
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int int_value = 0;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool success = value.GetAsInteger(&int_value);
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK(success);
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      writer->AppendInt32(int_value);
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_DOUBLE: {
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      double double_value = 0;
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool success = value.GetAsDouble(&double_value);
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK(success);
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      writer->AppendDouble(double_value);
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    case base::Value::TYPE_STRING: {
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      std::string string_value;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool success = value.GetAsString(&string_value);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DCHECK(success);
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      writer->AppendString(string_value);
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    }
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    default:
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      DLOG(ERROR) << "Unexpected type " << value.GetType();
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      break;
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AppendBasicTypeValueDataAsVariant(MessageWriter* writer,
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                       const base::Value& value) {
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  MessageWriter sub_writer(NULL);
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  writer->OpenVariant(GetTypeSignature(value), &sub_writer);
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AppendBasicTypeValueData(&sub_writer, value);
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  writer->CloseContainer(&sub_writer);
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace dbus
257