Bug 1734067 [wpt PR 31108] - Update wpt metadata, a=testonly
[gecko.git] / widget / nsBaseClipboard.cpp
blobfb7974fb9d600c45b3eb131f3b93c0d3d52b88bf
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::LogLevel;
17 static mozilla::LazyLogModule sBaseClipboardLog("BaseClipboard");
19 nsBaseClipboard::nsBaseClipboard()
20 : mEmptyingForSetData(false), mIgnoreEmptyNotification(false) {}
22 nsBaseClipboard::~nsBaseClipboard() {
23 EmptyClipboard(kSelectionClipboard);
24 EmptyClipboard(kGlobalClipboard);
25 EmptyClipboard(kFindClipboard);
28 NS_IMPL_ISUPPORTS(nsBaseClipboard, nsIClipboard)
30 /**
31 * Sets the transferable object
34 NS_IMETHODIMP nsBaseClipboard::SetData(nsITransferable* aTransferable,
35 nsIClipboardOwner* anOwner,
36 int32_t aWhichClipboard) {
37 NS_ASSERTION(aTransferable, "clipboard given a null transferable");
39 MOZ_LOG(sBaseClipboardLog, LogLevel::Debug, ("%s", __FUNCTION__));
41 if (aTransferable == mTransferable && anOwner == mClipboardOwner) {
42 MOZ_LOG(sBaseClipboardLog, LogLevel::Debug,
43 ("%s: skipping update.", __FUNCTION__));
44 return NS_OK;
46 bool selectClipPresent;
47 SupportsSelectionClipboard(&selectClipPresent);
48 bool findClipPresent;
49 SupportsFindClipboard(&findClipPresent);
50 if (!selectClipPresent && !findClipPresent &&
51 aWhichClipboard != kGlobalClipboard)
52 return NS_ERROR_FAILURE;
54 mEmptyingForSetData = true;
55 if (NS_FAILED(EmptyClipboard(aWhichClipboard))) {
56 MOZ_LOG(sBaseClipboardLog, LogLevel::Debug,
57 ("%s: emptying clipboard failed.", __FUNCTION__));
59 mEmptyingForSetData = false;
61 mClipboardOwner = anOwner;
62 mTransferable = aTransferable;
64 nsresult rv = NS_ERROR_FAILURE;
65 if (mTransferable) {
66 rv = SetNativeClipboardData(aWhichClipboard);
68 if (NS_FAILED(rv)) {
69 MOZ_LOG(sBaseClipboardLog, LogLevel::Debug,
70 ("%s: setting native clipboard data failed.", __FUNCTION__));
73 return rv;
76 /**
77 * Gets the transferable object
80 NS_IMETHODIMP nsBaseClipboard::GetData(nsITransferable* aTransferable,
81 int32_t aWhichClipboard) {
82 NS_ASSERTION(aTransferable, "clipboard given a null transferable");
84 MOZ_LOG(sBaseClipboardLog, LogLevel::Debug, ("%s", __FUNCTION__));
86 bool selectClipPresent;
87 SupportsSelectionClipboard(&selectClipPresent);
88 bool findClipPresent;
89 SupportsFindClipboard(&findClipPresent);
90 if (!selectClipPresent && !findClipPresent &&
91 aWhichClipboard != kGlobalClipboard)
92 return NS_ERROR_FAILURE;
94 if (aTransferable)
95 return GetNativeClipboardData(aTransferable, aWhichClipboard);
97 return NS_ERROR_FAILURE;
100 NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard) {
101 MOZ_LOG(sBaseClipboardLog, LogLevel::Debug,
102 ("%s: clipboard=%i", __FUNCTION__, aWhichClipboard));
104 bool selectClipPresent;
105 SupportsSelectionClipboard(&selectClipPresent);
106 bool findClipPresent;
107 SupportsFindClipboard(&findClipPresent);
108 if (!selectClipPresent && !findClipPresent &&
109 aWhichClipboard != kGlobalClipboard)
110 return NS_ERROR_FAILURE;
112 if (mIgnoreEmptyNotification) return NS_OK;
114 if (mClipboardOwner) {
115 mClipboardOwner->LosingOwnership(mTransferable);
116 mClipboardOwner = nullptr;
119 mTransferable = nullptr;
120 return NS_OK;
123 NS_IMETHODIMP
124 nsBaseClipboard::HasDataMatchingFlavors(const nsTArray<nsCString>& aFlavorList,
125 int32_t aWhichClipboard,
126 bool* outResult) {
127 *outResult = true; // say we always do.
128 return NS_OK;
131 NS_IMETHODIMP
132 nsBaseClipboard::SupportsSelectionClipboard(bool* _retval) {
133 *_retval = false; // we don't support the selection clipboard by default.
134 return NS_OK;
137 NS_IMETHODIMP
138 nsBaseClipboard::SupportsFindClipboard(bool* _retval) {
139 *_retval = false; // we don't support the find clipboard by default.
140 return NS_OK;