nsBaseClipboard.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #include "nsBaseClipboard.h"
  6. #include "nsIClipboardOwner.h"
  7. #include "nsCOMPtr.h"
  8. #include "nsXPCOM.h"
  9. #include "nsISupportsPrimitives.h"
  10. nsBaseClipboard::nsBaseClipboard()
  11. : mEmptyingForSetData(false)
  12. , mIgnoreEmptyNotification(false)
  13. {
  14. }
  15. nsBaseClipboard::~nsBaseClipboard()
  16. {
  17. EmptyClipboard(kSelectionClipboard);
  18. EmptyClipboard(kGlobalClipboard);
  19. EmptyClipboard(kFindClipboard);
  20. }
  21. NS_IMPL_ISUPPORTS(nsBaseClipboard, nsIClipboard)
  22. /**
  23. * Sets the transferable object
  24. *
  25. */
  26. NS_IMETHODIMP nsBaseClipboard::SetData(nsITransferable * aTransferable, nsIClipboardOwner * anOwner,
  27. int32_t aWhichClipboard)
  28. {
  29. NS_ASSERTION ( aTransferable, "clipboard given a null transferable" );
  30. if (aTransferable == mTransferable && anOwner == mClipboardOwner)
  31. return NS_OK;
  32. bool selectClipPresent;
  33. SupportsSelectionClipboard(&selectClipPresent);
  34. bool findClipPresent;
  35. SupportsFindClipboard(&findClipPresent);
  36. if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard )
  37. return NS_ERROR_FAILURE;
  38. mEmptyingForSetData = true;
  39. EmptyClipboard(aWhichClipboard);
  40. mEmptyingForSetData = false;
  41. mClipboardOwner = anOwner;
  42. mTransferable = aTransferable;
  43. nsresult rv = NS_ERROR_FAILURE;
  44. if (mTransferable) {
  45. rv = SetNativeClipboardData(aWhichClipboard);
  46. }
  47. return rv;
  48. }
  49. /**
  50. * Gets the transferable object
  51. *
  52. */
  53. NS_IMETHODIMP nsBaseClipboard::GetData(nsITransferable * aTransferable, int32_t aWhichClipboard)
  54. {
  55. NS_ASSERTION ( aTransferable, "clipboard given a null transferable" );
  56. bool selectClipPresent;
  57. SupportsSelectionClipboard(&selectClipPresent);
  58. bool findClipPresent;
  59. SupportsFindClipboard(&findClipPresent);
  60. if ( !selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard )
  61. return NS_ERROR_FAILURE;
  62. if ( aTransferable )
  63. return GetNativeClipboardData(aTransferable, aWhichClipboard);
  64. return NS_ERROR_FAILURE;
  65. }
  66. NS_IMETHODIMP nsBaseClipboard::EmptyClipboard(int32_t aWhichClipboard)
  67. {
  68. bool selectClipPresent;
  69. SupportsSelectionClipboard(&selectClipPresent);
  70. bool findClipPresent;
  71. SupportsFindClipboard(&findClipPresent);
  72. if (!selectClipPresent && !findClipPresent && aWhichClipboard != kGlobalClipboard)
  73. return NS_ERROR_FAILURE;
  74. if (mIgnoreEmptyNotification)
  75. return NS_OK;
  76. if (mClipboardOwner) {
  77. mClipboardOwner->LosingOwnership(mTransferable);
  78. mClipboardOwner = nullptr;
  79. }
  80. mTransferable = nullptr;
  81. return NS_OK;
  82. }
  83. NS_IMETHODIMP
  84. nsBaseClipboard::HasDataMatchingFlavors(const char** aFlavorList,
  85. uint32_t aLength,
  86. int32_t aWhichClipboard,
  87. bool* outResult)
  88. {
  89. *outResult = true; // say we always do.
  90. return NS_OK;
  91. }
  92. NS_IMETHODIMP
  93. nsBaseClipboard::SupportsSelectionClipboard(bool* _retval)
  94. {
  95. *_retval = false; // we don't support the selection clipboard by default.
  96. return NS_OK;
  97. }
  98. NS_IMETHODIMP
  99. nsBaseClipboard::SupportsFindClipboard(bool* _retval)
  100. {
  101. *_retval = false; // we don't support the find clipboard by default.
  102. return NS_OK;
  103. }