Bug 1253840 - Remove .ini file as it's no longer necessary by passing on all platform...
[gecko.git] / widget / nsClipboardProxy.cpp
blob04e5cdc7d518f59a9458e5617a7f2683ec35b910
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 "nsClipboardProxy.h"
7 #if defined(ACCESSIBILITY) && defined(XP_WIN)
8 # include "mozilla/a11y/Compatibility.h"
9 #endif
10 #include "mozilla/ClipboardWriteRequestChild.h"
11 #include "mozilla/dom/ContentChild.h"
12 #include "mozilla/net/CookieJarSettings.h"
13 #include "mozilla/Maybe.h"
14 #include "mozilla/Unused.h"
15 #include "nsArrayUtils.h"
16 #include "nsISupportsPrimitives.h"
17 #include "nsCOMPtr.h"
18 #include "nsComponentManagerUtils.h"
19 #include "nsXULAppAPI.h"
20 #include "nsContentUtils.h"
21 #include "PermissionMessageUtils.h"
23 using namespace mozilla;
24 using namespace mozilla::dom;
26 NS_IMPL_ISUPPORTS(nsClipboardProxy, nsIClipboard, nsIClipboardProxy)
28 nsClipboardProxy::nsClipboardProxy() : mClipboardCaps(false, false, false) {}
30 NS_IMETHODIMP
31 nsClipboardProxy::SetData(nsITransferable* aTransferable,
32 nsIClipboardOwner* anOwner, int32_t aWhichClipboard) {
33 #if defined(ACCESSIBILITY) && defined(XP_WIN)
34 a11y::Compatibility::SuppressA11yForClipboardCopy();
35 #endif
37 ContentChild* child = ContentChild::GetSingleton();
38 IPCTransferable ipcTransferable;
39 nsContentUtils::TransferableToIPCTransferable(aTransferable, &ipcTransferable,
40 false, nullptr);
41 child->SendSetClipboard(std::move(ipcTransferable), aWhichClipboard);
42 return NS_OK;
45 NS_IMETHODIMP nsClipboardProxy::AsyncSetData(
46 int32_t aWhichClipboard, nsIAsyncSetClipboardDataCallback* aCallback,
47 nsIAsyncSetClipboardData** _retval) {
48 RefPtr<ClipboardWriteRequestChild> request =
49 MakeRefPtr<ClipboardWriteRequestChild>(aCallback);
50 ContentChild::GetSingleton()->SendPClipboardWriteRequestConstructor(
51 request, aWhichClipboard);
52 request.forget(_retval);
53 return NS_OK;
56 NS_IMETHODIMP
57 nsClipboardProxy::GetData(nsITransferable* aTransferable,
58 int32_t aWhichClipboard) {
59 nsTArray<nsCString> types;
60 aTransferable->FlavorsTransferableCanImport(types);
62 IPCTransferableData transferable;
63 ContentChild::GetSingleton()->SendGetClipboard(types, aWhichClipboard,
64 &transferable);
65 return nsContentUtils::IPCTransferableDataToTransferable(
66 transferable, false /* aAddDataFlavor */, aTransferable,
67 false /* aFilterUnknownFlavors */);
70 NS_IMETHODIMP
71 nsClipboardProxy::EmptyClipboard(int32_t aWhichClipboard) {
72 ContentChild::GetSingleton()->SendEmptyClipboard(aWhichClipboard);
73 return NS_OK;
76 NS_IMETHODIMP
77 nsClipboardProxy::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
78 int32_t aWhichClipboard,
79 bool* aHasType) {
80 *aHasType = false;
82 ContentChild::GetSingleton()->SendClipboardHasType(aFlavorList,
83 aWhichClipboard, aHasType);
85 return NS_OK;
88 NS_IMETHODIMP
89 nsClipboardProxy::IsClipboardTypeSupported(int32_t aWhichClipboard,
90 bool* aIsSupported) {
91 switch (aWhichClipboard) {
92 case kGlobalClipboard:
93 // We always support the global clipboard.
94 *aIsSupported = true;
95 return NS_OK;
96 case kSelectionClipboard:
97 *aIsSupported = mClipboardCaps.supportsSelectionClipboard();
98 return NS_OK;
99 case kFindClipboard:
100 *aIsSupported = mClipboardCaps.supportsFindClipboard();
101 return NS_OK;
102 case kSelectionCache:
103 *aIsSupported = mClipboardCaps.supportsSelectionCache();
104 return NS_OK;
107 *aIsSupported = false;
108 return NS_OK;
111 void nsClipboardProxy::SetCapabilities(
112 const ClipboardCapabilities& aClipboardCaps) {
113 mClipboardCaps = aClipboardCaps;
116 RefPtr<DataFlavorsPromise> nsClipboardProxy::AsyncHasDataMatchingFlavors(
117 const nsTArray<nsCString>& aFlavorList, int32_t aWhichClipboard) {
118 auto promise = MakeRefPtr<DataFlavorsPromise::Private>(__func__);
119 ContentChild::GetSingleton()
120 ->SendClipboardHasTypesAsync(aFlavorList, aWhichClipboard)
121 ->Then(
122 GetMainThreadSerialEventTarget(), __func__,
123 /* resolve */
124 [promise](nsTArray<nsCString> types) {
125 promise->Resolve(std::move(types), __func__);
127 /* reject */
128 [promise](mozilla::ipc::ResponseRejectReason aReason) {
129 promise->Reject(NS_ERROR_FAILURE, __func__);
132 return promise.forget();
135 RefPtr<GenericPromise> nsClipboardProxy::AsyncGetData(
136 nsITransferable* aTransferable, int32_t aWhichClipboard) {
137 if (!aTransferable) {
138 return GenericPromise::CreateAndReject(NS_ERROR_FAILURE, __func__);
141 // Get a list of flavors this transferable can import
142 nsTArray<nsCString> flavors;
143 nsresult rv = aTransferable->FlavorsTransferableCanImport(flavors);
144 if (NS_FAILED(rv)) {
145 return GenericPromise::CreateAndReject(rv, __func__);
148 nsCOMPtr<nsITransferable> transferable(aTransferable);
149 auto promise = MakeRefPtr<GenericPromise::Private>(__func__);
150 ContentChild::GetSingleton()
151 ->SendGetClipboardAsync(flavors, aWhichClipboard)
152 ->Then(
153 GetMainThreadSerialEventTarget(), __func__,
154 /* resolve */
155 [promise, transferable](
156 const IPCTransferableDataOrError& ipcTransferableDataOrError) {
157 if (ipcTransferableDataOrError.type() ==
158 IPCTransferableDataOrError::Tnsresult) {
159 promise->Reject(ipcTransferableDataOrError.get_nsresult(),
160 __func__);
161 return;
164 nsresult rv = nsContentUtils::IPCTransferableDataToTransferable(
165 ipcTransferableDataOrError.get_IPCTransferableData(),
166 false /* aAddDataFlavor */, transferable,
167 false /* aFilterUnknownFlavors */);
168 if (NS_FAILED(rv)) {
169 promise->Reject(rv, __func__);
170 return;
173 promise->Resolve(true, __func__);
175 /* reject */
176 [promise](mozilla::ipc::ResponseRejectReason aReason) {
177 promise->Reject(NS_ERROR_FAILURE, __func__);
180 return promise.forget();