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/. */
5 
6 #ifndef nsClipboard_h__
7 #define nsClipboard_h__
8 
9 #include "nsBaseClipboard.h"
10 #include "nsIObserver.h"
11 #include "nsIURI.h"
12 #include <windows.h>
13 
14 class nsITransferable;
15 class nsIWidget;
16 class nsIFile;
17 struct IDataObject;
18 
19 /**
20  * Native Win32 Clipboard wrapper
21  */
22 
23 class nsClipboard : public nsBaseClipboard, public nsIObserver {
24   virtual ~nsClipboard();
25 
26  public:
27   nsClipboard();
28 
29   NS_DECL_ISUPPORTS_INHERITED
30 
31   // nsIObserver
32   NS_DECL_NSIOBSERVER
33 
34   // nsIClipboard
35   NS_IMETHOD HasDataMatchingFlavors(const char** aFlavorList, uint32_t aLength,
36                                     int32_t aWhichClipboard,
37                                     bool* _retval) override;
38   NS_IMETHOD EmptyClipboard(int32_t aWhichClipboard) override;
39 
40   // Internal Native Routines
41   static nsresult CreateNativeDataObject(nsITransferable* aTransferable,
42                                          IDataObject** aDataObj, nsIURI* uri);
43   static nsresult SetupNativeDataObject(nsITransferable* aTransferable,
44                                         IDataObject* aDataObj);
45   static nsresult GetDataFromDataObject(IDataObject* aDataObject, UINT anIndex,
46                                         nsIWidget* aWindow,
47                                         nsITransferable* aTransferable);
48   static nsresult GetNativeDataOffClipboard(nsIWidget* aWindow, UINT aIndex,
49                                             UINT aFormat, void** aData,
50                                             uint32_t* aLen);
51   static nsresult GetNativeDataOffClipboard(IDataObject* aDataObject,
52                                             UINT aIndex, UINT aFormat,
53                                             const char* aMIMEImageFormat,
54                                             void** aData, uint32_t* aLen);
55   static nsresult GetGlobalData(HGLOBAL aHGBL, void** aData, uint32_t* aLen);
56 
57   // This function returns the internal Windows clipboard format identifier
58   // for a given Mime string. The default is to map kHTMLMime ("text/html")
59   // to the clipboard format CF_HTML ("HTLM Format"), but it can also be
60   // registered as clipboard format "text/html" to support previous versions
61   // of Gecko.
62   static UINT GetFormat(const char* aMimeStr, bool aMapHTMLMime = true);
63 
64   static UINT CF_HTML;
65   static UINT CF_CUSTOMTYPES;
66 
67  protected:
68   NS_IMETHOD SetNativeClipboardData(int32_t aWhichClipboard) override;
69   NS_IMETHOD GetNativeClipboardData(nsITransferable* aTransferable,
70                                     int32_t aWhichClipboard) override;
71 
72   static bool IsInternetShortcut(const nsAString& inFileName);
73   static bool FindURLFromLocalFile(IDataObject* inDataObject, UINT inIndex,
74                                    void** outData, uint32_t* outDataLen);
75   static bool FindURLFromNativeURL(IDataObject* inDataObject, UINT inIndex,
76                                    void** outData, uint32_t* outDataLen);
77   static bool FindUnicodeFromPlainText(IDataObject* inDataObject, UINT inIndex,
78                                        void** outData, uint32_t* outDataLen);
79   static bool FindPlatformHTML(IDataObject* inDataObject, UINT inIndex,
80                                void** outData, uint32_t* outStartOfData,
81                                uint32_t* outDataLen);
82   static void ResolveShortcut(nsIFile* inFileName, nsACString& outURL);
83 
84   nsIWidget* mWindow;
85 };
86 
87 #define SET_FORMATETC(fe, cf, td, asp, li, med) \
88   {                                             \
89     (fe).cfFormat = cf;                         \
90     (fe).ptd = td;                              \
91     (fe).dwAspect = asp;                        \
92     (fe).lindex = li;                           \
93     (fe).tymed = med;                           \
94   }
95 
96 #endif  // nsClipboard_h__
97