widget/xpwidgets/nsBaseClipboard.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/widget/xpwidgets/nsBaseClipboard.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,139 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsBaseClipboard.h"
    1.10 +
    1.11 +#include "nsIClipboardOwner.h"
    1.12 +#include "nsCOMPtr.h"
    1.13 +#include "nsXPCOM.h"
    1.14 +#include "nsISupportsPrimitives.h"
    1.15 +
    1.16 +nsBaseClipboard::nsBaseClipboard()
    1.17 +{
    1.18 +  mClipboardOwner          = nullptr;
    1.19 +  mTransferable            = nullptr;
    1.20 +  mIgnoreEmptyNotification = false;
    1.21 +  mEmptyingForSetData      = false;
    1.22 +}
    1.23 +
    1.24 +nsBaseClipboard::~nsBaseClipboard()
    1.25 +{
    1.26 +  EmptyClipboard(kSelectionClipboard);
    1.27 +  EmptyClipboard(kGlobalClipboard);
    1.28 +  EmptyClipboard(kFindClipboard);
    1.29 +}
    1.30 +
    1.31 +NS_IMPL_ISUPPORTS(nsBaseClipboard, nsIClipboard)
    1.32 +
    1.33 +/**
    1.34 +  * Sets the transferable object
    1.35 +  *
    1.36 +  */
    1.37 +NS_IMETHODIMP nsBaseClipboard::SetData(nsITransferable * aTransferable, nsIClipboardOwner * anOwner,
    1.38 +                                        int32_t aWhichClipboard)
    1.39 +{
    1.40 +  NS_ASSERTION ( aTransferable, "clipboard given a null transferable" );
    1.41 +
    1.42 +  if (aTransferable == mTransferable && anOwner == mClipboardOwner)
    1.43 +    return NS_OK;
    1.44 +  bool selectClipPresent;
    1.45 +  SupportsSelectionClipboard(&selectClipPresent);
    1.46 +  bool findClipPresent;
    1.47 +  SupportsFindClipboard(&findClipPresent);
    1.48 +  if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard )
    1.49 +    return NS_ERROR_FAILURE;
    1.50 +
    1.51 +  mEmptyingForSetData = true;
    1.52 +  EmptyClipboard(aWhichClipboard);
    1.53 +  mEmptyingForSetData = false;
    1.54 +
    1.55 +  mClipboardOwner = anOwner;
    1.56 +  if ( anOwner )
    1.57 +    NS_ADDREF(mClipboardOwner);
    1.58 +
    1.59 +  mTransferable = aTransferable;
    1.60 +  
    1.61 +  nsresult rv = NS_ERROR_FAILURE;
    1.62 +
    1.63 +  if ( mTransferable ) {
    1.64 +    NS_ADDREF(mTransferable);
    1.65 +    if (!mPrivacyHandler) {
    1.66 +      rv = NS_NewClipboardPrivacyHandler(getter_AddRefs(mPrivacyHandler));
    1.67 +      NS_ENSURE_SUCCESS(rv, rv);
    1.68 +    }
    1.69 +    rv = mPrivacyHandler->PrepareDataForClipboard(mTransferable);
    1.70 +    NS_ENSURE_SUCCESS(rv, rv);
    1.71 +    rv = SetNativeClipboardData(aWhichClipboard);
    1.72 +  }
    1.73 +  
    1.74 +  return rv;
    1.75 +}
    1.76 +
    1.77 +/**
    1.78 +  * Gets the transferable object
    1.79 +  *
    1.80 +  */
    1.81 +NS_IMETHODIMP nsBaseClipboard::GetData(nsITransferable * aTransferable, int32_t aWhichClipboard)
    1.82 +{
    1.83 +  NS_ASSERTION ( aTransferable, "clipboard given a null transferable" );
    1.84 +
    1.85 +  bool selectClipPresent;
    1.86 +  SupportsSelectionClipboard(&selectClipPresent);
    1.87 +  bool findClipPresent;
    1.88 +  SupportsFindClipboard(&findClipPresent);
    1.89 +  if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard )
    1.90 +    return NS_ERROR_FAILURE;
    1.91 +
    1.92 +  if ( aTransferable )
    1.93 +    return GetNativeClipboardData(aTransferable, aWhichClipboard);
    1.94 +
    1.95 +  return NS_ERROR_FAILURE;
    1.96 +}
    1.97 +
    1.98 +NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard)
    1.99 +{
   1.100 +  bool selectClipPresent;
   1.101 +  SupportsSelectionClipboard(&selectClipPresent);
   1.102 +  bool findClipPresent;
   1.103 +  SupportsFindClipboard(&findClipPresent);
   1.104 +  if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard )
   1.105 +    return NS_ERROR_FAILURE;
   1.106 +
   1.107 +  if (mIgnoreEmptyNotification)
   1.108 +    return NS_OK;
   1.109 +
   1.110 +  if ( mClipboardOwner ) {
   1.111 +    mClipboardOwner->LosingOwnership(mTransferable);
   1.112 +    NS_RELEASE(mClipboardOwner);
   1.113 +  }
   1.114 +
   1.115 +  NS_IF_RELEASE(mTransferable);
   1.116 +
   1.117 +  return NS_OK;
   1.118 +}
   1.119 +
   1.120 +NS_IMETHODIMP
   1.121 +nsBaseClipboard::HasDataMatchingFlavors(const char** aFlavorList,
   1.122 +                                        uint32_t aLength,
   1.123 +                                        int32_t aWhichClipboard,
   1.124 +                                        bool* outResult) 
   1.125 +{
   1.126 +  *outResult = true;  // say we always do.
   1.127 +  return NS_OK;
   1.128 +}
   1.129 +
   1.130 +NS_IMETHODIMP
   1.131 +nsBaseClipboard::SupportsSelectionClipboard(bool* _retval)
   1.132 +{
   1.133 +  *_retval = false;   // we don't support the selection clipboard by default.
   1.134 +  return NS_OK;
   1.135 +}
   1.136 +
   1.137 +NS_IMETHODIMP
   1.138 +nsBaseClipboard::SupportsFindClipboard(bool* _retval)
   1.139 +{
   1.140 +  *_retval = false;   // we don't support the find clipboard by default.
   1.141 +  return NS_OK;
   1.142 +}

mercurial