Backed out changeset 8aaffdf63d09 (bug 1920575) for causing bc failures on browser_si...
[gecko.git] / widget / nsBaseClipboard.h
blob7caadb41f500d05db2509ed88769ab2f93e72c83
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/Array.h"
10 #include "mozilla/dom/PContent.h"
11 #include "mozilla/Logging.h"
12 #include "mozilla/MoveOnlyFunction.h"
13 #include "mozilla/Result.h"
14 #include "nsIClipboard.h"
15 #include "nsITransferable.h"
16 #include "nsCOMPtr.h"
18 extern mozilla::LazyLogModule gWidgetClipboardLog;
19 #define MOZ_CLIPBOARD_LOG(...) \
20 MOZ_LOG(gWidgetClipboardLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
21 #define MOZ_CLIPBOARD_LOG_ENABLED() \
22 MOZ_LOG_TEST(gWidgetClipboardLog, mozilla::LogLevel::Debug)
24 class nsITransferable;
25 class nsIClipboardOwner;
26 class nsIPrincipal;
27 class nsIWidget;
29 namespace mozilla::dom {
30 class WindowContext;
31 } // namespace mozilla::dom
33 /**
34 * A base clipboard class for all platform, so that they can share the same
35 * implementation.
37 class nsBaseClipboard : public nsIClipboard {
38 public:
39 explicit nsBaseClipboard(
40 const mozilla::dom::ClipboardCapabilities& aClipboardCaps);
42 // nsISupports
43 NS_DECL_ISUPPORTS
45 // nsIClipboard
46 NS_IMETHOD SetData(
47 nsITransferable* aTransferable, nsIClipboardOwner* aOwner,
48 ClipboardType aWhichClipboard,
49 mozilla::dom::WindowContext* aWindowContext) override final;
50 NS_IMETHOD AsyncSetData(ClipboardType aWhichClipboard,
51 mozilla::dom::WindowContext* aSettingWindowContext,
52 nsIAsyncClipboardRequestCallback* aCallback,
53 nsIAsyncSetClipboardData** _retval) override final;
54 NS_IMETHOD GetData(
55 nsITransferable* aTransferable, ClipboardType aWhichClipboard,
56 mozilla::dom::WindowContext* aWindowContext) override final;
57 NS_IMETHOD GetDataSnapshot(
58 const nsTArray<nsCString>& aFlavorList, ClipboardType aWhichClipboard,
59 mozilla::dom::WindowContext* aRequestingWindowContext,
60 nsIPrincipal* aRequestingPrincipal,
61 nsIClipboardGetDataSnapshotCallback* aCallback) override final;
62 NS_IMETHOD GetDataSnapshotSync(
63 const nsTArray<nsCString>& aFlavorList, ClipboardType aWhichClipboard,
64 mozilla::dom::WindowContext* aRequestingWindowContext,
65 nsIClipboardDataSnapshot** _retval) override final;
66 NS_IMETHOD EmptyClipboard(ClipboardType aWhichClipboard) override final;
67 NS_IMETHOD HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
68 ClipboardType aWhichClipboard,
69 bool* aOutResult) override final;
70 NS_IMETHOD IsClipboardTypeSupported(ClipboardType aWhichClipboard,
71 bool* aRetval) override final;
73 void GetDataSnapshotInternal(
74 const nsTArray<nsCString>& aFlavorList,
75 nsIClipboard::ClipboardType aClipboardType,
76 mozilla::dom::WindowContext* aRequestingWindowContext,
77 nsIClipboardGetDataSnapshotCallback* aCallback);
79 using GetDataCallback = mozilla::MoveOnlyFunction<void(nsresult)>;
80 using HasMatchingFlavorsCallback = mozilla::MoveOnlyFunction<void(
81 mozilla::Result<nsTArray<nsCString>, nsresult>)>;
83 mozilla::Maybe<uint64_t> GetClipboardCacheInnerWindowId(
84 ClipboardType aClipboardType);
86 protected:
87 virtual ~nsBaseClipboard();
89 // Implement the native clipboard behavior.
90 NS_IMETHOD SetNativeClipboardData(nsITransferable* aTransferable,
91 ClipboardType aWhichClipboard) = 0;
92 NS_IMETHOD GetNativeClipboardData(nsITransferable* aTransferable,
93 ClipboardType aWhichClipboard) = 0;
94 virtual void AsyncGetNativeClipboardData(nsITransferable* aTransferable,
95 ClipboardType aWhichClipboard,
96 GetDataCallback&& aCallback);
97 virtual nsresult EmptyNativeClipboardData(ClipboardType aWhichClipboard) = 0;
98 virtual mozilla::Result<int32_t, nsresult> GetNativeClipboardSequenceNumber(
99 ClipboardType aWhichClipboard) = 0;
100 virtual mozilla::Result<bool, nsresult> HasNativeClipboardDataMatchingFlavors(
101 const nsTArray<nsCString>& aFlavorList,
102 ClipboardType aWhichClipboard) = 0;
103 virtual void AsyncHasNativeClipboardDataMatchingFlavors(
104 const nsTArray<nsCString>& aFlavorList, ClipboardType aWhichClipboard,
105 HasMatchingFlavorsCallback&& aCallback);
107 void ClearClipboardCache(ClipboardType aClipboardType);
109 private:
110 void RejectPendingAsyncSetDataRequestIfAny(ClipboardType aClipboardType);
112 class AsyncSetClipboardData final : public nsIAsyncSetClipboardData {
113 public:
114 NS_DECL_ISUPPORTS
115 NS_DECL_NSIASYNCSETCLIPBOARDDATA
117 AsyncSetClipboardData(nsIClipboard::ClipboardType aClipboardType,
118 nsBaseClipboard* aClipboard,
119 mozilla::dom::WindowContext* aRequestingWindowContext,
120 nsIAsyncClipboardRequestCallback* aCallback);
122 private:
123 virtual ~AsyncSetClipboardData() = default;
124 bool IsValid() const {
125 // If this request is no longer valid, the callback should be notified.
126 MOZ_ASSERT_IF(!mClipboard, !mCallback);
127 return !!mClipboard;
129 void MaybeNotifyCallback(nsresult aResult);
131 // The clipboard type defined in nsIClipboard.
132 nsIClipboard::ClipboardType mClipboardType;
133 // It is safe to use a raw pointer as it will be nullified (by calling
134 // NotifyCallback()) once nsBaseClipboard stops tracking us. This is
135 // also used to indicate whether this request is valid.
136 nsBaseClipboard* mClipboard;
137 RefPtr<mozilla::dom::WindowContext> mWindowContext;
138 // mCallback will be nullified once the callback is notified to ensure the
139 // callback is only notified once.
140 nsCOMPtr<nsIAsyncClipboardRequestCallback> mCallback;
143 class ClipboardDataSnapshot final : public nsIClipboardDataSnapshot {
144 public:
145 ClipboardDataSnapshot(
146 nsIClipboard::ClipboardType aClipboardType, int32_t aSequenceNumber,
147 nsTArray<nsCString>&& aFlavors, bool aFromCache,
148 nsBaseClipboard* aClipboard,
149 mozilla::dom::WindowContext* aRequestingWindowContext);
151 NS_DECL_ISUPPORTS
152 NS_DECL_NSICLIPBOARDDATASNAPSHOT
154 private:
155 virtual ~ClipboardDataSnapshot() = default;
156 bool IsValid();
158 // The clipboard type defined in nsIClipboard.
159 const nsIClipboard::ClipboardType mClipboardType;
160 // The sequence number associated with the clipboard content for this
161 // request. If it doesn't match with the current sequence number in system
162 // clipboard, this request targets stale data and is deemed invalid.
163 const int32_t mSequenceNumber;
164 // List of available data types for clipboard content.
165 const nsTArray<nsCString> mFlavors;
166 // Data should be read from cache.
167 const bool mFromCache;
168 // This is also used to indicate whether this request is still valid.
169 RefPtr<nsBaseClipboard> mClipboard;
170 // The requesting window, which is used for Content Analysis purposes.
171 RefPtr<mozilla::dom::WindowContext> mRequestingWindowContext;
174 class ClipboardCache final {
175 public:
176 ~ClipboardCache() {
177 // In order to notify the old clipboard owner.
178 Clear();
182 * Clear the cached transferable and notify the original clipboard owner
183 * that it has lost ownership.
185 void Clear();
186 void Update(nsITransferable* aTransferable,
187 nsIClipboardOwner* aClipboardOwner, int32_t aSequenceNumber,
188 mozilla::Maybe<uint64_t> aInnerWindowId) {
189 // Clear first to notify the old clipboard owner.
190 Clear();
191 mTransferable = aTransferable;
192 mClipboardOwner = aClipboardOwner;
193 mSequenceNumber = aSequenceNumber;
194 mInnerWindowId = aInnerWindowId;
196 nsITransferable* GetTransferable() const { return mTransferable; }
197 nsIClipboardOwner* GetClipboardOwner() const { return mClipboardOwner; }
198 int32_t GetSequenceNumber() const { return mSequenceNumber; }
199 mozilla::Maybe<uint64_t> GetInnerWindowId() const { return mInnerWindowId; }
200 nsresult GetData(nsITransferable* aTransferable) const;
202 private:
203 nsCOMPtr<nsITransferable> mTransferable;
204 nsCOMPtr<nsIClipboardOwner> mClipboardOwner;
205 int32_t mSequenceNumber = -1;
206 mozilla::Maybe<uint64_t> mInnerWindowId;
209 void MaybeRetryGetAvailableFlavors(
210 const nsTArray<nsCString>& aFlavorList,
211 nsIClipboard::ClipboardType aWhichClipboard,
212 nsIClipboardGetDataSnapshotCallback* aCallback, int32_t aRetryCount,
213 mozilla::dom::WindowContext* aRequestingWindowContext);
215 // Return clipboard cache if the cached data is valid, otherwise clear the
216 // cached data and returns null.
217 ClipboardCache* GetClipboardCacheIfValid(ClipboardType aClipboardType);
219 mozilla::Result<nsTArray<nsCString>, nsresult> GetFlavorsFromClipboardCache(
220 ClipboardType aClipboardType);
221 nsresult GetDataFromClipboardCache(nsITransferable* aTransferable,
222 ClipboardType aClipboardType);
223 void RequestUserConfirmation(ClipboardType aClipboardType,
224 const nsTArray<nsCString>& aFlavorList,
225 mozilla::dom::WindowContext* aWindowContext,
226 nsIPrincipal* aRequestingPrincipal,
227 nsIClipboardGetDataSnapshotCallback* aCallback);
229 already_AddRefed<nsIClipboardDataSnapshot>
230 MaybeCreateGetRequestFromClipboardCache(
231 const nsTArray<nsCString>& aFlavorList, ClipboardType aClipboardType,
232 mozilla::dom::WindowContext* aRequestingWindowContext);
234 // Track the pending request for each clipboard type separately. And only need
235 // to track the latest request for each clipboard type as the prior pending
236 // request will be canceled when a new request is made.
237 mozilla::Array<RefPtr<AsyncSetClipboardData>,
238 nsIClipboard::kClipboardTypeCount>
239 mPendingWriteRequests;
241 mozilla::Array<mozilla::UniquePtr<ClipboardCache>,
242 nsIClipboard::kClipboardTypeCount>
243 mCaches;
244 const mozilla::dom::ClipboardCapabilities mClipboardCaps;
245 bool mIgnoreEmptyNotification = false;
248 #endif // nsBaseClipboard_h__