tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

DataTransferItem.h (4578B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_dom_DataTransferItem_h
      8 #define mozilla_dom_DataTransferItem_h
      9 
     10 #include "mozilla/dom/DOMString.h"
     11 #include "mozilla/dom/DataTransfer.h"
     12 #include "mozilla/dom/File.h"
     13 #include "nsVariant.h"
     14 
     15 namespace mozilla {
     16 class ErrorResult;
     17 
     18 namespace dom {
     19 
     20 class DataTransfer;
     21 class FileSystemEntry;
     22 class FunctionStringCallback;
     23 
     24 class DataTransferItem final : public nsISupports, public nsWrapperCache {
     25 public:
     26  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     27  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DataTransferItem);
     28 
     29 public:
     30  // The spec only talks about the "file" and "string" kinds. Due to the Moz*
     31  // APIs, it is possible to attach any type to a DataTransferItem, meaning that
     32  // we can have other kinds then just FILE and STRING. These others are simply
     33  // marked as "other" and can only be produced throug the Moz* APIs.
     34  enum eKind {
     35    KIND_FILE,
     36    KIND_STRING,
     37    KIND_OTHER,
     38  };
     39 
     40  DataTransferItem(DataTransfer* aDataTransfer, const nsAString& aType,
     41                   eKind aKind = KIND_OTHER)
     42      : mIndex(0),
     43        mChromeOnly(false),
     44        mKind(aKind),
     45        mType(aType),
     46        mDoNotAttemptToLoadData(false),
     47        mDataTransfer(aDataTransfer) {
     48    MOZ_ASSERT(mDataTransfer, "Must be associated with a DataTransfer");
     49  }
     50 
     51  already_AddRefed<DataTransferItem> Clone(DataTransfer* aDataTransfer) const;
     52 
     53  virtual JSObject* WrapObject(JSContext* aCx,
     54                               JS::Handle<JSObject*> aGivenProto) override;
     55 
     56  void GetAsString(FunctionStringCallback* aCallback,
     57                   nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
     58 
     59  void GetKind(nsAString& aKind) const {
     60    switch (mKind) {
     61      case KIND_FILE:
     62        aKind = u"file"_ns;
     63        return;
     64      case KIND_STRING:
     65        aKind = u"string"_ns;
     66        return;
     67      default:
     68        aKind = u"other"_ns;
     69        return;
     70    }
     71  }
     72 
     73  void GetInternalType(nsAString& aType) const { aType = mType; }
     74  bool IsInternalType(const nsAString& aType) const { return aType == mType; }
     75 
     76  void GetType(nsAString& aType);
     77 
     78  eKind Kind() const { return mKind; }
     79 
     80  already_AddRefed<File> GetAsFile(nsIPrincipal& aSubjectPrincipal,
     81                                   ErrorResult& aRv);
     82 
     83  already_AddRefed<FileSystemEntry> GetAsEntry(nsIPrincipal& aSubjectPrincipal,
     84                                               ErrorResult& aRv);
     85 
     86  DataTransfer* GetParentObject() const { return mDataTransfer; }
     87 
     88  nsIPrincipal* Principal() const { return mPrincipal; }
     89  void SetPrincipal(nsIPrincipal* aPrincipal) { mPrincipal = aPrincipal; }
     90 
     91  // @return cached data, if available.
     92  //         otherwise: if available, `mDataTransfer`'s transferable data.
     93  //                    otherwise the data is retrieved from the clipboard (for
     94  //                    paste events) or the drag session.
     95  already_AddRefed<nsIVariant> DataNoSecurityCheck();
     96  // Data may return null if the clipboard state has changed since the type was
     97  // detected.
     98  already_AddRefed<nsIVariant> Data(nsIPrincipal* aPrincipal, ErrorResult& aRv);
     99 
    100  // Note: This can modify the mKind.  Callers of this method must let the
    101  // relevant DataTransfer know, because its types list can change as a result.
    102  void SetData(nsIVariant* aData);
    103 
    104  uint32_t Index() const { return mIndex; }
    105  void SetIndex(uint32_t aIndex) { mIndex = aIndex; }
    106  void FillInExternalData();
    107 
    108  bool ChromeOnly() const { return mChromeOnly; }
    109  void SetChromeOnly(bool aChromeOnly) { mChromeOnly = aChromeOnly; }
    110 
    111  static eKind KindFromData(nsIVariant* aData);
    112 
    113 private:
    114  ~DataTransferItem() = default;
    115  already_AddRefed<File> CreateFileFromInputStream(nsIInputStream* aStream);
    116  already_AddRefed<File> CreateFileFromInputStream(
    117      nsIInputStream* aStream, const char* aFileNameKey,
    118      const nsAString& aContentType);
    119 
    120  // The index in the 2d mIndexedItems array
    121  uint32_t mIndex;
    122 
    123  bool mChromeOnly;
    124  eKind mKind;
    125  const nsString mType;
    126  nsCOMPtr<nsIVariant> mData;
    127  bool mDoNotAttemptToLoadData;
    128  nsCOMPtr<nsIPrincipal> mPrincipal;
    129  RefPtr<DataTransfer> mDataTransfer;
    130 
    131  // File cache for nsIFile application/x-moz-file entries.
    132  RefPtr<File> mCachedFile;
    133 };
    134 
    135 }  // namespace dom
    136 }  // namespace mozilla
    137 
    138 #endif /* mozilla_dom_DataTransferItem_h */