1 /*
   2  * Copyright (c) 2011, 2017, 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 #include "config.h"
  27 
  28 #include "NotImplemented.h"
  29 
  30 #include "Image.h"
  31 #include "CachedImage.h"
  32 #include "DragActions.h"
  33 #include "DragClient.h"
  34 #include <wtf/Vector.h>
  35 #include <wtf/text/WTFString.h>
  36 
  37 #include <wtf/java/JavaEnv.h>
  38 #include "DragClientJava.h"
  39 
  40 namespace WebCore {
  41 
  42 // ---- DragImage.h ---- //
  43 IntSize dragImageSize(DragImageRef pr)
  44 {
  45     return pr ? roundedIntSize(pr->size()) : IntSize();
  46 }
  47 
  48 DragImageRef scaleDragImage(DragImageRef pr, FloatSize)
  49 {
  50     //TODO: pass to java
  51     notImplemented();
  52     return pr;
  53 }
  54 
  55 DragImageRef dissolveDragImageToFraction(DragImageRef pr, float)
  56 {
  57     //TODO: pass to java
  58     notImplemented();
  59     return pr;
  60 }
  61 
  62 DragImageRef createDragImageFromImage(Image* img, ImageOrientationDescription)
  63 {
  64     return img;
  65 }
  66 
  67 DragImageRef createDragImageIconForCachedImage(CachedImage *cimg)
  68 {
  69     if (cimg->hasImage()) return nullptr;
  70     return createDragImageFromImage(cimg->image(), ImageOrientationDescription(RespectImageOrientation)); // todo tav valid orientation?
  71 }
  72 
  73 void deleteDragImage(DragImageRef)
  74 {
  75     // Since DragImageRef is a RefPtr, there's nothing additional we need to do to
  76     // delete it. It will be released when it falls out of scope.
  77 }
  78 
  79 DragImageRef createDragImageIconForCachedImageFilename(const String&)
  80 {
  81     return 0;
  82 }
  83 
  84 
  85 DragClientJava::DragClientJava(const JLObject &webPage)
  86     : m_webPage(webPage)
  87 {
  88 }
  89 
  90 DragClientJava::~DragClientJava()
  91 {
  92 }
  93 
  94 void DragClientJava::dragControllerDestroyed()
  95 {
  96     delete this;
  97 }
  98 
  99 void DragClientJava::willPerformDragDestinationAction(
 100     DragDestinationAction,
 101     const DragData&)
 102 {
 103     notImplemented();
 104 }
 105 
 106 void DragClientJava::willPerformDragSourceAction(
 107     DragSourceAction,
 108     const IntPoint&,
 109     DataTransfer&)
 110 {
 111     notImplemented();
 112 }
 113 
 114 DragDestinationAction DragClientJava::actionMaskForDrag(const DragData&)
 115 {
 116     //TODO: check input element and produce correct respond
 117     notImplemented();
 118     return DragDestinationActionAny;
 119 }
 120 
 121 //We work in window rather than view coordinates here
 122 DragSourceAction DragClientJava::dragSourceActionMaskForPoint(const IntPoint&)
 123 {
 124     //TODO: check input element and produce correct respond
 125     notImplemented();
 126     return DragSourceActionAny;
 127 }
 128 
 129 void DragClientJava::startDrag(
 130     DragImage dragImage,
 131     const IntPoint& dragImageOrigin,
 132     const IntPoint& eventPos,
 133     const FloatPoint&,
 134     DataTransfer& DataTransfer,
 135     Frame&,
 136     DragSourceAction)
 137 {
 138     JNIEnv* env = WebCore_GetJavaEnv();
 139     static jmethodID mid = env->GetMethodID(
 140         PG_GetWebPageClass(env),
 141         "fwkStartDrag", "("
 142         "Ljava/lang/Object;"
 143         "II"
 144         "II"
 145         "[Ljava/lang/String;"
 146         "[Ljava/lang/Object;"
 147         ")V");
 148     ASSERT(mid);
 149 
 150     static JGClass clsString(env->FindClass("java/lang/String"));
 151     static JGClass clsObject(env->FindClass("java/lang/Object"));
 152 
 153     Vector<String> mimeTypes(DataTransfer.typesPrivate());
 154     JLObjectArray jmimeTypes(env->NewObjectArray(mimeTypes.size(), clsString, NULL));
 155     JLObjectArray jvalues(env->NewObjectArray(mimeTypes.size(), clsObject, NULL));
 156     CheckAndClearException(env); // OOME
 157 
 158     {
 159         //we are temporary changing DataTransfer security context
 160         //for transfer-to-Java purposes.
 161 
 162         DataTransferAccessPolicy actualJSPolicy = DataTransfer.policy();
 163         DataTransfer.setAccessPolicy(DataTransferAccessPolicy::Readable); //XXX DataTransferReadable);
 164 
 165         int index = 0;
 166         Vector<String>::const_iterator end = mimeTypes.end();
 167         for(Vector<String>::const_iterator i = mimeTypes.begin();
 168             end!=i;
 169             ++i, ++index)
 170         {
 171             String value( DataTransfer.getData(*i) );
 172 
 173             env->SetObjectArrayElement(
 174                 jmimeTypes,
 175                 index,
 176                 (jstring)i->toJavaString(env));
 177 
 178             env->SetObjectArrayElement(
 179                 jvalues,
 180                 index,
 181                 (jstring)value.toJavaString(env));
 182         }
 183 
 184         DataTransfer.setAccessPolicy(actualJSPolicy);
 185     }
 186 
 187     // Attention! [jimage] can be the instance of WCImage or WCImageFrame class.
 188     // The nature of raster is too different to make a conversion inside the native code.
 189     jobject jimage = dragImage.get() && dragImage.get()->javaImage()
 190                   ? jobject(*(dragImage.get()->javaImage())) : nullptr;
 191 
 192     env->CallVoidMethod(m_webPage, mid, jimage,
 193         eventPos.x() - dragImageOrigin.x(),
 194         eventPos.y() - dragImageOrigin.y(),
 195         eventPos.x(),
 196         eventPos.y(),
 197         jobjectArray(jmimeTypes),
 198         jobjectArray(jvalues) );
 199     CheckAndClearException(env);
 200 }
 201 
 202 } // namespace WebCore