Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / widget / windows / nsNativeDragTarget.cpp
blobcb56635897c008bac17db755d0fc959743e16081
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/. */
6 #include <stdio.h>
7 #include "nsIDragService.h"
8 #include "nsWidgetsCID.h"
9 #include "nsNativeDragTarget.h"
10 #include "nsDragService.h"
11 #include "nsINode.h"
12 #include "nsCOMPtr.h"
14 #include "nsIWidget.h"
15 #include "nsWindow.h"
16 #include "nsClipboard.h"
17 #include "KeyboardLayout.h"
19 #include "mozilla/dom/MouseEventBinding.h"
20 #include "mozilla/MouseEvents.h"
22 using namespace mozilla;
23 using namespace mozilla::widget;
25 // This is cached for Leave notification
26 static POINTL gDragLastPoint;
28 bool nsNativeDragTarget::gDragImageChanged = false;
31 * class nsNativeDragTarget
33 nsNativeDragTarget::nsNativeDragTarget(nsIWidget* aWidget)
34 : m_cRef(0),
35 mEffectsAllowed(DROPEFFECT_MOVE | DROPEFFECT_COPY | DROPEFFECT_LINK),
36 mEffectsPreferred(DROPEFFECT_NONE),
37 mTookOwnRef(false),
38 mWidget(aWidget),
39 mDropTargetHelper(nullptr) {
40 mHWnd = (HWND)mWidget->GetNativeData(NS_NATIVE_WINDOW);
42 mDragService = do_GetService("@mozilla.org/widget/dragservice;1");
45 nsNativeDragTarget::~nsNativeDragTarget() {
46 if (mDropTargetHelper) {
47 mDropTargetHelper->Release();
48 mDropTargetHelper = nullptr;
52 // IUnknown methods - see iunknown.h for documentation
53 STDMETHODIMP
54 nsNativeDragTarget::QueryInterface(REFIID riid, void** ppv) {
55 *ppv = nullptr;
57 if (IID_IUnknown == riid || IID_IDropTarget == riid) *ppv = this;
59 if (nullptr != *ppv) {
60 ((LPUNKNOWN)*ppv)->AddRef();
61 return S_OK;
64 return E_NOINTERFACE;
67 STDMETHODIMP_(ULONG)
68 nsNativeDragTarget::AddRef(void) {
69 ++m_cRef;
70 NS_LOG_ADDREF(this, m_cRef, "nsNativeDragTarget", sizeof(*this));
71 return m_cRef;
74 STDMETHODIMP_(ULONG) nsNativeDragTarget::Release(void) {
75 --m_cRef;
76 NS_LOG_RELEASE(this, m_cRef, "nsNativeDragTarget");
77 if (0 != m_cRef) return m_cRef;
79 delete this;
80 return 0;
83 void nsNativeDragTarget::GetGeckoDragAction(DWORD grfKeyState,
84 LPDWORD pdwEffect,
85 uint32_t* aGeckoAction) {
86 // If a window is disabled or a modal window is on top of it
87 // (which implies it is disabled), then we should not allow dropping.
88 if (!mWidget->IsEnabled()) {
89 *pdwEffect = DROPEFFECT_NONE;
90 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_NONE;
91 return;
94 // If the user explicitly uses a modifier key, they want the associated action
95 // Shift + Control -> LINK, Shift -> MOVE, Ctrl -> COPY
96 DWORD desiredEffect = DROPEFFECT_NONE;
97 if ((grfKeyState & MK_CONTROL) && (grfKeyState & MK_SHIFT)) {
98 desiredEffect = DROPEFFECT_LINK;
99 } else if (grfKeyState & MK_SHIFT) {
100 desiredEffect = DROPEFFECT_MOVE;
101 } else if (grfKeyState & MK_CONTROL) {
102 desiredEffect = DROPEFFECT_COPY;
105 // Determine the desired effect from what is allowed and preferred.
106 if (!(desiredEffect &= mEffectsAllowed)) {
107 // No modifier key effect is set which is also allowed, check
108 // the preference of the data.
109 desiredEffect = mEffectsPreferred & mEffectsAllowed;
110 if (!desiredEffect) {
111 // No preference is set, so just fall back to the allowed effect itself
112 desiredEffect = mEffectsAllowed;
116 // Otherwise we should specify the first available effect
117 // from MOVE, COPY, or LINK.
118 if (desiredEffect & DROPEFFECT_MOVE) {
119 *pdwEffect = DROPEFFECT_MOVE;
120 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_MOVE;
121 } else if (desiredEffect & DROPEFFECT_COPY) {
122 *pdwEffect = DROPEFFECT_COPY;
123 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_COPY;
124 } else if (desiredEffect & DROPEFFECT_LINK) {
125 *pdwEffect = DROPEFFECT_LINK;
126 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_LINK;
127 } else {
128 *pdwEffect = DROPEFFECT_NONE;
129 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_NONE;
133 inline bool IsKeyDown(char key) { return GetKeyState(key) < 0; }
135 void nsNativeDragTarget::DispatchDragDropEvent(EventMessage aEventMessage,
136 const POINTL& aPT) {
137 WidgetDragEvent event(true, aEventMessage, mWidget);
139 nsWindow* win = static_cast<nsWindow*>(mWidget);
140 win->InitEvent(event);
141 POINT cpos;
143 cpos.x = aPT.x;
144 cpos.y = aPT.y;
146 if (mHWnd != nullptr) {
147 ::ScreenToClient(mHWnd, &cpos);
148 event.mRefPoint = LayoutDeviceIntPoint(cpos.x, cpos.y);
149 } else {
150 event.mRefPoint = LayoutDeviceIntPoint(0, 0);
153 ModifierKeyState modifierKeyState;
154 modifierKeyState.InitInputEvent(event);
156 nsDragSession* currSession =
157 static_cast<nsDragSession*>(mDragService->GetCurrentSession(mWidget));
158 if (currSession) {
159 event.mInputSource = currSession->GetInputSource();
160 } else {
161 event.mInputSource = dom::MouseEvent_Binding::MOZ_SOURCE_MOUSE;
164 mWidget->DispatchInputEvent(&event);
167 void nsNativeDragTarget::ProcessDrag(EventMessage aEventMessage,
168 DWORD grfKeyState, POINTL ptl,
169 DWORD* pdwEffect) {
170 // Before dispatching the event make sure we have the correct drop action set
171 uint32_t geckoAction;
172 GetGeckoDragAction(grfKeyState, pdwEffect, &geckoAction);
174 // Set the current action into the Gecko specific type
175 RefPtr<nsDragSession> currSession =
176 static_cast<nsDragSession*>(mDragService->GetCurrentSession(mWidget));
177 if (!currSession) {
178 return;
181 currSession->SetDragAction(geckoAction);
183 // Dispatch the event into Gecko
184 DispatchDragDropEvent(aEventMessage, ptl);
186 // If TakeChildProcessDragAction returns something other than
187 // DRAGDROP_ACTION_UNINITIALIZED, it means that the last event was sent
188 // to the child process and this event is also being sent to the child
189 // process. In this case, use the last event's action instead.
190 currSession->GetDragAction(&geckoAction);
192 int32_t childDragAction = currSession->TakeChildProcessDragAction();
193 if (childDragAction != nsIDragService::DRAGDROP_ACTION_UNINITIALIZED) {
194 geckoAction = childDragAction;
197 if (nsIDragService::DRAGDROP_ACTION_LINK & geckoAction) {
198 *pdwEffect = DROPEFFECT_LINK;
199 } else if (nsIDragService::DRAGDROP_ACTION_COPY & geckoAction) {
200 *pdwEffect = DROPEFFECT_COPY;
201 } else if (nsIDragService::DRAGDROP_ACTION_MOVE & geckoAction) {
202 *pdwEffect = DROPEFFECT_MOVE;
203 } else {
204 *pdwEffect = DROPEFFECT_NONE;
207 if (aEventMessage != eDrop) {
208 // Get the cached drag effect from the drag service, the data member should
209 // have been set by whoever handled the WidgetGUIEvent or nsIDOMEvent on
210 // drags.
211 bool canDrop;
212 currSession->GetCanDrop(&canDrop);
213 if (!canDrop) {
214 *pdwEffect = DROPEFFECT_NONE;
218 // Clear the cached value
219 currSession->SetCanDrop(false);
222 // IDropTarget methods
223 STDMETHODIMP
224 nsNativeDragTarget::DragEnter(LPDATAOBJECT pIDataSource, DWORD grfKeyState,
225 POINTL ptl, DWORD* pdwEffect) {
226 if (!mDragService) {
227 return E_FAIL;
230 mEffectsAllowed = *pdwEffect;
231 AddLinkSupportIfCanBeGenerated(pIDataSource);
233 // Drag and drop image helper
234 if (GetDropTargetHelper()) {
235 // We get a lot of crashes (often uncaught by our handler) later on during
236 // DragOver calls, see bug 1465513. It looks like this might be because
237 // we're not cleaning up previous drags fully and now released resources get
238 // used. Calling IDropTargetHelper::DragLeave before DragEnter seems to fix
239 // this for at least one reproduction of this crash.
240 GetDropTargetHelper()->DragLeave();
241 POINT pt = {ptl.x, ptl.y};
242 GetDropTargetHelper()->DragEnter(mHWnd, pIDataSource, &pt, *pdwEffect);
245 // save a ref to this, in case the window is destroyed underneath us
246 NS_ASSERTION(!mTookOwnRef, "own ref already taken!");
247 this->AddRef();
248 mTookOwnRef = true;
250 // tell the drag service about this drag (it may have come from an
251 // outside app).
252 RefPtr<nsDragSession> session =
253 static_cast<nsDragSession*>(mDragService->StartDragSession(mWidget));
254 MOZ_ASSERT(session);
256 void* tempOutData = nullptr;
257 uint32_t tempDataLen = 0;
258 nsresult loadResult = nsClipboard::GetNativeDataOffClipboard(
259 pIDataSource, 0, ::RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT),
260 nullptr, &tempOutData, &tempDataLen);
261 if (NS_SUCCEEDED(loadResult) && tempOutData) {
262 mEffectsPreferred = *((DWORD*)tempOutData);
263 free(tempOutData);
264 } else {
265 // We have no preference if we can't obtain it
266 mEffectsPreferred = DROPEFFECT_NONE;
269 // Set the native data object into drag session
270 session->SetIDataObject(pIDataSource);
272 // Now process the native drag state and then dispatch the event
273 ProcessDrag(eDragEnter, grfKeyState, ptl, pdwEffect);
275 return S_OK;
278 void nsNativeDragTarget::AddLinkSupportIfCanBeGenerated(
279 LPDATAOBJECT aIDataSource) {
280 // If we don't have a link effect, but we can generate one, fix the
281 // drop effect to include it.
282 if (!(mEffectsAllowed & DROPEFFECT_LINK) && aIDataSource) {
283 if (S_OK == ::OleQueryLinkFromData(aIDataSource)) {
284 mEffectsAllowed |= DROPEFFECT_LINK;
289 STDMETHODIMP
290 nsNativeDragTarget::DragOver(DWORD grfKeyState, POINTL ptl, LPDWORD pdwEffect) {
291 if (!mDragService) {
292 return E_FAIL;
295 bool dragImageChanged = gDragImageChanged;
296 gDragImageChanged = false;
298 // If a LINK effect could be generated previously from a DragEnter(),
299 // then we should include it as an allowed effect.
300 mEffectsAllowed = (*pdwEffect) | (mEffectsAllowed & DROPEFFECT_LINK);
302 RefPtr<nsDragSession> currentDragSession =
303 static_cast<nsDragSession*>(mDragService->GetCurrentSession(mWidget));
304 if (!currentDragSession) {
305 return S_OK; // Drag was canceled.
308 // without the AddRef() |this| can get destroyed in an event handler
309 this->AddRef();
311 // Drag and drop image helper
312 if (GetDropTargetHelper()) {
313 if (dragImageChanged) {
314 // See comment in nsNativeDragTarget::DragEnter.
315 GetDropTargetHelper()->DragLeave();
316 // The drop helper only updates the image during DragEnter, so emulate
317 // a DragEnter if the image was changed.
318 POINT pt = {ptl.x, ptl.y};
319 GetDropTargetHelper()->DragEnter(
320 mHWnd, currentDragSession->GetDataObject(), &pt, *pdwEffect);
322 POINT pt = {ptl.x, ptl.y};
323 GetDropTargetHelper()->DragOver(&pt, *pdwEffect);
326 ModifierKeyState modifierKeyState;
327 currentDragSession->FireDragEventAtSource(eDrag,
328 modifierKeyState.GetModifiers());
329 // Now process the native drag state and then dispatch the event
330 ProcessDrag(eDragOver, grfKeyState, ptl, pdwEffect);
332 this->Release();
334 return S_OK;
337 STDMETHODIMP
338 nsNativeDragTarget::DragLeave() {
339 if (!mDragService) {
340 return E_FAIL;
343 // Drag and drop image helper
344 if (GetDropTargetHelper()) {
345 GetDropTargetHelper()->DragLeave();
348 // dispatch the event into Gecko
349 DispatchDragDropEvent(eDragExit, gDragLastPoint);
351 nsCOMPtr<nsIDragSession> currentDragSession =
352 mDragService->GetCurrentSession(mWidget);
354 if (currentDragSession) {
355 nsCOMPtr<nsINode> sourceNode;
356 currentDragSession->GetSourceNode(getter_AddRefs(sourceNode));
358 if (!sourceNode) {
359 // We're leaving a window while doing a drag that was
360 // initiated in a different app. End the drag session, since
361 // we're done with it for now (until the user drags back into
362 // mozilla).
363 ModifierKeyState modifierKeyState;
364 currentDragSession->EndDragSession(false,
365 modifierKeyState.GetModifiers());
369 // release the ref that was taken in DragEnter
370 NS_ASSERTION(mTookOwnRef, "want to release own ref, but not taken!");
371 if (mTookOwnRef) {
372 this->Release();
373 mTookOwnRef = false;
376 return S_OK;
379 void nsNativeDragTarget::DragCancel() {
380 // Cancel the drag session if we did DragEnter.
381 if (mTookOwnRef) {
382 if (GetDropTargetHelper()) {
383 GetDropTargetHelper()->DragLeave();
385 if (mDragService) {
386 ModifierKeyState modifierKeyState;
387 RefPtr<nsIDragSession> session = mDragService->GetCurrentSession(mWidget);
388 if (session) {
389 session->EndDragSession(false, modifierKeyState.GetModifiers());
392 this->Release(); // matching the AddRef in DragEnter
393 mTookOwnRef = false;
397 STDMETHODIMP
398 nsNativeDragTarget::Drop(LPDATAOBJECT pData, DWORD grfKeyState, POINTL aPT,
399 LPDWORD pdwEffect) {
400 if (!mDragService) {
401 return E_FAIL;
404 mEffectsAllowed = *pdwEffect;
405 AddLinkSupportIfCanBeGenerated(pData);
407 // Drag and drop image helper
408 if (GetDropTargetHelper()) {
409 POINT pt = {aPT.x, aPT.y};
410 GetDropTargetHelper()->Drop(pData, &pt, *pdwEffect);
413 // Set the native data object into the drag service
414 RefPtr<nsDragSession> currentDragSession =
415 static_cast<nsDragSession*>(mDragService->GetCurrentSession(mWidget));
416 if (!currentDragSession) {
417 return S_OK;
419 currentDragSession->SetIDataObject(pData);
421 // NOTE: ProcessDrag spins the event loop which may destroy arbitrary objects.
422 // We use strong refs to prevent it from destroying these:
423 RefPtr<nsNativeDragTarget> kungFuDeathGrip = this;
425 // Now process the native drag state and then dispatch the event
426 ProcessDrag(eDrop, grfKeyState, aPT, pdwEffect);
428 currentDragSession =
429 static_cast<nsDragSession*>(mDragService->GetCurrentSession(mWidget));
430 if (!currentDragSession) {
431 return S_OK; // DragCancel() was called.
434 // Let the win drag session know whether it experienced
435 // a drop event within the application. Drop will not oocur if the
436 // drop landed outside the app. (used in tab tear off, bug 455884)
437 currentDragSession->SetDroppedLocal();
439 // Tell the drag session we're done with it.
440 // Use GetMessagePos to get the position of the mouse at the last message
441 // seen by the event loop. (Bug 489729)
442 DWORD pos = ::GetMessagePos();
443 POINT cpos;
444 cpos.x = GET_X_LPARAM(pos);
445 cpos.y = GET_Y_LPARAM(pos);
446 currentDragSession->SetDragEndPoint(cpos.x, cpos.y);
447 ModifierKeyState modifierKeyState;
448 currentDragSession->EndDragSession(true, modifierKeyState.GetModifiers());
450 // release the ref that was taken in DragEnter
451 NS_ASSERTION(mTookOwnRef, "want to release own ref, but not taken!");
452 if (mTookOwnRef) {
453 this->Release();
454 mTookOwnRef = false;
457 return S_OK;
461 * By lazy loading mDropTargetHelper we save 50-70ms of startup time
462 * which is ~5% of startup time.
464 IDropTargetHelper* nsNativeDragTarget::GetDropTargetHelper() {
465 if (!mDropTargetHelper) {
466 CoCreateInstance(CLSID_DragDropHelper, nullptr, CLSCTX_INPROC_SERVER,
467 IID_IDropTargetHelper, (LPVOID*)&mDropTargetHelper);
470 return mDropTargetHelper;