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 "mozilla/Unused.h"
7 #include "nsArrayUtils.h"
8 #include "nsClipboardProxy.h"
9 #include "nsISupportsPrimitives.h"
11 #include "nsComponentManagerUtils.h"
12 #include "nsXULAppAPI.h"
13 #include "nsContentUtils.h"
14 #include "nsStringStream.h"
16 using namespace mozilla
;
17 using namespace mozilla::dom
;
19 NS_IMPL_ISUPPORTS(nsClipboardProxy
, nsIClipboard
, nsIClipboardProxy
)
21 nsClipboardProxy::nsClipboardProxy() : mClipboardCaps(false, false) {}
24 nsClipboardProxy::SetData(nsITransferable
* aTransferable
,
25 nsIClipboardOwner
* anOwner
, int32_t aWhichClipboard
) {
26 ContentChild
* child
= ContentChild::GetSingleton();
28 IPCDataTransfer ipcDataTransfer
;
29 nsContentUtils::TransferableToIPCTransferable(aTransferable
, &ipcDataTransfer
,
30 false, child
, nullptr);
32 bool isPrivateData
= aTransferable
->GetIsPrivateData();
33 nsCOMPtr
<nsIPrincipal
> requestingPrincipal
=
34 aTransferable
->GetRequestingPrincipal();
35 nsContentPolicyType contentPolicyType
= aTransferable
->GetContentPolicyType();
36 child
->SendSetClipboard(ipcDataTransfer
, isPrivateData
,
37 IPC::Principal(requestingPrincipal
),
38 contentPolicyType
, aWhichClipboard
);
44 nsClipboardProxy::GetData(nsITransferable
* aTransferable
,
45 int32_t aWhichClipboard
) {
46 nsTArray
<nsCString
> types
;
47 aTransferable
->FlavorsTransferableCanImport(types
);
50 IPCDataTransfer dataTransfer
;
51 ContentChild::GetSingleton()->SendGetClipboard(types
, aWhichClipboard
,
54 auto& items
= dataTransfer
.items();
55 for (uint32_t j
= 0; j
< items
.Length(); ++j
) {
56 const IPCDataTransferItem
& item
= items
[j
];
58 if (item
.data().type() == IPCDataTransferData::TnsString
) {
59 nsCOMPtr
<nsISupportsString
> dataWrapper
=
60 do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID
, &rv
);
61 NS_ENSURE_SUCCESS(rv
, rv
);
63 const nsString
& data
= item
.data().get_nsString();
64 rv
= dataWrapper
->SetData(data
);
65 NS_ENSURE_SUCCESS(rv
, rv
);
67 rv
= aTransferable
->SetTransferData(item
.flavor().get(), dataWrapper
);
68 NS_ENSURE_SUCCESS(rv
, rv
);
69 } else if (item
.data().type() == IPCDataTransferData::TShmem
) {
70 // If this is an image, convert it into an nsIInputStream.
71 const nsCString
& flavor
= item
.flavor();
72 mozilla::ipc::Shmem data
= item
.data().get_Shmem();
73 if (flavor
.EqualsLiteral(kJPEGImageMime
) ||
74 flavor
.EqualsLiteral(kJPGImageMime
) ||
75 flavor
.EqualsLiteral(kPNGImageMime
) ||
76 flavor
.EqualsLiteral(kGIFImageMime
)) {
77 nsCOMPtr
<nsIInputStream
> stream
;
79 NS_NewCStringInputStream(
80 getter_AddRefs(stream
),
81 nsDependentCSubstring(data
.get
<char>(), data
.Size
<char>()));
83 rv
= aTransferable
->SetTransferData(flavor
.get(), stream
);
84 NS_ENSURE_SUCCESS(rv
, rv
);
85 } else if (flavor
.EqualsLiteral(kNativeHTMLMime
) ||
86 flavor
.EqualsLiteral(kRTFMime
) ||
87 flavor
.EqualsLiteral(kCustomTypesMime
)) {
88 nsCOMPtr
<nsISupportsCString
> dataWrapper
=
89 do_CreateInstance(NS_SUPPORTS_CSTRING_CONTRACTID
, &rv
);
90 NS_ENSURE_SUCCESS(rv
, rv
);
92 rv
= dataWrapper
->SetData(
93 nsDependentCSubstring(data
.get
<char>(), data
.Size
<char>()));
94 NS_ENSURE_SUCCESS(rv
, rv
);
96 rv
= aTransferable
->SetTransferData(item
.flavor().get(), dataWrapper
);
97 NS_ENSURE_SUCCESS(rv
, rv
);
100 mozilla::Unused
<< ContentChild::GetSingleton()->DeallocShmem(data
);
108 nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard
) {
109 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard
);
114 nsClipboardProxy::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
115 int32_t aWhichClipboard
,
119 ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList
,
120 aWhichClipboard
, aHasType
);
126 nsClipboardProxy::SupportsSelectionClipboard(bool* aIsSupported
) {
127 *aIsSupported
= mClipboardCaps
.supportsSelectionClipboard();
132 nsClipboardProxy::SupportsFindClipboard(bool* aIsSupported
) {
133 *aIsSupported
= mClipboardCaps
.supportsFindClipboard();
137 void nsClipboardProxy::SetCapabilities(
138 const ClipboardCapabilities
& aClipboardCaps
) {
139 mClipboardCaps
= aClipboardCaps
;