1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/widget/xpwidgets/nsClipboardHelper.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,136 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "nsClipboardHelper.h" 1.11 + 1.12 +// basics 1.13 +#include "nsCOMPtr.h" 1.14 +#include "nsXPCOM.h" 1.15 +#include "nsISupportsPrimitives.h" 1.16 +#include "nsIServiceManager.h" 1.17 + 1.18 +// helpers 1.19 +#include "nsIClipboard.h" 1.20 +#include "nsIDocument.h" 1.21 +#include "nsIDOMDocument.h" 1.22 +#include "nsITransferable.h" 1.23 +#include "nsReadableUtils.h" 1.24 + 1.25 +NS_IMPL_ISUPPORTS(nsClipboardHelper, nsIClipboardHelper) 1.26 + 1.27 +/***************************************************************************** 1.28 + * nsClipboardHelper ctor / dtor 1.29 + *****************************************************************************/ 1.30 + 1.31 +nsClipboardHelper::nsClipboardHelper() 1.32 +{ 1.33 +} 1.34 + 1.35 +nsClipboardHelper::~nsClipboardHelper() 1.36 +{ 1.37 + // no members, nothing to destroy 1.38 +} 1.39 + 1.40 +/***************************************************************************** 1.41 + * nsIClipboardHelper methods 1.42 + *****************************************************************************/ 1.43 + 1.44 +NS_IMETHODIMP 1.45 +nsClipboardHelper::CopyStringToClipboard(const nsAString& aString, 1.46 + int32_t aClipboardID, 1.47 + nsIDOMDocument* aDocument) 1.48 +{ 1.49 + nsresult rv; 1.50 + 1.51 + // get the clipboard 1.52 + nsCOMPtr<nsIClipboard> 1.53 + clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv)); 1.54 + NS_ENSURE_SUCCESS(rv, rv); 1.55 + NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE); 1.56 + 1.57 + bool clipboardSupported; 1.58 + // don't go any further if they're asking for the selection 1.59 + // clipboard on a platform which doesn't support it (i.e., unix) 1.60 + if (nsIClipboard::kSelectionClipboard == aClipboardID) { 1.61 + rv = clipboard->SupportsSelectionClipboard(&clipboardSupported); 1.62 + NS_ENSURE_SUCCESS(rv, rv); 1.63 + if (!clipboardSupported) 1.64 + return NS_ERROR_FAILURE; 1.65 + } 1.66 + 1.67 + // don't go any further if they're asking for the find clipboard on a platform 1.68 + // which doesn't support it (i.e., non-osx) 1.69 + if (nsIClipboard::kFindClipboard == aClipboardID) { 1.70 + rv = clipboard->SupportsFindClipboard(&clipboardSupported); 1.71 + NS_ENSURE_SUCCESS(rv, rv); 1.72 + if (!clipboardSupported) 1.73 + return NS_ERROR_FAILURE; 1.74 + } 1.75 + 1.76 + // create a transferable for putting data on the clipboard 1.77 + nsCOMPtr<nsITransferable> 1.78 + trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv)); 1.79 + NS_ENSURE_SUCCESS(rv, rv); 1.80 + NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE); 1.81 + 1.82 + nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDocument); 1.83 + nsILoadContext* loadContext = doc ? doc->GetLoadContext() : nullptr; 1.84 + trans->Init(loadContext); 1.85 + 1.86 + // Add the text data flavor to the transferable 1.87 + rv = trans->AddDataFlavor(kUnicodeMime); 1.88 + NS_ENSURE_SUCCESS(rv, rv); 1.89 + 1.90 + // get wStrings to hold clip data 1.91 + nsCOMPtr<nsISupportsString> 1.92 + data(do_CreateInstance("@mozilla.org/supports-string;1", &rv)); 1.93 + NS_ENSURE_SUCCESS(rv, rv); 1.94 + NS_ENSURE_TRUE(data, NS_ERROR_FAILURE); 1.95 + 1.96 + // populate the string 1.97 + rv = data->SetData(aString); 1.98 + NS_ENSURE_SUCCESS(rv, rv); 1.99 + 1.100 + // qi the data object an |nsISupports| so that when the transferable holds 1.101 + // onto it, it will addref the correct interface. 1.102 + nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv)); 1.103 + NS_ENSURE_SUCCESS(rv, rv); 1.104 + NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE); 1.105 + 1.106 + // set the transfer data 1.107 + rv = trans->SetTransferData(kUnicodeMime, genericData, 1.108 + aString.Length() * 2); 1.109 + NS_ENSURE_SUCCESS(rv, rv); 1.110 + 1.111 + // put the transferable on the clipboard 1.112 + rv = clipboard->SetData(trans, nullptr, aClipboardID); 1.113 + NS_ENSURE_SUCCESS(rv, rv); 1.114 + 1.115 + return NS_OK; 1.116 +} 1.117 + 1.118 +NS_IMETHODIMP 1.119 +nsClipboardHelper::CopyString(const nsAString& aString, nsIDOMDocument* aDocument) 1.120 +{ 1.121 + nsresult rv; 1.122 + 1.123 + // copy to the global clipboard. it's bad if this fails in any way. 1.124 + rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard, aDocument); 1.125 + NS_ENSURE_SUCCESS(rv, rv); 1.126 + 1.127 + // unix also needs us to copy to the selection clipboard. this will 1.128 + // fail in CopyStringToClipboard if we're not on a platform that 1.129 + // supports the selection clipboard. (this could have been #ifdef 1.130 + // XP_UNIX, but using the SupportsSelectionClipboard call is the 1.131 + // more correct thing to do. 1.132 + // 1.133 + // if this fails in any way other than "not being unix", we'll get 1.134 + // the assertion we need in CopyStringToClipboard, and we needn't 1.135 + // assert again here. 1.136 + CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard, aDocument); 1.137 + 1.138 + return NS_OK; 1.139 +}