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"
11 #include "nsISupportsPrimitives.h"
13 nsBaseClipboard::nsBaseClipboard()
15 mClipboardOwner
= nullptr;
16 mTransferable
= nullptr;
17 mIgnoreEmptyNotification
= false;
18 mEmptyingForSetData
= false;
21 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
, nsIClipboardOwner
* anOwner
,
35 int32_t aWhichClipboard
)
37 NS_ASSERTION ( aTransferable
, "clipboard given a null transferable" );
39 if (aTransferable
== mTransferable
&& anOwner
== mClipboardOwner
)
41 bool selectClipPresent
;
42 SupportsSelectionClipboard(&selectClipPresent
);
44 SupportsFindClipboard(&findClipPresent
);
45 if ( !selectClipPresent
&& !findClipPresent
&& aWhichClipboard
!= kGlobalClipboard
)
46 return NS_ERROR_FAILURE
;
48 mEmptyingForSetData
= true;
49 EmptyClipboard(aWhichClipboard
);
50 mEmptyingForSetData
= false;
52 mClipboardOwner
= anOwner
;
54 NS_ADDREF(mClipboardOwner
);
56 mTransferable
= aTransferable
;
58 nsresult rv
= NS_ERROR_FAILURE
;
60 if ( mTransferable
) {
61 NS_ADDREF(mTransferable
);
62 rv
= SetNativeClipboardData(aWhichClipboard
);
69 * Gets the transferable object
72 NS_IMETHODIMP
nsBaseClipboard::GetData(nsITransferable
* aTransferable
, int32_t aWhichClipboard
)
74 NS_ASSERTION ( aTransferable
, "clipboard given a null transferable" );
76 bool selectClipPresent
;
77 SupportsSelectionClipboard(&selectClipPresent
);
79 SupportsFindClipboard(&findClipPresent
);
80 if ( !selectClipPresent
&& !findClipPresent
&& aWhichClipboard
!= kGlobalClipboard
)
81 return NS_ERROR_FAILURE
;
84 return GetNativeClipboardData(aTransferable
, aWhichClipboard
);
86 return NS_ERROR_FAILURE
;
89 NS_IMETHODIMP
nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard
)
91 bool selectClipPresent
;
92 SupportsSelectionClipboard(&selectClipPresent
);
94 SupportsFindClipboard(&findClipPresent
);
95 if ( !selectClipPresent
&& !findClipPresent
&& aWhichClipboard
!= kGlobalClipboard
)
96 return NS_ERROR_FAILURE
;
98 if (mIgnoreEmptyNotification
)
101 if ( mClipboardOwner
) {
102 mClipboardOwner
->LosingOwnership(mTransferable
);
103 NS_RELEASE(mClipboardOwner
);
106 NS_IF_RELEASE(mTransferable
);
112 nsBaseClipboard::HasDataMatchingFlavors(const char** aFlavorList
,
114 int32_t aWhichClipboard
,
117 *outResult
= true; // say we always do.
122 nsBaseClipboard::SupportsSelectionClipboard(bool* _retval
)
124 *_retval
= false; // we don't support the selection clipboard by default.
129 nsBaseClipboard::SupportsFindClipboard(bool* _retval
)
131 *_retval
= false; // we don't support the find clipboard by default.