javamanager/javainstaller/javasifplugin/src/resultsserver.cpp
changeset 21 2a9601315dfc
child 35 85266cc22c7f
child 60 6c158198356e
equal deleted inserted replaced
18:e8e63152f320 21:2a9601315dfc
       
     1 /*
       
     2 * Copyright (c) 2009-2010 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 the License "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:  Comms server,
       
    15 *    part of Java platform 2.0 javarestoreconverter process
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 #include <iostream>
       
    21 #include <unistd.h>
       
    22 
       
    23 #include "comms.h"
       
    24 #include "javasymbianoslayer.h"
       
    25 #include "logger.h"
       
    26 #include "resultsserver.h"
       
    27 
       
    28 using namespace java::comms;
       
    29 using namespace std;
       
    30 
       
    31 ResultsServer::ResultsServer(COpaqueNamedParams& aResults, CComponentInfo& aInfo) :
       
    32         iResults(aResults), iInfo(aInfo)
       
    33 {
       
    34 }
       
    35 
       
    36 ResultsServer::~ResultsServer()
       
    37 {
       
    38     // Stop server if running
       
    39     stop();
       
    40 
       
    41     iIntPairs.clear();
       
    42     iStringPairs.clear();
       
    43 }
       
    44 
       
    45 int ResultsServer::start()
       
    46 {
       
    47     iRunning = 1;
       
    48     iComms.registerDefaultListener(this);
       
    49     return iComms.start(IPC_ADDRESS_JAVA_SIF_PLUGIN_C);
       
    50 }
       
    51 
       
    52 int ResultsServer::stop()
       
    53 {
       
    54     if (iRunning > 0)
       
    55     {
       
    56         iRunning = 0;
       
    57         iComms.unregisterDefaultListener(this);
       
    58         return iComms.stop();
       
    59     }
       
    60     else
       
    61     {
       
    62         return 0;
       
    63     }
       
    64 }
       
    65 
       
    66 /**
       
    67  * Communicates with Java Installer. The following messages are used.
       
    68  *
       
    69  * Message Name        Id      Contents
       
    70  *
       
    71  *
       
    72  * Installer Result    601     message := length named_int_value* length named_string_value*
       
    73  *                             length := <int telling how many entries the following table has>
       
    74  *                             named_int_value := name int_value
       
    75  *                             named_string_value := name string_value
       
    76  *                             name := <string>
       
    77  *                             int_value := <int>
       
    78  *                             string_value := <string>
       
    79  * Installer Result
       
    80  *   Response          602     int response    (always 0)
       
    81  *
       
    82  *
       
    83  */
       
    84 void ResultsServer::processMessage(CommsMessage& aMessage)
       
    85 {
       
    86     TInt err = KErrNone;
       
    87 
       
    88     switch (aMessage.getMessageId())
       
    89     {
       
    90         case INSTALLER_RESULT_MESSAGE_ID:
       
    91         {
       
    92             int int_table_len;
       
    93             int string_table_len;
       
    94             int int_value;
       
    95             std::wstring name;
       
    96             std::wstring value;
       
    97 
       
    98             // Read raw message data
       
    99             aMessage >> int_table_len;
       
   100             for (int idx = 0; idx < int_table_len; idx++)
       
   101             {
       
   102                 aMessage >> name >> int_value;
       
   103                 iIntPairs.insert(std::make_pair(name, int_value));
       
   104             }
       
   105 
       
   106             aMessage >> string_table_len;
       
   107             for (int idx = 0; idx < string_table_len; idx++)
       
   108             {
       
   109                 aMessage >> name >> value;
       
   110                 iStringPairs.insert(std::make_pair(name, value));
       
   111             }
       
   112 
       
   113             // Which operation the Java Installer has executed
       
   114             int operation = iIntPairs[L"operation"];
       
   115             // result is KErrNone if the operation succeeded
       
   116             int result = iIntPairs[L"result"];
       
   117 
       
   118             if (KErrNone != result)
       
   119             {
       
   120                 // return common error information
       
   121                 TRAP(err, iResults.AddIntL(KSifOutParam_ExtendedErrCode, result));
       
   122                 if (KErrNone != err)
       
   123                 {
       
   124                     ELOG1(EJavaInstaller,
       
   125                         "ResultsServer::processMessage iResults.AddIntL ExtendedErrCode err %d",
       
   126                         err);
       
   127                 }
       
   128 
       
   129                 TRAP(err, iResults.AddIntL(KSifOutParam_ErrCode, result));
       
   130                 if (KErrNone != err)
       
   131                 {
       
   132                     ELOG1(EJavaInstaller,
       
   133                         "ResultsServer::processMessage iResults.AddIntL ErrCode err %d", err);
       
   134                 }
       
   135 
       
   136                 TRAP(err, iResults.AddIntL(
       
   137                     KSifOutParam_ErrCategory, iIntPairs[L"error-category"]));
       
   138                 if (KErrNone != err)
       
   139                 {
       
   140                     ELOG1(EJavaInstaller,
       
   141                         "ResultsServer::processMessage iResults.AddIntL ErrCategory err %d",
       
   142                         err);
       
   143                 }
       
   144 
       
   145                 HBufC *message = wstringToBuf(iStringPairs[L"error-message"]);
       
   146                 if (message == NULL)
       
   147                 {
       
   148                     ELOG(EJavaInstaller,
       
   149                           "ResultsServer::processMessage iResults.wstringToBuf returned NULL ");
       
   150                 }
       
   151                 else
       
   152                 {
       
   153                     TRAP(err, iResults.AddStringL(KSifOutParam_ErrMessage, *message));
       
   154                     if (KErrNone != err)
       
   155                     {
       
   156                         ELOG1(EJavaInstaller,
       
   157                             "ResultsServer::processMessage iResults.AddStringL ErrMessage err %d",
       
   158                             err);
       
   159                     }
       
   160                     delete message;
       
   161                 }
       
   162 
       
   163                 message = wstringToBuf(iStringPairs[L"error-details"]);
       
   164                 if (message == NULL)
       
   165                 {
       
   166                     ELOG(EJavaInstaller,
       
   167                           "ResultsServer::processMessage iResults.wstringToBuf 2 returned NULL ");
       
   168                 }
       
   169                 else
       
   170                 {
       
   171                     TRAP(err, iResults.AddStringL(KSifOutParam_ErrMessageDetails, *message));
       
   172                     if (KErrNone != err)
       
   173                     {
       
   174                         ELOG1(EJavaInstaller,
       
   175                             "ResultsServer::processMessage iResults.AddStringL ErrMessageDetails "
       
   176                             "err %d", err);
       
   177                     }
       
   178                     delete message;
       
   179                 }
       
   180 
       
   181                 if (INSTALL_OPERATION == operation)
       
   182                 {
       
   183                     ELOG1(EJavaInstaller,
       
   184                         "ResultsServer::processMessage, Install failed with error %d", result);
       
   185                 }
       
   186                 else if (UNINSTALL_OPERATION == operation)
       
   187                 {
       
   188                     ELOG1(EJavaInstaller,
       
   189                         "ResultsServer::processMessage, Uninstall failed with error %d", result);
       
   190                 }
       
   191                 else if (COMPONENT_INFO_OPERATION == operation)
       
   192                 {
       
   193                     ELOG1(EJavaInstaller,
       
   194                         "ResultsServer::processMessage, Component info failed with error %d",
       
   195                         result);
       
   196                 }
       
   197             }
       
   198             else
       
   199             {
       
   200                 // operation succeeded
       
   201 
       
   202                 if (INSTALL_OPERATION == operation)
       
   203                 {
       
   204                     // Return the component ids of the installed Java application.
       
   205                     TComponentId resultComponentId = iIntPairs[L"suite-cid"];
       
   206                     TRAP(err, iResults.AddIntL(KSifOutParam_ComponentId, resultComponentId));
       
   207                     if (KErrNone != err)
       
   208                     {
       
   209                         ELOG1(EJavaInstaller,
       
   210                               "ResultsServer::processMessage iResults.AddIntL cid error %d", err);
       
   211                     }
       
   212                 }
       
   213                 else if (UNINSTALL_OPERATION == operation)
       
   214                 {
       
   215                     // Return nothing if uninstall succeeds
       
   216                 }
       
   217                 else if (COMPONENT_INFO_OPERATION == operation)
       
   218                 {
       
   219                     TRAP(err, setComponentInfoL());
       
   220                     if (KErrNone != err)
       
   221                     {
       
   222                         ELOG1(EJavaInstaller,
       
   223                               "ResultsServer::processMessage Cannot set CComponentInfo, error %d",
       
   224                               err);
       
   225                     }
       
   226                 }
       
   227                 else
       
   228                 {
       
   229                     // Unknown operation
       
   230                     WLOG1(EJavaInstaller,
       
   231                         "ResultsServer::processMessage: Unknown operation (%d) executed",
       
   232                         operation);
       
   233                 }
       
   234             }
       
   235 
       
   236             // Reply always with 'Response' message.
       
   237             CommsMessage reply;
       
   238             reply.replyTo(aMessage);
       
   239             reply.setMessageId(INSTALLER_RESULT_RESPONSE_MESSAGE_ID);
       
   240             reply << 0;
       
   241 
       
   242             int err = iComms.send(reply);
       
   243             if (err != 0)
       
   244             {
       
   245                 ELOG1(EJavaInstaller,
       
   246                     "ResultsServer: Sending reply to Java Installer failed, err %d", err);
       
   247             }
       
   248         }
       
   249         break;
       
   250 
       
   251         default:
       
   252         {
       
   253             // Unknown message. Ignore it.
       
   254             WLOG1(EJavaInstaller,
       
   255                 "ResultsServer: Unknown message received. Msg Id = %d",
       
   256                 aMessage.getMessageId());
       
   257         }
       
   258         break;
       
   259     }
       
   260 
       
   261     clearData();
       
   262 }
       
   263 
       
   264 
       
   265 void ResultsServer::clearData()
       
   266 {
       
   267     iIntPairs.clear();
       
   268     iStringPairs.clear();
       
   269 }
       
   270 
       
   271 
       
   272 void ResultsServer::setComponentInfoL()
       
   273 {
       
   274     // Component == MIDlet Suite
       
   275     TCapabilitySet emptySet;
       
   276     emptySet.SetEmpty();
       
   277 
       
   278     std::wstring wSuiteName = iStringPairs[L"suite-name"];
       
   279     TPtr16 suiteName((TUint16 *)wSuiteName.c_str(), wSuiteName.size(), wSuiteName.size());
       
   280 
       
   281     std::wstring wVersion = iStringPairs[L"version"];
       
   282     TPtr16 version((TUint16 *)wVersion.c_str(), wVersion.size(), wVersion.size());
       
   283 
       
   284     std::wstring wVendor = iStringPairs[L"vendor"];
       
   285     TPtr16 vendor((TUint16 *)wVendor.c_str(), wVendor.size(), wVendor.size());
       
   286 
       
   287     std::wstring wSuiteGid = iStringPairs[L"suite-gid"];
       
   288     TPtr16 suiteGid((TUint16 *)wSuiteGid.c_str(), wSuiteGid.size(), wSuiteGid.size());
       
   289 
       
   290     RPointerArray<CComponentInfo::CApplicationInfo> applications;
       
   291     CleanupResetAndDestroyPushL(applications);
       
   292     int n = 1;
       
   293     do
       
   294     {
       
   295         // construct wstring 'midlet-uid-<n>'. e.g. 'midlet-uid-1'
       
   296         std::wstring midletUidN(L"midlet-uid-");
       
   297         std::wstringstream ss;
       
   298         ss << midletUidN;
       
   299         ss << n;
       
   300         ss >> midletUidN;
       
   301 
       
   302         //LOG1WSTR(EJavaInstaller, EInfo,
       
   303         //         "ResultsServer::processMessage: checking %s", midletUidN.c_str());
       
   304 
       
   305         int uid = iIntPairs[midletUidN];
       
   306         if (uid == 0)
       
   307         {
       
   308             // no more midlets in the suite
       
   309             break;
       
   310         }
       
   311 
       
   312         // construct wstring 'midlet-name-<n>'. e.g. 'midlet-name-1'
       
   313         std::wstring midletNameN(L"midlet-name-");
       
   314         std::wstringstream ss2;
       
   315         ss2 << midletNameN;
       
   316         ss2 << n;
       
   317         ss2 >> midletNameN;
       
   318         std::wstring wMidletName = iStringPairs[midletNameN];
       
   319         TPtr16 midletName((TUint16 *)wMidletName.c_str(), wMidletName.size(), wMidletName.size());
       
   320 
       
   321         // Construct application information and append it to applications list.
       
   322         CComponentInfo::CApplicationInfo* applicationInfo =
       
   323             CComponentInfo::CApplicationInfo::NewLC(
       
   324                 TUid::Uid(uid), midletName, KNullDesC(), KNullDesC());
       
   325         applications.AppendL(applicationInfo);
       
   326         CleanupStack::Pop(applicationInfo);
       
   327 
       
   328         n++;
       
   329     }
       
   330     while (n < 10000);  // sanity check: no suite can have 10000 midlets
       
   331 
       
   332     CComponentInfo::CNode *rootNode = NULL;
       
   333     rootNode = CComponentInfo::CNode::NewLC(
       
   334                    KSoftwareTypeJava,
       
   335                    suiteName,
       
   336                    version,
       
   337                    vendor,
       
   338                    EActivated, // Java applications are always active
       
   339                    (Usif::TInstallStatus)(iIntPairs[L"install-status"]),
       
   340                    iIntPairs[L"suite-cid"],  // TComponentId
       
   341                    suiteGid,
       
   342                    (Usif::TAuthenticity)(iIntPairs[L"authenticity"]),
       
   343                    emptySet, // Java applications use J2ME permissions, not capabilities
       
   344                    iIntPairs[L"component-size"], // max installed size is given by java installer
       
   345                    ETrue, // suite has always at least one MIDlet
       
   346                    EFalse, // drive selection is not required
       
   347                    &applications
       
   348                );
       
   349 
       
   350     // Store whole component info tree
       
   351     iInfo.SetRootNodeL(rootNode);
       
   352     CleanupStack::Pop(rootNode);
       
   353     CleanupStack::PopAndDestroy(&applications);
       
   354 }