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 "HeadlessClipboard.h"
7 #include "nsISupportsPrimitives.h"
8 #include "nsComponentManagerUtils.h"
14 NS_IMPL_ISUPPORTS(HeadlessClipboard
, nsIClipboard
)
16 HeadlessClipboard::HeadlessClipboard()
17 : mClipboard(MakeUnique
<HeadlessClipboardData
>()) {}
20 HeadlessClipboard::SetData(nsITransferable
* aTransferable
,
21 nsIClipboardOwner
* anOwner
,
22 int32_t aWhichClipboard
) {
23 if (aWhichClipboard
!= kGlobalClipboard
) {
24 return NS_ERROR_NOT_IMPLEMENTED
;
27 // Clear out the clipboard in order to set the new data.
28 EmptyClipboard(aWhichClipboard
);
30 // Only support plain text for now.
31 nsCOMPtr
<nsISupports
> clip
;
33 aTransferable
->GetTransferData(kUnicodeMime
, getter_AddRefs(clip
));
37 nsCOMPtr
<nsISupportsString
> wideString
= do_QueryInterface(clip
);
39 return NS_ERROR_NOT_IMPLEMENTED
;
41 nsAutoString utf16string
;
42 wideString
->GetData(utf16string
);
43 mClipboard
->SetText(utf16string
);
49 HeadlessClipboard::GetData(nsITransferable
* aTransferable
,
50 int32_t aWhichClipboard
) {
51 if (aWhichClipboard
!= kGlobalClipboard
) {
52 return NS_ERROR_NOT_IMPLEMENTED
;
56 nsCOMPtr
<nsISupportsString
> dataWrapper
=
57 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID
, &rv
);
58 rv
= dataWrapper
->SetData(mClipboard
->GetText());
59 if (NS_WARN_IF(NS_FAILED(rv
))) {
62 nsCOMPtr
<nsISupports
> genericDataWrapper
= do_QueryInterface(dataWrapper
);
63 rv
= aTransferable
->SetTransferData(kUnicodeMime
, genericDataWrapper
);
64 if (NS_WARN_IF(NS_FAILED(rv
))) {
71 HeadlessClipboard::EmptyClipboard(int32_t aWhichClipboard
) {
72 if (aWhichClipboard
!= kGlobalClipboard
) {
73 return NS_ERROR_NOT_IMPLEMENTED
;
80 HeadlessClipboard::HasDataMatchingFlavors(
81 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
,
84 if (aWhichClipboard
!= kGlobalClipboard
) {
85 return NS_ERROR_NOT_IMPLEMENTED
;
87 // Retrieve the union of all aHasType in aFlavorList
88 for (auto& flavor
: aFlavorList
) {
89 if (flavor
.EqualsLiteral(kUnicodeMime
) && mClipboard
->HasText()) {
98 HeadlessClipboard::SupportsSelectionClipboard(bool* aIsSupported
) {
99 *aIsSupported
= false;
104 HeadlessClipboard::SupportsFindClipboard(bool* _retval
) {
105 NS_ENSURE_ARG_POINTER(_retval
);
111 } // namespace widget
112 } // namespace mozilla