no bug - Bumping Firefox l10n changesets r=release a=l10n-bump DONTBUILD CLOSED TREE
[gecko.git] / widget / android / nsClipboard.cpp
blobc2a03dd5408e4a5bca5a108acc553011944cf6c9
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 "nsMemory.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
22 * releases.
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());
39 // static
40 nsresult nsClipboard::GetTextFromTransferable(nsITransferable* aTransferable,
41 nsString& aText,
42 nsString& aHTML) {
43 nsTArray<nsCString> flavors;
44 nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
45 if (NS_FAILED(rv)) {
46 return rv;
49 for (auto& flavorStr : flavors) {
50 if (flavorStr.EqualsLiteral(kTextMime)) {
51 nsCOMPtr<nsISupports> item;
52 nsresult rv =
53 aTransferable->GetTransferData(kTextMime, getter_AddRefs(item));
54 if (NS_WARN_IF(NS_FAILED(rv))) {
55 continue;
57 nsCOMPtr<nsISupportsString> supportsString = do_QueryInterface(item);
58 if (supportsString) {
59 supportsString->GetData(aText);
61 } else if (flavorStr.EqualsLiteral(kHTMLMime)) {
62 nsCOMPtr<nsISupports> item;
63 nsresult rv =
64 aTransferable->GetTransferData(kHTMLMime, getter_AddRefs(item));
65 if (NS_WARN_IF(NS_FAILED(rv))) {
66 continue;
68 nsCOMPtr<nsISupportsString> supportsString = do_QueryInterface(item);
69 if (supportsString) {
70 supportsString->GetData(aHTML);
74 return NS_OK;
77 NS_IMETHODIMP
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;
88 nsString text;
89 nsString html;
90 nsresult rv = GetTextFromTransferable(aTransferable, text, html);
91 if (NS_FAILED(rv)) {
92 return rv;
95 if (!html.IsEmpty() &&
96 java::Clipboard::SetHTML(java::GeckoAppShell::GetApplicationContext(),
97 text, html)) {
98 return NS_OK;
100 if (!text.IsEmpty() &&
101 java::Clipboard::SetText(java::GeckoAppShell::GetApplicationContext(),
102 text)) {
103 return NS_OK;
106 return NS_ERROR_FAILURE;
109 NS_IMETHODIMP
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);
128 if (!text) {
129 continue;
131 nsString buffer = text->ToString();
132 if (buffer.IsEmpty()) {
133 continue;
135 nsCOMPtr<nsISupports> wrapper;
136 nsPrimitiveHelpers::CreatePrimitiveForData(flavorStr, buffer.get(),
137 buffer.Length() * 2,
138 getter_AddRefs(wrapper));
139 if (wrapper) {
140 aTransferable->SetTransferData(flavorStr.get(), wrapper);
141 return NS_OK;
143 continue;
146 mozilla::jni::ByteArray::LocalRef bytes;
147 nsresult rv = java::Clipboard::GetRawData(flavorStr, &bytes);
148 if (NS_FAILED(rv) || !bytes) {
149 continue;
151 nsCOMPtr<nsIInputStream> byteStream;
152 rv = NS_NewByteInputStream(
153 getter_AddRefs(byteStream),
154 mozilla::Span(
155 reinterpret_cast<const char*>(bytes->GetElements().Elements()),
156 bytes->Length()),
157 NS_ASSIGNMENT_COPY);
158 if (NS_WARN_IF(NS_FAILED(rv))) {
159 continue;
161 rv = aTransferable->SetTransferData(flavorStr.get(), byteStream);
162 if (NS_WARN_IF(NS_FAILED(rv))) {
163 continue;
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());
180 return NS_OK;
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))) {
209 return true;
213 return false;