1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
4 /* This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef __nsClipboard_h_
9 #define __nsClipboard_h_
11 #include "mozilla/UniquePtr.h"
12 #include "mozilla/Span.h"
13 #include "nsBaseClipboard.h"
14 #include "nsIClipboard.h"
15 #include "nsIObserver.h"
17 #include "GUniquePtr.h"
21 # include "mozilla/Logging.h"
22 # include "nsTArray.h"
24 extern mozilla::LazyLogModule gClipboardLog
;
25 # define LOGCLIP(...) \
26 MOZ_LOG(gClipboardLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
27 # define LOGCLIP_ENABLED() \
28 MOZ_LOG_TEST(gClipboardLog, mozilla::LogLevel::Debug)
31 # define LOGCLIP_ENABLED() false
32 #endif /* MOZ_LOGGING */
34 class ClipboardTargets
{
35 friend class ClipboardData
;
37 mozilla::GUniquePtr
<GdkAtom
> mTargets
;
41 ClipboardTargets() = default;
42 ClipboardTargets(mozilla::GUniquePtr
<GdkAtom
> aTargets
, uint32_t aCount
)
43 : mTargets(std::move(aTargets
)), mCount(aCount
) {}
45 void Set(ClipboardTargets
);
46 ClipboardTargets
Clone();
52 mozilla::Span
<GdkAtom
> AsSpan() const { return {mTargets
.get(), mCount
}; }
53 explicit operator bool() const { return bool(mTargets
); }
57 mozilla::GUniquePtr
<char> mData
;
61 ClipboardData() = default;
63 void SetData(mozilla::Span
<const uint8_t>);
64 void SetText(mozilla::Span
<const char>);
65 void SetTargets(ClipboardTargets
);
67 ClipboardTargets
ExtractTargets();
68 mozilla::GUniquePtr
<char> ExtractText() {
70 return std::move(mData
);
73 mozilla::Span
<char> AsSpan() const { return {mData
.get(), mLength
}; }
74 explicit operator bool() const { return bool(mData
); }
77 enum class ClipboardDataType
{ Data
, Text
, Targets
};
79 class nsRetrievalContext
{
81 // We intentionally use unsafe thread refcount as clipboard is used in
83 NS_INLINE_DECL_REFCOUNTING(nsRetrievalContext
)
85 // Get actual clipboard content (GetClipboardData/GetClipboardText).
86 virtual ClipboardData
GetClipboardData(const char* aMimeType
,
87 int32_t aWhichClipboard
) = 0;
88 virtual mozilla::GUniquePtr
<char> GetClipboardText(
89 int32_t aWhichClipboard
) = 0;
91 // Get data mime types which can be obtained from clipboard.
92 ClipboardTargets
GetTargets(int32_t aWhichClipboard
);
94 // Clipboard/Primary selection owner changed. Clear internal cached data.
95 static void ClearCachedTargetsClipboard(GtkClipboard
* aClipboard
,
96 GdkEvent
* aEvent
, gpointer data
);
97 static void ClearCachedTargetsPrimary(GtkClipboard
* aClipboard
,
98 GdkEvent
* aEvent
, gpointer data
);
100 nsRetrievalContext() = default;
103 virtual ClipboardTargets
GetTargetsImpl(int32_t aWhichClipboard
) = 0;
104 virtual ~nsRetrievalContext();
106 static ClipboardTargets sClipboardTargets
;
107 static ClipboardTargets sPrimaryTargets
;
110 class nsClipboard
: public nsBaseClipboard
, public nsIObserver
{
114 NS_DECL_ISUPPORTS_INHERITED
117 // Make sure we are initialized, called from the factory
121 // Someone requested the selection
122 void SelectionGetEvent(GtkClipboard
* aGtkClipboard
,
123 GtkSelectionData
* aSelectionData
);
124 void SelectionClearEvent(GtkClipboard
* aGtkClipboard
);
126 // Clipboard owner changed
127 void OwnerChangedEvent(GtkClipboard
* aGtkClipboard
,
128 GdkEventOwnerChange
* aEvent
);
131 // Implement the native clipboard behavior.
132 NS_IMETHOD
SetNativeClipboardData(nsITransferable
* aTransferable
,
133 int32_t aWhichClipboard
) override
;
134 NS_IMETHOD
GetNativeClipboardData(nsITransferable
* aTransferable
,
135 int32_t aWhichClipboard
) override
;
136 void AsyncGetNativeClipboardData(nsITransferable
* aTransferable
,
137 int32_t aWhichClipboard
,
138 GetDataCallback
&& aCallback
) override
;
139 nsresult
EmptyNativeClipboardData(int32_t aWhichClipboard
) override
;
140 mozilla::Result
<int32_t, nsresult
> GetNativeClipboardSequenceNumber(
141 int32_t aWhichClipboard
) override
;
142 mozilla::Result
<bool, nsresult
> HasNativeClipboardDataMatchingFlavors(
143 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
) override
;
144 void AsyncHasNativeClipboardDataMatchingFlavors(
145 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
,
146 HasMatchingFlavorsCallback
&& aCallback
) override
;
149 virtual ~nsClipboard();
151 // Get our hands on the correct transferable, given a specific
153 nsITransferable
* GetTransferable(int32_t aWhichClipboard
);
155 void ClearTransferable(int32_t aWhichClipboard
);
156 void ClearCachedTargets(int32_t aWhichClipboard
);
158 bool FilterImportedFlavors(int32_t aWhichClipboard
,
159 nsTArray
<nsCString
>& aFlavors
);
161 // Hang on to our transferables so we can transfer data when asked.
162 nsCOMPtr
<nsITransferable
> mSelectionTransferable
;
163 nsCOMPtr
<nsITransferable
> mGlobalTransferable
;
164 RefPtr
<nsRetrievalContext
> mContext
;
166 // Sequence number of the system clipboard data.
167 int32_t mSelectionSequenceNumber
= 0;
168 int32_t mGlobalSequenceNumber
= 0;
171 extern const int kClipboardTimeout
;
172 extern const int kClipboardFastIterationNum
;
174 GdkAtom
GetSelectionAtom(int32_t aWhichClipboard
);
175 int GetGeckoClipboardType(GtkClipboard
* aGtkClipboard
);
177 #endif /* __nsClipboard_h_ */