Bug 1716846 [wpt PR 29402] - Update wpt metadata, a=testonly
[gecko.git] / widget / android / nsClipboard.cpp
blob6008b5264f1d61c19d2ba1908447914578b2888a
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"
9 #include "nsCOMPtr.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
20 * releases.
23 nsClipboard::nsClipboard() {}
25 NS_IMETHODIMP
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);
37 nsAutoString html;
38 nsAutoString text;
40 for (auto& flavorStr : flavors) {
41 if (flavorStr.EqualsLiteral(kUnicodeMime)) {
42 nsCOMPtr<nsISupports> item;
43 nsresult rv =
44 aTransferable->GetTransferData(kUnicodeMime, getter_AddRefs(item));
45 if (NS_WARN_IF(NS_FAILED(rv))) {
46 continue;
48 nsCOMPtr<nsISupportsString> supportsString = do_QueryInterface(item);
49 if (supportsString) {
50 supportsString->GetData(text);
52 } else if (flavorStr.EqualsLiteral(kHTMLMime)) {
53 nsCOMPtr<nsISupports> item;
54 nsresult rv =
55 aTransferable->GetTransferData(kHTMLMime, getter_AddRefs(item));
56 if (NS_WARN_IF(NS_FAILED(rv))) {
57 continue;
59 nsCOMPtr<nsISupportsString> supportsString = do_QueryInterface(item);
60 if (supportsString) {
61 supportsString->GetData(html);
66 if (!html.IsEmpty() &&
67 java::Clipboard::SetHTML(java::GeckoAppShell::GetApplicationContext(),
68 text, html)) {
69 return NS_OK;
71 if (!text.IsEmpty() &&
72 java::Clipboard::SetText(java::GeckoAppShell::GetApplicationContext(),
73 text)) {
74 return NS_OK;
77 return NS_ERROR_FAILURE;
80 NS_IMETHODIMP
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);
96 if (!text) {
97 continue;
99 nsString buffer = text->ToString();
100 if (buffer.IsEmpty()) {
101 continue;
103 nsCOMPtr<nsISupports> wrapper;
104 nsPrimitiveHelpers::CreatePrimitiveForData(flavorStr, buffer.get(),
105 buffer.Length() * 2,
106 getter_AddRefs(wrapper));
107 if (wrapper) {
108 aTransferable->SetTransferData(flavorStr.get(), wrapper);
109 return NS_OK;
114 return NS_ERROR_FAILURE;
117 NS_IMETHODIMP
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());
127 return NS_OK;
130 NS_IMETHODIMP
131 nsClipboard::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
132 int32_t aWhichClipboard, bool* aHasText) {
133 *aHasText = false;
134 if (aWhichClipboard != kGlobalClipboard) return NS_ERROR_NOT_IMPLEMENTED;
136 if (!jni::IsAvailable()) {
137 return NS_ERROR_NOT_AVAILABLE;
140 for (auto& flavor : aFlavorList) {
141 bool hasData =
142 java::Clipboard::HasData(java::GeckoAppShell::GetApplicationContext(),
143 NS_ConvertASCIItoUTF16(flavor));
144 if (hasData) {
145 *aHasText = true;
146 return NS_OK;
150 return NS_OK;
153 NS_IMETHODIMP
154 nsClipboard::SupportsSelectionClipboard(bool* aIsSupported) {
155 *aIsSupported = false;
156 return NS_OK;
159 NS_IMETHODIMP
160 nsClipboard::SupportsFindClipboard(bool* _retval) {
161 *_retval = false;
162 return NS_OK;