javamanager/javacaptain/inc.s60/pmc.h
branchRCL_3
changeset 19 04becd199f91
child 64 0ea12c182930
equal deleted inserted replaced
16:f5050f1da672 19: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:  Pmc
       
    15 *
       
    16 */
       
    17 
       
    18 #ifndef PMC_H
       
    19 #define PMC_H
       
    20 
       
    21 #include <e32std.h>
       
    22 #include <e32base.h>
       
    23 
       
    24 #include <map>
       
    25 
       
    26 #include <AknGlobalNote.h>
       
    27 
       
    28 
       
    29 #include "javaoslayer.h"
       
    30 #include "logger.h"
       
    31 #include "scopedlocks.h"
       
    32 
       
    33 #include "pmcinterface.h"
       
    34 
       
    35 using namespace java::util;
       
    36 
       
    37 namespace java
       
    38 {
       
    39 
       
    40 namespace captain
       
    41 {
       
    42 class CoreInterface;
       
    43 class ProcessManagementEventsInterface;
       
    44 
       
    45 class ProcessEvents
       
    46 {
       
    47 public:
       
    48     virtual ~ProcessEvents() {}
       
    49     virtual void handleTerminated(const int& pid, const int& exitCode) = 0;
       
    50 };
       
    51 
       
    52 class Pmc : public PmcInterface,
       
    53         public ProcessEvents
       
    54 {
       
    55 public:
       
    56     Pmc();
       
    57     virtual ~Pmc();
       
    58 
       
    59     bool start(CoreInterface* aCore,
       
    60                ProcessManagementEventsInterface* aProcessManagementEventsDispatcher);
       
    61     bool stop();
       
    62 
       
    63     // PmcInterface
       
    64     virtual int launch(const cmdLine_t& cmdLine, const int& options);
       
    65     virtual int terminate(const int& pid);
       
    66     virtual int kill(const int& pid);
       
    67 
       
    68     // ProcessEvents
       
    69     virtual void handleTerminated(const int& pid, const int& exitCode);
       
    70 
       
    71 protected:
       
    72 
       
    73     class ProcessActive : public CActive
       
    74     {
       
    75         ProcessActive(); // cannot be used
       
    76     public:
       
    77         ProcessActive(RProcess* aProcess, ProcessEvents* aEvents, bool aPrewarm)
       
    78                 :CActive(EPriorityStandard), mRProcess(aProcess), mEvents(aEvents), mExited(false)
       
    79         {
       
    80             JELOG4(EJavaCaptain, EInfo);
       
    81             CActiveScheduler::Add(this);
       
    82             mRProcess->Logon(iStatus);
       
    83             SetActive();
       
    84             JavaOsLayer::startUpTrace("CAPTAIN: Process resume", -1, -1);
       
    85             if (aPrewarm)
       
    86             {
       
    87                 // Can't use EPriorityLow, because the compiler takes wrong
       
    88                 // enum from CActive.
       
    89                 mRProcess->SetPriority(/*EPriorityLow*/ (TProcessPriority)150);
       
    90             }
       
    91             mRProcess->Resume();
       
    92         }
       
    93         virtual ~ProcessActive()
       
    94         {
       
    95             JELOG4(EJavaCaptain, EInfo);
       
    96             Cancel();
       
    97             if (!mExited)
       
    98             {
       
    99                 mRProcess->Close();
       
   100                 delete mRProcess;
       
   101             }
       
   102         }
       
   103 
       
   104         virtual void RunL()
       
   105         {
       
   106             JELOG4(EJavaCaptain, EInfo);
       
   107             mEvents->handleTerminated(mRProcess->Id(), mRProcess->ExitReason());
       
   108             mRProcess->Close();
       
   109             delete mRProcess;
       
   110             mExited = true;
       
   111         }
       
   112         virtual void DoCancel()
       
   113         {
       
   114             JELOG4(EJavaCaptain, EInfo);
       
   115             mRProcess->LogonCancel(iStatus);
       
   116         }
       
   117 
       
   118         virtual bool isExited() const
       
   119         {
       
   120             JELOG4(EJavaCaptain, EInfo);
       
   121             return mExited;
       
   122         }
       
   123         virtual void Terminate(const int& reason)
       
   124         {
       
   125             JELOG4(EJavaCaptain, EInfo);
       
   126             if (!mExited)
       
   127             {
       
   128                 mRProcess->Terminate(reason);
       
   129                 showErrorNote();
       
   130             }
       
   131         }
       
   132         virtual void Kill(const int& reason)
       
   133         {
       
   134             JELOG4(EJavaCaptain, EInfo);
       
   135             if (!mExited)
       
   136             {
       
   137                 mRProcess->Kill(reason);
       
   138                 showErrorNote();
       
   139             }
       
   140         }
       
   141 
       
   142     private:
       
   143         void showErrorNote()
       
   144         {
       
   145             LOG(EJavaCaptain, EInfo, "Process failed to exit gracefully");
       
   146 #ifdef RD_JAVA_EXIT_ERROR_DIALOG
       
   147             //Print the dialog showing that force process kill was done.
       
   148             CAknGlobalNote* globalNote = CAknGlobalNote::NewL();
       
   149             globalNote->ShowNoteL(EAknGlobalErrorNote,
       
   150                                   _L("Midp process failed to exit gracefully."));
       
   151             delete globalNote;
       
   152 #endif
       
   153         }
       
   154 
       
   155     private:
       
   156         RProcess*       mRProcess;
       
   157         ProcessEvents*  mEvents;
       
   158         bool            mExited;
       
   159     };
       
   160 
       
   161     CoreInterface*                          mCore;
       
   162     ProcessManagementEventsInterface*       mProcessManagementEventsDispatcher;
       
   163 
       
   164     typedef std::map<int, ProcessActive*>   processes_t;
       
   165     processes_t                             mProcesses;
       
   166     java::util::ScopedMutex                 mProcessesMutex;
       
   167 };
       
   168 
       
   169 } // namespace captain
       
   170 } // namespace java
       
   171 
       
   172 #endif // PMC_H