qthighway/xqserviceutil/src/processinfo.cpp
changeset 27 6bfad47013df
equal deleted inserted replaced
26:3d09643def13 27:6bfad47013df
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 *
       
     5 * This program is free software: you can redistribute it and/or modify
       
     6 * it under the terms of the GNU Lesser General Public License as published by
       
     7 * the Free Software Foundation, version 2.1 of the License.
       
     8 * 
       
     9 * This program is distributed in the hope that it will be useful,
       
    10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    12 * GNU Lesser General Public License for more details.
       
    13 *
       
    14 * You should have received a copy of the GNU Lesser General Public License
       
    15 * along with this program.  If not, 
       
    16 * see "http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html/".
       
    17 *
       
    18 * Description:                                                         
       
    19 *
       
    20 */
       
    21 
       
    22 #include "processinfo.h"
       
    23 
       
    24 #include "xqservicelog.h"
       
    25 
       
    26 #include <QCoreApplication>
       
    27 
       
    28 #define TIMER_DELAY 3000000 // 3000000 microseconds == 3s
       
    29 
       
    30 // static data initialization
       
    31 
       
    32 ProcessInfoDestroyer *ProcessInfoDestroyer::instance = 0;
       
    33 CProcessInfo::ProcessInfoMap CProcessInfo::iProcessInfoMap;
       
    34 
       
    35 // ProcessInfoMap implementation
       
    36 
       
    37 CProcessInfo::ProcessInfoMap::~ProcessInfoMap()
       
    38 {
       
    39     CProcessInfo::Destroy();
       
    40     ProcessInfoDestroyer::disable();
       
    41 }
       
    42 
       
    43 // CProcessInfo implementation
       
    44 
       
    45 CProcessInfo::CProcessInfo(const TUid& appUid):
       
    46         CActive(CActive::EPriorityStandard), 
       
    47         iAppUid(appUid)
       
    48 {
       
    49     XQSERVICE_DEBUG_PRINT("CProcessInfo::CProcessInfo");
       
    50     
       
    51     CActiveScheduler::Add(this);
       
    52 }
       
    53 
       
    54 CProcessInfo::~CProcessInfo()
       
    55 {
       
    56     XQSERVICE_DEBUG_PRINT("CProcessInfo::~CProcessInfo");
       
    57     
       
    58     // Cancel asynch request, normally it should be done in DoCancel() 
       
    59     // but we dont wont to cancel request when we cancel active object
       
    60     User::CancelMiscNotifier(iStatus);
       
    61     
       
    62     Cancel();
       
    63 }
       
    64 
       
    65 void CProcessInfo::Destroy()
       
    66 {
       
    67     foreach (CProcessInfo* info, iProcessInfoMap.map.values())
       
    68         delete info;
       
    69     iProcessInfoMap.map.clear();
       
    70 }
       
    71 
       
    72 void CProcessInfo::AddProcessL(const TUid& appUid, RProcess& appProcess)
       
    73 {
       
    74     XQSERVICE_DEBUG_PRINT("CProcessInfo::AddProcessL");
       
    75     
       
    76     CProcessInfo* self = new(ELeave) CProcessInfo(appUid);
       
    77     CleanupStack::PushL(self);
       
    78     self->ConstructL(appProcess);
       
    79     CleanupStack::Pop(self);
       
    80 }
       
    81 
       
    82 bool CProcessInfo::EnsureProcessCanStartL(const TUid& appUid)
       
    83 {
       
    84     XQSERVICE_DEBUG_PRINT("CProcessInfo::EnsureProcessCanStartL");
       
    85   
       
    86     bool ret = true;
       
    87     
       
    88     CProcessInfo* previousProcess = iProcessInfoMap.map[appUid.iUid];
       
    89     if (previousProcess) {
       
    90         // Timer is for ensure that wait will end. 
       
    91         // There is possibility that destroying process notification could be lost.
       
    92         RTimer securityTimer;
       
    93         securityTimer.CreateLocal();
       
    94         CleanupClosePushL(securityTimer);
       
    95         
       
    96         TRequestStatus timerStatus;
       
    97         securityTimer.After(timerStatus, TIMER_DELAY);
       
    98         User::WaitForRequest(previousProcess->iStatus, timerStatus);
       
    99         
       
   100         if (previousProcess->iStatus == KRequestPending)
       
   101             ret = false;
       
   102         
       
   103         CleanupStack::PopAndDestroy();
       
   104         delete previousProcess;
       
   105         iProcessInfoMap.map.remove(appUid.iUid);
       
   106     }
       
   107     return ret;
       
   108 }
       
   109 
       
   110 void CProcessInfo::RunL()
       
   111 {
       
   112     XQSERVICE_DEBUG_PRINT("CProcessInfo::RunL");
       
   113     
       
   114     iProcessInfoMap.map.remove(iAppUid.iUid);
       
   115     delete this;
       
   116 }
       
   117 
       
   118 void CProcessInfo::ConstructL(RProcess& appProcess)
       
   119 {
       
   120     XQSERVICE_DEBUG_PRINT("CProcessInfo::ConstructL");
       
   121     
       
   122     SetActive();
       
   123     
       
   124     EnsureProcessCanStartL(iAppUid);
       
   125     iProcessInfoMap.map.insert(iAppUid.iUid, this);
       
   126     appProcess.NotifyDestruction(iStatus);
       
   127     
       
   128     ProcessInfoDestroyer::enable();
       
   129 }
       
   130 
       
   131 void CProcessInfo::DoCancel()
       
   132 {
       
   133     XQSERVICE_DEBUG_PRINT("CProcessInfo::DoCancel");
       
   134     
       
   135     // Cancel asynch request, normally it should be done in DoCancel() 
       
   136     // but we dont wont to cancel request when we cancel active object.
       
   137     // Cancel asynch request is in ~CProcessInfo().
       
   138 }
       
   139 
       
   140 // ProcessInfoDestroyer implementation
       
   141 
       
   142 void ProcessInfoDestroyer::enable()
       
   143 {
       
   144     if (!instance)
       
   145         instance = new ProcessInfoDestroyer;
       
   146 }
       
   147 
       
   148 void ProcessInfoDestroyer::disable()
       
   149 {
       
   150     if (instance) {
       
   151         delete instance;
       
   152         instance = 0;
       
   153     }
       
   154 }
       
   155 
       
   156 ProcessInfoDestroyer::ProcessInfoDestroyer()
       
   157 {
       
   158     connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(destroy()));
       
   159 }
       
   160 
       
   161 void ProcessInfoDestroyer::destroy()
       
   162 {
       
   163     CProcessInfo::Destroy();
       
   164 }
       
   165