nsDragServiceProxy.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* -*- Mode: C++; tab-width: 4; 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 "nsDragServiceProxy.h"
  7. #include "nsIDocument.h"
  8. #include "nsISupportsPrimitives.h"
  9. #include "mozilla/dom/TabChild.h"
  10. #include "mozilla/gfx/2D.h"
  11. #include "mozilla/UniquePtr.h"
  12. #include "mozilla/Unused.h"
  13. #include "nsContentUtils.h"
  14. using mozilla::ipc::Shmem;
  15. using mozilla::dom::TabChild;
  16. using mozilla::dom::OptionalShmem;
  17. NS_IMPL_ISUPPORTS_INHERITED0(nsDragServiceProxy, nsBaseDragService)
  18. nsDragServiceProxy::nsDragServiceProxy()
  19. {
  20. }
  21. nsDragServiceProxy::~nsDragServiceProxy()
  22. {
  23. }
  24. nsresult
  25. nsDragServiceProxy::InvokeDragSessionImpl(nsIArray* aArrayTransferables,
  26. nsIScriptableRegion* aRegion,
  27. uint32_t aActionType)
  28. {
  29. nsCOMPtr<nsIDocument> doc = do_QueryInterface(mSourceDocument);
  30. NS_ENSURE_STATE(doc->GetDocShell());
  31. TabChild* child = TabChild::GetFrom(doc->GetDocShell());
  32. NS_ENSURE_STATE(child);
  33. nsTArray<mozilla::dom::IPCDataTransfer> dataTransfers;
  34. nsContentUtils::TransferablesToIPCTransferables(aArrayTransferables,
  35. dataTransfers,
  36. false,
  37. child->Manager(),
  38. nullptr);
  39. LayoutDeviceIntRect dragRect;
  40. if (mHasImage || mSelection) {
  41. nsPresContext* pc;
  42. RefPtr<mozilla::gfx::SourceSurface> surface;
  43. DrawDrag(mSourceNode, aRegion, mScreenPosition, &dragRect, &surface, &pc);
  44. if (surface) {
  45. RefPtr<mozilla::gfx::DataSourceSurface> dataSurface =
  46. surface->GetDataSurface();
  47. if (dataSurface) {
  48. size_t length;
  49. int32_t stride;
  50. Shmem surfaceData;
  51. nsContentUtils::GetSurfaceData(dataSurface, &length, &stride, child,
  52. &surfaceData);
  53. // Save the surface data to shared memory.
  54. if (!surfaceData.IsReadable() || !surfaceData.get<char>()) {
  55. NS_WARNING("Failed to create shared memory for drag session.");
  56. return NS_ERROR_FAILURE;
  57. }
  58. mozilla::Unused <<
  59. child->SendInvokeDragSession(dataTransfers, aActionType, surfaceData,
  60. stride, static_cast<uint8_t>(dataSurface->GetFormat()),
  61. dragRect);
  62. StartDragSession();
  63. return NS_OK;
  64. }
  65. }
  66. }
  67. mozilla::Unused << child->SendInvokeDragSession(dataTransfers, aActionType,
  68. mozilla::void_t(), 0, 0, dragRect);
  69. StartDragSession();
  70. return NS_OK;
  71. }