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 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsClipboardHelper.h" |
michael@0 | 8 | |
michael@0 | 9 | // basics |
michael@0 | 10 | #include "nsCOMPtr.h" |
michael@0 | 11 | #include "nsXPCOM.h" |
michael@0 | 12 | #include "nsISupportsPrimitives.h" |
michael@0 | 13 | #include "nsIServiceManager.h" |
michael@0 | 14 | |
michael@0 | 15 | // helpers |
michael@0 | 16 | #include "nsIClipboard.h" |
michael@0 | 17 | #include "nsIDocument.h" |
michael@0 | 18 | #include "nsIDOMDocument.h" |
michael@0 | 19 | #include "nsITransferable.h" |
michael@0 | 20 | #include "nsReadableUtils.h" |
michael@0 | 21 | |
michael@0 | 22 | NS_IMPL_ISUPPORTS(nsClipboardHelper, nsIClipboardHelper) |
michael@0 | 23 | |
michael@0 | 24 | /***************************************************************************** |
michael@0 | 25 | * nsClipboardHelper ctor / dtor |
michael@0 | 26 | *****************************************************************************/ |
michael@0 | 27 | |
michael@0 | 28 | nsClipboardHelper::nsClipboardHelper() |
michael@0 | 29 | { |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | nsClipboardHelper::~nsClipboardHelper() |
michael@0 | 33 | { |
michael@0 | 34 | // no members, nothing to destroy |
michael@0 | 35 | } |
michael@0 | 36 | |
michael@0 | 37 | /***************************************************************************** |
michael@0 | 38 | * nsIClipboardHelper methods |
michael@0 | 39 | *****************************************************************************/ |
michael@0 | 40 | |
michael@0 | 41 | NS_IMETHODIMP |
michael@0 | 42 | nsClipboardHelper::CopyStringToClipboard(const nsAString& aString, |
michael@0 | 43 | int32_t aClipboardID, |
michael@0 | 44 | nsIDOMDocument* aDocument) |
michael@0 | 45 | { |
michael@0 | 46 | nsresult rv; |
michael@0 | 47 | |
michael@0 | 48 | // get the clipboard |
michael@0 | 49 | nsCOMPtr<nsIClipboard> |
michael@0 | 50 | clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv)); |
michael@0 | 51 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 52 | NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE); |
michael@0 | 53 | |
michael@0 | 54 | bool clipboardSupported; |
michael@0 | 55 | // don't go any further if they're asking for the selection |
michael@0 | 56 | // clipboard on a platform which doesn't support it (i.e., unix) |
michael@0 | 57 | if (nsIClipboard::kSelectionClipboard == aClipboardID) { |
michael@0 | 58 | rv = clipboard->SupportsSelectionClipboard(&clipboardSupported); |
michael@0 | 59 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 60 | if (!clipboardSupported) |
michael@0 | 61 | return NS_ERROR_FAILURE; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | // don't go any further if they're asking for the find clipboard on a platform |
michael@0 | 65 | // which doesn't support it (i.e., non-osx) |
michael@0 | 66 | if (nsIClipboard::kFindClipboard == aClipboardID) { |
michael@0 | 67 | rv = clipboard->SupportsFindClipboard(&clipboardSupported); |
michael@0 | 68 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 69 | if (!clipboardSupported) |
michael@0 | 70 | return NS_ERROR_FAILURE; |
michael@0 | 71 | } |
michael@0 | 72 | |
michael@0 | 73 | // create a transferable for putting data on the clipboard |
michael@0 | 74 | nsCOMPtr<nsITransferable> |
michael@0 | 75 | trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv)); |
michael@0 | 76 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 77 | NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE); |
michael@0 | 78 | |
michael@0 | 79 | nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDocument); |
michael@0 | 80 | nsILoadContext* loadContext = doc ? doc->GetLoadContext() : nullptr; |
michael@0 | 81 | trans->Init(loadContext); |
michael@0 | 82 | |
michael@0 | 83 | // Add the text data flavor to the transferable |
michael@0 | 84 | rv = trans->AddDataFlavor(kUnicodeMime); |
michael@0 | 85 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 86 | |
michael@0 | 87 | // get wStrings to hold clip data |
michael@0 | 88 | nsCOMPtr<nsISupportsString> |
michael@0 | 89 | data(do_CreateInstance("@mozilla.org/supports-string;1", &rv)); |
michael@0 | 90 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 91 | NS_ENSURE_TRUE(data, NS_ERROR_FAILURE); |
michael@0 | 92 | |
michael@0 | 93 | // populate the string |
michael@0 | 94 | rv = data->SetData(aString); |
michael@0 | 95 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 96 | |
michael@0 | 97 | // qi the data object an |nsISupports| so that when the transferable holds |
michael@0 | 98 | // onto it, it will addref the correct interface. |
michael@0 | 99 | nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv)); |
michael@0 | 100 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 101 | NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE); |
michael@0 | 102 | |
michael@0 | 103 | // set the transfer data |
michael@0 | 104 | rv = trans->SetTransferData(kUnicodeMime, genericData, |
michael@0 | 105 | aString.Length() * 2); |
michael@0 | 106 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 107 | |
michael@0 | 108 | // put the transferable on the clipboard |
michael@0 | 109 | rv = clipboard->SetData(trans, nullptr, aClipboardID); |
michael@0 | 110 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 111 | |
michael@0 | 112 | return NS_OK; |
michael@0 | 113 | } |
michael@0 | 114 | |
michael@0 | 115 | NS_IMETHODIMP |
michael@0 | 116 | nsClipboardHelper::CopyString(const nsAString& aString, nsIDOMDocument* aDocument) |
michael@0 | 117 | { |
michael@0 | 118 | nsresult rv; |
michael@0 | 119 | |
michael@0 | 120 | // copy to the global clipboard. it's bad if this fails in any way. |
michael@0 | 121 | rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard, aDocument); |
michael@0 | 122 | NS_ENSURE_SUCCESS(rv, rv); |
michael@0 | 123 | |
michael@0 | 124 | // unix also needs us to copy to the selection clipboard. this will |
michael@0 | 125 | // fail in CopyStringToClipboard if we're not on a platform that |
michael@0 | 126 | // supports the selection clipboard. (this could have been #ifdef |
michael@0 | 127 | // XP_UNIX, but using the SupportsSelectionClipboard call is the |
michael@0 | 128 | // more correct thing to do. |
michael@0 | 129 | // |
michael@0 | 130 | // if this fails in any way other than "not being unix", we'll get |
michael@0 | 131 | // the assertion we need in CopyStringToClipboard, and we needn't |
michael@0 | 132 | // assert again here. |
michael@0 | 133 | CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard, aDocument); |
michael@0 | 134 | |
michael@0 | 135 | return NS_OK; |
michael@0 | 136 | } |