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 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         "Z"
 148         ")V");
 149     ASSERT(mid);
 150 
 151     static JGClass clsString(env->FindClass("java/lang/String"));
 152     static JGClass clsObject(env->FindClass("java/lang/Object"));
 153 
 154     Vector<String> mimeTypes(DataTransfer.typesPrivate());
 155     JLObjectArray jmimeTypes(env->NewObjectArray(mimeTypes.size(), clsString, NULL));
 156     JLObjectArray jvalues(env->NewObjectArray(mimeTypes.size(), clsObject, NULL));
 157     CheckAndClearException(env); // OOME
 158 
 159     {
 160         //we are temporary changing DataTransfer security context
 161         //for transfer-to-Java purposes.
 162 
 163         DataTransferAccessPolicy actualJSPolicy = DataTransfer.policy();
 164         DataTransfer.setAccessPolicy(DataTransferAccessPolicy::Readable); //XXX DataTransferReadable);
 165 
 166         int index = 0;
 167         Vector<String>::const_iterator end = mimeTypes.end();
 168         for(Vector<String>::const_iterator i = mimeTypes.begin();
 169             end!=i;
 170             ++i, ++index)
 171         {
 172             String value( DataTransfer.getData(*i) );
 173 
 174             env->SetObjectArrayElement(
 175                 jmimeTypes,
 176                 index,
 177                 (jstring)i->toJavaString(env));
 178 
 179             env->SetObjectArrayElement(
 180                 jvalues,
 181                 index,
 182                 (jstring)value.toJavaString(env));
 183         }
 184 
 185         DataTransfer.setAccessPolicy(actualJSPolicy);
 186     }
 187 
 188     // Attention! [jimage] can be the instance of WCImage or WCImageFrame class.
 189     // The nature of raster is too different to make a conversion inside the native code.
 190     jobject jimage = dragImage.get() && dragImage.get()->javaImage()
 191                   ? jobject(*(dragImage.get()->javaImage())) : nullptr;
 192 
 193     bool isImageSource = dragSourceAction & DragSourceActionImage;
 194 
 195     env->CallVoidMethod(m_webPage, mid, jimage,
 196         eventPos.x() - dragImageOrigin.x(),
 197         eventPos.y() - dragImageOrigin.y(),
 198         eventPos.x(),
 199         eventPos.y(),
 200         jobjectArray(jmimeTypes),
 201         jobjectArray(jvalues),
 202         bool_to_jbool(isImageSource));
 203     CheckAndClearException(env);
 204 }
 205 
 206 } // namespace WebCore