michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsBaseClipboard.h" michael@0: michael@0: #include "nsIClipboardOwner.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsXPCOM.h" michael@0: #include "nsISupportsPrimitives.h" michael@0: michael@0: nsBaseClipboard::nsBaseClipboard() michael@0: { michael@0: mClipboardOwner = nullptr; michael@0: mTransferable = nullptr; michael@0: mIgnoreEmptyNotification = false; michael@0: mEmptyingForSetData = false; michael@0: } michael@0: michael@0: nsBaseClipboard::~nsBaseClipboard() michael@0: { michael@0: EmptyClipboard(kSelectionClipboard); michael@0: EmptyClipboard(kGlobalClipboard); michael@0: EmptyClipboard(kFindClipboard); michael@0: } michael@0: michael@0: NS_IMPL_ISUPPORTS(nsBaseClipboard, nsIClipboard) michael@0: michael@0: /** michael@0: * Sets the transferable object michael@0: * michael@0: */ michael@0: NS_IMETHODIMP nsBaseClipboard::SetData(nsITransferable * aTransferable, nsIClipboardOwner * anOwner, michael@0: int32_t aWhichClipboard) michael@0: { michael@0: NS_ASSERTION ( aTransferable, "clipboard given a null transferable" ); michael@0: michael@0: if (aTransferable == mTransferable && anOwner == mClipboardOwner) michael@0: return NS_OK; michael@0: bool selectClipPresent; michael@0: SupportsSelectionClipboard(&selectClipPresent); michael@0: bool findClipPresent; michael@0: SupportsFindClipboard(&findClipPresent); michael@0: if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard ) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: mEmptyingForSetData = true; michael@0: EmptyClipboard(aWhichClipboard); michael@0: mEmptyingForSetData = false; michael@0: michael@0: mClipboardOwner = anOwner; michael@0: if ( anOwner ) michael@0: NS_ADDREF(mClipboardOwner); michael@0: michael@0: mTransferable = aTransferable; michael@0: michael@0: nsresult rv = NS_ERROR_FAILURE; michael@0: michael@0: if ( mTransferable ) { michael@0: NS_ADDREF(mTransferable); michael@0: if (!mPrivacyHandler) { michael@0: rv = NS_NewClipboardPrivacyHandler(getter_AddRefs(mPrivacyHandler)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: } michael@0: rv = mPrivacyHandler->PrepareDataForClipboard(mTransferable); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: rv = SetNativeClipboardData(aWhichClipboard); michael@0: } michael@0: michael@0: return rv; michael@0: } michael@0: michael@0: /** michael@0: * Gets the transferable object michael@0: * michael@0: */ michael@0: NS_IMETHODIMP nsBaseClipboard::GetData(nsITransferable * aTransferable, int32_t aWhichClipboard) michael@0: { michael@0: NS_ASSERTION ( aTransferable, "clipboard given a null transferable" ); michael@0: michael@0: bool selectClipPresent; michael@0: SupportsSelectionClipboard(&selectClipPresent); michael@0: bool findClipPresent; michael@0: SupportsFindClipboard(&findClipPresent); michael@0: if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard ) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if ( aTransferable ) michael@0: return GetNativeClipboardData(aTransferable, aWhichClipboard); michael@0: michael@0: return NS_ERROR_FAILURE; michael@0: } michael@0: michael@0: NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard) michael@0: { michael@0: bool selectClipPresent; michael@0: SupportsSelectionClipboard(&selectClipPresent); michael@0: bool findClipPresent; michael@0: SupportsFindClipboard(&findClipPresent); michael@0: if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard ) michael@0: return NS_ERROR_FAILURE; michael@0: michael@0: if (mIgnoreEmptyNotification) michael@0: return NS_OK; michael@0: michael@0: if ( mClipboardOwner ) { michael@0: mClipboardOwner->LosingOwnership(mTransferable); michael@0: NS_RELEASE(mClipboardOwner); michael@0: } michael@0: michael@0: NS_IF_RELEASE(mTransferable); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseClipboard::HasDataMatchingFlavors(const char** aFlavorList, michael@0: uint32_t aLength, michael@0: int32_t aWhichClipboard, michael@0: bool* outResult) michael@0: { michael@0: *outResult = true; // say we always do. michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseClipboard::SupportsSelectionClipboard(bool* _retval) michael@0: { michael@0: *_retval = false; // we don't support the selection clipboard by default. michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsBaseClipboard::SupportsFindClipboard(bool* _retval) michael@0: { michael@0: *_retval = false; // we don't support the find clipboard by default. michael@0: return NS_OK; michael@0: }