DataTransferItemList.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_DataTransferItemList_h
  6. #define mozilla_dom_DataTransferItemList_h
  7. #include "mozilla/dom/DataTransfer.h"
  8. #include "mozilla/dom/DataTransferItem.h"
  9. #include "mozilla/dom/FileList.h"
  10. namespace mozilla {
  11. namespace dom {
  12. class DataTransferItem;
  13. class DataTransferItemList final : public nsISupports
  14. , public nsWrapperCache
  15. {
  16. public:
  17. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  18. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DataTransferItemList);
  19. DataTransferItemList(DataTransfer* aDataTransfer, bool aIsExternal)
  20. : mDataTransfer(aDataTransfer)
  21. , mIsExternal(aIsExternal)
  22. {
  23. MOZ_ASSERT(aDataTransfer);
  24. // We always allocate an index 0 in our DataTransferItemList. This is done
  25. // in order to maintain the invariants according to the spec. Mainly, within
  26. // the spec's list, there is intended to be a single copy of each mime type,
  27. // for string typed items. File typed items are allowed to have duplicates.
  28. // In the old moz* system, this was modeled by having multiple indexes, each
  29. // of which was independent. Files were fetched from all indexes, but
  30. // strings were only fetched from the first index. In order to maintain this
  31. // correlation and avoid breaking code with the new changes, index 0 is now
  32. // always present and used to store strings, and all file items are given
  33. // their own index starting at index 1.
  34. mIndexedItems.SetLength(1);
  35. }
  36. already_AddRefed<DataTransferItemList> Clone(DataTransfer* aDataTransfer) const;
  37. virtual JSObject* WrapObject(JSContext* aCx,
  38. JS::Handle<JSObject*> aGivenProto) override;
  39. uint32_t Length() const
  40. {
  41. return mItems.Length();
  42. };
  43. DataTransferItem* Add(const nsAString& aData, const nsAString& aType,
  44. nsIPrincipal& aSubjectPrincipal,
  45. ErrorResult& rv);
  46. DataTransferItem* Add(File& aData,
  47. nsIPrincipal& aSubjectPrincipal,
  48. ErrorResult& aRv);
  49. void Remove(uint32_t aIndex,
  50. nsIPrincipal& aSubjectPrincipal,
  51. ErrorResult& aRv);
  52. DataTransferItem* IndexedGetter(uint32_t aIndex, bool& aFound) const;
  53. DataTransfer* GetParentObject() const
  54. {
  55. return mDataTransfer;
  56. }
  57. void Clear(nsIPrincipal& aSubjectPrincipal, ErrorResult& aRv);
  58. already_AddRefed<DataTransferItem>
  59. SetDataWithPrincipal(const nsAString& aType, nsIVariant* aData,
  60. uint32_t aIndex, nsIPrincipal* aPrincipal,
  61. bool aInsertOnly, bool aHidden, ErrorResult& aRv);
  62. already_AddRefed<FileList> Files(nsIPrincipal* aPrincipal);
  63. // Moz-style helper methods for interacting with the stored data
  64. void MozRemoveByTypeAt(const nsAString& aType, uint32_t aIndex,
  65. nsIPrincipal& aSubjectPrincipal,
  66. ErrorResult& aRv);
  67. DataTransferItem* MozItemByTypeAt(const nsAString& aType, uint32_t aIndex);
  68. const nsTArray<RefPtr<DataTransferItem>>* MozItemsAt(uint32_t aIndex);
  69. uint32_t MozItemCount() const;
  70. // Causes everything in indexes above 0 to shift down one index.
  71. void PopIndexZero();
  72. // Delete every item in the DataTransferItemList, without checking for
  73. // permissions or read-only status (for internal use only).
  74. void ClearAllItems();
  75. private:
  76. void ClearDataHelper(DataTransferItem* aItem, uint32_t aIndexHint,
  77. uint32_t aMozOffsetHint,
  78. nsIPrincipal& aSubjectPrincipal,
  79. ErrorResult& aRv);
  80. DataTransferItem* AppendNewItem(uint32_t aIndex, const nsAString& aType,
  81. nsIVariant* aData, nsIPrincipal* aPrincipal,
  82. bool aHidden);
  83. void RegenerateFiles();
  84. void GenerateFiles(FileList* aFiles, nsIPrincipal* aFilesPrincipal);
  85. ~DataTransferItemList() {}
  86. RefPtr<DataTransfer> mDataTransfer;
  87. bool mIsExternal;
  88. RefPtr<FileList> mFiles;
  89. // The principal for which mFiles is cached
  90. nsCOMPtr<nsIPrincipal> mFilesPrincipal;
  91. // mItems is the list of items that corresponds to the spec concept of a
  92. // DataTransferItemList. That is, this is the thing the spec's indexed getter
  93. // operates on. The items in here are a subset of the items present in the
  94. // arrays that live in mIndexedItems.
  95. nsTArray<RefPtr<DataTransferItem>> mItems;
  96. // mIndexedItems represents all our items. For any given index, all items at
  97. // that index have different types in the GetType() sense. That means that
  98. // representing multiple items with the same type (e.g. multiple files)
  99. // requires using multiple indices.
  100. //
  101. // There is always a (possibly empty) list of items at index 0, so
  102. // mIndexedItems.Length() >= 1 at all times.
  103. nsTArray<nsTArray<RefPtr<DataTransferItem>>> mIndexedItems;
  104. };
  105. } // namespace dom
  106. } // namespace mozilla
  107. #endif // mozilla_dom_DataTransferItemList_h