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         "Ljava/lang/String;"
 149         ")V");
 150     ASSERT(mid);
 151 
 152     static JGClass clsString(env->FindClass("java/lang/String"));
 153     static JGClass clsObject(env->FindClass("java/lang/Object"));
 154 
 155     Vector<String> mimeTypes(DataTransfer.typesPrivate());
 156     JLObjectArray jmimeTypes(env->NewObjectArray(mimeTypes.size(), clsString, NULL));
 157     JLObjectArray jvalues(env->NewObjectArray(mimeTypes.size(), clsObject, NULL));
 158     CheckAndClearException(env); // OOME
 159 
 160     {
 161         //we are temporary changing DataTransfer security context
 162         //for transfer-to-Java purposes.
 163 
 164         DataTransferAccessPolicy actualJSPolicy = DataTransfer.policy();
 165         DataTransfer.setAccessPolicy(DataTransferAccessPolicy::Readable); //XXX DataTransferReadable);
 166 
 167         int index = 0;
 168         Vector<String>::const_iterator end = mimeTypes.end();
 169         for(Vector<String>::const_iterator i = mimeTypes.begin();
 170             end!=i;
 171             ++i, ++index)
 172         {
 173             String value( DataTransfer.getData(*i) );
 174 
 175             env->SetObjectArrayElement(
 176                 jmimeTypes,
 177                 index,
 178                 (jstring)i->toJavaString(env));
 179 
 180             env->SetObjectArrayElement(
 181                 jvalues,
 182                 index,
 183                 (jstring)value.toJavaString(env));
 184         }
 185 
 186         DataTransfer.setAccessPolicy(actualJSPolicy);
 187     }
 188 
 189     // Attention! [jimage] can be the instance of WCImage or WCImageFrame class.
 190     // The nature of raster is too different to make a conversion inside the native code.
 191     jobject jimage = dragImage.get() && dragImage.get()->javaImage()
 192                   ? jobject(*(dragImage.get()->javaImage())) : nullptr;
 193 
 194     bool isImageSource = dragSourceAction & DragSourceActionImage;
 195 
 196     env->CallVoidMethod(m_webPage, mid, jimage,
 197         eventPos.x() - dragImageOrigin.x(),
 198         eventPos.y() - dragImageOrigin.y(),
 199         eventPos.x(),
 200         eventPos.y(),
 201         jobjectArray(jmimeTypes),
 202         jobjectArray(jvalues),
 203         bool_to_jbool(isImageSource),
 204         (jstring)m_fileExt.toJavaString(env));
 205     CheckAndClearException(env);
 206 }
 207 
 208 void DragClientJava::updateFileExtension(const URL& imageURL)
 209 {
 210     if (imageURL.isNull() || imageURL.isEmpty()) {
 211         m_fileExt = emptyString();
 212         return;
 213     }
 214 
 215     String lastPathComponent = imageURL.lastPathComponent();
 216     String extension;
 217     size_t dotOffset = lastPathComponent.reverseFind('.');
 218     if (dotOffset != notFound)
 219         extension = lastPathComponent.substring(dotOffset + 1);
 220     else
 221         extension = "png"; // Fallback to png if image does not have an extension
 222 
 223     m_fileExt = extension;
 224 }
 225 
 226 } // namespace WebCore