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/dom/ContentChild.h"
6 #include "nsClipboard.h"
7 #include "nsISupportsPrimitives.h"
9 #include "nsComponentManagerUtils.h"
10 #include "nsXULAppAPI.h"
12 using namespace mozilla
;
13 using mozilla::dom::ContentChild
;
15 #define LOG_TAG "Clipboard"
16 #define LOGI(args...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, ## args)
17 #define LOGE(args...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, ## args)
19 NS_IMPL_ISUPPORTS(nsClipboard
, nsIClipboard
)
21 nsClipboard::nsClipboard()
26 nsClipboard::SetData(nsITransferable
*aTransferable
,
27 nsIClipboardOwner
*anOwner
, int32_t aWhichClipboard
)
29 if (aWhichClipboard
!= kGlobalClipboard
) {
30 return NS_ERROR_NOT_IMPLEMENTED
;
33 nsCOMPtr
<nsISupports
> tmp
;
35 nsresult rv
= aTransferable
->GetTransferData(kUnicodeMime
, getter_AddRefs(tmp
),
37 if (NS_WARN_IF(NS_FAILED(rv
))) {
40 nsCOMPtr
<nsISupportsString
> supportsString
= do_QueryInterface(tmp
);
41 // No support for non-text data
42 if (NS_WARN_IF(!supportsString
)) {
43 LOGE("No support for non-text data. See bug 952456.");
44 return NS_ERROR_NOT_IMPLEMENTED
;
47 supportsString
->GetData(buffer
);
49 if (XRE_GetProcessType() == GeckoProcessType_Default
) {
52 bool isPrivateData
= false;
53 aTransferable
->GetIsPrivateData(&isPrivateData
);
54 ContentChild::GetSingleton()->SendSetClipboardText(buffer
, isPrivateData
,
62 nsClipboard::GetData(nsITransferable
*aTransferable
, int32_t aWhichClipboard
)
64 if (aWhichClipboard
!= kGlobalClipboard
) {
65 return NS_ERROR_NOT_IMPLEMENTED
;
69 if (XRE_GetProcessType() == GeckoProcessType_Default
) {
72 ContentChild::GetSingleton()->SendGetClipboardText(aWhichClipboard
, &buffer
);
76 nsCOMPtr
<nsISupportsString
> dataWrapper
=
77 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID
, &rv
);
78 if (NS_WARN_IF(NS_FAILED(rv
))) {
82 rv
= dataWrapper
->SetData(buffer
);
83 if (NS_WARN_IF(NS_FAILED(rv
))) {
87 // If our data flavor has already been added, this will fail. But we don't care
88 aTransferable
->AddDataFlavor(kUnicodeMime
);
90 nsCOMPtr
<nsISupports
> nsisupportsDataWrapper
=
91 do_QueryInterface(dataWrapper
);
92 rv
= aTransferable
->SetTransferData(kUnicodeMime
, nsisupportsDataWrapper
,
93 buffer
.Length() * sizeof(PRUnichar
));
94 if (NS_WARN_IF(NS_FAILED(rv
))) {
102 nsClipboard::EmptyClipboard(int32_t aWhichClipboard
)
104 if (aWhichClipboard
!= kGlobalClipboard
) {
105 return NS_ERROR_NOT_IMPLEMENTED
;
107 if (XRE_GetProcessType() == GeckoProcessType_Default
) {
108 mClipboard
.Truncate(0);
110 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard
);
117 nsClipboard::HasDataMatchingFlavors(const char **aFlavorList
,
118 uint32_t aLength
, int32_t aWhichClipboard
,
122 if (aWhichClipboard
!= kGlobalClipboard
) {
123 return NS_ERROR_NOT_IMPLEMENTED
;
125 if (XRE_GetProcessType() == GeckoProcessType_Default
) {
126 *aHasText
= !mClipboard
.IsEmpty();
128 ContentChild::GetSingleton()->SendClipboardHasText(aWhichClipboard
, aHasText
);
134 nsClipboard::SupportsSelectionClipboard(bool *aIsSupported
)
136 *aIsSupported
= false;
141 nsClipboard::SupportsFindClipboard(bool* _retval
)
143 NS_ENSURE_ARG_POINTER(_retval
);