gecko-dev/widget/xpwidgets/nsClipboardHelper.cpp
Ehsan Akhgari e368dc9c85 Bug 579517 - Part 1: Automated conversion of NSPR numeric types to stdint types in Gecko; r=bsmedberg
This patch was generated by a script.  Here's the source of the script for
future reference:

function convert() {
echo "Converting $1 to $2..."
find . ! -wholename "*nsprpub*" \
       ! -wholename "*security/nss*" \
       ! -wholename "*/.hg*" \
       ! -wholename "obj-ff-dbg*" \
       ! -name nsXPCOMCID.h \
       ! -name prtypes.h \
         -type f \
      \( -iname "*.cpp" \
         -o -iname "*.h" \
         -o -iname "*.c" \
         -o -iname "*.cc" \
         -o -iname "*.idl" \
         -o -iname "*.ipdl" \
         -o -iname "*.ipdlh" \
         -o -iname "*.mm" \) | \
    xargs -n 1 sed -i -e "s/\b$1\b/$2/g"
}

convert PRInt8 int8_t
convert PRUint8 uint8_t
convert PRInt16 int16_t
convert PRUint16 uint16_t
convert PRInt32 int32_t
convert PRUint32 uint32_t
convert PRInt64 int64_t
convert PRUint64 uint64_t

convert PRIntn int
convert PRUintn unsigned

convert PRSize size_t

convert PROffset32 int32_t
convert PROffset64 int64_t

convert PRPtrdiff ptrdiff_t

convert PRFloat64 double
2012-08-22 11:56:38 -04:00

127 lines
4.1 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "nsClipboardHelper.h"
// basics
#include "nsCOMPtr.h"
#include "nsXPCOM.h"
#include "nsISupportsPrimitives.h"
#include "nsIServiceManager.h"
// helpers
#include "nsIClipboard.h"
#include "nsITransferable.h"
#include "nsReadableUtils.h"
#include "nsIDocument.h"
NS_IMPL_ISUPPORTS1(nsClipboardHelper, nsIClipboardHelper)
/*****************************************************************************
* nsClipboardHelper ctor / dtor
*****************************************************************************/
nsClipboardHelper::nsClipboardHelper()
{
}
nsClipboardHelper::~nsClipboardHelper()
{
// no members, nothing to destroy
}
/*****************************************************************************
* nsIClipboardHelper methods
*****************************************************************************/
NS_IMETHODIMP
nsClipboardHelper::CopyStringToClipboard(const nsAString& aString,
int32_t aClipboardID,
nsIDOMDocument* aDocument)
{
nsresult rv;
// get the clipboard
nsCOMPtr<nsIClipboard>
clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(clipboard, NS_ERROR_FAILURE);
// don't go any further if they're asking for the selection
// clipboard on a platform which doesn't support it (i.e., unix)
if (nsIClipboard::kSelectionClipboard == aClipboardID) {
bool clipboardSupported;
rv = clipboard->SupportsSelectionClipboard(&clipboardSupported);
NS_ENSURE_SUCCESS(rv, rv);
if (!clipboardSupported)
return NS_ERROR_FAILURE;
}
// create a transferable for putting data on the clipboard
nsCOMPtr<nsITransferable>
trans(do_CreateInstance("@mozilla.org/widget/transferable;1", &rv));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aDocument);
nsILoadContext* loadContext = doc ? doc->GetLoadContext() : nullptr;
trans->Init(loadContext);
// Add the text data flavor to the transferable
rv = trans->AddDataFlavor(kUnicodeMime);
NS_ENSURE_SUCCESS(rv, rv);
// get wStrings to hold clip data
nsCOMPtr<nsISupportsString>
data(do_CreateInstance("@mozilla.org/supports-string;1", &rv));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(data, NS_ERROR_FAILURE);
// populate the string
rv = data->SetData(aString);
NS_ENSURE_SUCCESS(rv, rv);
// qi the data object an |nsISupports| so that when the transferable holds
// onto it, it will addref the correct interface.
nsCOMPtr<nsISupports> genericData(do_QueryInterface(data, &rv));
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(genericData, NS_ERROR_FAILURE);
// set the transfer data
rv = trans->SetTransferData(kUnicodeMime, genericData,
aString.Length() * 2);
NS_ENSURE_SUCCESS(rv, rv);
// put the transferable on the clipboard
rv = clipboard->SetData(trans, nullptr, aClipboardID);
NS_ENSURE_SUCCESS(rv, rv);
return NS_OK;
}
NS_IMETHODIMP
nsClipboardHelper::CopyString(const nsAString& aString, nsIDOMDocument* aDocument)
{
nsresult rv;
// copy to the global clipboard. it's bad if this fails in any way.
rv = CopyStringToClipboard(aString, nsIClipboard::kGlobalClipboard, aDocument);
NS_ENSURE_SUCCESS(rv, rv);
// unix also needs us to copy to the selection clipboard. this will
// fail in CopyStringToClipboard if we're not on a platform that
// supports the selection clipboard. (this could have been #ifdef
// XP_UNIX, but using the SupportsSelectionClipboard call is the
// more correct thing to do.
//
// if this fails in any way other than "not being unix", we'll get
// the assertion we need in CopyStringToClipboard, and we needn't
// assert again here.
CopyStringToClipboard(aString, nsIClipboard::kSelectionClipboard, aDocument);
return NS_OK;
}