dom/push/PushUtil.cpp
author Masayuki Nakano <masayuki@d-toybox.com>
Sat, 11 Jun 2016 11:06:37 +0900
changeset 341662 b64f33cd6b5431a6bd97c3afea39f943ca2b9022
parent 334344 662a8ccb9a3e851cafc9db882783e89cbb3a048a
child 439273 cb123c84818a3c491fb9d63f00891d23f515cf6f
permissions -rw-r--r--
Bug 1278014 part.2 Define mozilla::SelectionType as an enum class and use it instead of RawSelectionType as far as possible r=smaug This patch defines mozilla::SelectionType as an enum class. This is safer than nsISelectionController::SELECTION_* since setting illegal value to its variable is checked at build time. So, as far as possible, this should be used everywhere (but of course, this isn't available in scriptable interfaces). And also this implements some useful methods for managing SelectionType and RawSelectionType which are implemented in layout/nsSelection.cpp because nsISelectionController is implemented by both PresShell and nsTextEditorState. Therefore, implementing one of them may make hard to find them. On the other hand, nsSelection.cpp is a better file name to look for them. Note that this patch creates mozilla::Selection::RawType() for binding. Native code should keep using Selection::Type() but the binding code needs to use RawType() due to impossible to convert from SelectionType to RawSelectionType without explicit cast. MozReview-Commit-ID: 81vX7A0hHQN

/* 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/. */

#include "mozilla/dom/PushUtil.h"

namespace mozilla {
namespace dom {

/* static */ bool
PushUtil::CopyArrayBufferToArray(const ArrayBuffer& aBuffer,
                                 nsTArray<uint8_t>& aArray)
{
  MOZ_ASSERT(aArray.IsEmpty());
  aBuffer.ComputeLengthAndData();
  return aArray.SetCapacity(aBuffer.Length(), fallible) &&
         aArray.InsertElementsAt(0, aBuffer.Data(), aBuffer.Length(), fallible);
}

/* static */ bool
PushUtil::CopyArrayBufferViewToArray(const ArrayBufferView& aView,
                                     nsTArray<uint8_t>& aArray)
{
  MOZ_ASSERT(aArray.IsEmpty());
  aView.ComputeLengthAndData();
  return aArray.SetCapacity(aView.Length(), fallible) &&
         aArray.InsertElementsAt(0, aView.Data(), aView.Length(), fallible);
}

/* static */ bool
PushUtil::CopyBufferSourceToArray(
  const OwningArrayBufferViewOrArrayBuffer& aSource, nsTArray<uint8_t>& aArray)
{
  if (aSource.IsArrayBuffer()) {
    return CopyArrayBufferToArray(aSource.GetAsArrayBuffer(), aArray);
  }
  if (aSource.IsArrayBufferView()) {
    return CopyArrayBufferViewToArray(aSource.GetAsArrayBufferView(), aArray);
  }
  MOZ_CRASH("Uninitialized union: expected buffer or view");
}

/* static */ void
PushUtil::CopyArrayToArrayBuffer(JSContext* aCx,
                                 const nsTArray<uint8_t>& aArray,
                                 JS::MutableHandle<JSObject*> aValue,
                                 ErrorResult& aRv)
{
  if (aArray.IsEmpty()) {
    aValue.set(nullptr);
    return;
  }
  JS::Rooted<JSObject*> buffer(aCx, ArrayBuffer::Create(aCx,
                                                        aArray.Length(),
                                                        aArray.Elements()));
  if (NS_WARN_IF(!buffer)) {
    aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
    return;
  }
  aValue.set(buffer);
}

} // namespace dom
} // namespace mozilla