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/. */
7 #ifndef mozilla_dom_ClipboardItem_h_
8 #define mozilla_dom_ClipboardItem_h_
10 #include "mozilla/dom/Blob.h"
11 #include "mozilla/dom/ClipboardBinding.h"
12 #include "mozilla/dom/PromiseNativeHandler.h"
13 #include "mozilla/MozPromise.h"
15 #include "nsIClipboard.h"
16 #include "nsWrapperCache.h"
18 class nsITransferable
;
20 namespace mozilla::dom
{
22 struct ClipboardItemOptions
;
23 template <typename KeyType
, typename ValueType
>
27 class ClipboardItem final
: public nsWrapperCache
{
29 class ItemEntry final
: public PromiseNativeHandler
,
30 public nsIAsyncClipboardRequestCallback
{
32 using GetDataPromise
=
33 MozPromise
<OwningStringOrBlob
, nsresult
, /* IsExclusive = */ true>;
35 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
36 NS_DECL_NSIASYNCCLIPBOARDREQUESTCALLBACK
37 NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(ItemEntry
, PromiseNativeHandler
)
39 explicit ItemEntry(nsIGlobalObject
* aGlobal
, const nsAString
& aType
)
40 : mGlobal(aGlobal
), mType(aType
) {
43 ItemEntry(nsIGlobalObject
* aGlobal
, const nsAString
& aType
,
44 const nsAString
& aData
)
45 : ItemEntry(aGlobal
, aType
) {
46 mLoadResult
.emplace(NS_OK
);
47 mData
.SetAsString() = aData
;
50 // PromiseNativeHandler
51 void ResolvedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
,
52 ErrorResult
& aRv
) override
;
53 void RejectedCallback(JSContext
* aCx
, JS::Handle
<JS::Value
> aValue
,
54 ErrorResult
& aRv
) override
;
56 const nsString
& Type() const { return mType
; }
57 RefPtr
<GetDataPromise
> GetData();
59 // Load data from system clipboard.
60 void LoadDataFromSystemClipboard(nsIAsyncGetClipboardData
* aDataGetter
);
61 void LoadDataFromDataPromise(Promise
& aDataPromise
);
63 // If clipboard data is in the process of loading from either system
64 // clipboard or data promise, add `aPromise` to the pending list which will
65 // be resolved/rejected later when process is finished. Otherwise,
66 // resolve/reject `aPromise` based on cached result and data.
67 void ReactGetTypePromise(Promise
& aPromise
);
71 if (!mPendingGetDataRequests
.IsEmpty()) {
72 RejectPendingPromises(NS_ERROR_FAILURE
);
76 void MaybeResolveGetTypePromise(const OwningStringOrBlob
& aData
,
78 void MaybeResolvePendingPromises(OwningStringOrBlob
&& aData
);
79 void RejectPendingPromises(nsresult rv
);
81 nsCOMPtr
<nsIGlobalObject
> mGlobal
;
83 // MIME type of this entry.
86 // Cache the loading result.
87 OwningStringOrBlob mData
;
88 Maybe
<nsresult
> mLoadResult
;
90 // Indicates if the data is still being loaded.
91 bool mIsLoadingData
= false;
92 nsCOMPtr
<nsITransferable
> mTransferable
;
94 // Pending promises for data retrieval requests.
95 nsTArray
<MozPromiseHolder
<GetDataPromise
>> mPendingGetDataRequests
;
96 nsTArray
<RefPtr
<Promise
>> mPendingGetTypeRequests
;
99 NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(ClipboardItem
)
100 NS_DECL_CYCLE_COLLECTION_NATIVE_WRAPPERCACHE_CLASS(ClipboardItem
)
102 ClipboardItem(nsISupports
* aOwner
, dom::PresentationStyle aPresentationStyle
,
103 nsTArray
<RefPtr
<ItemEntry
>>&& aItems
);
105 static already_AddRefed
<ClipboardItem
> Constructor(
106 const GlobalObject
& aGlobal
,
107 const Record
<nsString
, OwningNonNull
<Promise
>>& aItems
,
108 const ClipboardItemOptions
& aOptions
, ErrorResult
& aRv
);
110 dom::PresentationStyle
PresentationStyle() const {
111 return mPresentationStyle
;
113 void GetTypes(nsTArray
<nsString
>& aTypes
) const;
115 already_AddRefed
<Promise
> GetType(const nsAString
& aType
, ErrorResult
& aRv
);
117 nsISupports
* GetParentObject() const { return mOwner
; }
119 JSObject
* WrapObject(JSContext
* aCx
,
120 JS::Handle
<JSObject
*> aGivenProto
) override
;
122 const nsTArray
<RefPtr
<ItemEntry
>>& Entries() const { return mItems
; }
125 ~ClipboardItem() = default;
127 nsCOMPtr
<nsISupports
> mOwner
;
128 dom::PresentationStyle mPresentationStyle
;
129 nsTArray
<RefPtr
<ItemEntry
>> mItems
;
132 } // namespace mozilla::dom
134 #endif // mozilla_dom_ClipboardItem_h_