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 "nsIClipboardOwner.h"
12 nsBaseClipboard::nsBaseClipboard()
13 : mEmptyingForSetData(false), mIgnoreEmptyNotification(false) {}
15 nsBaseClipboard::~nsBaseClipboard() {
16 EmptyClipboard(kSelectionClipboard
);
17 EmptyClipboard(kGlobalClipboard
);
18 EmptyClipboard(kFindClipboard
);
21 NS_IMPL_ISUPPORTS(nsBaseClipboard
, nsIClipboard
)
24 * Sets the transferable object
27 NS_IMETHODIMP
nsBaseClipboard::SetData(nsITransferable
* aTransferable
,
28 nsIClipboardOwner
* anOwner
,
29 int32_t aWhichClipboard
) {
30 NS_ASSERTION(aTransferable
, "clipboard given a null transferable");
32 if (aTransferable
== mTransferable
&& anOwner
== mClipboardOwner
)
34 bool selectClipPresent
;
35 SupportsSelectionClipboard(&selectClipPresent
);
37 SupportsFindClipboard(&findClipPresent
);
38 if (!selectClipPresent
&& !findClipPresent
&&
39 aWhichClipboard
!= kGlobalClipboard
)
40 return NS_ERROR_FAILURE
;
42 mEmptyingForSetData
= true;
43 EmptyClipboard(aWhichClipboard
);
44 mEmptyingForSetData
= false;
46 mClipboardOwner
= anOwner
;
47 mTransferable
= aTransferable
;
49 nsresult rv
= NS_ERROR_FAILURE
;
51 rv
= SetNativeClipboardData(aWhichClipboard
);
58 * Gets the transferable object
61 NS_IMETHODIMP
nsBaseClipboard::GetData(nsITransferable
* aTransferable
,
62 int32_t aWhichClipboard
) {
63 NS_ASSERTION(aTransferable
, "clipboard given a null transferable");
65 bool selectClipPresent
;
66 SupportsSelectionClipboard(&selectClipPresent
);
68 SupportsFindClipboard(&findClipPresent
);
69 if (!selectClipPresent
&& !findClipPresent
&&
70 aWhichClipboard
!= kGlobalClipboard
)
71 return NS_ERROR_FAILURE
;
74 return GetNativeClipboardData(aTransferable
, aWhichClipboard
);
76 return NS_ERROR_FAILURE
;
79 NS_IMETHODIMP
nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard
) {
80 bool selectClipPresent
;
81 SupportsSelectionClipboard(&selectClipPresent
);
83 SupportsFindClipboard(&findClipPresent
);
84 if (!selectClipPresent
&& !findClipPresent
&&
85 aWhichClipboard
!= kGlobalClipboard
)
86 return NS_ERROR_FAILURE
;
88 if (mIgnoreEmptyNotification
) return NS_OK
;
90 if (mClipboardOwner
) {
91 mClipboardOwner
->LosingOwnership(mTransferable
);
92 mClipboardOwner
= nullptr;
95 mTransferable
= nullptr;
100 nsBaseClipboard::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
101 int32_t aWhichClipboard
,
103 *outResult
= true; // say we always do.
108 nsBaseClipboard::SupportsSelectionClipboard(bool* _retval
) {
109 *_retval
= false; // we don't support the selection clipboard by default.
114 nsBaseClipboard::SupportsFindClipboard(bool* _retval
) {
115 *_retval
= false; // we don't support the find clipboard by default.