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/. */
6 #ifndef nsBaseClipboard_h__
7 #define nsBaseClipboard_h__
9 #include "mozilla/dom/PContent.h"
10 #include "mozilla/Logging.h"
11 #include "mozilla/MoveOnlyFunction.h"
12 #include "mozilla/Result.h"
13 #include "nsIClipboard.h"
14 #include "nsITransferable.h"
17 static mozilla::LazyLogModule
sWidgetClipboardLog("WidgetClipboard");
18 #define MOZ_CLIPBOARD_LOG(...) \
19 MOZ_LOG(sWidgetClipboardLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
20 #define MOZ_CLIPBOARD_LOG_ENABLED() \
21 MOZ_LOG_TEST(sWidgetClipboardLog, mozilla::LogLevel::Debug)
23 class nsITransferable
;
24 class nsIClipboardOwner
;
28 namespace mozilla::dom
{
30 } // namespace mozilla::dom
33 * A base clipboard class for all platform, so that they can share the same
36 class nsBaseClipboard
: public nsIClipboard
{
38 explicit nsBaseClipboard(
39 const mozilla::dom::ClipboardCapabilities
& aClipboardCaps
);
45 NS_IMETHOD
SetData(nsITransferable
* aTransferable
, nsIClipboardOwner
* aOwner
,
46 int32_t aWhichClipboard
) override final
;
47 NS_IMETHOD
AsyncSetData(int32_t aWhichClipboard
,
48 nsIAsyncClipboardRequestCallback
* aCallback
,
49 nsIAsyncSetClipboardData
** _retval
) override final
;
51 nsITransferable
* aTransferable
, int32_t aWhichClipboard
,
52 mozilla::dom::WindowContext
* aWindowContext
) override final
;
53 NS_IMETHOD
AsyncGetData(
54 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
,
55 mozilla::dom::WindowContext
* aRequestingWindowContext
,
56 nsIPrincipal
* aRequestingPrincipal
,
57 nsIAsyncClipboardGetCallback
* aCallback
) override final
;
58 NS_IMETHOD
EmptyClipboard(int32_t aWhichClipboard
) override final
;
59 NS_IMETHOD
HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
60 int32_t aWhichClipboard
,
61 bool* aOutResult
) override final
;
62 NS_IMETHOD
IsClipboardTypeSupported(int32_t aWhichClipboard
,
63 bool* aRetval
) override final
;
65 void AsyncGetDataInternal(
66 const nsTArray
<nsCString
>& aFlavorList
, int32_t aClipboardType
,
67 mozilla::dom::WindowContext
* aRequestingWindowContext
,
68 nsIAsyncClipboardGetCallback
* aCallback
);
70 using GetDataCallback
= mozilla::MoveOnlyFunction
<void(nsresult
)>;
71 using HasMatchingFlavorsCallback
= mozilla::MoveOnlyFunction
<void(
72 mozilla::Result
<nsTArray
<nsCString
>, nsresult
>)>;
75 virtual ~nsBaseClipboard();
77 // Implement the native clipboard behavior.
78 NS_IMETHOD
SetNativeClipboardData(nsITransferable
* aTransferable
,
79 int32_t aWhichClipboard
) = 0;
80 NS_IMETHOD
GetNativeClipboardData(nsITransferable
* aTransferable
,
81 int32_t aWhichClipboard
) = 0;
82 virtual void AsyncGetNativeClipboardData(nsITransferable
* aTransferable
,
83 int32_t aWhichClipboard
,
84 GetDataCallback
&& aCallback
);
85 virtual nsresult
EmptyNativeClipboardData(int32_t aWhichClipboard
) = 0;
86 virtual mozilla::Result
<int32_t, nsresult
> GetNativeClipboardSequenceNumber(
87 int32_t aWhichClipboard
) = 0;
88 virtual mozilla::Result
<bool, nsresult
> HasNativeClipboardDataMatchingFlavors(
89 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
) = 0;
90 virtual void AsyncHasNativeClipboardDataMatchingFlavors(
91 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
,
92 HasMatchingFlavorsCallback
&& aCallback
);
94 void ClearClipboardCache(int32_t aClipboardType
);
97 void RejectPendingAsyncSetDataRequestIfAny(int32_t aClipboardType
);
99 class AsyncSetClipboardData final
: public nsIAsyncSetClipboardData
{
102 NS_DECL_NSIASYNCSETCLIPBOARDDATA
104 AsyncSetClipboardData(int32_t aClipboardType
, nsBaseClipboard
* aClipboard
,
105 nsIAsyncClipboardRequestCallback
* aCallback
);
108 virtual ~AsyncSetClipboardData() = default;
109 bool IsValid() const {
110 // If this request is no longer valid, the callback should be notified.
111 MOZ_ASSERT_IF(!mClipboard
, !mCallback
);
114 void MaybeNotifyCallback(nsresult aResult
);
116 // The clipboard type defined in nsIClipboard.
117 int32_t mClipboardType
;
118 // It is safe to use a raw pointer as it will be nullified (by calling
119 // NotifyCallback()) once nsBaseClipboard stops tracking us. This is
120 // also used to indicate whether this request is valid.
121 nsBaseClipboard
* mClipboard
;
122 // mCallback will be nullified once the callback is notified to ensure the
123 // callback is only notified once.
124 nsCOMPtr
<nsIAsyncClipboardRequestCallback
> mCallback
;
127 class AsyncGetClipboardData final
: public nsIAsyncGetClipboardData
{
129 AsyncGetClipboardData(
130 int32_t aClipboardType
, int32_t aSequenceNumber
,
131 nsTArray
<nsCString
>&& aFlavors
, bool aFromCache
,
132 nsBaseClipboard
* aClipboard
,
133 mozilla::dom::WindowContext
* aRequestingWindowContext
);
136 NS_DECL_NSIASYNCGETCLIPBOARDDATA
139 virtual ~AsyncGetClipboardData() = default;
142 // The clipboard type defined in nsIClipboard.
143 const int32_t mClipboardType
;
144 // The sequence number associated with the clipboard content for this
145 // request. If it doesn't match with the current sequence number in system
146 // clipboard, this request targets stale data and is deemed invalid.
147 const int32_t mSequenceNumber
;
148 // List of available data types for clipboard content.
149 const nsTArray
<nsCString
> mFlavors
;
150 // Data should be read from cache.
151 const bool mFromCache
;
152 // This is also used to indicate whether this request is still valid.
153 RefPtr
<nsBaseClipboard
> mClipboard
;
154 // The requesting window, which is used for Content Analysis purposes.
155 RefPtr
<mozilla::dom::WindowContext
> mRequestingWindowContext
;
158 class ClipboardCache final
{
161 // In order to notify the old clipboard owner.
166 * Clear the cached transferable and notify the original clipboard owner
167 * that it has lost ownership.
170 void Update(nsITransferable
* aTransferable
,
171 nsIClipboardOwner
* aClipboardOwner
, int32_t aSequenceNumber
) {
172 // Clear first to notify the old clipboard owner.
174 mTransferable
= aTransferable
;
175 mClipboardOwner
= aClipboardOwner
;
176 mSequenceNumber
= aSequenceNumber
;
178 nsITransferable
* GetTransferable() const { return mTransferable
; }
179 nsIClipboardOwner
* GetClipboardOwner() const { return mClipboardOwner
; }
180 int32_t GetSequenceNumber() const { return mSequenceNumber
; }
181 nsresult
GetData(nsITransferable
* aTransferable
) const;
184 nsCOMPtr
<nsITransferable
> mTransferable
;
185 nsCOMPtr
<nsIClipboardOwner
> mClipboardOwner
;
186 int32_t mSequenceNumber
= -1;
189 void MaybeRetryGetAvailableFlavors(
190 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
,
191 nsIAsyncClipboardGetCallback
* aCallback
, int32_t aRetryCount
,
192 mozilla::dom::WindowContext
* aRequestingWindowContext
);
194 // Return clipboard cache if the cached data is valid, otherwise clear the
195 // cached data and returns null.
196 ClipboardCache
* GetClipboardCacheIfValid(int32_t aClipboardType
);
198 mozilla::Result
<nsTArray
<nsCString
>, nsresult
> GetFlavorsFromClipboardCache(
199 int32_t aClipboardType
);
200 nsresult
GetDataFromClipboardCache(nsITransferable
* aTransferable
,
201 int32_t aClipboardType
);
203 void RequestUserConfirmation(int32_t aClipboardType
,
204 const nsTArray
<nsCString
>& aFlavorList
,
205 mozilla::dom::WindowContext
* aWindowContext
,
206 nsIPrincipal
* aRequestingPrincipal
,
207 nsIAsyncClipboardGetCallback
* aCallback
);
209 // Track the pending request for each clipboard type separately. And only need
210 // to track the latest request for each clipboard type as the prior pending
211 // request will be canceled when a new request is made.
212 RefPtr
<AsyncSetClipboardData
>
213 mPendingWriteRequests
[nsIClipboard::kClipboardTypeCount
];
215 mozilla::UniquePtr
<ClipboardCache
> mCaches
[nsIClipboard::kClipboardTypeCount
];
216 const mozilla::dom::ClipboardCapabilities mClipboardCaps
;
217 bool mIgnoreEmptyNotification
= false;
220 #endif // nsBaseClipboard_h__