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::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
)
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__
));
46 bool selectClipPresent
;
47 SupportsSelectionClipboard(&selectClipPresent
);
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
;
66 rv
= SetNativeClipboardData(aWhichClipboard
);
69 MOZ_LOG(sBaseClipboardLog
, LogLevel::Debug
,
70 ("%s: setting native clipboard data failed.", __FUNCTION__
));
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
);
89 SupportsFindClipboard(&findClipPresent
);
90 if (!selectClipPresent
&& !findClipPresent
&&
91 aWhichClipboard
!= kGlobalClipboard
)
92 return NS_ERROR_FAILURE
;
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;
124 nsBaseClipboard::HasDataMatchingFlavors(const nsTArray
<nsCString
>& aFlavorList
,
125 int32_t aWhichClipboard
,
127 *outResult
= true; // say we always do.
132 nsBaseClipboard::SupportsSelectionClipboard(bool* _retval
) {
133 *_retval
= false; // we don't support the selection clipboard by default.
138 nsBaseClipboard::SupportsFindClipboard(bool* _retval
) {
139 *_retval
= false; // we don't support the find clipboard by default.