Backed out 2 changesets (bug 1855992) for causing talos failures @ mozilla::net:...
[gecko.git] / widget / nsBaseClipboard.h
blob7205d074faa149222fa83bde22a56eb3e8d3c5ef
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 nsIWidget;
27 /**
28 * A base clipboard class for all platform, so that they can share the same
29 * implementation.
31 class nsBaseClipboard : public nsIClipboard {
32 public:
33 explicit nsBaseClipboard(
34 const mozilla::dom::ClipboardCapabilities& aClipboardCaps);
36 // nsISupports
37 NS_DECL_ISUPPORTS
39 // nsIClipboard
40 NS_IMETHOD SetData(nsITransferable* aTransferable, nsIClipboardOwner* aOwner,
41 int32_t aWhichClipboard) override final;
42 NS_IMETHOD AsyncSetData(int32_t aWhichClipboard,
43 nsIAsyncClipboardRequestCallback* aCallback,
44 nsIAsyncSetClipboardData** _retval) override final;
45 NS_IMETHOD GetData(nsITransferable* aTransferable,
46 int32_t aWhichClipboard) override final;
47 NS_IMETHOD EmptyClipboard(int32_t aWhichClipboard) override final;
48 NS_IMETHOD HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
49 int32_t aWhichClipboard,
50 bool* aOutResult) override final;
51 NS_IMETHOD IsClipboardTypeSupported(int32_t aWhichClipboard,
52 bool* aRetval) override final;
53 RefPtr<mozilla::GenericPromise> AsyncGetData(
54 nsITransferable* aTransferable, int32_t aWhichClipboard) override final;
55 RefPtr<DataFlavorsPromise> AsyncHasDataMatchingFlavors(
56 const nsTArray<nsCString>& aFlavorList,
57 int32_t aWhichClipboard) override final;
59 using GetDataCallback = mozilla::MoveOnlyFunction<void(nsresult)>;
60 using HasMatchingFlavorsCallback = mozilla::MoveOnlyFunction<void(
61 mozilla::Result<nsTArray<nsCString>, nsresult>)>;
63 protected:
64 virtual ~nsBaseClipboard();
66 // Implement the native clipboard behavior.
67 NS_IMETHOD SetNativeClipboardData(nsITransferable* aTransferable,
68 int32_t aWhichClipboard) = 0;
69 NS_IMETHOD GetNativeClipboardData(nsITransferable* aTransferable,
70 int32_t aWhichClipboard) = 0;
71 virtual void AsyncGetNativeClipboardData(nsITransferable* aTransferable,
72 int32_t aWhichClipboard,
73 GetDataCallback&& aCallback);
74 virtual nsresult EmptyNativeClipboardData(int32_t aWhichClipboard) = 0;
75 virtual mozilla::Result<int32_t, nsresult> GetNativeClipboardSequenceNumber(
76 int32_t aWhichClipboard) = 0;
77 virtual mozilla::Result<bool, nsresult> HasNativeClipboardDataMatchingFlavors(
78 const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard) = 0;
79 virtual void AsyncHasNativeClipboardDataMatchingFlavors(
80 const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard,
81 HasMatchingFlavorsCallback&& aCallback);
83 void ClearClipboardCache(int32_t aClipboardType);
85 private:
86 void RejectPendingAsyncSetDataRequestIfAny(int32_t aClipboardType);
88 class AsyncSetClipboardData final : public nsIAsyncSetClipboardData {
89 public:
90 NS_DECL_ISUPPORTS
91 NS_DECL_NSIASYNCSETCLIPBOARDDATA
93 AsyncSetClipboardData(int32_t aClipboardType, nsBaseClipboard* aClipboard,
94 nsIAsyncClipboardRequestCallback* aCallback);
96 private:
97 virtual ~AsyncSetClipboardData() = default;
98 bool IsValid() const {
99 // If this request is no longer valid, the callback should be notified.
100 MOZ_ASSERT_IF(!mClipboard, !mCallback);
101 return !!mClipboard;
103 void MaybeNotifyCallback(nsresult aResult);
105 // The clipboard type defined in nsIClipboard.
106 int32_t mClipboardType;
107 // It is safe to use a raw pointer as it will be nullified (by calling
108 // NotifyCallback()) once nsBaseClipboard stops tracking us. This is
109 // also used to indicate whether this request is valid.
110 nsBaseClipboard* mClipboard;
111 // mCallback will be nullified once the callback is notified to ensure the
112 // callback is only notified once.
113 nsCOMPtr<nsIAsyncClipboardRequestCallback> mCallback;
116 class ClipboardCache final {
117 public:
118 ~ClipboardCache() {
119 // In order to notify the old clipboard owner.
120 Clear();
124 * Clear the cached transferable and notify the original clipboard owner
125 * that it has lost ownership.
127 void Clear();
128 void Update(nsITransferable* aTransferable,
129 nsIClipboardOwner* aClipboardOwner, int32_t aSequenceNumber) {
130 // Clear first to notify the old clipboard owner.
131 Clear();
132 mTransferable = aTransferable;
133 mClipboardOwner = aClipboardOwner;
134 mSequenceNumber = aSequenceNumber;
136 nsITransferable* GetTransferable() const { return mTransferable; }
137 nsIClipboardOwner* GetClipboardOwner() const { return mClipboardOwner; }
138 int32_t GetSequenceNumber() const { return mSequenceNumber; }
140 private:
141 nsCOMPtr<nsITransferable> mTransferable;
142 nsCOMPtr<nsIClipboardOwner> mClipboardOwner;
143 int32_t mSequenceNumber = -1;
146 // Return clipboard cache if the cached data is valid, otherwise clear the
147 // cached data and returns null.
148 ClipboardCache* GetClipboardCacheIfValid(int32_t aClipboardType);
150 mozilla::Result<nsTArray<nsCString>, nsresult> GetFlavorsFromClipboardCache(
151 int32_t aClipboardType);
152 nsresult GetDataFromClipboardCache(nsITransferable* aTransferable,
153 int32_t aClipboardType);
155 // Track the pending request for each clipboard type separately. And only need
156 // to track the latest request for each clipboard type as the prior pending
157 // request will be canceled when a new request is made.
158 RefPtr<AsyncSetClipboardData>
159 mPendingWriteRequests[nsIClipboard::kClipboardTypeCount];
161 mozilla::UniquePtr<ClipboardCache> mCaches[nsIClipboard::kClipboardTypeCount];
162 const mozilla::dom::ClipboardCapabilities mClipboardCaps;
163 bool mIgnoreEmptyNotification = false;
166 #endif // nsBaseClipboard_h__