1 /*
   2  * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 #import "sun_lwawt_macosx_CDropTargetContextPeer.h"
  27 
  28 #import "CDataTransferer.h"
  29 #import "CDropTarget.h"
  30 #import "DnDUtilities.h"
  31 #import "ThreadUtilities.h"
  32 #import "JNIUtilities.h"
  33 
  34 jclass jc_CDropTargetContextPeer = NULL;
  35 #define GET_DTCP_CLASS() \
  36     GET_CLASS(jc_CDropTargetContextPeer, "sun/lwawt/macosx/CDropTargetContextPeer");
  37 
  38 #define GET_DTCP_CLASS_RETURN(ret) \
  39     GET_CLASS_RETURN(jc_CDropTargetContextPeer, "sun/lwawt/macosx/CDropTargetContextPeer", ret);
  40 
  41 
  42 static void TransferFailed(JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jdroptransfer, jlong jformat) {
  43     AWT_ASSERT_NOT_APPKIT_THREAD;
  44     GET_DTCP_CLASS();
  45     DECLARE_METHOD(transferFailedMethod, jc_CDropTargetContextPeer, "transferFailed", "(J)V");
  46     (*env)->CallVoidMethod(env, jthis, transferFailedMethod, jformat); // AWT_THREADING Safe (!appKit)
  47     CHECK_EXCEPTION();
  48 }
  49 
  50 static CDropTarget* GetCDropTarget(jlong jdroptarget) {
  51     CDropTarget* dropTarget = (CDropTarget*) jlong_to_ptr(jdroptarget);
  52 
  53     // Make sure the drop target is of the right kind:
  54     if ([dropTarget isKindOfClass:[CDropTarget class]]) {
  55         return dropTarget;
  56     }
  57 
  58     return nil;
  59 }
  60 
  61 
  62 /*
  63  * Class:     sun_lwawt_macosx_CDropTargetContextPeer
  64  * Method:    startTransfer
  65  * Signature: (JJ)J
  66  */
  67 JNIEXPORT jlong JNICALL Java_sun_lwawt_macosx_CDropTargetContextPeer_startTransfer
  68   (JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jformat)
  69 {
  70 
  71     jlong result = (jlong) 0L;
  72 
  73     // Currently startTransfer and endTransfer are synchronous since [CDropTarget copyDraggingDataForFormat]
  74     // works off a data copy and doesn't have to go to the native event thread to get the data.
  75     // We can have endTransfer just call startTransfer.
  76 
  77 JNI_COCOA_ENTER(env);
  78     // Get the drop target native object:
  79     CDropTarget* dropTarget = GetCDropTarget(jdroptarget);
  80     if (dropTarget == nil) {
  81         DLog2(@"[CDropTargetContextPeer startTransfer]: GetCDropTarget failed for %d.\n", (NSInteger) jdroptarget);
  82         TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);
  83         return result;
  84     }
  85 
  86     GET_DTCP_CLASS_RETURN(result);
  87     DECLARE_METHOD_RETURN(newDataMethod, jc_CDropTargetContextPeer, "newData", "(J[B)V", result);
  88     if ((*env)->ExceptionOccurred(env) || !newDataMethod) {
  89         DLog2(@"[CDropTargetContextPeer startTransfer]: couldn't get newData method for %d.\n", (NSInteger) jdroptarget);
  90         TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);
  91         return result;
  92     }
  93 
  94     // Get data from drop target:
  95     jobject jdropdata = [dropTarget copyDraggingDataForFormat:jformat];
  96     if (!jdropdata) {
  97         DLog2(@"[CDropTargetContextPeer startTransfer]: copyDraggingDataForFormat failed for %d.\n", (NSInteger) jdroptarget);
  98         TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);
  99         return result;
 100     }
 101 
 102     // Pass the data to drop target:
 103     @try {
 104         (*env)->CallVoidMethod(env, jthis, newDataMethod, jformat, jdropdata); // AWT_THREADING Safe (!appKit)
 105     } @catch (NSException *ex) {
 106         DLog2(@"[CDropTargetContextPeer startTransfer]: exception in newData() for %d.\n", (NSInteger) jdroptarget);
 107         (*env)->DeleteGlobalRef(env, jdropdata);
 108         TransferFailed(env, jthis, jdroptarget, (jlong) 0L, jformat);
 109         return result;
 110     }
 111 
 112     // if no error return dropTarget's draggingSequence
 113     result = [dropTarget getDraggingSequenceNumber];
 114 JNI_COCOA_EXIT(env);
 115 
 116     return result;
 117 }
 118 
 119 /*
 120  * Class:     sun_lwawt_macosx_CDropTargetContextPeer
 121  * Method:    addTransfer
 122  * Signature: (JJJ)V
 123  */
 124 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CDropTargetContextPeer_addTransfer
 125   (JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jdroptransfer, jlong jformat)
 126 {
 127     // Currently startTransfer and endTransfer are synchronous since [CDropTarget copyDraggingDataForFormat]
 128     // works off a data copy and doesn't have to go to the native event thread to get the data.
 129     // We can have endTransfer just call startTransfer.
 130 
 131     Java_sun_lwawt_macosx_CDropTargetContextPeer_startTransfer(env, jthis, jdroptarget, jformat);
 132 
 133     return;
 134 }
 135 
 136 /*
 137  * Class:     sun_lwawt_macosx_CDropTargetContextPeer
 138  * Method:    dropDone
 139  * Signature: (JJZZI)V
 140  */
 141 JNIEXPORT void JNICALL Java_sun_lwawt_macosx_CDropTargetContextPeer_dropDone
 142   (JNIEnv *env, jobject jthis, jlong jdroptarget, jlong jdroptransfer, jboolean jislocal, jboolean jsuccess, jint jdropaction)
 143 {
 144     // Get the drop target native object:
 145 JNI_COCOA_ENTER(env);
 146     CDropTarget* dropTarget = GetCDropTarget(jdroptarget);
 147     if (dropTarget == nil) {
 148         DLog2(@"[CDropTargetContextPeer dropDone]: GetCDropTarget failed for %d.\n", (NSInteger) jdroptarget);
 149         return;
 150     }
 151 
 152     // Notify drop target Java is all done with this dragging sequence:
 153     [dropTarget javaDraggingEnded:(jlong)jdroptransfer success:jsuccess action:jdropaction];
 154 JNI_COCOA_EXIT(env);
 155 
 156     return;
 157 }