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"
11 #include "nsPrimitiveHelpers.h"
13 using namespace mozilla
;
15 NS_IMPL_ISUPPORTS(nsClipboard
, nsIClipboard
)
17 /* The Android clipboard only supports text and doesn't support mime types
18 * so we assume all clipboard data is text/unicode for now. Documentation
19 * indicates that support for other data types is planned for future
23 nsClipboard::nsClipboard() {}
26 nsClipboard::SetData(nsITransferable
* aTransferable
, nsIClipboardOwner
* anOwner
,
27 int32_t aWhichClipboard
) {
28 if (aWhichClipboard
!= kGlobalClipboard
) return NS_ERROR_NOT_IMPLEMENTED
;
30 if (!jni::IsAvailable()) {
31 return NS_ERROR_NOT_AVAILABLE
;
34 nsTArray
<nsCString
> flavors
;
35 aTransferable
->FlavorsTransferableCanImport(flavors
);
40 for (auto& flavorStr
: flavors
) {
41 if (flavorStr
.EqualsLiteral(kUnicodeMime
)) {
42 nsCOMPtr
<nsISupports
> item
;
44 aTransferable
->GetTransferData(kUnicodeMime
, getter_AddRefs(item
));
45 if (NS_WARN_IF(NS_FAILED(rv
))) {
48 nsCOMPtr
<nsISupportsString
> supportsString
= do_QueryInterface(item
);
50 supportsString
->GetData(text
);
52 } else if (flavorStr
.EqualsLiteral(kHTMLMime
)) {
53 nsCOMPtr
<nsISupports
> item
;
55 aTransferable
->GetTransferData(kHTMLMime
, getter_AddRefs(item
));
56 if (NS_WARN_IF(NS_FAILED(rv
))) {
59 nsCOMPtr
<nsISupportsString
> supportsString
= do_QueryInterface(item
);
61 supportsString
->GetData(html
);
66 if (!html
.IsEmpty() &&
67 java::Clipboard::SetHTML(java::GeckoAppShell::GetApplicationContext(),
71 if (!text
.IsEmpty() &&
72 java::Clipboard::SetText(java::GeckoAppShell::GetApplicationContext(),
77 return NS_ERROR_FAILURE
;
81 nsClipboard::GetData(nsITransferable
* aTransferable
, int32_t aWhichClipboard
) {
82 if (aWhichClipboard
!= kGlobalClipboard
) return NS_ERROR_NOT_IMPLEMENTED
;
84 if (!jni::IsAvailable()) {
85 return NS_ERROR_NOT_AVAILABLE
;
88 nsTArray
<nsCString
> flavors
;
89 aTransferable
->FlavorsTransferableCanImport(flavors
);
91 for (auto& flavorStr
: flavors
) {
92 if (flavorStr
.EqualsLiteral(kUnicodeMime
) ||
93 flavorStr
.EqualsLiteral(kHTMLMime
)) {
94 auto text
= java::Clipboard::GetData(
95 java::GeckoAppShell::GetApplicationContext(), flavorStr
);
99 nsString buffer
= text
->ToString();
100 if (buffer
.IsEmpty()) {
103 nsCOMPtr
<nsISupports
> wrapper
;
104 nsPrimitiveHelpers::CreatePrimitiveForData(flavorStr
, buffer
.get(),
106 getter_AddRefs(wrapper
));
108 aTransferable
->SetTransferData(flavorStr
.get(), wrapper
);
114 return NS_ERROR_FAILURE
;
118 nsClipboard::EmptyClipboard(int32_t aWhichClipboard
) {
119 if (aWhichClipboard
!= kGlobalClipboard
) return NS_ERROR_NOT_IMPLEMENTED
;
121 if (!jni::IsAvailable()) {
122 return NS_ERROR_NOT_AVAILABLE
;
125 java::Clipboard::ClearText(java::GeckoAppShell::GetApplicationContext());
131 nsClipboard::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
132 int32_t aWhichClipboard
, bool* aHasText
) {
134 if (aWhichClipboard
!= kGlobalClipboard
) return NS_ERROR_NOT_IMPLEMENTED
;
136 if (!jni::IsAvailable()) {
137 return NS_ERROR_NOT_AVAILABLE
;
140 for (auto& flavor
: aFlavorList
) {
142 java::Clipboard::HasData(java::GeckoAppShell::GetApplicationContext(),
143 NS_ConvertASCIItoUTF16(flavor
));
154 nsClipboard::SupportsSelectionClipboard(bool* aIsSupported
) {
155 *aIsSupported
= false;
160 nsClipboard::SupportsFindClipboard(bool* _retval
) {