1 /*
   2  * Copyright (c) 2011, 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 #include "config.h"
   5 
   6 #include "JavaEnv.h"
   7 
   8 JavaVM* jvm = 0;
   9 
  10 
  11 jclass dumpRenderTreeClass;
  12 static jmethodID waitUntilDoneMID;
  13 static jmethodID notifyDoneMID;
  14 static jmethodID overridePreferenceMID;
  15 static jmethodID getBackForwardItemCountMID;
  16 static jmethodID clearBackForwardListMID;
  17 static jmethodID resolveURLMID;
  18 static jmethodID loadURLMID;
  19 static jmethodID goBackForwardMID;
  20 
  21 jclass getDumpRenderTreeClass() { return dumpRenderTreeClass; }
  22 jmethodID getWaitUntillDoneMethodId() { return waitUntilDoneMID; }
  23 jmethodID getNotifyDoneMID() { return notifyDoneMID; }
  24 jmethodID getOverridePreferenceMID() { return overridePreferenceMID; }
  25 jmethodID getGetBackForwardItemCountMID() { return getBackForwardItemCountMID; }
  26 jmethodID getClearBackForwardListMID() { return clearBackForwardListMID; }
  27 jmethodID getResolveURLMID() { return resolveURLMID; }
  28 jmethodID getLoadURLMID() { return loadURLMID; }
  29 jmethodID getGoBackForward() { return goBackForwardMID; }
  30 
  31 static void initRefs(JNIEnv* env) {
  32     if (!dumpRenderTreeClass) {
  33         jclass cls =  env->FindClass("com/sun/javafx/webkit/drt/DumpRenderTree");
  34         dumpRenderTreeClass = (jclass)env->NewGlobalRef(cls);
  35         if (JNI_TRUE == env->ExceptionCheck()) {
  36                 env->ExceptionDescribe();
  37                 env->ExceptionClear();
  38                 return;
  39             }
  40         ASSERT(dumpRenderTreeClass);
  41         waitUntilDoneMID = env->GetStaticMethodID(dumpRenderTreeClass, "waitUntilDone", "()V");
  42         ASSERT(waitUntilDoneMID);
  43         notifyDoneMID = env->GetStaticMethodID(dumpRenderTreeClass, "notifyDone", "()V");
  44         ASSERT(notifyDoneMID);
  45         overridePreferenceMID = env->GetStaticMethodID(dumpRenderTreeClass, "overridePreference", "(Ljava/lang/String;Ljava/lang/String;)V");
  46         ASSERT(overridePreferenceMID);
  47         getBackForwardItemCountMID = env->GetStaticMethodID(dumpRenderTreeClass, "getBackForwardItemCount", "()I");
  48         ASSERT(getBackForwardItemCountMID);
  49         clearBackForwardListMID = env->GetStaticMethodID(dumpRenderTreeClass, "clearBackForwardList", "()V");
  50         ASSERT(clearBackForwardListMID);
  51         resolveURLMID = env->GetStaticMethodID(dumpRenderTreeClass, "resolveURL", "(Ljava/lang/String;)Ljava/lang/String;");
  52         ASSERT(resolveURLMID);
  53         loadURLMID = env->GetStaticMethodID(dumpRenderTreeClass, "loadURL", "(Ljava/lang/String;)V");
  54         ASSERT(loadURLMID);
  55         goBackForwardMID = env->GetStaticMethodID(dumpRenderTreeClass, "goBackForward", "(I)V");
  56         ASSERT(goBackForwardMID);
  57     }
  58 }
  59 
  60 
  61 JNIEnv* JNICALL DumpRenderTree_GetJavaEnv()
  62 {
  63     void* env;
  64     jvm->GetEnv(&env, JNI_VERSION_1_2);
  65     return (JNIEnv*)env;
  66 }
  67 
  68 bool CheckAndClearException(JNIEnv* env)
  69 {
  70     if (JNI_TRUE == env->ExceptionCheck()) {
  71         env->ExceptionDescribe();
  72         env->ExceptionClear();
  73         return true;
  74     }
  75     return false;
  76 }
  77 
  78 #ifdef __cplusplus
  79 extern "C" {
  80 #endif
  81 
  82 JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
  83 {
  84     jvm = vm;
  85     JNIEnv *env;
  86     if (jvm->GetEnv((void **)&env, JNI_VERSION_1_2)) {
  87         fprintf(stderr, "DumpRenderTree::JNI_OnLoad() failed \n");
  88              return JNI_ERR; /* JNI version not supported */
  89          }
  90      initRefs(env);
  91     return JNI_VERSION_1_2;
  92 }
  93 
  94 JNIEXPORT void JNICALL JNI_OnUnLoad(JavaVM* vm, void* reserved)
  95 {
  96     JNIEnv *env = DumpRenderTree_GetJavaEnv();
  97     env->DeleteGlobalRef(dumpRenderTreeClass);
  98     jvm = 0;
  99 }
 100 
 101 #if OS(WINDOWS)
 102 #include <Windows.h>
 103 #include <math.h>
 104 
 105 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
 106 {
 107     if (fdwReason == DLL_PROCESS_ATTACH) {
 108 #if defined(_MSC_VER) && _MSC_VER >= 1800 && _MSC_VER < 1900 && defined(_M_X64) || defined(__x86_64__)
 109         // The VS2013 runtime has a bug where it mis-detects AVX-capable processors
 110         // if the feature has been disabled in firmware. This causes us to crash
 111         // in some of the math functions. For now, we disable those optimizations
 112         // because Microsoft is not going to fix the problem in VS2013.
 113         // FIXME: Remove this workaround when we switch to VS2015+.
 114         _set_FMA3_enable(0);
 115 #endif
 116     }
 117 
 118     return TRUE;
 119 }
 120 #endif
 121 
 122 #ifdef __cplusplus
 123 }
 124 #endif