Line data Source code
1 : /* This Source Code Form is subject to the terms of the Mozilla Public
2 : * License, v. 2.0. If a copy of the MPL was not distributed with this
3 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 :
5 : #include "HeadlessClipboard.h"
6 :
7 : #include "nsISupportsPrimitives.h"
8 : #include "nsComponentManagerUtils.h"
9 : #include "nsCOMPtr.h"
10 :
11 : namespace mozilla {
12 : namespace widget {
13 :
14 0 : NS_IMPL_ISUPPORTS(HeadlessClipboard, nsIClipboard)
15 :
16 0 : HeadlessClipboard::HeadlessClipboard()
17 0 : : mClipboard(MakeUnique<HeadlessClipboardData>())
18 : {
19 0 : }
20 :
21 : NS_IMETHODIMP
22 0 : HeadlessClipboard::SetData(nsITransferable *aTransferable,
23 : nsIClipboardOwner *anOwner,
24 : int32_t aWhichClipboard)
25 : {
26 0 : if (aWhichClipboard != kGlobalClipboard) {
27 0 : return NS_ERROR_NOT_IMPLEMENTED;
28 : }
29 :
30 : // Clear out the clipboard in order to set the new data.
31 0 : EmptyClipboard(aWhichClipboard);
32 :
33 : // Only support plain text for now.
34 0 : nsCOMPtr<nsISupports> clip;
35 : uint32_t len;
36 0 : nsresult rv = aTransferable->GetTransferData(kUnicodeMime,
37 0 : getter_AddRefs(clip),
38 0 : &len);
39 0 : if (NS_FAILED(rv)) {
40 0 : return rv;
41 : }
42 0 : nsCOMPtr<nsISupportsString> wideString = do_QueryInterface(clip);
43 0 : if (!wideString) {
44 0 : return NS_ERROR_NOT_IMPLEMENTED;
45 : }
46 0 : nsAutoString utf16string;
47 0 : wideString->GetData(utf16string);
48 0 : mClipboard->SetText(utf16string);
49 :
50 0 : return NS_OK;
51 : }
52 :
53 : NS_IMETHODIMP
54 0 : HeadlessClipboard::GetData(nsITransferable *aTransferable,
55 : int32_t aWhichClipboard)
56 : {
57 0 : if (aWhichClipboard != kGlobalClipboard) {
58 0 : return NS_ERROR_NOT_IMPLEMENTED;
59 : }
60 :
61 : nsresult rv;
62 : nsCOMPtr<nsISupportsString> dataWrapper =
63 0 : do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
64 0 : rv = dataWrapper->SetData(mClipboard->GetText());
65 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
66 0 : return rv;
67 : }
68 0 : nsCOMPtr<nsISupports> genericDataWrapper = do_QueryInterface(dataWrapper);
69 0 : uint32_t len = mClipboard->GetText().Length() * sizeof(char16_t);
70 0 : rv = aTransferable->SetTransferData(kUnicodeMime, genericDataWrapper, len);
71 0 : if (NS_WARN_IF(NS_FAILED(rv))) {
72 0 : return rv;
73 : }
74 0 : return NS_OK;
75 : }
76 :
77 : NS_IMETHODIMP
78 0 : HeadlessClipboard::EmptyClipboard(int32_t aWhichClipboard)
79 : {
80 0 : if (aWhichClipboard != kGlobalClipboard) {
81 0 : return NS_ERROR_NOT_IMPLEMENTED;
82 : }
83 0 : mClipboard->Clear();
84 0 : return NS_OK;
85 : }
86 :
87 : NS_IMETHODIMP
88 0 : HeadlessClipboard::HasDataMatchingFlavors(const char **aFlavorList,
89 : uint32_t aLength, int32_t aWhichClipboard,
90 : bool *aHasType)
91 : {
92 0 : *aHasType = false;
93 0 : if (aWhichClipboard != kGlobalClipboard) {
94 0 : return NS_ERROR_NOT_IMPLEMENTED;
95 : }
96 : // Retrieve the union of all aHasType in aFlavorList
97 0 : for (uint32_t i = 0; i < aLength; ++i) {
98 0 : const char *flavor = aFlavorList[i];
99 0 : if (!flavor) {
100 0 : continue;
101 : }
102 0 : if (!strcmp(flavor, kUnicodeMime) && mClipboard->HasText()) {
103 0 : *aHasType = true;
104 : }
105 : }
106 0 : return NS_OK;
107 : }
108 :
109 : NS_IMETHODIMP
110 0 : HeadlessClipboard::SupportsSelectionClipboard(bool *aIsSupported)
111 : {
112 0 : *aIsSupported = false;
113 0 : return NS_OK;
114 : }
115 :
116 : NS_IMETHODIMP
117 0 : HeadlessClipboard::SupportsFindClipboard(bool* _retval)
118 : {
119 0 : NS_ENSURE_ARG_POINTER(_retval);
120 :
121 0 : *_retval = false;
122 0 : return NS_OK;
123 : }
124 :
125 : } // namespace widget
126 : } // namespace mozilla
|