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 "nsIClipboard.h"
14 #include "nsIObserver.h"
16 #include "GUniquePtr.h"
20 # include "mozilla/Logging.h"
21 # include "nsTArray.h"
23 extern mozilla::LazyLogModule gClipboardLog
;
24 # define LOGCLIP(...) \
25 MOZ_LOG(gClipboardLog, mozilla::LogLevel::Debug, (__VA_ARGS__))
26 # define LOGCLIP_ENABLED() \
27 MOZ_LOG_TEST(gClipboardLog, mozilla::LogLevel::Debug)
30 # define LOGCLIP_ENABLED() false
31 #endif /* MOZ_LOGGING */
33 class ClipboardTargets
{
34 friend class ClipboardData
;
36 mozilla::GUniquePtr
<GdkAtom
> mTargets
;
40 ClipboardTargets() = default;
41 ClipboardTargets(mozilla::GUniquePtr
<GdkAtom
> aTargets
, uint32_t aCount
)
42 : mTargets(std::move(aTargets
)), mCount(aCount
) {}
44 void Set(ClipboardTargets
);
45 ClipboardTargets
Clone();
51 mozilla::Span
<GdkAtom
> AsSpan() const { return {mTargets
.get(), mCount
}; }
52 explicit operator bool() const { return bool(mTargets
); }
56 mozilla::GUniquePtr
<char> mData
;
60 ClipboardData() = default;
62 void SetData(mozilla::Span
<const uint8_t>);
63 void SetText(mozilla::Span
<const char>);
64 void SetTargets(ClipboardTargets
);
66 ClipboardTargets
ExtractTargets();
67 mozilla::GUniquePtr
<char> ExtractText() {
69 return std::move(mData
);
72 mozilla::Span
<char> AsSpan() const { return {mData
.get(), mLength
}; }
73 explicit operator bool() const { return bool(mData
); }
76 enum class ClipboardDataType
{ Data
, Text
, Targets
};
78 class nsRetrievalContext
{
80 // We intentionally use unsafe thread refcount as clipboard is used in
82 NS_INLINE_DECL_REFCOUNTING(nsRetrievalContext
)
84 // Get actual clipboard content (GetClipboardData/GetClipboardText).
85 virtual ClipboardData
GetClipboardData(const char* aMimeType
,
86 int32_t aWhichClipboard
) = 0;
87 virtual mozilla::GUniquePtr
<char> GetClipboardText(
88 int32_t aWhichClipboard
) = 0;
90 // Get data mime types which can be obtained from clipboard.
91 ClipboardTargets
GetTargets(int32_t aWhichClipboard
);
93 // Clipboard/Primary selection owner changed. Clear internal cached data.
94 static void ClearCachedTargetsClipboard(GtkClipboard
* aClipboard
,
95 GdkEvent
* aEvent
, gpointer data
);
96 static void ClearCachedTargetsPrimary(GtkClipboard
* aClipboard
,
97 GdkEvent
* aEvent
, gpointer data
);
102 virtual ClipboardTargets
GetTargetsImpl(int32_t aWhichClipboard
) = 0;
103 virtual ~nsRetrievalContext();
105 static ClipboardTargets sClipboardTargets
;
106 static ClipboardTargets sPrimaryTargets
;
109 class nsClipboard
: public nsIClipboard
, public nsIObserver
{
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
);
127 virtual ~nsClipboard();
129 // Get our hands on the correct transferable, given a specific
131 nsITransferable
* GetTransferable(int32_t aWhichClipboard
);
133 // Send clipboard data by nsITransferable
134 void SetTransferableData(nsITransferable
* aTransferable
, nsCString
& aFlavor
,
135 const char* aClipboardData
,
136 uint32_t aClipboardDataLength
);
138 void ClearTransferable(int32_t aWhichClipboard
);
139 void ClearCachedTargets(int32_t aWhichClipboard
);
141 bool FilterImportedFlavors(int32_t aWhichClipboard
,
142 nsTArray
<nsCString
>& aFlavors
);
144 // Hang on to our owners and transferables so we can transfer data
146 nsCOMPtr
<nsIClipboardOwner
> mSelectionOwner
;
147 nsCOMPtr
<nsIClipboardOwner
> mGlobalOwner
;
148 nsCOMPtr
<nsITransferable
> mSelectionTransferable
;
149 nsCOMPtr
<nsITransferable
> mGlobalTransferable
;
150 RefPtr
<nsRetrievalContext
> mContext
;
153 extern const int kClipboardTimeout
;
154 extern const int kClipboardFastIterationNum
;
156 GdkAtom
GetSelectionAtom(int32_t aWhichClipboard
);
157 int GetGeckoClipboardType(GtkClipboard
* aGtkClipboard
);
159 #endif /* __nsClipboard_h_ */