javamanager/javacaptain/src/amc.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 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:  Amc
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <set>
       
    20 #include <algorithm>
       
    21 
       
    22 #include "logger.h"
       
    23 #include "javacommonutils.h"
       
    24 
       
    25 #include "commsendpoint.h"
       
    26 #include "comms.h"
       
    27 #include "commsmessage.h"
       
    28 
       
    29 #include "amc.h"
       
    30 
       
    31 #include "coreinterface.h"
       
    32 #include "applicationmanagementeventsinterface.h"
       
    33 
       
    34 namespace java
       
    35 {
       
    36 namespace captain
       
    37 {
       
    38 
       
    39 Amc::Amc():mCore(0), mAmEventsDispatcher(0), mStopRequest(0)
       
    40 {
       
    41     JELOG2(EJavaCaptain);
       
    42 }
       
    43 
       
    44 Amc::~Amc()
       
    45 {
       
    46     JELOG2(EJavaCaptain);
       
    47     mCore = 0;
       
    48     mAmEventsDispatcher = 0;
       
    49     mStopRequest = 0;
       
    50 }
       
    51 
       
    52 bool Amc::start(CoreInterface* aCore,
       
    53                 ApplicationManagementEventsInterface* aAmEventsDispatcher)
       
    54 {
       
    55     JELOG2(EJavaCaptain);
       
    56     mCore = aCore;
       
    57     mAmEventsDispatcher = aAmEventsDispatcher;
       
    58     mCore->getComms()->registerListener(PLUGIN_ID_AMC_C, this);
       
    59     return true;
       
    60 }
       
    61 
       
    62 bool Amc::stop()
       
    63 {
       
    64     JELOG2(EJavaCaptain);
       
    65     if (mCore)
       
    66     {
       
    67         mCore->getComms()->unregisterListener(PLUGIN_ID_AMC_C, this);
       
    68     }
       
    69     return true;
       
    70 }
       
    71 
       
    72 void Amc::arLaunched(const Uid&, const int&)
       
    73 {   // Ignored
       
    74     JELOG2(EJavaCaptain);
       
    75 }
       
    76 
       
    77 void Amc::arTerminated(const Uid& aUID, const int& /*aExitCode*/)
       
    78 {
       
    79     JELOG2(EJavaCaptain);
       
    80     // status is hard coded to be ok (=0) on purpose because
       
    81     // javainstaller only cares that application has exited
       
    82     if (mStopRequest && mStopRequest->updateStatus(aUID, 0))
       
    83     {
       
    84         delete mStopRequest;
       
    85         mStopRequest = 0;
       
    86     }
       
    87 }
       
    88 
       
    89 // CommsListener methods
       
    90 void Amc::processMessage(CommsMessage& aMessage)
       
    91 {
       
    92     JELOG2(EJavaCaptain);
       
    93 
       
    94     switch (aMessage.getMessageId())
       
    95     {
       
    96     case AMC_MSG_ID_REQUEST:
       
    97     {
       
    98         int operation = 0;
       
    99         int options = 0;
       
   100         getAmcRequestParams(aMessage, operation, options);
       
   101         LOG2(EJavaCaptain, EInfo, "Amc::Request %s, %s",
       
   102              amcRequestOperation2String(operation),
       
   103              amcRequestOptions2String(options));
       
   104         switch (operation)
       
   105         {
       
   106         case AMC_REQUEST_OPERATION_START:
       
   107             handleStart(aMessage);
       
   108             break;
       
   109 
       
   110         case AMC_REQUEST_OPERATION_STOP:
       
   111             handleStop(aMessage);
       
   112             break;
       
   113 
       
   114         case AMC_REQUEST_OPERATION_UPDATE:
       
   115             handleUpdate(aMessage);
       
   116             break;
       
   117 
       
   118         default:
       
   119             ELOG1(EJavaCaptain, "Unknown AMC operation %d", operation);
       
   120             break;
       
   121         }
       
   122     }
       
   123     break;
       
   124 
       
   125     default:
       
   126         ELOG1(EJavaCaptain, "Unknown message sent to AMC %d", aMessage.getMessageId());
       
   127         break;
       
   128     }
       
   129 }
       
   130 
       
   131 void Amc::handleStart(CommsMessage& aMessage)
       
   132 {
       
   133     JELOG2(EJavaCaptain);
       
   134 
       
   135     int numOfUids = 0;
       
   136     aMessage >> numOfUids;
       
   137 
       
   138     if (numOfUids > 0)
       
   139     {
       
   140         Uid uid;
       
   141         for (int i = 0 ; i < numOfUids ; i++)
       
   142         {
       
   143             aMessage >> uid;
       
   144             LOG1WSTR(EJavaCaptain, EInfo, "handleStart() uid = %s", uid.toString());
       
   145             mCore->getRtc()->launch(uid);
       
   146         }
       
   147     }
       
   148 }
       
   149 
       
   150 void Amc::handleStop(CommsMessage& aMessage)
       
   151 {
       
   152     JELOG2(EJavaCaptain);
       
   153 
       
   154     int numOfUids = 0;
       
   155     aMessage >> numOfUids;
       
   156 
       
   157     if (numOfUids == 0)
       
   158     {
       
   159         CommsMessage message;
       
   160         message.setReceiver(aMessage.getSender());
       
   161         message.setModuleId(aMessage.getModuleId());
       
   162         message.setMessageRef(aMessage.getMessageRef());
       
   163         setAmcResponseParamaters(message, 0);
       
   164         mCore->getComms()->send(message);
       
   165     }
       
   166     else if (numOfUids > 0)
       
   167     {
       
   168         Uid* uids = new Uid[numOfUids];
       
   169         for (int i = 0 ; i < numOfUids ; i++)
       
   170         {
       
   171             aMessage >> uids[i];
       
   172             LOG1WSTR(EJavaCaptain, EInfo, "handleStop uid[] = %s", uids[i].toString());
       
   173         }
       
   174 
       
   175         StopRequest* request = new StopRequest(mCore,
       
   176                                                aMessage.getSender(),
       
   177                                                aMessage.getModuleId(),
       
   178                                                aMessage.getMessageRef(),
       
   179                                                numOfUids, &uids[0]);
       
   180 
       
   181         // Permission check should be done for STOP_APPLICATION permission.
       
   182         // More strict check is used until javainstaller gets required
       
   183         // permission in place
       
   184         bool allowed = aMessage.hasPermission(LAUNCH_APPLICATION);
       
   185         if (allowed)
       
   186         {
       
   187             if (request->stopUids())
       
   188             {
       
   189                 delete request;
       
   190             }
       
   191             else
       
   192             {
       
   193                 mStopRequest = request;
       
   194             }
       
   195         }
       
   196         else
       
   197         {
       
   198             WLOG(EJavaCaptain, "AMC STOP not allowed");
       
   199             request->sendStopNotAllowed(numOfUids, &uids[0]);
       
   200             delete request;
       
   201         }
       
   202         delete [] uids;
       
   203     }
       
   204 }
       
   205 
       
   206 void Amc::handleUpdate(CommsMessage& aMessage)
       
   207 {
       
   208     JELOG2(EJavaCaptain);
       
   209 
       
   210     std::wstring uid;
       
   211 
       
   212     uids_t beforeUids;
       
   213     uids_t afterUids;
       
   214 
       
   215     int numOfUids = 0;
       
   216     int i = 0;
       
   217     aMessage >> numOfUids;
       
   218     for (i = 0 ; i < numOfUids ; i++)
       
   219     {
       
   220         aMessage >> uid;
       
   221         beforeUids.insert(Uid(uid));
       
   222         LOG2(EJavaCaptain, EInfo, "before uids[%d] = %s", i, std::string(uid.begin(), uid.end()).c_str());
       
   223     }
       
   224     aMessage >> numOfUids;
       
   225     for (i = 0 ; i < numOfUids ; i++)
       
   226     {
       
   227         aMessage >> uid;
       
   228         afterUids.insert(Uid(uid));
       
   229         LOG2(EJavaCaptain, EInfo, "after uids[%d] = %s", i, std::string(uid.begin(), uid.end()).c_str());
       
   230     }
       
   231 
       
   232     LOG1(EJavaCaptain, EInfo, "beforeUids.size() = %d", beforeUids.size());
       
   233     LOG1(EJavaCaptain, EInfo, "afterUids.size() = %d", afterUids.size());
       
   234 
       
   235     uids_t resultUids;
       
   236 
       
   237     // Enable all uids which were disabled at the beginning of the installation
       
   238     std::set_union(afterUids.begin(), afterUids.end(),
       
   239                    beforeUids.begin(), beforeUids.end(),
       
   240                    std::inserter(resultUids, resultUids.end()));
       
   241     if (resultUids.size() > 0)
       
   242     {
       
   243         for (uids_t::const_iterator iter = resultUids.begin();
       
   244                 iter != resultUids.end() ; ++iter)
       
   245         {
       
   246             mCore->getRtc()->enable(*iter);
       
   247         }
       
   248         resultUids.clear();
       
   249     }
       
   250 
       
   251     // Added -- exists only in afterUids collection
       
   252     std::set_difference(afterUids.begin(), afterUids.end(),
       
   253                         beforeUids.begin(), beforeUids.end(),
       
   254                         std::inserter(resultUids, resultUids.end()));
       
   255     if (resultUids.size() > 0)
       
   256     {
       
   257         mAmEventsDispatcher->amAdded(resultUids);
       
   258         resultUids.clear();
       
   259     }
       
   260 
       
   261     // Updated -- exists in both collections
       
   262     std::set_intersection(beforeUids.begin(), beforeUids.end(),
       
   263                           afterUids.begin(), afterUids.end(),
       
   264                           std::inserter(resultUids, resultUids.end()));
       
   265     if (resultUids.size() > 0)
       
   266     {
       
   267         mAmEventsDispatcher->amUpdated(resultUids);
       
   268         resultUids.clear();
       
   269     }
       
   270 
       
   271     // Deleted -- exists only in beforeUids collection
       
   272     std::set_difference(beforeUids.begin(), beforeUids.end(),
       
   273                         afterUids.begin(), afterUids.end(),
       
   274                         std::inserter(resultUids, resultUids.end()));
       
   275     if (resultUids.size() > 0)
       
   276     {
       
   277         mAmEventsDispatcher->amDeleted(resultUids);
       
   278         resultUids.clear();
       
   279     }
       
   280 
       
   281 }
       
   282 
       
   283 
       
   284 
       
   285 } // namespace captain
       
   286 } // namespace java
       
   287