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 
6 #include <ole2.h>
7 #include <oleidl.h>
8 #include <shlobj.h>
9 #include <shlwapi.h>
10 
11 // shellapi.h is needed to build with WIN32_LEAN_AND_MEAN
12 #include <shellapi.h>
13 
14 #include "mozilla/RefPtr.h"
15 #include "nsDragService.h"
16 #include "nsITransferable.h"
17 #include "nsDataObj.h"
18 
19 #include "nsWidgetsCID.h"
20 #include "nsNativeDragTarget.h"
21 #include "nsNativeDragSource.h"
22 #include "nsClipboard.h"
23 #include "mozilla/dom/Document.h"
24 #include "nsDataObjCollection.h"
25 
26 #include "nsArrayUtils.h"
27 #include "nsString.h"
28 #include "nsEscape.h"
29 #include "nsIScreenManager.h"
30 #include "nsToolkit.h"
31 #include "nsCRT.h"
32 #include "nsDirectoryServiceDefs.h"
33 #include "nsUnicharUtils.h"
34 #include "nsRect.h"
35 #include "nsMathUtils.h"
36 #include "WinUtils.h"
37 #include "KeyboardLayout.h"
38 #include "gfxContext.h"
39 #include "mozilla/gfx/2D.h"
40 #include "mozilla/gfx/DataSurfaceHelpers.h"
41 #include "mozilla/gfx/Tools.h"
42 
43 using namespace mozilla;
44 using namespace mozilla::gfx;
45 using namespace mozilla::widget;
46 
47 //-------------------------------------------------------------------------
48 //
49 // DragService constructor
50 //
51 //-------------------------------------------------------------------------
nsDragService()52 nsDragService::nsDragService()
53     : mDataObject(nullptr), mSentLocalDropEvent(false) {}
54 
55 //-------------------------------------------------------------------------
56 //
57 // DragService destructor
58 //
59 //-------------------------------------------------------------------------
~nsDragService()60 nsDragService::~nsDragService() { NS_IF_RELEASE(mDataObject); }
61 
CreateDragImage(nsINode * aDOMNode,const Maybe<CSSIntRegion> & aRegion,SHDRAGIMAGE * psdi)62 bool nsDragService::CreateDragImage(nsINode* aDOMNode,
63                                     const Maybe<CSSIntRegion>& aRegion,
64                                     SHDRAGIMAGE* psdi) {
65   if (!psdi) return false;
66 
67   memset(psdi, 0, sizeof(SHDRAGIMAGE));
68   if (!aDOMNode) return false;
69 
70   // Prepare the drag image
71   LayoutDeviceIntRect dragRect;
72   RefPtr<SourceSurface> surface;
73   nsPresContext* pc;
74   DrawDrag(aDOMNode, aRegion, mScreenPosition, &dragRect, &surface, &pc);
75   if (!surface) return false;
76 
77   uint32_t bmWidth = dragRect.Width(), bmHeight = dragRect.Height();
78 
79   if (bmWidth == 0 || bmHeight == 0) return false;
80 
81   psdi->crColorKey = CLR_NONE;
82 
83   RefPtr<DataSourceSurface> dataSurface = Factory::CreateDataSourceSurface(
84       IntSize(bmWidth, bmHeight), SurfaceFormat::B8G8R8A8);
85   NS_ENSURE_TRUE(dataSurface, false);
86 
87   DataSourceSurface::MappedSurface map;
88   if (!dataSurface->Map(DataSourceSurface::MapType::READ_WRITE, &map)) {
89     return false;
90   }
91 
92   RefPtr<DrawTarget> dt = Factory::CreateDrawTargetForData(
93       BackendType::CAIRO, map.mData, dataSurface->GetSize(), map.mStride,
94       dataSurface->GetFormat());
95   if (!dt) {
96     dataSurface->Unmap();
97     return false;
98   }
99 
100   dt->DrawSurface(
101       surface,
102       Rect(0, 0, dataSurface->GetSize().width, dataSurface->GetSize().height),
103       Rect(0, 0, surface->GetSize().width, surface->GetSize().height),
104       DrawSurfaceOptions(), DrawOptions(1.0f, CompositionOp::OP_SOURCE));
105   dt->Flush();
106 
107   BITMAPV5HEADER bmih;
108   memset((void*)&bmih, 0, sizeof(BITMAPV5HEADER));
109   bmih.bV5Size = sizeof(BITMAPV5HEADER);
110   bmih.bV5Width = bmWidth;
111   bmih.bV5Height = -(int32_t)bmHeight;  // flip vertical
112   bmih.bV5Planes = 1;
113   bmih.bV5BitCount = 32;
114   bmih.bV5Compression = BI_BITFIELDS;
115   bmih.bV5RedMask = 0x00FF0000;
116   bmih.bV5GreenMask = 0x0000FF00;
117   bmih.bV5BlueMask = 0x000000FF;
118   bmih.bV5AlphaMask = 0xFF000000;
119 
120   HDC hdcSrc = CreateCompatibleDC(nullptr);
121   void* lpBits = nullptr;
122   if (hdcSrc) {
123     psdi->hbmpDragImage =
124         ::CreateDIBSection(hdcSrc, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
125                            (void**)&lpBits, nullptr, 0);
126     if (psdi->hbmpDragImage && lpBits) {
127       CopySurfaceDataToPackedArray(map.mData, static_cast<uint8_t*>(lpBits),
128                                    dataSurface->GetSize(), map.mStride,
129                                    BytesPerPixel(dataSurface->GetFormat()));
130     }
131 
132     psdi->sizeDragImage.cx = bmWidth;
133     psdi->sizeDragImage.cy = bmHeight;
134 
135     LayoutDeviceIntPoint screenPoint =
136         ConvertToUnscaledDevPixels(pc, mScreenPosition);
137     psdi->ptOffset.x = screenPoint.x - dragRect.X();
138     psdi->ptOffset.y = screenPoint.y - dragRect.Y();
139 
140     DeleteDC(hdcSrc);
141   }
142 
143   dataSurface->Unmap();
144 
145   return psdi->hbmpDragImage != nullptr;
146 }
147 
148 //-------------------------------------------------------------------------
InvokeDragSessionImpl(nsIArray * anArrayTransferables,const Maybe<CSSIntRegion> & aRegion,uint32_t aActionType)149 nsresult nsDragService::InvokeDragSessionImpl(
150     nsIArray* anArrayTransferables, const Maybe<CSSIntRegion>& aRegion,
151     uint32_t aActionType) {
152   // Try and get source URI of the items that are being dragged
153   nsIURI* uri = nullptr;
154 
155   RefPtr<dom::Document> doc(mSourceDocument);
156   if (doc) {
157     uri = doc->GetDocumentURI();
158   }
159 
160   uint32_t numItemsToDrag = 0;
161   nsresult rv = anArrayTransferables->GetLength(&numItemsToDrag);
162   if (!numItemsToDrag) return NS_ERROR_FAILURE;
163 
164   // The clipboard class contains some static utility methods that we
165   // can use to create an IDataObject from the transferable
166 
167   // if we're dragging more than one item, we need to create a
168   // "collection" object to fake out the OS. This collection contains
169   // one |IDataObject| for each transferable. If there is just the one
170   // (most cases), only pass around the native |IDataObject|.
171   RefPtr<IDataObject> itemToDrag;
172   if (numItemsToDrag > 1) {
173     nsDataObjCollection* dataObjCollection = new nsDataObjCollection();
174     if (!dataObjCollection) return NS_ERROR_OUT_OF_MEMORY;
175     itemToDrag = dataObjCollection;
176     for (uint32_t i = 0; i < numItemsToDrag; ++i) {
177       nsCOMPtr<nsITransferable> trans =
178           do_QueryElementAt(anArrayTransferables, i);
179       if (trans) {
180         RefPtr<IDataObject> dataObj;
181         rv = nsClipboard::CreateNativeDataObject(trans, getter_AddRefs(dataObj),
182                                                  uri);
183         NS_ENSURE_SUCCESS(rv, rv);
184         // Add the flavors to the collection object too
185         rv = nsClipboard::SetupNativeDataObject(trans, dataObjCollection);
186         NS_ENSURE_SUCCESS(rv, rv);
187 
188         dataObjCollection->AddDataObject(dataObj);
189       }
190     }
191   }  // if dragging multiple items
192   else {
193     nsCOMPtr<nsITransferable> trans =
194         do_QueryElementAt(anArrayTransferables, 0);
195     if (trans) {
196       rv = nsClipboard::CreateNativeDataObject(trans,
197                                                getter_AddRefs(itemToDrag), uri);
198       NS_ENSURE_SUCCESS(rv, rv);
199     }
200   }  // else dragging a single object
201 
202   // Create a drag image if support is available
203   IDragSourceHelper* pdsh;
204   if (SUCCEEDED(CoCreateInstance(CLSID_DragDropHelper, nullptr,
205                                  CLSCTX_INPROC_SERVER, IID_IDragSourceHelper,
206                                  (void**)&pdsh))) {
207     SHDRAGIMAGE sdi;
208     if (CreateDragImage(mSourceNode, aRegion, &sdi)) {
209       if (FAILED(pdsh->InitializeFromBitmap(&sdi, itemToDrag)))
210         DeleteObject(sdi.hbmpDragImage);
211     }
212     pdsh->Release();
213   }
214 
215   // Kick off the native drag session
216   return StartInvokingDragSession(itemToDrag, aActionType);
217 }
218 
LayoutDevicePointToCSSPoint(const LayoutDevicePoint & aDevPos,CSSPoint & aCSSPos)219 static bool LayoutDevicePointToCSSPoint(const LayoutDevicePoint& aDevPos,
220                                         CSSPoint& aCSSPos) {
221   nsCOMPtr<nsIScreenManager> screenMgr =
222       do_GetService("@mozilla.org/gfx/screenmanager;1");
223   if (!screenMgr) {
224     return false;
225   }
226 
227   nsCOMPtr<nsIScreen> screen;
228   screenMgr->ScreenForRect(NSToIntRound(aDevPos.x), NSToIntRound(aDevPos.y), 1,
229                            1, getter_AddRefs(screen));
230   if (!screen) {
231     return false;
232   }
233 
234   int32_t w, h;  // unused
235   LayoutDeviceIntPoint screenOriginDev;
236   screen->GetRect(&screenOriginDev.x, &screenOriginDev.y, &w, &h);
237 
238   double scale;
239   screen->GetDefaultCSSScaleFactor(&scale);
240   LayoutDeviceToCSSScale devToCSSScale =
241       CSSToLayoutDeviceScale(scale).Inverse();
242 
243   // Desktop pixels and CSS pixels share the same screen origin.
244   CSSIntPoint screenOriginCSS;
245   screen->GetRectDisplayPix(&screenOriginCSS.x, &screenOriginCSS.y, &w, &h);
246 
247   aCSSPos = (aDevPos - screenOriginDev) * devToCSSScale + screenOriginCSS;
248   return true;
249 }
250 
251 //-------------------------------------------------------------------------
StartInvokingDragSession(IDataObject * aDataObj,uint32_t aActionType)252 nsresult nsDragService::StartInvokingDragSession(IDataObject* aDataObj,
253                                                  uint32_t aActionType) {
254   // To do the drag we need to create an object that
255   // implements the IDataObject interface (for OLE)
256   RefPtr<nsNativeDragSource> nativeDragSrc =
257       new nsNativeDragSource(mDataTransfer);
258 
259   // Now figure out what the native drag effect should be
260   DWORD winDropRes;
261   DWORD effects = DROPEFFECT_SCROLL;
262   if (aActionType & DRAGDROP_ACTION_COPY) {
263     effects |= DROPEFFECT_COPY;
264   }
265   if (aActionType & DRAGDROP_ACTION_MOVE) {
266     effects |= DROPEFFECT_MOVE;
267   }
268   if (aActionType & DRAGDROP_ACTION_LINK) {
269     effects |= DROPEFFECT_LINK;
270   }
271 
272   // XXX not sure why we bother to cache this, it can change during
273   // the drag
274   mDragAction = aActionType;
275   mSentLocalDropEvent = false;
276 
277   // Start dragging
278   StartDragSession();
279   OpenDragPopup();
280 
281   RefPtr<IDataObjectAsyncCapability> pAsyncOp;
282   // Offer to do an async drag
283   if (SUCCEEDED(aDataObj->QueryInterface(IID_IDataObjectAsyncCapability,
284                                          getter_AddRefs(pAsyncOp)))) {
285     pAsyncOp->SetAsyncMode(VARIANT_TRUE);
286   } else {
287     MOZ_ASSERT_UNREACHABLE("When did our data object stop being async");
288   }
289 
290   // Call the native D&D method
291   HRESULT res = ::DoDragDrop(aDataObj, nativeDragSrc, effects, &winDropRes);
292 
293   // In  cases where the drop operation completed outside the application,
294   // update the source node's DataTransfer dropEffect value so it is up to date.
295   if (!mSentLocalDropEvent) {
296     uint32_t dropResult;
297     // Order is important, since multiple flags can be returned.
298     if (winDropRes & DROPEFFECT_COPY)
299       dropResult = DRAGDROP_ACTION_COPY;
300     else if (winDropRes & DROPEFFECT_LINK)
301       dropResult = DRAGDROP_ACTION_LINK;
302     else if (winDropRes & DROPEFFECT_MOVE)
303       dropResult = DRAGDROP_ACTION_MOVE;
304     else
305       dropResult = DRAGDROP_ACTION_NONE;
306 
307     if (mDataTransfer) {
308       if (res == DRAGDROP_S_DROP)  // Success
309         mDataTransfer->SetDropEffectInt(dropResult);
310       else
311         mDataTransfer->SetDropEffectInt(DRAGDROP_ACTION_NONE);
312     }
313   }
314 
315   mUserCancelled = nativeDragSrc->UserCancelled();
316 
317   // We're done dragging, get the cursor position and end the drag
318   // Use GetMessagePos to get the position of the mouse at the last message
319   // seen by the event loop. (Bug 489729)
320   // Note that we must convert this from device pixels back to Windows logical
321   // pixels (bug 818927).
322   DWORD pos = ::GetMessagePos();
323   CSSPoint cssPos;
324   if (!LayoutDevicePointToCSSPoint(
325           LayoutDevicePoint(GET_X_LPARAM(pos), GET_Y_LPARAM(pos)), cssPos)) {
326     // fallback to the simple scaling
327     POINT pt = {GET_X_LPARAM(pos), GET_Y_LPARAM(pos)};
328     HMONITOR monitor = ::MonitorFromPoint(pt, MONITOR_DEFAULTTOPRIMARY);
329     double dpiScale = widget::WinUtils::LogToPhysFactor(monitor);
330     cssPos.x = GET_X_LPARAM(pos) / dpiScale;
331     cssPos.y = GET_Y_LPARAM(pos) / dpiScale;
332   }
333   // We have to abuse SetDragEndPoint to pass CSS pixels because
334   // Event::GetScreenCoords will not convert pixels for dragend events
335   // until bug 1224754 is fixed.
336   SetDragEndPoint(
337       LayoutDeviceIntPoint(NSToIntRound(cssPos.x), NSToIntRound(cssPos.y)));
338   ModifierKeyState modifierKeyState;
339   EndDragSession(true, modifierKeyState.GetModifiers());
340 
341   mDoingDrag = false;
342 
343   return DRAGDROP_S_DROP == res ? NS_OK : NS_ERROR_FAILURE;
344 }
345 
346 //-------------------------------------------------------------------------
347 // Make Sure we have the right kind of object
GetDataObjCollection(IDataObject * aDataObj)348 nsDataObjCollection* nsDragService::GetDataObjCollection(
349     IDataObject* aDataObj) {
350   nsDataObjCollection* dataObjCol = nullptr;
351   if (aDataObj) {
352     nsIDataObjCollection* dataObj;
353     if (aDataObj->QueryInterface(IID_IDataObjCollection, (void**)&dataObj) ==
354         S_OK) {
355       dataObjCol = static_cast<nsDataObjCollection*>(aDataObj);
356       dataObj->Release();
357     }
358   }
359 
360   return dataObjCol;
361 }
362 
363 //-------------------------------------------------------------------------
364 NS_IMETHODIMP
GetNumDropItems(uint32_t * aNumItems)365 nsDragService::GetNumDropItems(uint32_t* aNumItems) {
366   if (!mDataObject) {
367     *aNumItems = 0;
368     return NS_OK;
369   }
370 
371   if (IsCollectionObject(mDataObject)) {
372     nsDataObjCollection* dataObjCol = GetDataObjCollection(mDataObject);
373     if (dataObjCol) {
374       *aNumItems = dataObjCol->GetNumDataObjects();
375     } else {
376       // If the count cannot be determined just return 0.
377       // This can happen if we have collection data of type
378       // MULTI_MIME ("Mozilla/IDataObjectCollectionFormat") on the clipboard
379       // from another process but we can't obtain an IID_IDataObjCollection
380       // from this process.
381       *aNumItems = 0;
382     }
383   } else {
384     // Next check if we have a file drop. Return the number of files in
385     // the file drop as the number of items we have, pretending like we
386     // actually have > 1 drag item.
387     FORMATETC fe2;
388     SET_FORMATETC(fe2, CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
389     if (mDataObject->QueryGetData(&fe2) == S_OK) {
390       STGMEDIUM stm;
391       if (mDataObject->GetData(&fe2, &stm) == S_OK) {
392         HDROP hdrop = (HDROP)GlobalLock(stm.hGlobal);
393         *aNumItems = ::DragQueryFileW(hdrop, 0xFFFFFFFF, nullptr, 0);
394         ::GlobalUnlock(stm.hGlobal);
395         ::ReleaseStgMedium(&stm);
396         // Data may be provided later, so assume we have 1 item
397         if (*aNumItems == 0) *aNumItems = 1;
398       } else
399         *aNumItems = 1;
400     } else
401       *aNumItems = 1;
402   }
403 
404   return NS_OK;
405 }
406 
407 //-------------------------------------------------------------------------
408 NS_IMETHODIMP
GetData(nsITransferable * aTransferable,uint32_t anItem)409 nsDragService::GetData(nsITransferable* aTransferable, uint32_t anItem) {
410   // This typcially happens on a drop, the target would be asking
411   // for it's transferable to be filled in
412   // Use a static clipboard utility method for this
413   if (!mDataObject) return NS_ERROR_FAILURE;
414 
415   nsresult dataFound = NS_ERROR_FAILURE;
416 
417   if (IsCollectionObject(mDataObject)) {
418     // multiple items, use |anItem| as an index into our collection
419     nsDataObjCollection* dataObjCol = GetDataObjCollection(mDataObject);
420     uint32_t cnt = dataObjCol->GetNumDataObjects();
421     if (anItem < cnt) {
422       IDataObject* dataObj = dataObjCol->GetDataObjectAt(anItem);
423       dataFound = nsClipboard::GetDataFromDataObject(dataObj, 0, nullptr,
424                                                      aTransferable);
425     } else
426       NS_WARNING("Index out of range!");
427   } else {
428     // If they are asking for item "0", we can just get it...
429     if (anItem == 0) {
430       dataFound = nsClipboard::GetDataFromDataObject(mDataObject, anItem,
431                                                      nullptr, aTransferable);
432     } else {
433       // It better be a file drop, or else non-zero indexes are invalid!
434       FORMATETC fe2;
435       SET_FORMATETC(fe2, CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
436       if (mDataObject->QueryGetData(&fe2) == S_OK)
437         dataFound = nsClipboard::GetDataFromDataObject(mDataObject, anItem,
438                                                        nullptr, aTransferable);
439       else
440         NS_WARNING(
441             "Reqesting non-zero index, but clipboard data is not a "
442             "collection!");
443     }
444   }
445   return dataFound;
446 }
447 
448 //---------------------------------------------------------
449 NS_IMETHODIMP
SetIDataObject(IDataObject * aDataObj)450 nsDragService::SetIDataObject(IDataObject* aDataObj) {
451   // When the native drag starts the DragService gets
452   // the IDataObject that is being dragged
453   NS_IF_RELEASE(mDataObject);
454   mDataObject = aDataObj;
455   NS_IF_ADDREF(mDataObject);
456 
457   return NS_OK;
458 }
459 
460 //---------------------------------------------------------
SetDroppedLocal()461 void nsDragService::SetDroppedLocal() {
462   // Sent from the native drag handler, letting us know
463   // a drop occurred within the application vs. outside of it.
464   mSentLocalDropEvent = true;
465   return;
466 }
467 
468 //-------------------------------------------------------------------------
469 NS_IMETHODIMP
IsDataFlavorSupported(const char * aDataFlavor,bool * _retval)470 nsDragService::IsDataFlavorSupported(const char* aDataFlavor, bool* _retval) {
471   if (!aDataFlavor || !mDataObject || !_retval) return NS_ERROR_FAILURE;
472 
473 #ifdef DEBUG
474   if (strcmp(aDataFlavor, kTextMime) == 0)
475     NS_WARNING(
476         "DO NOT USE THE text/plain DATA FLAVOR ANY MORE. USE text/unicode "
477         "INSTEAD");
478 #endif
479 
480   *_retval = false;
481 
482   FORMATETC fe;
483   UINT format = 0;
484 
485   if (IsCollectionObject(mDataObject)) {
486     // We know we have one of our special collection objects.
487     format = nsClipboard::GetFormat(aDataFlavor);
488     SET_FORMATETC(fe, format, 0, DVASPECT_CONTENT, -1,
489                   TYMED_HGLOBAL | TYMED_FILE | TYMED_GDI);
490 
491     // See if any one of the IDataObjects in the collection supports
492     // this data type
493     nsDataObjCollection* dataObjCol = GetDataObjCollection(mDataObject);
494     if (dataObjCol) {
495       uint32_t cnt = dataObjCol->GetNumDataObjects();
496       for (uint32_t i = 0; i < cnt; ++i) {
497         IDataObject* dataObj = dataObjCol->GetDataObjectAt(i);
498         if (S_OK == dataObj->QueryGetData(&fe)) *_retval = true;  // found it!
499       }
500     }
501   }  // if special collection object
502   else {
503     // Ok, so we have a single object. Check to see if has the correct
504     // data type. Since this can come from an outside app, we also
505     // need to see if we need to perform text->unicode conversion if
506     // the client asked for unicode and it wasn't available.
507     format = nsClipboard::GetFormat(aDataFlavor);
508     SET_FORMATETC(fe, format, 0, DVASPECT_CONTENT, -1,
509                   TYMED_HGLOBAL | TYMED_FILE | TYMED_GDI);
510     if (mDataObject->QueryGetData(&fe) == S_OK)
511       *_retval = true;  // found it!
512     else {
513       // We haven't found the exact flavor the client asked for, but
514       // maybe we can still find it from something else that's on the
515       // clipboard
516       if (strcmp(aDataFlavor, kUnicodeMime) == 0) {
517         // client asked for unicode and it wasn't present, check if we
518         // have CF_TEXT.  We'll handle the actual data substitution in
519         // the data object.
520         format = nsClipboard::GetFormat(kTextMime);
521         SET_FORMATETC(fe, format, 0, DVASPECT_CONTENT, -1,
522                       TYMED_HGLOBAL | TYMED_FILE | TYMED_GDI);
523         if (mDataObject->QueryGetData(&fe) == S_OK)
524           *_retval = true;  // found it!
525       } else if (strcmp(aDataFlavor, kURLMime) == 0) {
526         // client asked for a url and it wasn't present, but if we
527         // have a file, then we have a URL to give them (the path, or
528         // the internal URL if an InternetShortcut).
529         format = nsClipboard::GetFormat(kFileMime);
530         SET_FORMATETC(fe, format, 0, DVASPECT_CONTENT, -1,
531                       TYMED_HGLOBAL | TYMED_FILE | TYMED_GDI);
532         if (mDataObject->QueryGetData(&fe) == S_OK)
533           *_retval = true;  // found it!
534       }
535     }  // else try again
536   }
537 
538   return NS_OK;
539 }
540 
541 //
542 // IsCollectionObject
543 //
544 // Determine if this is a single |IDataObject| or one of our private
545 // collection objects. We know the difference because our collection
546 // object will respond to supporting the private |MULTI_MIME| format.
547 //
IsCollectionObject(IDataObject * inDataObj)548 bool nsDragService::IsCollectionObject(IDataObject* inDataObj) {
549   bool isCollection = false;
550 
551   // setup the format object to ask for the MULTI_MIME format. We only
552   // need to do this once
553   static UINT sFormat = 0;
554   static FORMATETC sFE;
555   if (!sFormat) {
556     sFormat = nsClipboard::GetFormat(MULTI_MIME);
557     SET_FORMATETC(sFE, sFormat, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL);
558   }
559 
560   // ask the object if it supports it. If yes, we have a collection
561   // object
562   if (inDataObj->QueryGetData(&sFE) == S_OK) isCollection = true;
563 
564   return isCollection;
565 
566 }  // IsCollectionObject
567 
568 //
569 // EndDragSession
570 //
571 // Override the default to make sure that we release the data object
572 // when the drag ends. It seems that OLE doesn't like to let apps quit
573 // w/out crashing when we're still holding onto their data
574 //
575 NS_IMETHODIMP
EndDragSession(bool aDoneDrag,uint32_t aKeyModifiers)576 nsDragService::EndDragSession(bool aDoneDrag, uint32_t aKeyModifiers) {
577   // Bug 100180: If we've got mouse events captured, make sure we release it -
578   // that way, if we happen to call EndDragSession before diving into a nested
579   // event loop, we can still respond to mouse events.
580   if (::GetCapture()) {
581     ::ReleaseCapture();
582   }
583 
584   nsBaseDragService::EndDragSession(aDoneDrag, aKeyModifiers);
585   NS_IF_RELEASE(mDataObject);
586 
587   return NS_OK;
588 }
589 
590 NS_IMETHODIMP
UpdateDragImage(nsINode * aImage,int32_t aImageX,int32_t aImageY)591 nsDragService::UpdateDragImage(nsINode* aImage, int32_t aImageX,
592                                int32_t aImageY) {
593   if (!mDataObject) {
594     return NS_OK;
595   }
596 
597   nsBaseDragService::UpdateDragImage(aImage, aImageX, aImageY);
598 
599   IDragSourceHelper* pdsh;
600   if (SUCCEEDED(CoCreateInstance(CLSID_DragDropHelper, nullptr,
601                                  CLSCTX_INPROC_SERVER, IID_IDragSourceHelper,
602                                  (void**)&pdsh))) {
603     SHDRAGIMAGE sdi;
604     if (CreateDragImage(mSourceNode, Nothing(), &sdi)) {
605       nsNativeDragTarget::DragImageChanged();
606       if (FAILED(pdsh->InitializeFromBitmap(&sdi, mDataObject)))
607         DeleteObject(sdi.hbmpDragImage);
608     }
609     pdsh->Release();
610   }
611 
612   return NS_OK;
613 }
614