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 #pragma once
  27 
  28 #include <wtf/java/JavaRef.h>
  29 
  30 #include <jni.h>
  31 
  32 extern JavaVM* jvm;
  33 
  34 ALWAYS_INLINE JNIEnv* JNICALL WebCore_GetJavaEnv()
  35 {
  36     void* env;
  37     jvm->GetEnv(&env, JNI_VERSION_1_2);
  38     return (JNIEnv*)env;
  39 }
  40 
  41 #define WC_GETJAVAENV_CHKRET(_env_var, ... /* ret val */)   \
  42     JNIEnv* _env_var = WebCore_GetJavaEnv(); \
  43     if (!_env_var) return __VA_ARGS__;
  44 
  45 extern bool CheckAndClearException(JNIEnv* env);
  46 
  47 namespace WebCore {
  48 
  49 jclass PG_GetFontClass(JNIEnv* env);
  50 jclass PG_GetGlyphBufferClass(JNIEnv* env);
  51 jclass PG_GetFontCustomPlatformDataClass(JNIEnv* env);
  52 jclass PG_GetGraphicsImageDecoderClass(JNIEnv* env);
  53 jclass PG_GetGraphicsContextClass(JNIEnv* env);
  54 jclass PG_GetGraphicsManagerClass(JNIEnv* env);
  55 jclass PG_GetImageClass(JNIEnv* env);
  56 jclass PG_GetImageFrameClass(JNIEnv* env);
  57 jclass PG_GetMediaPlayerClass(JNIEnv* env);
  58 jclass PG_GetPathClass(JNIEnv* env);
  59 jclass PG_GetPathIteratorClass(JNIEnv* env);
  60 jclass PG_GetRectangleClass(JNIEnv* env);
  61 jclass PG_GetRefClass(JNIEnv* env);
  62 jclass PG_GetRenderQueueClass(JNIEnv* env);
  63 jclass PG_GetTransformClass(JNIEnv* env);
  64 jclass PG_GetWebPageClass(JNIEnv* env);
  65 jclass PG_GetColorChooserClass(JNIEnv* env);
  66 
  67 jclass getTimerClass(JNIEnv* env);
  68 
  69 JLObject PL_GetLogger(JNIEnv* env, const char* name);
  70 void PL_ResumeCount(JNIEnv* env, jobject perfLogger, const char* probe);
  71 void PL_SuspendCount(JNIEnv* env, jobject perfLogger, const char* probe);
  72 bool PL_IsEnabled(JNIEnv* env, jobject perfLogger);
  73 
  74 JLObject PL_GetGraphicsManager(JNIEnv* env);
  75 
  76 
  77 //Log wrapper
  78 struct EntryJavaLogger
  79 {
  80     JNIEnv     *m_env;
  81     jobject     m_perfLogger;
  82     const char *m_probe;
  83 
  84     EntryJavaLogger(
  85          JNIEnv *env,
  86          jobject global_perfLogger,
  87          const char* probe
  88     ) : m_env(env)
  89       , m_perfLogger(global_perfLogger)
  90       , m_probe(probe)
  91     {
  92         PL_ResumeCount(m_env, m_perfLogger, m_probe);
  93     }
  94 
  95     ~EntryJavaLogger()
  96     {
  97         PL_SuspendCount(m_env, m_perfLogger, m_probe);
  98     }
  99 };
 100 
 101 
 102 } // namespace WebCore
 103 
 104 namespace WTF {
 105 template<bool daemon> class AttachThreadToJavaEnv {
 106 public:
 107     AttachThreadToJavaEnv()
 108     {
 109         m_status = jvm->GetEnv((void **)&m_env, JNI_VERSION_1_2);
 110         if (m_status == JNI_EDETACHED) {
 111             if (daemon) {
 112                 jvm->AttachCurrentThreadAsDaemon((void **)&m_env, nullptr);
 113             } else {
 114                 jvm->AttachCurrentThread((void **)&m_env, nullptr);
 115             }
 116         }
 117     }
 118 
 119     ~AttachThreadToJavaEnv()
 120     {
 121         if (m_status == JNI_EDETACHED) {
 122             jvm->DetachCurrentThread();
 123         }
 124     }
 125 
 126     JNIEnv* env() { return m_env; }
 127 private:
 128     JNIEnv* m_env;
 129     int m_status;
 130 
 131 };
 132 
 133 using AttachThreadAsDaemonToJavaEnv = AttachThreadToJavaEnv<true>;
 134 using AttachThreadAsNonDaemonToJavaEnv = AttachThreadToJavaEnv<false>;
 135 } // namespace
 136 
 137 //example: LOG_PERF_RECORD(env, "XXXX", "setUpIterator")
 138 //the line
 139 //  com.sun.webkit.perf.XXXX.level = ALL
 140 //have to be added into the file <wk_root>/WebKitBuild/<Debug|Release>/dist/logging.properties
 141 #define LOG_PERF_RECORD(env, LOG_NAME, LOG_RECORD) \
 142     static JGObject __logger__(WebCore::PL_GetLogger(env, LOG_NAME)); \
 143     WebCore::EntryJavaLogger __el__(env, __logger__, LOG_RECORD);
 144 
 145 #define jlong_to_ptr(a) ((void*)(uintptr_t)(a))
 146 #define ptr_to_jlong(a) ((jlong)(uintptr_t)(a))
 147 
 148 #define bool_to_jbool(a) ((a) ? JNI_TRUE : JNI_FALSE)
 149 #define jbool_to_bool(a) (((a) == JNI_TRUE) ? true : false)
 150 
 151 #define JINT_SZ sizeof(jint)
 152 #define JFLOAT_SZ sizeof(jfloat)
 153