1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #include "nsBaseClipboard.h"
8 #include "mozilla/Logging.h"
10 #include "nsIClipboardOwner.h"
15 using mozilla::GenericPromise
;
16 using mozilla::LogLevel
;
18 nsBaseClipboard::nsBaseClipboard() : mEmptyingForSetData(false) {}
20 nsBaseClipboard::~nsBaseClipboard() {
21 EmptyClipboard(kSelectionClipboard
);
22 EmptyClipboard(kGlobalClipboard
);
23 EmptyClipboard(kFindClipboard
);
26 NS_IMPL_ISUPPORTS(nsBaseClipboard
, nsIClipboard
)
29 * Sets the transferable object
32 NS_IMETHODIMP
nsBaseClipboard::SetData(nsITransferable
* aTransferable
,
33 nsIClipboardOwner
* anOwner
,
34 int32_t aWhichClipboard
) {
35 NS_ASSERTION(aTransferable
, "clipboard given a null transferable");
37 CLIPBOARD_LOG("%s", __FUNCTION__
);
39 if (aTransferable
== mTransferable
&& anOwner
== mClipboardOwner
) {
40 CLIPBOARD_LOG("%s: skipping update.", __FUNCTION__
);
43 bool selectClipPresent
;
44 SupportsSelectionClipboard(&selectClipPresent
);
46 SupportsFindClipboard(&findClipPresent
);
47 if (!selectClipPresent
&& !findClipPresent
&&
48 aWhichClipboard
!= kGlobalClipboard
)
49 return NS_ERROR_FAILURE
;
51 mEmptyingForSetData
= true;
52 if (NS_FAILED(EmptyClipboard(aWhichClipboard
))) {
53 CLIPBOARD_LOG("%s: emptying clipboard failed.", __FUNCTION__
);
55 mEmptyingForSetData
= false;
57 mClipboardOwner
= anOwner
;
58 mTransferable
= aTransferable
;
60 nsresult rv
= NS_ERROR_FAILURE
;
62 mIgnoreEmptyNotification
= true;
63 rv
= SetNativeClipboardData(aWhichClipboard
);
64 mIgnoreEmptyNotification
= false;
67 CLIPBOARD_LOG("%s: setting native clipboard data failed.", __FUNCTION__
);
74 * Gets the transferable object
77 NS_IMETHODIMP
nsBaseClipboard::GetData(nsITransferable
* aTransferable
,
78 int32_t aWhichClipboard
) {
79 NS_ASSERTION(aTransferable
, "clipboard given a null transferable");
81 CLIPBOARD_LOG("%s", __FUNCTION__
);
83 bool selectClipPresent
;
84 SupportsSelectionClipboard(&selectClipPresent
);
86 SupportsFindClipboard(&findClipPresent
);
87 if (!selectClipPresent
&& !findClipPresent
&&
88 aWhichClipboard
!= kGlobalClipboard
)
89 return NS_ERROR_FAILURE
;
92 return GetNativeClipboardData(aTransferable
, aWhichClipboard
);
94 return NS_ERROR_FAILURE
;
97 RefPtr
<GenericPromise
> nsBaseClipboard::AsyncGetData(
98 nsITransferable
* aTransferable
, int32_t aWhichClipboard
) {
99 nsresult rv
= GetData(aTransferable
, aWhichClipboard
);
101 return GenericPromise::CreateAndReject(rv
, __func__
);
104 return GenericPromise::CreateAndResolve(true, __func__
);
107 NS_IMETHODIMP
nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard
) {
108 CLIPBOARD_LOG("%s: clipboard=%i", __FUNCTION__
, aWhichClipboard
);
110 bool selectClipPresent
;
111 SupportsSelectionClipboard(&selectClipPresent
);
112 bool findClipPresent
;
113 SupportsFindClipboard(&findClipPresent
);
114 if (!selectClipPresent
&& !findClipPresent
&&
115 aWhichClipboard
!= kGlobalClipboard
)
116 return NS_ERROR_FAILURE
;
118 if (mIgnoreEmptyNotification
) {
122 if (mClipboardOwner
) {
123 mClipboardOwner
->LosingOwnership(mTransferable
);
124 mClipboardOwner
= nullptr;
127 mTransferable
= nullptr;
132 nsBaseClipboard::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
133 int32_t aWhichClipboard
,
135 *outResult
= true; // say we always do.
139 RefPtr
<DataFlavorsPromise
> nsBaseClipboard::AsyncHasDataMatchingFlavors(
140 const nsTArray
<nsCString
>& aFlavorList
, int32_t aWhichClipboard
) {
141 nsTArray
<nsCString
> results
;
142 for (const auto& flavor
: aFlavorList
) {
143 bool hasMatchingFlavor
= false;
144 nsresult rv
= HasDataMatchingFlavors(AutoTArray
<nsCString
, 1>{flavor
},
145 aWhichClipboard
, &hasMatchingFlavor
);
146 if (NS_SUCCEEDED(rv
) && hasMatchingFlavor
) {
147 results
.AppendElement(flavor
);
151 return DataFlavorsPromise::CreateAndResolve(std::move(results
), __func__
);
155 nsBaseClipboard::SupportsSelectionClipboard(bool* _retval
) {
156 *_retval
= false; // we don't support the selection clipboard by default.
161 nsBaseClipboard::SupportsFindClipboard(bool* _retval
) {
162 *_retval
= false; // we don't support the find clipboard by default.