javaruntimes/midp/runtime/src/runtimenative.cpp
branchRCL_3
changeset 19 04becd199f91
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Native methods of the MIDP runtime.
       
    15  *
       
    16 */
       
    17 
       
    18 
       
    19 #include <iostream>
       
    20 #include <fstream>
       
    21 
       
    22 #include "exceptionbase.h"
       
    23 #include "javacommonutils.h"
       
    24 #include "javainifileutils.h"
       
    25 #include "javajniutils.h"
       
    26 #include "javaoslayer.h"
       
    27 #include "logger.h"
       
    28 #include "javauid.h"
       
    29 
       
    30 #include "platformimpl.h"
       
    31 
       
    32 #include "midpruntimeinternalsupport.h"
       
    33 
       
    34 #include "commsclientendpoint.h"
       
    35 #include "commsmessage.h"
       
    36 
       
    37 #include "midpauthenticationmodule.h"
       
    38 
       
    39 #include "platformrequestinterface.h"
       
    40 
       
    41 #include "com_nokia_mj_impl_rt_main_Main.h"
       
    42 #include "com_nokia_mj_impl_rt_midp_DrmUtil.h"
       
    43 #include "com_nokia_mj_impl_rt_midp_MidletLifeCycle.h"
       
    44 #include "com_nokia_mj_impl_rt_midp_MemoryLogger.h"
       
    45 #include "com_nokia_mj_impl_rt_midp_RuntimeErrorDialog.h"
       
    46 #include "javax_microedition_midlet_MIDlet.h"
       
    47 
       
    48 /**
       
    49  * A collection of MIDP runtime native methods.
       
    50  */
       
    51 
       
    52 using namespace java::util;
       
    53 using namespace java::runtime;
       
    54 
       
    55 
       
    56 /**
       
    57  * A method for informing native starter about the UID of the MIDlet. This
       
    58  * is used when UID is received in Java part in pre warm start. The native
       
    59  * starter needs the UID in order to be able to serve AppilcationInfo
       
    60  * native interface.
       
    61  * @param jmuid UID of the MIDlet as String
       
    62  * @param jmsuid UID of the MIDlet suite as String
       
    63  * @param handle A pointer to native starter.
       
    64  */
       
    65 JNIEXPORT
       
    66 void JNICALL Java_com_nokia_mj_impl_rt_midp_MidletLifeCycle__1setUids
       
    67 (JNIEnv* env, jobject /*obj*/, jstring jmuid, jstring jmsuid, jint handle)
       
    68 {
       
    69     JELOG2(EJavaRuntime);
       
    70 
       
    71     // Send the UID to native.
       
    72     Uid midletUid(JniUtils::jstringToWstring(env, jmuid));
       
    73     Uid midletSuiteUid(JniUtils::jstringToWstring(env, jmsuid));
       
    74 
       
    75     try
       
    76     {
       
    77         MidpStarterInternalSupport* starter =
       
    78             reinterpret_cast<MidpStarterInternalSupport*>(handle);
       
    79 
       
    80         starter->setUids(midletUid, midletSuiteUid);
       
    81     }
       
    82 
       
    83     catch (ExceptionBase& ex)
       
    84     {
       
    85         ELOG1(EJavaRuntime,"ERROR in setting UID. ExceptionBase: %s",
       
    86               ex.toString().c_str());
       
    87     }
       
    88     catch (std::exception& e)
       
    89     {
       
    90 
       
    91         ELOG1(EJavaRuntime,"ERROR in setting UID. std::exception: %s",
       
    92               e.what());
       
    93     }
       
    94 }
       
    95 
       
    96 /**
       
    97  * A method for informing native starter that a application* is about to close.
       
    98  * @param handle A pointer to native starter.
       
    99  */
       
   100 void closeIndImpl(jint handle)
       
   101 {
       
   102     JELOG2(EJavaRuntime);
       
   103 //    throw -1;
       
   104     try
       
   105     {
       
   106         MidpStarterInternalSupport* starter =
       
   107             reinterpret_cast<MidpStarterInternalSupport*>(handle);
       
   108 
       
   109         starter->closeRuntimeInd();
       
   110     }
       
   111     catch (ExceptionBase& ex)
       
   112     {
       
   113         ELOG1(EJavaRuntime,"ERROR in close ind. ExceptionBase: %s",
       
   114               ex.toString().c_str());
       
   115     }
       
   116     catch (std::exception& e)
       
   117     {
       
   118 
       
   119         ELOG1(EJavaRuntime,"ERROR in close ind. std::exception: %s",
       
   120               e.what());
       
   121     }
       
   122 }
       
   123 
       
   124 /**
       
   125  * A method for informing native starter that a application* is about to close.
       
   126  * @param handle A pointer to native starter.
       
   127  */
       
   128 JNIEXPORT
       
   129 void JNICALL Java_com_nokia_mj_impl_rt_main_Main__1closeInd
       
   130 (JNIEnv* /*env*/, jclass /*peer*/, jint handle)
       
   131 {
       
   132     JELOG2(EJavaRuntime);
       
   133     closeIndImpl(handle);
       
   134 }
       
   135 
       
   136 /**
       
   137  * A method for informing native starter that a MIDlet is entered into
       
   138  * destroyed state.
       
   139  * @param handle A pointer to native starter.
       
   140  */
       
   141 JNIEXPORT
       
   142 void JNICALL Java_com_nokia_mj_impl_rt_midp_MidletLifeCycle__1closeInd
       
   143 (JNIEnv* /*env*/, jclass /*peer*/, jint handle)
       
   144 {
       
   145     JELOG2(EJavaRuntime);
       
   146     closeIndImpl(handle);
       
   147 }
       
   148 
       
   149 /**
       
   150  * Consume the rigth objects of the MIDlet if needed.
       
   151  * @param uri Uri to the platform request handler.
       
   152  * @param drmContentId Content id of the file.
       
   153  * @param startPhase true if starting the MIDlet, false on closing.
       
   154  * @param handle A handle to platform dependent object. Must be 0 on MIDlet
       
   155  *        start and valid pointer on MIDlet stop. On success start case
       
   156  *        the valid handle is reurned into Java side.
       
   157  * @return Integer object containing valid pointer to native object and
       
   158  *         String containing error string in failure cases.
       
   159  */
       
   160 JNIEXPORT
       
   161 jobject JNICALL Java_com_nokia_mj_impl_rt_midp_DrmUtil__1consumeRights
       
   162 (JNIEnv* env, jclass, jstring uri, jstring drmContentId, jboolean startPhase,
       
   163  jint handle)
       
   164 {
       
   165     JELOG2(EJavaRuntime);
       
   166 
       
   167     std::string status;
       
   168     try
       
   169     {
       
   170         consumeRigthsImpl(JniUtils::jstringToWstring(env, uri),
       
   171                           JniUtils::jstringToWstring(env, drmContentId),
       
   172                           status, startPhase, handle);
       
   173     }
       
   174     catch (ExceptionBase& ex)
       
   175     {
       
   176         ELOG1(EJavaRuntime, "ERROR in PlatformRequest: ExceptionBase: %s",
       
   177               ex.toString().c_str());
       
   178         status = "Internal error";
       
   179     }
       
   180     catch (std::exception& e)
       
   181     {
       
   182         status = "Internal Error";
       
   183         ELOG1(EJavaRuntime,"ERROR in PlatformRequest. std::exception: %s",
       
   184               e.what());
       
   185     }
       
   186     if (status.length() == 0)
       
   187     {
       
   188         jclass integerClass = env->FindClass("java/lang/Integer");
       
   189         if (integerClass != 0)
       
   190         {
       
   191             jmethodID constructor =
       
   192                 env->GetMethodID(integerClass, "<init>", "(I)V");
       
   193             if (integerClass != 0)
       
   194             {
       
   195                 return env->NewObject(integerClass, constructor, handle);
       
   196             }
       
   197             else
       
   198             {
       
   199                 status = "J2ME internal error (DRM 1)";
       
   200             }
       
   201         }
       
   202         else
       
   203         {
       
   204             status = "J2ME internal error (DRM 2)";
       
   205         }
       
   206         ELOG(EJavaRuntime, status.c_str());
       
   207     }
       
   208     return env->NewStringUTF(status.c_str());
       
   209 }
       
   210 
       
   211 /**
       
   212  * A method for informing native starter about the UID of the MIDlet. This
       
   213  * is used when UID is received in Java part in pre warm start. The native
       
   214  * starter needs the UID in order to be able to serve AppilcationInfo
       
   215  * native interface.
       
   216  * @param juid UID of the MIDlet as String
       
   217  * @param handle A pointer to native starter.
       
   218  */
       
   219 JNIEXPORT
       
   220 void JNICALL Java_com_nokia_mj_impl_rt_midp_MidletLifeCycle__1restoreNormalProcessPriority
       
   221 (JNIEnv* /*env*/, jobject /*obj*/)
       
   222 {
       
   223     JELOG2(EJavaRuntime);
       
   224 #ifdef __SYMBIAN32__
       
   225     RProcess proc;
       
   226     proc.SetPriority(EPriorityForeground);
       
   227 #endif // __SYMBIAN32__
       
   228 }
       
   229 /**
       
   230  * Do the platform request.
       
   231  * @param uri Uri to the platform request handler.
       
   232  */
       
   233 JNIEXPORT
       
   234 void JNICALL Java_javax_microedition_midlet_MIDlet__1managePlatformRequest
       
   235 (JNIEnv* env, jobject, jstring uri)
       
   236 {
       
   237     JELOG2(EJavaRuntime);
       
   238 
       
   239     try
       
   240     {
       
   241         std::auto_ptr<PlatformRequestInterface>
       
   242         handler(getPlatformRequestHandlerObj());
       
   243         if (handler.get())
       
   244         {
       
   245             handler->handleUri(JniUtils::jstringToWstring(env,uri));
       
   246         }
       
   247         else
       
   248         {
       
   249             JniUtils::throwNewException(env,
       
   250                                         "javax/microedition/io/ConnectionNotFoundException",
       
   251                                         "URL parsing failed");
       
   252         }
       
   253     }
       
   254     catch (ExceptionBase& ex)
       
   255     {
       
   256         ELOG1(EJavaRuntime, "ERROR in PlatformRequest: ExceptionBase: %s",
       
   257               ex.toString().c_str());
       
   258         if (PlatformRequestInterface::CONNECTION_NOT_SUPPORTED == ex.mErrCode)
       
   259         {
       
   260             JniUtils::throwNewException(env,
       
   261                                         "javax/microedition/io/ConnectionNotFoundException",
       
   262                                         "Unsupported scheme");
       
   263         }
       
   264         else
       
   265         {
       
   266             JniUtils::throwNewException(env,
       
   267                                         "javax/microedition/io/ConnectionNotFoundException",
       
   268                                         ex.toString());
       
   269         }
       
   270     }
       
   271     catch (std::exception& e)
       
   272     {
       
   273         ELOG1(EJavaRuntime,"ERROR in PlatformRequest. std::exception: %s",
       
   274               e.what());
       
   275     }
       
   276 }
       
   277 
       
   278 /**
       
   279  * A utility for writing the heap size into a file.
       
   280  * @param file A file to be written in.
       
   281  * @param heapSize The heap size in bytes.
       
   282  */
       
   283 JNIEXPORT void JNICALL Java_com_nokia_mj_impl_rt_midp_MemoryLogger__1writeFile(
       
   284     JNIEnv* jniEnv, jobject, jstring file, jint heapSize)
       
   285 {
       
   286     JELOG2(EJavaRuntime);
       
   287     try
       
   288     {
       
   289         jboolean iscopy;
       
   290         const char* fileName = jniEnv->GetStringUTFChars(file, &iscopy);
       
   291         LOG2(EJavaRuntime, EInfo, "Writing heap size %d to file %s.", heapSize, fileName);
       
   292 
       
   293         std::ofstream heapFile;
       
   294         heapFile.open(fileName);
       
   295         heapFile << heapSize;
       
   296         heapFile.close();
       
   297         jniEnv->ReleaseStringUTFChars(file, fileName);
       
   298 
       
   299     }
       
   300 
       
   301     catch (ExceptionBase& ex)
       
   302     {
       
   303         ELOG1(EJavaRuntime,"ERROR in write file. ExceptionBase: %s",
       
   304               ex.toString().c_str());
       
   305     }
       
   306     catch (std::exception& e)
       
   307     {
       
   308         ELOG1(EJavaRuntime,"ERROR in write file. std::exception: %s",
       
   309               e.what());
       
   310     }
       
   311 }
       
   312 
       
   313 /**
       
   314  * A utility for reading the heap size from a file.
       
   315  * @param file A file to be read from.
       
   316  * @retrun heapSize The heap size in bytes.
       
   317  */
       
   318 JNIEXPORT jint JNICALL Java_com_nokia_mj_impl_rt_midp_MemoryLogger__1readFile(
       
   319     JNIEnv* jniEnv, jclass, jstring file)
       
   320 {
       
   321     JELOG2(EJavaRuntime);
       
   322     jint heapSize = 0;
       
   323     try
       
   324     {
       
   325         jboolean iscopy;
       
   326         const char* fileName = jniEnv->GetStringUTFChars(file, &iscopy);
       
   327 
       
   328         std::ifstream heapFile;
       
   329         heapFile.open(fileName, std::ifstream::in);
       
   330         heapFile >> heapSize;
       
   331         heapFile.close();
       
   332         jniEnv->ReleaseStringUTFChars(file, fileName);
       
   333         LOG2(EJavaRuntime, EInfo, "Heap size %d read from file %s.", heapSize, fileName);
       
   334     }
       
   335 
       
   336     catch (ExceptionBase& ex)
       
   337     {
       
   338         ELOG1(EJavaRuntime,"ERROR in read file. ExceptionBase: %s",
       
   339               ex.toString().c_str());
       
   340     }
       
   341     catch (std::exception& e)
       
   342     {
       
   343         ELOG1(EJavaRuntime,"ERROR in read file. std::exception: %s",
       
   344               e.what());
       
   345     }
       
   346     return heapSize;
       
   347 }
       
   348 
       
   349 /**
       
   350  * A  utility for getting the stack trace for further processing.
       
   351  * destroyed state.
       
   352  * @param handle A pointer to native starter.
       
   353  */
       
   354 JNIEXPORT void JNICALL
       
   355 Java_com_nokia_mj_impl_rt_midp_RuntimeErrorDialog__1getStackTrace
       
   356 (JNIEnv* jniEnv, jclass, jthrowable th, jobject printStream)
       
   357 {
       
   358     /*
       
   359      * call Throwable.printStackTrace(java.io.PrintStream)
       
   360      * this method is not part of CLDC spec, but it's supported by VM vendors
       
   361      */
       
   362     jclass classThrowable = jniEnv->GetObjectClass(th);
       
   363     jmethodID methodId = jniEnv->GetMethodID(classThrowable, "printStackTrace",
       
   364                          "(Ljava/io/PrintStream;)V");
       
   365     jniEnv->CallVoidMethod(th, methodId, printStream);
       
   366 }
       
   367