Backed out 2 changesets (bug 1879680) for causing spidermonkey failures on dumpValue...
[gecko.git] / widget / nsBaseClipboard.h
blob976c393d5b29868e24ec438a6890dfc8c8ad62c5
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"
15 #include "nsCOMPtr.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;
25 class nsIPrincipal;
26 class nsIWidget;
28 namespace mozilla::dom {
29 class WindowContext;
30 } // namespace mozilla::dom
32 /**
33 * A base clipboard class for all platform, so that they can share the same
34 * implementation.
36 class nsBaseClipboard : public nsIClipboard {
37 public:
38 explicit nsBaseClipboard(
39 const mozilla::dom::ClipboardCapabilities& aClipboardCaps);
41 // nsISupports
42 NS_DECL_ISUPPORTS
44 // nsIClipboard
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;
50 NS_IMETHOD GetData(nsITransferable* aTransferable,
51 int32_t aWhichClipboard) override final;
52 NS_IMETHOD AsyncGetData(
53 const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard,
54 mozilla::dom::WindowContext* aRequestingWindowContext,
55 nsIPrincipal* aRequestingPrincipal,
56 nsIAsyncClipboardGetCallback* aCallback) override final;
57 NS_IMETHOD EmptyClipboard(int32_t aWhichClipboard) override final;
58 NS_IMETHOD HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
59 int32_t aWhichClipboard,
60 bool* aOutResult) override final;
61 NS_IMETHOD IsClipboardTypeSupported(int32_t aWhichClipboard,
62 bool* aRetval) override final;
64 void AsyncGetDataInternal(const nsTArray<nsCString>& aFlavorList,
65 int32_t aClipboardType,
66 nsIAsyncClipboardGetCallback* aCallback);
68 using GetDataCallback = mozilla::MoveOnlyFunction<void(nsresult)>;
69 using HasMatchingFlavorsCallback = mozilla::MoveOnlyFunction<void(
70 mozilla::Result<nsTArray<nsCString>, nsresult>)>;
72 protected:
73 virtual ~nsBaseClipboard();
75 // Implement the native clipboard behavior.
76 NS_IMETHOD SetNativeClipboardData(nsITransferable* aTransferable,
77 int32_t aWhichClipboard) = 0;
78 NS_IMETHOD GetNativeClipboardData(nsITransferable* aTransferable,
79 int32_t aWhichClipboard) = 0;
80 virtual void AsyncGetNativeClipboardData(nsITransferable* aTransferable,
81 int32_t aWhichClipboard,
82 GetDataCallback&& aCallback);
83 virtual nsresult EmptyNativeClipboardData(int32_t aWhichClipboard) = 0;
84 virtual mozilla::Result<int32_t, nsresult> GetNativeClipboardSequenceNumber(
85 int32_t aWhichClipboard) = 0;
86 virtual mozilla::Result<bool, nsresult> HasNativeClipboardDataMatchingFlavors(
87 const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard) = 0;
88 virtual void AsyncHasNativeClipboardDataMatchingFlavors(
89 const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard,
90 HasMatchingFlavorsCallback&& aCallback);
92 void ClearClipboardCache(int32_t aClipboardType);
94 private:
95 void RejectPendingAsyncSetDataRequestIfAny(int32_t aClipboardType);
97 class AsyncSetClipboardData final : public nsIAsyncSetClipboardData {
98 public:
99 NS_DECL_ISUPPORTS
100 NS_DECL_NSIASYNCSETCLIPBOARDDATA
102 AsyncSetClipboardData(int32_t aClipboardType, nsBaseClipboard* aClipboard,
103 nsIAsyncClipboardRequestCallback* aCallback);
105 private:
106 virtual ~AsyncSetClipboardData() = default;
107 bool IsValid() const {
108 // If this request is no longer valid, the callback should be notified.
109 MOZ_ASSERT_IF(!mClipboard, !mCallback);
110 return !!mClipboard;
112 void MaybeNotifyCallback(nsresult aResult);
114 // The clipboard type defined in nsIClipboard.
115 int32_t mClipboardType;
116 // It is safe to use a raw pointer as it will be nullified (by calling
117 // NotifyCallback()) once nsBaseClipboard stops tracking us. This is
118 // also used to indicate whether this request is valid.
119 nsBaseClipboard* mClipboard;
120 // mCallback will be nullified once the callback is notified to ensure the
121 // callback is only notified once.
122 nsCOMPtr<nsIAsyncClipboardRequestCallback> mCallback;
125 class AsyncGetClipboardData final : public nsIAsyncGetClipboardData {
126 public:
127 AsyncGetClipboardData(int32_t aClipboardType, int32_t aSequenceNumber,
128 nsTArray<nsCString>&& aFlavors, bool aFromCache,
129 nsBaseClipboard* aClipboard);
131 NS_DECL_ISUPPORTS
132 NS_DECL_NSIASYNCGETCLIPBOARDDATA
134 private:
135 virtual ~AsyncGetClipboardData() = default;
136 bool IsValid();
138 // The clipboard type defined in nsIClipboard.
139 const int32_t mClipboardType;
140 // The sequence number associated with the clipboard content for this
141 // request. If it doesn't match with the current sequence number in system
142 // clipboard, this request targets stale data and is deemed invalid.
143 const int32_t mSequenceNumber;
144 // List of available data types for clipboard content.
145 const nsTArray<nsCString> mFlavors;
146 // Data should be read from cache.
147 const bool mFromCache;
148 // This is also used to indicate whether this request is still valid.
149 RefPtr<nsBaseClipboard> mClipboard;
152 class ClipboardCache final {
153 public:
154 ~ClipboardCache() {
155 // In order to notify the old clipboard owner.
156 Clear();
160 * Clear the cached transferable and notify the original clipboard owner
161 * that it has lost ownership.
163 void Clear();
164 void Update(nsITransferable* aTransferable,
165 nsIClipboardOwner* aClipboardOwner, int32_t aSequenceNumber) {
166 // Clear first to notify the old clipboard owner.
167 Clear();
168 mTransferable = aTransferable;
169 mClipboardOwner = aClipboardOwner;
170 mSequenceNumber = aSequenceNumber;
172 nsITransferable* GetTransferable() const { return mTransferable; }
173 nsIClipboardOwner* GetClipboardOwner() const { return mClipboardOwner; }
174 int32_t GetSequenceNumber() const { return mSequenceNumber; }
175 nsresult GetData(nsITransferable* aTransferable) const;
177 private:
178 nsCOMPtr<nsITransferable> mTransferable;
179 nsCOMPtr<nsIClipboardOwner> mClipboardOwner;
180 int32_t mSequenceNumber = -1;
183 void MaybeRetryGetAvailableFlavors(const nsTArray<nsCString>& aFlavorList,
184 int32_t aWhichClipboard,
185 nsIAsyncClipboardGetCallback* aCallback,
186 int32_t aRetryCount);
188 // Return clipboard cache if the cached data is valid, otherwise clear the
189 // cached data and returns null.
190 ClipboardCache* GetClipboardCacheIfValid(int32_t aClipboardType);
192 mozilla::Result<nsTArray<nsCString>, nsresult> GetFlavorsFromClipboardCache(
193 int32_t aClipboardType);
194 nsresult GetDataFromClipboardCache(nsITransferable* aTransferable,
195 int32_t aClipboardType);
197 void RequestUserConfirmation(int32_t aClipboardType,
198 const nsTArray<nsCString>& aFlavorList,
199 mozilla::dom::WindowContext* aWindowContext,
200 nsIPrincipal* aRequestingPrincipal,
201 nsIAsyncClipboardGetCallback* aCallback);
203 // Track the pending request for each clipboard type separately. And only need
204 // to track the latest request for each clipboard type as the prior pending
205 // request will be canceled when a new request is made.
206 RefPtr<AsyncSetClipboardData>
207 mPendingWriteRequests[nsIClipboard::kClipboardTypeCount];
209 mozilla::UniquePtr<ClipboardCache> mCaches[nsIClipboard::kClipboardTypeCount];
210 const mozilla::dom::ClipboardCapabilities mClipboardCaps;
211 bool mIgnoreEmptyNotification = false;
214 #endif // nsBaseClipboard_h__