VirtualBox

Changeset 13705 in vbox


Ignore:
Timestamp:
Oct 31, 2008 9:00:15 AM (16 years ago)
Author:
vboxsync
Message:

Completed power event handling:

  • pause running machines for the suspend event
  • resume paused machines for the resume event
  • save the state of running and paused machine for the battery low event
Location:
trunk/src/VBox/Main
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/HostPower.cpp

    r13680 r13705  
    2424*   Header Files                                                               *
    2525*******************************************************************************/
     26#include <iprt/mem.h>
    2627#include <VBox/com/ptr.h>
    2728#include "HostPower.h"
    2829#include "Logging.h"
    2930
    30 HostPowerService::HostPowerService(VirtualBox *aVirtualBox)
     31HostPowerService::HostPowerService(VirtualBox *aVirtualBox) : aMachineSuspended(NULL), cbMachineSuspended(0)
    3132{
    3233    mVirtualBox = aVirtualBox;
     
    4041void HostPowerService::notify(HostPowerEvent event)
    4142{
     43    VirtualBox::SessionMachineVector machines;
     44    mVirtualBox->getOpenedMachines (machines);
     45
    4246    switch (event)
    4347    {
    4448    case HostPowerEvent_Suspend:
     49        if (machines.size())
     50            aMachineSuspended = (BOOL *)RTMemAllocZ(sizeof(BOOL) * machines.size());
     51
     52        cbMachineSuspended = machines.size();
     53
     54        for (size_t i = 0; i < machines.size(); i++)
     55            processEvent(machines[i], HostPowerEvent_Suspend, (aMachineSuspended) ? &aMachineSuspended[i] : NULL);
     56
    4557        Log(("HostPowerService::notify SUSPEND\n"));
    4658        break;
     
    4860    case HostPowerEvent_Resume:
    4961        Log(("HostPowerService::notify RESUME\n"));
     62
     63        if (aMachineSuspended)
     64        {
     65            /* It's possible (in theory) that machines are created or destroyed between the suspend notification and the actual host suspend.
     66             * Ignore this edge case and just make sure not to access invalid data.
     67             */
     68            cbMachineSuspended = RT_MIN(machines.size(), cbMachineSuspended);
     69
     70            for (size_t i = 0; i < cbMachineSuspended; i++)
     71                processEvent(machines[i], HostPowerEvent_Resume, &aMachineSuspended[i]);
     72
     73            RTMemFree(aMachineSuspended);
     74            cbMachineSuspended = 0;
     75            aMachineSuspended  = NULL;
     76        }
    5077        break;
    5178
    5279    case HostPowerEvent_BatteryLow:
    5380        Log(("HostPowerService::notify BATTERY LOW\n"));
     81        for (size_t i = 0; i < machines.size(); i++)
     82            processEvent(machines[i], HostPowerEvent_BatteryLow, NULL);
    5483        break;
    5584    }
    56 
    57     VirtualBox::SessionMachineVector machines;
    58     mVirtualBox->getOpenedMachines (machines);
    59 
    60     for (size_t i = 0; i < machines.size(); i++)
    61         processEvent(machines[i], event);
    6285
    6386    machines.clear();
    6487}
    6588
    66 HRESULT HostPowerService::processEvent(SessionMachine *machine, HostPowerEvent event)
     89HRESULT HostPowerService::processEvent(SessionMachine *machine, HostPowerEvent event, BOOL *pMachineSuspended)
    6790{
    6891    MachineState_T state;
     
    7295    CheckComRCReturnRC (rc);
    7396
    74     if (state == MachineState_Running)
     97    /* Valid combinations:
     98     * running & suspend or battery low notification events
     99     * pause   & resume or battery low notification events
     100     */
     101    if (    (state == MachineState_Running && event != HostPowerEvent_Resume)
     102        ||  (state == MachineState_Paused  && event != HostPowerEvent_Suspend))
    75103    {
    76104        ComPtr <ISession> session;
     
    100128                case HostPowerEvent_Suspend:
    101129                    rc = console->Pause();
     130                    if (    SUCCEEDED(rc)
     131                        &&  pMachineSuspended)
     132                        *pMachineSuspended = TRUE;
    102133                    break;
     134
    103135                case HostPowerEvent_Resume:
     136                    Assert(pMachineSuspended);
     137                    if (*pMachineSuspended == TRUE)
     138                        rc = console->Resume();
     139                    break;
     140
    104141                case HostPowerEvent_BatteryLow:
     142                {
     143                    ComPtr<IProgress> progress;
     144
     145                    rc = console->SaveState(progress.asOutParam());
     146                    if (SUCCEEDED(rc))
     147                    {
     148                        /* Wait until the operation has been completed. */
     149                        progress->WaitForCompletion(-1);
     150
     151                        progress->COMGETTER(ResultCode)(&rc);
     152                        AssertMsg(SUCCEEDED(rc), ("SaveState WaitForCompletion failed with %x\n", rc));
     153                    }
     154
    105155                    break;
    106156                }
     157
     158                } /* switch (event) */
    107159            }
    108160        }
    109161fail:
    110162        session->Close();
    111 
    112163    }
    113164    return rc;
  • trunk/src/VBox/Main/include/HostPower.h

    r13688 r13705  
    4242
    4343    void    notify(HostPowerEvent event);
    44     HRESULT processEvent(SessionMachine *machine, HostPowerEvent event);
     44    HRESULT processEvent(SessionMachine *machine, HostPowerEvent event, BOOL *paMachineSuspended);
    4545
    4646protected:
    4747    ComObjPtr <VirtualBox, ComWeakRef> mVirtualBox;
     48
     49    BOOL    *aMachineSuspended;
     50    size_t   cbMachineSuspended;
    4851};
    4952
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette