dom/indexedDB/ipc/SerializationHelpers.h
author Dave Townsend <dtownsend@oxymoronical.com>
Fri, 26 Sep 2014 08:32:55 -0700
changeset 230683 8f7dd750e75fafc6460edd7f9fe6455029419f6c
parent 229139 00f11fa87d80199e1e4a8cc3ec976dc3580f6b4d
permissions -rw-r--r--
Bug 1068440: Uplift Add-on SDK to Firefox. https://github.com/mozilla/addon-sdk/compare/cc3242d1ca8c...cbf6cdd0d630

/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_indexeddb_serializationhelpers_h__
#define mozilla_dom_indexeddb_serializationhelpers_h__

#include "ipc/IPCMessageUtils.h"

#include "mozilla/dom/indexedDB/DatabaseInfo.h"
#include "mozilla/dom/indexedDB/Key.h"
#include "mozilla/dom/indexedDB/KeyPath.h"
#include "mozilla/dom/indexedDB/IDBCursor.h"
#include "mozilla/dom/indexedDB/IDBTransaction.h"

namespace IPC {

template <>
struct ParamTraits<mozilla::dom::quota::PersistenceType> :
  public ContiguousEnumSerializer<
           mozilla::dom::quota::PersistenceType,
           mozilla::dom::quota::PERSISTENCE_TYPE_PERSISTENT,
           mozilla::dom::quota::PERSISTENCE_TYPE_INVALID>
{ };

template <>
struct ParamTraits<mozilla::dom::indexedDB::Key>
{
  typedef mozilla::dom::indexedDB::Key paramType;

  static void Write(Message* aMsg, const paramType& aParam)
  {
    WriteParam(aMsg, aParam.mBuffer);
  }

  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
  {
    return ReadParam(aMsg, aIter, &aResult->mBuffer);
  }

  static void Log(const paramType& aParam, std::wstring* aLog)
  {
    LogParam(aParam.mBuffer, aLog);
  }
};

template <>
struct ParamTraits<mozilla::dom::indexedDB::KeyPath::KeyPathType> :
  public ContiguousEnumSerializer<
           mozilla::dom::indexedDB::KeyPath::KeyPathType,
           mozilla::dom::indexedDB::KeyPath::NONEXISTENT,
           mozilla::dom::indexedDB::KeyPath::ENDGUARD>
{ };

template <>
struct ParamTraits<mozilla::dom::indexedDB::KeyPath>
{
  typedef mozilla::dom::indexedDB::KeyPath paramType;

  static void Write(Message* aMsg, const paramType& aParam)
  {
    WriteParam(aMsg, aParam.mType);
    WriteParam(aMsg, aParam.mStrings);
  }

  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
  {
    return ReadParam(aMsg, aIter, &aResult->mType) &&
           ReadParam(aMsg, aIter, &aResult->mStrings);
  }

  static void Log(const paramType& aParam, std::wstring* aLog)
  {
    LogParam(aParam.mStrings, aLog);
  }
};

template <>
struct ParamTraits<mozilla::dom::indexedDB::IDBCursor::Direction> :
  public ContiguousEnumSerializer<
           mozilla::dom::indexedDB::IDBCursor::Direction,
           mozilla::dom::indexedDB::IDBCursor::NEXT,
           mozilla::dom::indexedDB::IDBCursor::DIRECTION_INVALID>
{ };

template <>
struct ParamTraits<mozilla::dom::indexedDB::IDBTransaction::Mode> :
  public ContiguousEnumSerializer<
           mozilla::dom::indexedDB::IDBTransaction::Mode,
           mozilla::dom::indexedDB::IDBTransaction::READ_ONLY,
           mozilla::dom::indexedDB::IDBTransaction::MODE_INVALID>
{ };

template <>
struct ParamTraits<mozilla::dom::indexedDB::IndexInfo>
{
  typedef mozilla::dom::indexedDB::IndexInfo paramType;

  static void Write(Message* aMsg, const paramType& aParam)
  {
    WriteParam(aMsg, aParam.name);
    WriteParam(aMsg, aParam.id);
    WriteParam(aMsg, aParam.keyPath);
    WriteParam(aMsg, aParam.unique);
    WriteParam(aMsg, aParam.multiEntry);
  }

  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
  {
    return ReadParam(aMsg, aIter, &aResult->name) &&
           ReadParam(aMsg, aIter, &aResult->id) &&
           ReadParam(aMsg, aIter, &aResult->keyPath) &&
           ReadParam(aMsg, aIter, &aResult->unique) &&
           ReadParam(aMsg, aIter, &aResult->multiEntry);
  }

  static void Log(const paramType& aParam, std::wstring* aLog)
  {
    LogParam(aParam.name, aLog);
    LogParam(aParam.id, aLog);
    LogParam(aParam.keyPath, aLog);
    LogParam(aParam.unique, aLog);
    LogParam(aParam.multiEntry, aLog);
  }
};

template <>
struct ParamTraits<mozilla::dom::indexedDB::ObjectStoreInfoGuts>
{
  typedef mozilla::dom::indexedDB::ObjectStoreInfoGuts paramType;

  static void Write(Message* aMsg, const paramType& aParam)
  {
    WriteParam(aMsg, aParam.name);
    WriteParam(aMsg, aParam.id);
    WriteParam(aMsg, aParam.keyPath);
    WriteParam(aMsg, aParam.autoIncrement);
    WriteParam(aMsg, aParam.indexes);
  }

  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
  {
    return ReadParam(aMsg, aIter, &aResult->name) &&
           ReadParam(aMsg, aIter, &aResult->id) &&
           ReadParam(aMsg, aIter, &aResult->keyPath) &&
           ReadParam(aMsg, aIter, &aResult->autoIncrement) &&
           ReadParam(aMsg, aIter, &aResult->indexes);
  }

