nsClipboardHelper.cpp 4.2 KB

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