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/. */
5 #include "mozilla/java/ClipboardWrappers.h"
6 #include "mozilla/java/GeckoAppShellWrappers.h"
7 #include "nsClipboard.h"
8 #include "nsISupportsPrimitives.h"
10 #include "nsComponentManagerUtils.h"
12 #include "nsStringStream.h"
13 #include "nsPrimitiveHelpers.h"
15 using namespace mozilla
;
17 NS_IMPL_ISUPPORTS_INHERITED0(nsClipboard
, nsBaseClipboard
)
19 /* The Android clipboard only supports text and doesn't support mime types
20 * so we assume all clipboard data is text/plain for now. Documentation
21 * indicates that support for other data types is planned for future
25 nsClipboard::nsClipboard()
26 : nsBaseClipboard(mozilla::dom::ClipboardCapabilities(
27 false /* supportsSelectionClipboard */,
28 false /* supportsFindClipboard */,
29 false /* supportsSelectionCache */)) {
30 java::Clipboard::StartTrackingClipboardData(
31 java::GeckoAppShell::GetApplicationContext());
34 nsClipboard::~nsClipboard() {
35 java::Clipboard::StopTrackingClipboardData(
36 java::GeckoAppShell::GetApplicationContext());
40 nsresult
nsClipboard::GetTextFromTransferable(nsITransferable
* aTransferable
,
43 nsTArray
<nsCString
> flavors
;
44 nsresult rv
= aTransferable
->FlavorsTransferableCanImport(flavors
);
49 for (auto& flavorStr
: flavors
) {
50 if (flavorStr
.EqualsLiteral(kTextMime
)) {
51 nsCOMPtr
<nsISupports
> item
;
53 aTransferable
->GetTransferData(kTextMime
, getter_AddRefs(item
));
54 if (NS_WARN_IF(NS_FAILED(rv
))) {
57 nsCOMPtr
<nsISupportsString
> supportsString
= do_QueryInterface(item
);
59 supportsString
->GetData(aText
);
61 } else if (flavorStr
.EqualsLiteral(kHTMLMime
)) {
62 nsCOMPtr
<nsISupports
> item
;
64 aTransferable
->GetTransferData(kHTMLMime
, getter_AddRefs(item
));
65 if (NS_WARN_IF(NS_FAILED(rv
))) {
68 nsCOMPtr
<nsISupportsString
> supportsString
= do_QueryInterface(item
);
70 supportsString
->GetData(aHTML
);
78 nsClipboard::SetNativeClipboardData(nsITransferable
* aTransferable
,
79 int32_t aWhichClipboard
) {
80 MOZ_DIAGNOSTIC_ASSERT(aTransferable
);
81 MOZ_DIAGNOSTIC_ASSERT(
82 nsIClipboard::IsClipboardTypeSupported(aWhichClipboard
));
84 if (!jni::IsAvailable()) {
85 return NS_ERROR_NOT_AVAILABLE
;
90 nsresult rv
= GetTextFromTransferable(aTransferable
, text
, html
);
95 if (!html
.IsEmpty() &&
96 java::Clipboard::SetHTML(java::GeckoAppShell::GetApplicationContext(),
100 if (!text
.IsEmpty() &&
101 java::Clipboard::SetText(java::GeckoAppShell::GetApplicationContext(),
106 return NS_ERROR_FAILURE
;
110 nsClipboard::GetNativeClipboardData(nsITransferable
* aTransferable
,
111 int32_t aWhichClipboard
) {
112 MOZ_DIAGNOSTIC_ASSERT(aTransferable
);
113 MOZ_DIAGNOSTIC_ASSERT(
114 nsIClipboard::IsClipboardTypeSupported(aWhichClipboard
));
116 if (!jni::IsAvailable()) {
117 return NS_ERROR_NOT_AVAILABLE
;
120 nsTArray
<nsCString
> flavors
;
121 aTransferable
->FlavorsTransferableCanImport(flavors
);
123 for (auto& flavorStr
: flavors
) {
124 if (flavorStr
.EqualsLiteral(kTextMime
) ||
125 flavorStr
.EqualsLiteral(kHTMLMime
)) {
126 auto text
= java::Clipboard::GetTextData(
127 java::GeckoAppShell::GetApplicationContext(), flavorStr
);
131 nsString buffer
= text
->ToString();
132 if (buffer
.IsEmpty()) {
135 nsCOMPtr
<nsISupports
> wrapper
;
136 nsPrimitiveHelpers::CreatePrimitiveForData(flavorStr
, buffer
.get(),
138 getter_AddRefs(wrapper
));
140 aTransferable
->SetTransferData(flavorStr
.get(), wrapper
);
146 mozilla::jni::ByteArray::LocalRef bytes
;
147 nsresult rv
= java::Clipboard::GetRawData(flavorStr
, &bytes
);
148 if (NS_FAILED(rv
) || !bytes
) {
151 nsCOMPtr
<nsIInputStream
> byteStream
;
152 rv
= NS_NewByteInputStream(
153 getter_AddRefs(byteStream
),
155 reinterpret_cast<const char*>(bytes
->GetElements().Elements()),
158 if (NS_WARN_IF(NS_FAILED(rv
))) {
161 rv
= aTransferable
->SetTransferData(flavorStr
.get(), byteStream
);
162 if (NS_WARN_IF(NS_FAILED(rv
))) {
167 return NS_ERROR_FAILURE
;
170 nsresult
nsClipboard::EmptyNativeClipboardData(int32_t aWhichClipboard
) {
171 MOZ_DIAGNOSTIC_ASSERT(
172 nsIClipboard::IsClipboardTypeSupported(aWhichClipboard
));
174 if (!jni::IsAvailable()) {
175 return NS_ERROR_NOT_AVAILABLE
;
178 java::Clipboard::Clear(java::GeckoAppShell::GetApplicationContext());
183 mozilla::Result
<int32_t, nsresult
>
184 nsClipboard::GetNativeClipboardSequenceNumber(int32_t aWhichClipboard
) {
185 MOZ_DIAGNOSTIC_ASSERT(
186 nsIClipboard::IsClipboardTypeSupported(aWhichClipboard
));
188 if (!jni::IsAvailable()) {
189 return Err(NS_ERROR_NOT_AVAILABLE
);
192 return java::Clipboard::GetSequenceNumber(
193 java::GeckoAppShell::GetApplicationContext());
196 mozilla::Result
<bool, nsresult
>
197 nsClipboard::HasNativeClipboardDataMatchingFlavors(
198 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
) {
199 MOZ_DIAGNOSTIC_ASSERT(
200 nsIClipboard::IsClipboardTypeSupported(aWhichClipboard
));
202 if (!jni::IsAvailable()) {
203 return Err(NS_ERROR_NOT_AVAILABLE
);
206 for (auto& flavor
: aFlavorList
) {
207 if (java::Clipboard::HasData(java::GeckoAppShell::GetApplicationContext(),
208 NS_ConvertASCIItoUTF16(flavor
))) {