  static void Log(const paramType& aParam, std::wstring* aLog)
  {
    LogParam(aParam.name, aLog);
    LogParam(aParam.id, aLog);
    LogParam(aParam.keyPath, aLog);
    LogParam(aParam.autoIncrement, aLog);
    LogParam(aParam.indexes, aLog);
  }
};

template <>
struct ParamTraits<mozilla::dom::indexedDB::DatabaseInfoGuts>
{
  typedef mozilla::dom::indexedDB::DatabaseInfoGuts paramType;

  static void Write(Message* aMsg, const paramType& aParam)
  {
    WriteParam(aMsg, aParam.name);
    WriteParam(aMsg, aParam.group);
    WriteParam(aMsg, aParam.origin);
    WriteParam(aMsg, aParam.version);
    WriteParam(aMsg, aParam.persistenceType);
    WriteParam(aMsg, aParam.nextObjectStoreId);
    WriteParam(aMsg, aParam.nextIndexId);
  }

  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
  {
    return ReadParam(aMsg, aIter, &aResult->name) &&
           ReadParam(aMsg, aIter, &aResult->group) &&
           ReadParam(aMsg, aIter, &aResult->origin) &&
           ReadParam(aMsg, aIter, &aResult->version) &&
           ReadParam(aMsg, aIter, &aResult->persistenceType) &&
           ReadParam(aMsg, aIter, &aResult->nextObjectStoreId) &&
           ReadParam(aMsg, aIter, &aResult->nextIndexId);
  }

  static void Log(const paramType& aParam, std::wstring* aLog)
  {
    LogParam(aParam.name, aLog);
    LogParam(aParam.group, aLog);
    LogParam(aParam.origin, aLog);
    LogParam(aParam.version, aLog);
    LogParam(aParam.nextObjectStoreId, aLog);
    LogParam(aParam.nextIndexId, aLog);
  }
};

template <>
struct ParamTraits<mozilla::dom::indexedDB::IndexUpdateInfo>
{
  typedef mozilla::dom::indexedDB::IndexUpdateInfo paramType;

  static void Write(Message* aMsg, const paramType& aParam)
  {
    WriteParam(aMsg, aParam.indexId);
    WriteParam(aMsg, aParam.indexUnique);
    WriteParam(aMsg, aParam.value);
  }

  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
  {
    return ReadParam(aMsg, aIter, &aResult->indexId) &&
           ReadParam(aMsg, aIter, &aResult->indexUnique) &&
           ReadParam(aMsg, aIter, &aResult->value);
  }

  static void Log(const paramType& aParam, std::wstring* aLog)
  {
    LogParam(aParam.indexId, aLog);
    LogParam(aParam.indexUnique, aLog);
    LogParam(aParam.value, aLog);
  }
};

template <>
struct ParamTraits<mozilla::dom::indexedDB::SerializedStructuredCloneReadInfo>
{
  typedef mozilla::dom::indexedDB::SerializedStructuredCloneReadInfo paramType;

  static void Write(Message* aMsg, const paramType& aParam)
  {
    WriteParam(aMsg, aParam.dataLength);
    if (aParam.dataLength) {
      aMsg->WriteBytes(aParam.data, aParam.dataLength);
    }
  }

  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
  {
    if (!ReadParam(aMsg, aIter, &aResult->dataLength)) {
      return false;
    }

    if (aResult->dataLength) {
      const char** buffer =
        const_cast<const char**>(reinterpret_cast<char**>(&aResult->data));
      if (!aMsg->ReadBytes(aIter, buffer, aResult->dataLength)) {
        return false;
      }
    } else {
      aResult->data = nullptr;
    }

    return true;
  }

  static void Log(const paramType& aParam, std::wstring* aLog)
  {
    LogParam(aParam.dataLength, aLog);
  }
};

template <>
struct ParamTraits<mozilla::dom::indexedDB::SerializedStructuredCloneWriteInfo>
{
  typedef mozilla::dom::indexedDB::SerializedStructuredCloneWriteInfo paramType;

  static void Write(Message* aMsg, const paramType& aParam)
  {
    WriteParam(aMsg, aParam.dataLength);
    if (aParam.dataLength) {
      aMsg->WriteBytes(aParam.data, aParam.dataLength);
    }
    WriteParam(aMsg, aParam.offsetToKeyProp);
  }

  static bool Read(const Message* aMsg, void** aIter, paramType* aResult)
  {
    if (!ReadParam(aMsg, aIter, &aResult->dataLength)) {
      return false;
    }

    if (aResult->dataLength) {
      const char** buffer =
        const_cast<const char**>(reinterpret_cast<char**>(&aResult->data));
      if (!aMsg->ReadBytes(aIter, buffer, aResult->dataLength)) {
        return false;
      }
    } else {
      aResult->data = nullptr;
    }

    if (!ReadParam(aMsg, aIter, &aResult->offsetToKeyProp)) {
      return false;
    }

    return true;
  }

  static void Log(const paramType& aParam, std::wstring* aLog)
  {
    LogParam(aParam.dataLength, aLog);
    LogParam(aParam.offsetToKeyProp, aLog);
  }
};

} // namespace IPC

#endif // mozilla_dom_indexeddb_serializationhelpers_h__