1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:expandtab:shiftwidth=4:tabstop=4:
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/. */
7 
8 #ifndef __nsClipboard_h_
9 #define __nsClipboard_h_
10 
11 #include "mozilla/UniquePtr.h"
12 #include "nsIClipboard.h"
13 #include "nsIObserver.h"
14 #include <gtk/gtk.h>
15 
16 #ifdef MOZ_LOGGING
17 #  include "mozilla/Logging.h"
18 #  include "nsTArray.h"
19 #  include "Units.h"
20 extern mozilla::LazyLogModule gClipboardLog;
21 #  define LOGCLIP(args) MOZ_LOG(gClipboardLog, mozilla::LogLevel::Debug, args)
22 #else
23 #  define LOGCLIP(args)
24 #endif /* MOZ_LOGGING */
25 
26 enum ClipboardDataType { CLIPBOARD_DATA, CLIPBOARD_TEXT, CLIPBOARD_TARGETS };
27 
28 class nsRetrievalContext {
29  public:
30   // Get actual clipboard content (GetClipboardData/GetClipboardText)
31   // which has to be released by ReleaseClipboardData().
32   virtual const char* GetClipboardData(const char* aMimeType,
33                                        int32_t aWhichClipboard,
34                                        uint32_t* aContentLength) = 0;
35   virtual const char* GetClipboardText(int32_t aWhichClipboard) = 0;
36   virtual void ReleaseClipboardData(const char* aClipboardData) = 0;
37 
38   // Get data mime types which can be obtained from clipboard.
39   // The returned array has to be released by g_free().
40   virtual GdkAtom* GetTargets(int32_t aWhichClipboard, int* aTargetNum) = 0;
41 
42   virtual bool HasSelectionSupport(void) = 0;
43 
44   virtual ~nsRetrievalContext() = default;
45 };
46 
47 class nsClipboard : public nsIClipboard, public nsIObserver {
48  public:
49   nsClipboard();
50 
51   NS_DECL_ISUPPORTS
52   NS_DECL_NSIOBSERVER
53   NS_DECL_NSICLIPBOARD
54 
55   // Make sure we are initialized, called from the factory
56   // constructor
57   nsresult Init(void);
58 
59   // Someone requested the selection
60   void SelectionGetEvent(GtkClipboard* aGtkClipboard,
61                          GtkSelectionData* aSelectionData);
62   void SelectionClearEvent(GtkClipboard* aGtkClipboard);
63 
64  private:
65   virtual ~nsClipboard();
66 
67   // Get our hands on the correct transferable, given a specific
68   // clipboard
69   nsITransferable* GetTransferable(int32_t aWhichClipboard);
70 
71   // Send clipboard data by nsITransferable
72   void SetTransferableData(nsITransferable* aTransferable, nsCString& aFlavor,
73                            const char* aClipboardData,
74                            uint32_t aClipboardDataLength);
75 
76   void ClearTransferable(int32_t aWhichClipboard);
77 
78   // Hang on to our owners and transferables so we can transfer data
79   // when asked.
80   nsCOMPtr<nsIClipboardOwner> mSelectionOwner;
81   nsCOMPtr<nsIClipboardOwner> mGlobalOwner;
82   nsCOMPtr<nsITransferable> mSelectionTransferable;
83   nsCOMPtr<nsITransferable> mGlobalTransferable;
84   mozilla::UniquePtr<nsRetrievalContext> mContext;
85 };
86 
87 extern const int kClipboardTimeout;
88 
89 GdkAtom GetSelectionAtom(int32_t aWhichClipboard);
90 int GetGeckoClipboardType(GtkClipboard* aGtkClipboard);
91 
92 #endif /* __nsClipboard_h_ */
93