author Boris Zbarsky <>
Mon, 10 Oct 2016 21:07:47 -0400
changeset 423590 1873ffaafd2598b605658031b389baca35df2aa5
parent 423589 9a4c0703e8251bd951f97919d8370de2926e2f07
child 423595 3e0c2cad052c8752f84ef746c2b54371971afb1f
permissions -rw-r--r--
Bug 1298243 part 3. Restrict mutation of a DataTransferItem's kind to codepaths that are actually changing its data. r=mystor

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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 */

#ifndef mozilla_dom_DataTransferItem_h
#define mozilla_dom_DataTransferItem_h

#include "mozilla/ErrorResult.h"
#include "mozilla/dom/DataTransfer.h"
#include "mozilla/dom/DOMString.h"
#include "mozilla/dom/File.h"

namespace mozilla {
namespace dom {

class FileSystemEntry;
class FunctionStringCallback;

class DataTransferItem final : public nsISupports
                             , public nsWrapperCache

  // The spec only talks about the "file" and "string" kinds. Due to the Moz*
  // APIs, it is possible to attach any type to a DataTransferItem, meaning that
  // we can have other kinds then just FILE and STRING. These others are simply
  // marked as "other" and can only be produced throug the Moz* APIs.
  enum eKind {

  DataTransferItem(DataTransfer* aDataTransfer, const nsAString& aType,
                   eKind aKind = KIND_OTHER)
    : mIndex(0)
    , mChromeOnly(false)
    , mKind(aKind)
    , mType(aType)
    , mDataTransfer(aDataTransfer)
    MOZ_ASSERT(mDataTransfer, "Must be associated with a DataTransfer");

  already_AddRefed<DataTransferItem> Clone(DataTransfer* aDataTransfer) const;

  virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;

  // NOTE: This accesses the subject principal, and should not be called from C++
  void GetAsString(FunctionStringCallback* aCallback,
                   const Maybe<nsIPrincipal*>& aPrincipal,
                   ErrorResult& aRv);

  void GetKind(nsAString& aKind) const
    switch (mKind) {
    case KIND_FILE:
      aKind = NS_LITERAL_STRING("file");
    case KIND_STRING:
      aKind = NS_LITERAL_STRING("string");
      aKind = NS_LITERAL_STRING("other");

  void GetType(nsAString& aType) const
    aType = mType;

  eKind Kind() const
    return mKind;

  GetAsFile(const Maybe<nsIPrincipal*>& aSubjectPrincipal, ErrorResult& aRv);

  GetAsEntry(const Maybe<nsIPrincipal*>& aSubjectPrincipal, ErrorResult& aRv);

  DataTransfer* GetParentObject() const
    return mDataTransfer;

  nsIPrincipal* Principal() const
    return mPrincipal;
  void SetPrincipal(nsIPrincipal* aPrincipal)
    mPrincipal = aPrincipal;

  already_AddRefed<nsIVariant> DataNoSecurityCheck();
  already_AddRefed<nsIVariant> Data(nsIPrincipal* aPrincipal, ErrorResult& aRv);

  // Note: This can modify the mKind.  Callers of this method must let the
  // relevant DataTransfer know, because its types list can change as a result.
  void SetData(nsIVariant* aData);

  uint32_t Index() const
    return mIndex;
  void SetIndex(uint32_t aIndex)
    mIndex = aIndex;
  void FillInExternalData();

  bool ChromeOnly() const
    return mChromeOnly;
  void SetChromeOnly(bool aChromeOnly)
    mChromeOnly = aChromeOnly;

  static eKind KindFromData(nsIVariant* aData);

  ~DataTransferItem() {}
  already_AddRefed<File> CreateFileFromInputStream(nsIInputStream* aStream);

  // The index in the 2d mIndexedItems array
  uint32_t mIndex;

  bool mChromeOnly;
  eKind mKind;
  const nsString mType;
  nsCOMPtr<nsIVariant> mData;
  nsCOMPtr<nsIPrincipal> mPrincipal;
  RefPtr<DataTransfer> mDataTransfer;

  // File cache for nsIFile application/x-moz-file entries.
  RefPtr<File> mCachedFile;

} // namespace dom
} // namespace mozilla

#endif /* mozilla_dom_DataTransferItem_h */