widget/windows/nsNativeDragTarget.cpp

Thu, 15 Jan 2015 15:59:08 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 15:59:08 +0100
branch
TOR_BUG_9701
changeset 10
ac0c01689b40
permissions
-rw-r--r--

Implement a real Private Browsing Mode condition by changing the API/ABI;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include <stdio.h>
michael@0 7 #include "nsIDragService.h"
michael@0 8 #include "nsWidgetsCID.h"
michael@0 9 #include "nsNativeDragTarget.h"
michael@0 10 #include "nsDragService.h"
michael@0 11 #include "nsIServiceManager.h"
michael@0 12 #include "nsIDOMNode.h"
michael@0 13 #include "nsCOMPtr.h"
michael@0 14
michael@0 15 #include "nsIWidget.h"
michael@0 16 #include "nsWindow.h"
michael@0 17 #include "nsClipboard.h"
michael@0 18 #include "KeyboardLayout.h"
michael@0 19
michael@0 20 #include "mozilla/MouseEvents.h"
michael@0 21
michael@0 22 using namespace mozilla;
michael@0 23 using namespace mozilla::widget;
michael@0 24
michael@0 25 /* Define Interface IDs */
michael@0 26 static NS_DEFINE_IID(kIDragServiceIID, NS_IDRAGSERVICE_IID);
michael@0 27
michael@0 28 // This is cached for Leave notification
michael@0 29 static POINTL gDragLastPoint;
michael@0 30
michael@0 31 /*
michael@0 32 * class nsNativeDragTarget
michael@0 33 */
michael@0 34 nsNativeDragTarget::nsNativeDragTarget(nsIWidget * aWidget)
michael@0 35 : m_cRef(0),
michael@0 36 mEffectsAllowed(DROPEFFECT_MOVE | DROPEFFECT_COPY | DROPEFFECT_LINK),
michael@0 37 mEffectsPreferred(DROPEFFECT_NONE),
michael@0 38 mTookOwnRef(false), mWidget(aWidget), mDropTargetHelper(nullptr)
michael@0 39 {
michael@0 40 static NS_DEFINE_IID(kCDragServiceCID, NS_DRAGSERVICE_CID);
michael@0 41
michael@0 42 mHWnd = (HWND)mWidget->GetNativeData(NS_NATIVE_WINDOW);
michael@0 43
michael@0 44 /*
michael@0 45 * Create/Get the DragService that we have implemented
michael@0 46 */
michael@0 47 CallGetService(kCDragServiceCID, &mDragService);
michael@0 48 }
michael@0 49
michael@0 50 nsNativeDragTarget::~nsNativeDragTarget()
michael@0 51 {
michael@0 52 NS_RELEASE(mDragService);
michael@0 53
michael@0 54 if (mDropTargetHelper) {
michael@0 55 mDropTargetHelper->Release();
michael@0 56 mDropTargetHelper = nullptr;
michael@0 57 }
michael@0 58 }
michael@0 59
michael@0 60 // IUnknown methods - see iunknown.h for documentation
michael@0 61 STDMETHODIMP
michael@0 62 nsNativeDragTarget::QueryInterface(REFIID riid, void** ppv)
michael@0 63 {
michael@0 64 *ppv=nullptr;
michael@0 65
michael@0 66 if (IID_IUnknown == riid || IID_IDropTarget == riid)
michael@0 67 *ppv=this;
michael@0 68
michael@0 69 if (nullptr!=*ppv) {
michael@0 70 ((LPUNKNOWN)*ppv)->AddRef();
michael@0 71 return S_OK;
michael@0 72 }
michael@0 73
michael@0 74 return E_NOINTERFACE;
michael@0 75 }
michael@0 76
michael@0 77 STDMETHODIMP_(ULONG)
michael@0 78 nsNativeDragTarget::AddRef(void)
michael@0 79 {
michael@0 80 ++m_cRef;
michael@0 81 NS_LOG_ADDREF(this, m_cRef, "nsNativeDragTarget", sizeof(*this));
michael@0 82 return m_cRef;
michael@0 83 }
michael@0 84
michael@0 85 STDMETHODIMP_(ULONG) nsNativeDragTarget::Release(void)
michael@0 86 {
michael@0 87 --m_cRef;
michael@0 88 NS_LOG_RELEASE(this, m_cRef, "nsNativeDragTarget");
michael@0 89 if (0 != m_cRef)
michael@0 90 return m_cRef;
michael@0 91
michael@0 92 delete this;
michael@0 93 return 0;
michael@0 94 }
michael@0 95
michael@0 96 void
michael@0 97 nsNativeDragTarget::GetGeckoDragAction(DWORD grfKeyState, LPDWORD pdwEffect,
michael@0 98 uint32_t * aGeckoAction)
michael@0 99 {
michael@0 100 // If a window is disabled or a modal window is on top of it
michael@0 101 // (which implies it is disabled), then we should not allow dropping.
michael@0 102 if (!mWidget->IsEnabled()) {
michael@0 103 *pdwEffect = DROPEFFECT_NONE;
michael@0 104 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_NONE;
michael@0 105 return;
michael@0 106 }
michael@0 107
michael@0 108 // If the user explicitly uses a modifier key, they want the associated action
michael@0 109 // Shift + Control -> LINK, Shift -> MOVE, Ctrl -> COPY
michael@0 110 DWORD desiredEffect = DROPEFFECT_NONE;
michael@0 111 if ((grfKeyState & MK_CONTROL) && (grfKeyState & MK_SHIFT)) {
michael@0 112 desiredEffect = DROPEFFECT_LINK;
michael@0 113 } else if (grfKeyState & MK_SHIFT) {
michael@0 114 desiredEffect = DROPEFFECT_MOVE;
michael@0 115 } else if (grfKeyState & MK_CONTROL) {
michael@0 116 desiredEffect = DROPEFFECT_COPY;
michael@0 117 }
michael@0 118
michael@0 119 // Determine the desired effect from what is allowed and preferred.
michael@0 120 if (!(desiredEffect &= mEffectsAllowed)) {
michael@0 121 // No modifier key effect is set which is also allowed, check
michael@0 122 // the preference of the data.
michael@0 123 desiredEffect = mEffectsPreferred & mEffectsAllowed;
michael@0 124 if (!desiredEffect) {
michael@0 125 // No preference is set, so just fall back to the allowed effect itself
michael@0 126 desiredEffect = mEffectsAllowed;
michael@0 127 }
michael@0 128 }
michael@0 129
michael@0 130 // Otherwise we should specify the first available effect
michael@0 131 // from MOVE, COPY, or LINK.
michael@0 132 if (desiredEffect & DROPEFFECT_MOVE) {
michael@0 133 *pdwEffect = DROPEFFECT_MOVE;
michael@0 134 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_MOVE;
michael@0 135 } else if (desiredEffect & DROPEFFECT_COPY) {
michael@0 136 *pdwEffect = DROPEFFECT_COPY;
michael@0 137 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_COPY;
michael@0 138 } else if (desiredEffect & DROPEFFECT_LINK) {
michael@0 139 *pdwEffect = DROPEFFECT_LINK;
michael@0 140 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_LINK;
michael@0 141 } else {
michael@0 142 *pdwEffect = DROPEFFECT_NONE;
michael@0 143 *aGeckoAction = nsIDragService::DRAGDROP_ACTION_NONE;
michael@0 144 }
michael@0 145 }
michael@0 146
michael@0 147 inline
michael@0 148 bool
michael@0 149 IsKeyDown(char key)
michael@0 150 {
michael@0 151 return GetKeyState(key) < 0;
michael@0 152 }
michael@0 153
michael@0 154 void
michael@0 155 nsNativeDragTarget::DispatchDragDropEvent(uint32_t aEventType, POINTL aPT)
michael@0 156 {
michael@0 157 nsEventStatus status;
michael@0 158 WidgetDragEvent event(true, aEventType, mWidget);
michael@0 159
michael@0 160 nsWindow * win = static_cast<nsWindow *>(mWidget);
michael@0 161 win->InitEvent(event);
michael@0 162 POINT cpos;
michael@0 163
michael@0 164 cpos.x = aPT.x;
michael@0 165 cpos.y = aPT.y;
michael@0 166
michael@0 167 if (mHWnd != nullptr) {
michael@0 168 ::ScreenToClient(mHWnd, &cpos);
michael@0 169 event.refPoint.x = cpos.x;
michael@0 170 event.refPoint.y = cpos.y;
michael@0 171 } else {
michael@0 172 event.refPoint.x = 0;
michael@0 173 event.refPoint.y = 0;
michael@0 174 }
michael@0 175
michael@0 176 ModifierKeyState modifierKeyState;
michael@0 177 modifierKeyState.InitInputEvent(event);
michael@0 178
michael@0 179 event.inputSource = static_cast<nsBaseDragService*>(mDragService)->GetInputSource();
michael@0 180
michael@0 181 mWidget->DispatchEvent(&event, status);
michael@0 182 }
michael@0 183
michael@0 184 void
michael@0 185 nsNativeDragTarget::ProcessDrag(uint32_t aEventType,
michael@0 186 DWORD grfKeyState,
michael@0 187 POINTL ptl,
michael@0 188 DWORD* pdwEffect)
michael@0 189 {
michael@0 190 // Before dispatching the event make sure we have the correct drop action set
michael@0 191 uint32_t geckoAction;
michael@0 192 GetGeckoDragAction(grfKeyState, pdwEffect, &geckoAction);
michael@0 193
michael@0 194 // Set the current action into the Gecko specific type
michael@0 195 nsCOMPtr<nsIDragSession> currSession;
michael@0 196 mDragService->GetCurrentSession(getter_AddRefs(currSession));
michael@0 197 if (!currSession) {
michael@0 198 return;
michael@0 199 }
michael@0 200
michael@0 201 currSession->SetDragAction(geckoAction);
michael@0 202
michael@0 203 // Dispatch the event into Gecko
michael@0 204 DispatchDragDropEvent(aEventType, ptl);
michael@0 205
michael@0 206 if (aEventType != NS_DRAGDROP_DROP) {
michael@0 207 // Get the cached drag effect from the drag service, the data member should
michael@0 208 // have been set by whoever handled the WidgetGUIEvent or nsIDOMEvent on
michael@0 209 // drags.
michael@0 210 bool canDrop;
michael@0 211 currSession->GetCanDrop(&canDrop);
michael@0 212 if (!canDrop) {
michael@0 213 *pdwEffect = DROPEFFECT_NONE;
michael@0 214 }
michael@0 215 }
michael@0 216
michael@0 217 // Clear the cached value
michael@0 218 currSession->SetCanDrop(false);
michael@0 219 }
michael@0 220
michael@0 221 // IDropTarget methods
michael@0 222 STDMETHODIMP
michael@0 223 nsNativeDragTarget::DragEnter(LPDATAOBJECT pIDataSource,
michael@0 224 DWORD grfKeyState,
michael@0 225 POINTL ptl,
michael@0 226 DWORD* pdwEffect)
michael@0 227 {
michael@0 228 if (!mDragService) {
michael@0 229 return E_FAIL;
michael@0 230 }
michael@0 231
michael@0 232 mEffectsAllowed = *pdwEffect;
michael@0 233 AddLinkSupportIfCanBeGenerated(pIDataSource);
michael@0 234
michael@0 235 // Drag and drop image helper
michael@0 236 if (GetDropTargetHelper()) {
michael@0 237 POINT pt = { ptl.x, ptl.y };
michael@0 238 GetDropTargetHelper()->DragEnter(mHWnd, pIDataSource, &pt, *pdwEffect);
michael@0 239 }
michael@0 240
michael@0 241 // save a ref to this, in case the window is destroyed underneath us
michael@0 242 NS_ASSERTION(!mTookOwnRef, "own ref already taken!");
michael@0 243 this->AddRef();
michael@0 244 mTookOwnRef = true;
michael@0 245
michael@0 246 // tell the drag service about this drag (it may have come from an
michael@0 247 // outside app).
michael@0 248 mDragService->StartDragSession();
michael@0 249
michael@0 250 void* tempOutData = nullptr;
michael@0 251 uint32_t tempDataLen = 0;
michael@0 252 nsresult loadResult = nsClipboard::GetNativeDataOffClipboard(
michael@0 253 pIDataSource, 0, ::RegisterClipboardFormat(CFSTR_PREFERREDDROPEFFECT), nullptr, &tempOutData, &tempDataLen);
michael@0 254 if (NS_SUCCEEDED(loadResult) && tempOutData) {
michael@0 255 mEffectsPreferred = *((DWORD*)tempOutData);
michael@0 256 nsMemory::Free(tempOutData);
michael@0 257 } else {
michael@0 258 // We have no preference if we can't obtain it
michael@0 259 mEffectsPreferred = DROPEFFECT_NONE;
michael@0 260 }
michael@0 261
michael@0 262 // Set the native data object into drag service
michael@0 263 //
michael@0 264 // This cast is ok because in the constructor we created a
michael@0 265 // the actual implementation we wanted, so we know this is
michael@0 266 // a nsDragService. It should be a private interface, though.
michael@0 267 nsDragService * winDragService =
michael@0 268 static_cast<nsDragService *>(mDragService);
michael@0 269 winDragService->SetIDataObject(pIDataSource);
michael@0 270
michael@0 271 // Now process the native drag state and then dispatch the event
michael@0 272 ProcessDrag(NS_DRAGDROP_ENTER, grfKeyState, ptl, pdwEffect);
michael@0 273
michael@0 274 return S_OK;
michael@0 275 }
michael@0 276
michael@0 277 void
michael@0 278 nsNativeDragTarget::AddLinkSupportIfCanBeGenerated(LPDATAOBJECT aIDataSource)
michael@0 279 {
michael@0 280 // If we don't have a link effect, but we can generate one, fix the
michael@0 281 // drop effect to include it.
michael@0 282 if (!(mEffectsAllowed & DROPEFFECT_LINK) && aIDataSource) {
michael@0 283 if (S_OK == ::OleQueryLinkFromData(aIDataSource)) {
michael@0 284 mEffectsAllowed |= DROPEFFECT_LINK;
michael@0 285 }
michael@0 286 }
michael@0 287 }
michael@0 288
michael@0 289 STDMETHODIMP
michael@0 290 nsNativeDragTarget::DragOver(DWORD grfKeyState,
michael@0 291 POINTL ptl,
michael@0 292 LPDWORD pdwEffect)
michael@0 293 {
michael@0 294 if (!mDragService) {
michael@0 295 return E_FAIL;
michael@0 296 }
michael@0 297
michael@0 298 // If a LINK effect could be generated previously from a DragEnter(),
michael@0 299 // then we should include it as an allowed effect.
michael@0 300 mEffectsAllowed = (*pdwEffect) | (mEffectsAllowed & DROPEFFECT_LINK);
michael@0 301
michael@0 302 nsCOMPtr<nsIDragSession> currentDragSession;
michael@0 303 mDragService->GetCurrentSession(getter_AddRefs(currentDragSession));
michael@0 304 if (!currentDragSession) {
michael@0 305 return S_OK; // Drag was canceled.
michael@0 306 }
michael@0 307
michael@0 308 // without the AddRef() |this| can get destroyed in an event handler
michael@0 309 this->AddRef();
michael@0 310
michael@0 311 // Drag and drop image helper
michael@0 312 if (GetDropTargetHelper()) {
michael@0 313 POINT pt = { ptl.x, ptl.y };
michael@0 314 GetDropTargetHelper()->DragOver(&pt, *pdwEffect);
michael@0 315 }
michael@0 316
michael@0 317 mDragService->FireDragEventAtSource(NS_DRAGDROP_DRAG);
michael@0 318 // Now process the native drag state and then dispatch the event
michael@0 319 ProcessDrag(NS_DRAGDROP_OVER, grfKeyState, ptl, pdwEffect);
michael@0 320
michael@0 321 this->Release();
michael@0 322
michael@0 323 return S_OK;
michael@0 324 }
michael@0 325
michael@0 326 STDMETHODIMP
michael@0 327 nsNativeDragTarget::DragLeave()
michael@0 328 {
michael@0 329 if (!mDragService) {
michael@0 330 return E_FAIL;
michael@0 331 }
michael@0 332
michael@0 333 // Drag and drop image helper
michael@0 334 if (GetDropTargetHelper()) {
michael@0 335 GetDropTargetHelper()->DragLeave();
michael@0 336 }
michael@0 337
michael@0 338 // dispatch the event into Gecko
michael@0 339 DispatchDragDropEvent(NS_DRAGDROP_EXIT, gDragLastPoint);
michael@0 340
michael@0 341 nsCOMPtr<nsIDragSession> currentDragSession;
michael@0 342 mDragService->GetCurrentSession(getter_AddRefs(currentDragSession));
michael@0 343
michael@0 344 if (currentDragSession) {
michael@0 345 nsCOMPtr<nsIDOMNode> sourceNode;
michael@0 346 currentDragSession->GetSourceNode(getter_AddRefs(sourceNode));
michael@0 347
michael@0 348 if (!sourceNode) {
michael@0 349 // We're leaving a window while doing a drag that was
michael@0 350 // initiated in a different app. End the drag session, since
michael@0 351 // we're done with it for now (until the user drags back into
michael@0 352 // mozilla).
michael@0 353 mDragService->EndDragSession(false);
michael@0 354 }
michael@0 355 }
michael@0 356
michael@0 357 // release the ref that was taken in DragEnter
michael@0 358 NS_ASSERTION(mTookOwnRef, "want to release own ref, but not taken!");
michael@0 359 if (mTookOwnRef) {
michael@0 360 this->Release();
michael@0 361 mTookOwnRef = false;
michael@0 362 }
michael@0 363
michael@0 364 return S_OK;
michael@0 365 }
michael@0 366
michael@0 367 void
michael@0 368 nsNativeDragTarget::DragCancel()
michael@0 369 {
michael@0 370 // Cancel the drag session if we did DragEnter.
michael@0 371 if (mTookOwnRef) {
michael@0 372 if (GetDropTargetHelper()) {
michael@0 373 GetDropTargetHelper()->DragLeave();
michael@0 374 }
michael@0 375 if (mDragService) {
michael@0 376 mDragService->EndDragSession(false);
michael@0 377 }
michael@0 378 this->Release(); // matching the AddRef in DragEnter
michael@0 379 mTookOwnRef = false;
michael@0 380 }
michael@0 381 }
michael@0 382
michael@0 383 STDMETHODIMP
michael@0 384 nsNativeDragTarget::Drop(LPDATAOBJECT pData,
michael@0 385 DWORD grfKeyState,
michael@0 386 POINTL aPT,
michael@0 387 LPDWORD pdwEffect)
michael@0 388 {
michael@0 389 if (!mDragService) {
michael@0 390 return E_FAIL;
michael@0 391 }
michael@0 392
michael@0 393 mEffectsAllowed = *pdwEffect;
michael@0 394 AddLinkSupportIfCanBeGenerated(pData);
michael@0 395
michael@0 396 // Drag and drop image helper
michael@0 397 if (GetDropTargetHelper()) {
michael@0 398 POINT pt = { aPT.x, aPT.y };
michael@0 399 GetDropTargetHelper()->Drop(pData, &pt, *pdwEffect);
michael@0 400 }
michael@0 401
michael@0 402 // Set the native data object into the drag service
michael@0 403 //
michael@0 404 // This cast is ok because in the constructor we created a
michael@0 405 // the actual implementation we wanted, so we know this is
michael@0 406 // a nsDragService (but it should still be a private interface)
michael@0 407 nsDragService* winDragService = static_cast<nsDragService*>(mDragService);
michael@0 408 winDragService->SetIDataObject(pData);
michael@0 409
michael@0 410 // NOTE: ProcessDrag spins the event loop which may destroy arbitrary objects.
michael@0 411 // We use strong refs to prevent it from destroying these:
michael@0 412 nsRefPtr<nsNativeDragTarget> kungFuDeathGrip = this;
michael@0 413 nsCOMPtr<nsIDragService> serv = mDragService;
michael@0 414
michael@0 415 // Now process the native drag state and then dispatch the event
michael@0 416 ProcessDrag(NS_DRAGDROP_DROP, grfKeyState, aPT, pdwEffect);
michael@0 417
michael@0 418 nsCOMPtr<nsIDragSession> currentDragSession;
michael@0 419 serv->GetCurrentSession(getter_AddRefs(currentDragSession));
michael@0 420 if (!currentDragSession) {
michael@0 421 return S_OK; // DragCancel() was called.
michael@0 422 }
michael@0 423
michael@0 424 // Let the win drag service know whether this session experienced
michael@0 425 // a drop event within the application. Drop will not oocur if the
michael@0 426 // drop landed outside the app. (used in tab tear off, bug 455884)
michael@0 427 winDragService->SetDroppedLocal();
michael@0 428
michael@0 429 // tell the drag service we're done with the session
michael@0 430 // Use GetMessagePos to get the position of the mouse at the last message
michael@0 431 // seen by the event loop. (Bug 489729)
michael@0 432 DWORD pos = ::GetMessagePos();
michael@0 433 POINT cpos;
michael@0 434 cpos.x = GET_X_LPARAM(pos);
michael@0 435 cpos.y = GET_Y_LPARAM(pos);
michael@0 436 winDragService->SetDragEndPoint(nsIntPoint(cpos.x, cpos.y));
michael@0 437 serv->EndDragSession(true);
michael@0 438
michael@0 439 // release the ref that was taken in DragEnter
michael@0 440 NS_ASSERTION(mTookOwnRef, "want to release own ref, but not taken!");
michael@0 441 if (mTookOwnRef) {
michael@0 442 this->Release();
michael@0 443 mTookOwnRef = false;
michael@0 444 }
michael@0 445
michael@0 446 return S_OK;
michael@0 447 }
michael@0 448
michael@0 449 /**
michael@0 450 * By lazy loading mDropTargetHelper we save 50-70ms of startup time
michael@0 451 * which is ~5% of startup time.
michael@0 452 */
michael@0 453 IDropTargetHelper*
michael@0 454 nsNativeDragTarget::GetDropTargetHelper()
michael@0 455 {
michael@0 456 if (!mDropTargetHelper) {
michael@0 457 CoCreateInstance(CLSID_DragDropHelper, nullptr, CLSCTX_INPROC_SERVER,
michael@0 458 IID_IDropTargetHelper, (LPVOID*)&mDropTargetHelper);
michael@0 459 }
michael@0 460
michael@0 461 return mDropTargetHelper;
michael@0 462 }

mercurial