Bug 1783243 [wpt PR 35340] - [wptmanifest] Improve parser/serializer comment support...
[gecko.git] / widget / nsBaseClipboard.cpp
blob5de21b10bc76db8fd5a552e25a949c7de7b3ce93
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"
11 #include "nsCOMPtr.h"
12 #include "nsError.h"
13 #include "nsXPCOM.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)
28 /**
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__);
41 return NS_OK;
43 bool selectClipPresent;
44 SupportsSelectionClipboard(&selectClipPresent);
45 bool findClipPresent;
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;
61 if (mTransferable) {
62 mIgnoreEmptyNotification = true;
63 rv = SetNativeClipboardData(aWhichClipboard);
64 mIgnoreEmptyNotification = false;
66 if (NS_FAILED(rv)) {
67 CLIPBOARD_LOG("%s: setting native clipboard data failed.", __FUNCTION__);
70 return rv;
73 /**
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);
85 bool findClipPresent;
86 SupportsFindClipboard(&findClipPresent);
87 if (!selectClipPresent && !findClipPresent &&
88 aWhichClipboard != kGlobalClipboard)
89 return NS_ERROR_FAILURE;
91 if (aTransferable)
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);
100 if (NS_FAILED(rv)) {
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) {
119 return NS_OK;
122 if (mClipboardOwner) {
123 mClipboardOwner->LosingOwnership(mTransferable);
124 mClipboardOwner = nullptr;
127 mTransferable = nullptr;
128 return NS_OK;
131 NS_IMETHODIMP
132 nsBaseClipboard::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
133 int32_t aWhichClipboard,
134 bool* outResult) {
135 *outResult = true; // say we always do.
136 return NS_OK;
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__);
154 NS_IMETHODIMP
155 nsBaseClipboard::SupportsSelectionClipboard(bool* _retval) {
156 *_retval = false; // we don't support the selection clipboard by default.
157 return NS_OK;
160 NS_IMETHODIMP
161 nsBaseClipboard::SupportsFindClipboard(bool* _retval) {
162 *_retval = false; // we don't support the find clipboard by default.
163 return NS_OK;