Bug 1769547 - Do not MOZ_CRASH() on missing process r=nika
[gecko.git] / widget / gtk / nsClipboard.h
blobb3b0b179a493789cb82168f057ebda319c8a94eb
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=2:tabstop=2:
3 */
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"
15 #include "nsCOMPtr.h"
16 #include "GUniquePtr.h"
17 #include <gtk/gtk.h>
19 #ifdef MOZ_LOGGING
20 # include "mozilla/Logging.h"
21 # include "nsTArray.h"
22 # include "Units.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)
28 #else
29 # define LOGCLIP(...)
30 # define LOGCLIP_ENABLED() false
31 #endif /* MOZ_LOGGING */
33 class ClipboardTargets {
34 friend class ClipboardData;
36 mozilla::GUniquePtr<GdkAtom> mTargets;
37 uint32_t mCount = 0;
39 public:
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();
46 void Clear() {
47 mTargets = nullptr;
48 mCount = 0;
51 mozilla::Span<GdkAtom> AsSpan() const { return {mTargets.get(), mCount}; }
52 explicit operator bool() const { return bool(mTargets); }
55 class ClipboardData {
56 mozilla::GUniquePtr<char> mData;
57 uint32_t mLength = 0;
59 public:
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() {
68 mLength = 0;
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 {
79 public:
80 // We intentionally use unsafe thread refcount as clipboard is used in
81 // main thread only.
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);
99 nsRetrievalContext();
101 protected:
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 {
110 public:
111 nsClipboard();
113 NS_DECL_ISUPPORTS
114 NS_DECL_NSIOBSERVER
115 NS_DECL_NSICLIPBOARD
117 // Make sure we are initialized, called from the factory
118 // constructor
119 nsresult Init(void);
121 // Someone requested the selection
122 void SelectionGetEvent(GtkClipboard* aGtkClipboard,
123 GtkSelectionData* aSelectionData);
124 void SelectionClearEvent(GtkClipboard* aGtkClipboard);
126 private:
127 virtual ~nsClipboard();
129 // Get our hands on the correct transferable, given a specific
130 // clipboard
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
145 // when asked.
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_ */