Fri, 16 Jan 2015 04:50:19 +0100
Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "nsBaseClipboard.h" |
michael@0 | 7 | |
michael@0 | 8 | #include "nsIClipboardOwner.h" |
michael@0 | 9 | #include "nsCOMPtr.h" |
michael@0 | 10 | #include "nsXPCOM.h" |
michael@0 | 11 | #include "nsISupportsPrimitives.h" |
michael@0 | 12 | |
michael@0 | 13 | nsBaseClipboard::nsBaseClipboard() |
michael@0 | 14 | { |
michael@0 | 15 | mClipboardOwner = nullptr; |
michael@0 | 16 | mTransferable = nullptr; |
michael@0 | 17 | mIgnoreEmptyNotification = false; |
michael@0 | 18 | mEmptyingForSetData = false; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | nsBaseClipboard::~nsBaseClipboard() |
michael@0 | 22 | { |
michael@0 | 23 | EmptyClipboard(kSelectionClipboard); |
michael@0 | 24 | EmptyClipboard(kGlobalClipboard); |
michael@0 | 25 | EmptyClipboard(kFindClipboard); |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | NS_IMPL_ISUPPORTS(nsBaseClipboard, nsIClipboard) |
michael@0 | 29 | |
michael@0 | 30 | /** |
michael@0 | 31 | * Sets the transferable object |
michael@0 | 32 | * |
michael@0 | 33 | */ |
michael@0 | 34 | NS_IMETHODIMP nsBaseClipboard::SetData(nsITransferable * aTransferable, nsIClipboardOwner * anOwner, |
michael@0 | 35 | int32_t aWhichClipboard) |
michael@0 | 36 | { |
michael@0 | 37 | NS_ASSERTION ( aTransferable, "clipboard given a null transferable" ); |
michael@0 | 38 | |
michael@0 | 39 | if (aTransferable == mTransferable && anOwner == mClipboardOwner) |
michael@0 | 40 | return NS_OK; |
michael@0 | 41 | bool selectClipPresent; |
michael@0 | 42 | SupportsSelectionClipboard(&selectClipPresent); |
michael@0 | 43 | bool findClipPresent; |
michael@0 | 44 | SupportsFindClipboard(&findClipPresent); |
michael@0 | 45 | if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard ) |
michael@0 | 46 | return NS_ERROR_FAILURE; |
michael@0 | 47 | |
michael@0 | 48 | mEmptyingForSetData = true; |
michael@0 | 49 | EmptyClipboard(aWhichClipboard); |
michael@0 | 50 | mEmptyingForSetData = false; |
michael@0 | 51 | |
michael@0 | 52 | mClipboardOwner = anOwner; |
michael@0 | 53 | if ( anOwner ) |
michael@0 | 54 | NS_ADDREF(mClipboardOwner); |
michael@0 | 55 | |
michael@0 | 56 | mTransferable = aTransferable; |
michael@0 | 57 | |
michael@0 | 58 | nsresult rv = NS_ERROR_FAILURE; |
michael@0 | 59 | |
michael@0 | 60 | if ( mTransferable ) { |
michael@0 | 61 | NS_ADDREF(mTransferable); |
michael@0 | 62 | if (!mPrivacyHandler) { |
michael@0 | 63 | rv = NS_NewClipboardPrivacyHandler(getter_AddRefs(mPrivacyHandler)); |
michael@0 | 64 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 65 | } |
michael@0 | 66 | rv = mPrivacyHandler->PrepareDataForClipboard(mTransferable); |
michael@0 | 67 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 68 | rv = SetNativeClipboardData(aWhichClipboard); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | return rv; |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | /** |
michael@0 | 75 | * Gets the transferable object |
michael@0 | 76 | * |
michael@0 | 77 | */ |
michael@0 | 78 | NS_IMETHODIMP nsBaseClipboard::GetData(nsITransferable * aTransferable, int32_t aWhichClipboard) |
michael@0 | 79 | { |
michael@0 | 80 | NS_ASSERTION ( aTransferable, "clipboard given a null transferable" ); |
michael@0 | 81 | |
michael@0 | 82 | bool selectClipPresent; |
michael@0 | 83 | SupportsSelectionClipboard(&selectClipPresent); |
michael@0 | 84 | bool findClipPresent; |
michael@0 | 85 | SupportsFindClipboard(&findClipPresent); |
michael@0 | 86 | if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard ) |
michael@0 | 87 | return NS_ERROR_FAILURE; |
michael@0 | 88 | |
michael@0 | 89 | if ( aTransferable ) |
michael@0 | 90 | return GetNativeClipboardData(aTransferable, aWhichClipboard); |
michael@0 | 91 | |
michael@0 | 92 | return NS_ERROR_FAILURE; |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard) |
michael@0 | 96 | { |
michael@0 | 97 | bool selectClipPresent; |
michael@0 | 98 | SupportsSelectionClipboard(&selectClipPresent); |
michael@0 | 99 | bool findClipPresent; |
michael@0 | 100 | SupportsFindClipboard(&findClipPresent); |
michael@0 | 101 | if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard ) |
michael@0 | 102 | return NS_ERROR_FAILURE; |
michael@0 | 103 | |
michael@0 | 104 | if (mIgnoreEmptyNotification) |
michael@0 | 105 | return NS_OK; |
michael@0 | 106 | |
michael@0 | 107 | if ( mClipboardOwner ) { |
michael@0 | 108 | mClipboardOwner->LosingOwnership(mTransferable); |
michael@0 | 109 | NS_RELEASE(mClipboardOwner); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | NS_IF_RELEASE(mTransferable); |
michael@0 | 113 | |
michael@0 | 114 | return NS_OK; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | NS_IMETHODIMP |
michael@0 | 118 | nsBaseClipboard::HasDataMatchingFlavors(const char** aFlavorList, |
michael@0 | 119 | uint32_t aLength, |
michael@0 | 120 | int32_t aWhichClipboard, |
michael@0 | 121 | bool* outResult) |
michael@0 | 122 | { |
michael@0 | 123 | *outResult = true; // say we always do. |
michael@0 | 124 | return NS_OK; |
michael@0 | 125 | } |
michael@0 | 126 | |
michael@0 | 127 | NS_IMETHODIMP |
michael@0 | 128 | nsBaseClipboard::SupportsSelectionClipboard(bool* _retval) |
michael@0 | 129 | { |
michael@0 | 130 | *_retval = false; // we don't support the selection clipboard by default. |
michael@0 | 131 | return NS_OK; |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | NS_IMETHODIMP |
michael@0 | 135 | nsBaseClipboard::SupportsFindClipboard(bool* _retval) |
michael@0 | 136 | { |
michael@0 | 137 | *_retval = false; // we don't support the find clipboard by default. |
michael@0 | 138 | return NS_OK; |
michael@0 | 139 | } |