javaruntimes/midp/runtime/src.s60/platformrequesthandler.cpp
branchRCL_3
changeset 14 04becd199f91
child 17 0fd27995241b
equal deleted inserted replaced
13:f5050f1da672 14: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:  This class provides container for message.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <memory>
       
    20 #include <schemehandler.h>        // SchemeHandler
       
    21 #include <apgcli.h>               // for RApaLsSession
       
    22 #include <apacmdln.h>
       
    23 #include <sstream>
       
    24 
       
    25 #include "platformrequesthandler.h"
       
    26 
       
    27 #include "javasymbianoslayer.h"
       
    28 #include "javacommonutils.h"
       
    29 #include "s60commonutils.h"
       
    30 #include "exceptionbase.h"
       
    31 #include "logger.h"
       
    32 
       
    33 using namespace java::runtime;
       
    34 using namespace java::util;
       
    35 
       
    36 
       
    37 PlatformRequestInterface* getPlatformRequestHandlerObj()
       
    38 {
       
    39     JELOG2(EJavaRuntime);
       
    40     return new PlatformRequestHandler();
       
    41 }
       
    42 
       
    43 /**
       
    44  *
       
    45  */
       
    46 PlatformRequestHandler::PlatformRequestHandler()
       
    47 {
       
    48 }
       
    49 
       
    50 /**
       
    51  *
       
    52  */
       
    53 PlatformRequestHandler::~PlatformRequestHandler()
       
    54 {
       
    55 }
       
    56 
       
    57 /**
       
    58  * handleUri() supports at least following URIs:
       
    59  * file://, http://, https://, rtsp://, mailto://, wtai:, tel:, vtel:,
       
    60  * mms:, cti:, javaapp: and localapp:
       
    61  */
       
    62 void PlatformRequestHandler::handleUri(const std::wstring& aUri)
       
    63 {
       
    64     JELOG2(EJavaRuntime);
       
    65     TRAPD(err,launchAppL(aUri););
       
    66     if (err != KErrNone)
       
    67     {
       
    68         ELOG1(EJavaRuntime, "ERROR!!! PlatformRequestHandler::handleUri() %d",
       
    69               err);
       
    70         if (KErrNotFound == err)
       
    71         {
       
    72             if ((aUri.find(L"localapp:jam/launch?") == 0) ||
       
    73                     (aUri.find(L"localapp://jam/launch?") == 0) ||
       
    74                     (aUri.find(L"javaapp:") == 0))
       
    75             {
       
    76                 // The URI is supported but the MIDlet specified by the URI
       
    77                 // does not exist.
       
    78                 throw ExceptionBase(PlatformRequestInterface::UNEXPECTED_ERROR,
       
    79                                     "Cannot start Java application",__FILE__,__FUNCTION__,__LINE__);
       
    80             }
       
    81 
       
    82             throw ExceptionBase(PlatformRequestInterface::CONNECTION_NOT_SUPPORTED,
       
    83                                 "Not supported URI",__FILE__,__FUNCTION__,__LINE__);
       
    84         }
       
    85         else
       
    86         {
       
    87             std::string errTxt("Execution PlatformRequestHandler::handleUri() "
       
    88                                "failed to following error: ");
       
    89             errTxt.append(java::util::JavaCommonUtils::intToString(err));
       
    90             throw ExceptionBase(PlatformRequestInterface::UNEXPECTED_ERROR,
       
    91                                 errTxt,__FILE__,__FUNCTION__,__LINE__);
       
    92         }
       
    93     }
       
    94 }
       
    95 
       
    96 /**
       
    97  *
       
    98  */
       
    99 void PlatformRequestHandler::launchAppL(const std::wstring& aUri)
       
   100 {
       
   101     JELOG2(EJavaRuntime);
       
   102     LOG1(EJavaRuntime, EInfo, "Platform request. URI: %S", aUri.c_str());
       
   103     if (startsArbitraryNativeApp(aUri))
       
   104     {
       
   105         launchNativeAppL(aUri);
       
   106     }
       
   107     else
       
   108     {
       
   109         TPtrC ptr((const TUint16 *)aUri.c_str(), aUri.length());
       
   110         std::auto_ptr<CSchemeHandler> schemeHandler(CSchemeHandler::NewL(ptr));
       
   111         schemeHandler->HandleUrlStandaloneL(); // Process Uri in standalone mode.
       
   112     }
       
   113 }
       
   114 
       
   115 /**
       
   116  *
       
   117  */
       
   118 bool PlatformRequestHandler::startsArbitraryNativeApp(const std::wstring& aUri)
       
   119 {
       
   120     if (aUri.find(L"nativeapp:") == 0)
       
   121     {
       
   122         return true;
       
   123     }
       
   124 
       
   125     if (aUri.find(L"localapp:native/launch?") == 0 ||
       
   126             aUri.find(L"localapp://native/launch?") == 0)
       
   127     {
       
   128         return true;
       
   129     }
       
   130 
       
   131     return false;
       
   132 }
       
   133 
       
   134 /**
       
   135  *
       
   136  */
       
   137 void PlatformRequestHandler::launchNativeAppL(const std::wstring& aUri)
       
   138 {
       
   139     std::wstring appName;
       
   140 
       
   141     RApaLsSession apaSession;
       
   142     TInt err = apaSession.Connect();
       
   143     User::LeaveIfError(err);
       
   144     CleanupClosePushL(apaSession);
       
   145 
       
   146     // Support percent encoding in the URI
       
   147     std::wstring uri = JavaCommonUtils::percentDecode(aUri);
       
   148 
       
   149     // Start by by Uid?
       
   150     const std::wstring appUid(L"application-uid=");
       
   151     const std::wstring appExe(L"application-exe=");
       
   152     const std::wstring semiColon(L";");
       
   153     std::string::size_type idx = uri.find(appUid);
       
   154     std::string::size_type idxSemiColon;
       
   155     if (idx != std::string::npos)
       
   156     {
       
   157         // Parse Uid
       
   158         TUid uid;
       
   159         idx += appUid.size(); // skip argument name
       
   160         idxSemiColon = uri.find(semiColon, idx);
       
   161         if (idxSemiColon == std::string::npos)
       
   162         {
       
   163             idxSemiColon = uri.size();
       
   164         }
       
   165         std::wstring wstrUid = uri.substr(idx, (idxSemiColon - idx));
       
   166 
       
   167         LOG1(EJavaRuntime, EInfo,
       
   168              "PlatformRequestHandler: launchNativeAppL: Uid in req is : %S", wstrUid.c_str());
       
   169 
       
   170         long long tmpInt;
       
   171         std::wstringstream stream(wstrUid);
       
   172         if (wstrUid.find(L"0x") == 0 || wstrUid.find(L"0X") == 0)
       
   173         {
       
   174             // hexadecimal
       
   175             stream >> std::hex >> tmpInt;
       
   176         }
       
   177         else
       
   178         {
       
   179             stream >> tmpInt;
       
   180         }
       
   181         uid.iUid = tmpInt;
       
   182 
       
   183         // Find executable name from AppArc
       
   184         TApaAppInfo info;
       
   185         err = apaSession.GetAppInfo(info, uid);
       
   186         if (KErrNone != err)
       
   187         {
       
   188             ELOG2(EJavaRuntime,
       
   189                   "PlatformRequestHandler: launchNativeAppL: GetAppInfo for uid %x failed, err %d",
       
   190                   uid.iUid,
       
   191                   err);
       
   192             User::Leave(err);
       
   193         }
       
   194         info.iFullName.Append('\0');
       
   195         appName = (wchar_t *)(&(info.iFullName[0]));
       
   196         LOG1(EJavaRuntime, EInfo,
       
   197              "PlatformRequestHandler: launchNativeAppL: app to start is : %S", appName.c_str());
       
   198     }
       
   199     else if ((idx = uri.find(appExe)) != std::string::npos)
       
   200     {
       
   201         // Parse exe name
       
   202         idx += appExe.size(); // skip argument name
       
   203         idxSemiColon = uri.find(semiColon, idx);
       
   204         if (idxSemiColon == std::string::npos)
       
   205         {
       
   206             idxSemiColon = uri.size();
       
   207         }
       
   208         appName = uri.substr(idx, (idxSemiColon - idx));
       
   209 
       
   210         LOG1(EJavaRuntime, EInfo,
       
   211              "PlatformRequestHandler: launchNativeAppL: Exe name in req is : %S", appName.c_str());
       
   212     }
       
   213     else
       
   214     {
       
   215         // Illegal uri
       
   216         User::Leave(KErrArgument);
       
   217     }
       
   218 
       
   219     // Start forming command line
       
   220     CApaCommandLine* cmdLine = CApaCommandLine::NewLC();
       
   221 
       
   222     // Set executable name
       
   223     HBufC *pAppName = S60CommonUtils::wstringToDes(appName.c_str());
       
   224     if (pAppName)
       
   225     {
       
   226         CleanupStack::PushL(pAppName);
       
   227         cmdLine->SetExecutableNameL(*pAppName);
       
   228         CleanupStack::PopAndDestroy(pAppName);
       
   229     }
       
   230     cmdLine->SetCommandL(EApaCommandRun);
       
   231 
       
   232     // Content (document) specified?
       
   233     const std::wstring contentArg(L"content=");
       
   234     if ((idx = uri.find(contentArg)) != std::string::npos)
       
   235     {
       
   236         // Parse document name
       
   237         idx += contentArg.size(); // skip argument name
       
   238         idxSemiColon = uri.find(semiColon, idx);
       
   239         if (idxSemiColon == std::string::npos)
       
   240         {
       
   241             idxSemiColon = uri.size();
       
   242         }
       
   243         std::wstring content = uri.substr(idx, (idxSemiColon - idx));
       
   244 
       
   245         LOG1(EJavaRuntime, EInfo,
       
   246              "PlatformRequestHandler: launchNativeAppL: content: %S", content.c_str());
       
   247 
       
   248         HBufC *pContent = S60CommonUtils::wstringToDes(content.c_str());
       
   249         if (pContent)
       
   250         {
       
   251             CleanupStack::PushL(pContent);
       
   252             cmdLine->SetDocumentNameL(*pContent);
       
   253             CleanupStack::PopAndDestroy(pContent);
       
   254         }
       
   255     }
       
   256 
       
   257     // Other application arguments specified?
       
   258     const std::wstring appArgs(L"application-args=");
       
   259     if ((idx = uri.find(appArgs)) != std::string::npos)
       
   260     {
       
   261         // Parse applications args
       
   262         idx += appArgs.size(); // skip argument name
       
   263         // Rest of the command URI contains args
       
   264         idxSemiColon = uri.size();
       
   265         std::wstring args = uri.substr(idx, (idxSemiColon - idx));
       
   266 
       
   267         LOG1(EJavaRuntime, EInfo,
       
   268              "PlatformRequestHandler: launchNativeAppL: app args: %S", args.c_str());
       
   269 
       
   270         if (args.size() > 0)
       
   271         {
       
   272             // Convert args to 8 bit descriptor
       
   273             char* utf8string = NULL;
       
   274             try
       
   275             {
       
   276                 utf8string = JavaCommonUtils::wstringToUtf8(args);
       
   277             }
       
   278             catch (std::exception& e)
       
   279             {
       
   280                 ELOG1(EJavaRuntime,
       
   281                       "PlatformRequestHandler: launchNativeAppL: Cannot convert app args to"
       
   282                       " UTF-8. Error %s", e.what());
       
   283                 User::Leave(KErrArgument);
       
   284             }
       
   285             TInt argsLen = strlen(utf8string);
       
   286             TPtr8 ptrArgs((unsigned char *)utf8string, argsLen, argsLen + 4);
       
   287 
       
   288             cmdLine->SetTailEndL(ptrArgs);
       
   289             delete[] utf8string;
       
   290         }
       
   291     }
       
   292 
       
   293     // Start executable
       
   294     err = apaSession.StartApp(*cmdLine);
       
   295     if (KErrNone != err)
       
   296     {
       
   297         ELOG1(EJavaRuntime,
       
   298               "PlatformRequestHandler: launchNativeAppL: StartApp error %d", err);
       
   299         User::Leave(err);
       
   300     }
       
   301 
       
   302     CleanupStack::PopAndDestroy(cmdLine);
       
   303     CleanupStack::PopAndDestroy(); // apaSession
       
   304 }