VirtualBox

source: vbox/trunk/src/VBox/Main/HostPower.cpp@ 32928

Last change on this file since 32928 was 32056, checked in by vboxsync, 14 years ago

Applied a simple memory leak detector to libjpeg and VBoxC (Windows host only), the code completely disabled by default.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/** @file
2 *
3 * VirtualBox interface to host's power notification service
4 */
5
6/*
7 * Copyright (C) 2006-2007 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*******************************************************************************
20* Header Files *
21*******************************************************************************/
22
23#include "HostPower.h"
24#include "Logging.h"
25
26#include <VBox/com/ptr.h>
27
28#include "VirtualBoxImpl.h"
29
30#ifndef VBOX_WITH_VRDP_MEMLEAK_DETECTOR
31#include <iprt/mem.h>
32#endif /* !VBOX_WITH_VRDP_MEMLEAK_DETECTOR */
33
34HostPowerService::HostPowerService (VirtualBox *aVirtualBox)
35{
36 Assert(aVirtualBox != NULL);
37 mVirtualBox = aVirtualBox;
38}
39
40HostPowerService::~HostPowerService()
41{
42}
43
44void HostPowerService::notify(HostPowerEvent aEvent)
45{
46 SessionMachinesList machines;
47 VirtualBox::InternalControlList controls;
48
49 HRESULT rc = S_OK;
50
51 switch (aEvent)
52 {
53 case HostPowerEvent_Suspend:
54 {
55 LogFunc (("SUSPEND\n"));
56
57#ifdef VBOX_WITH_RESOURCE_USAGE_API
58 /* Suspend performance sampling to avoid unnecessary callbacks due to jumps in time. */
59 PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
60
61 if (perfcollector)
62 perfcollector->suspendSampling();
63#endif
64 mVirtualBox->getOpenedMachines(machines, &controls);
65
66 /* pause running VMs */
67 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
68 it != controls.end();
69 ++it)
70 {
71 ComPtr<IInternalSessionControl> pControl = *it;
72
73 /* get the remote console */
74 ComPtr<IConsole> console;
75 rc = pControl->GetRemoteConsole(console.asOutParam());
76 /* the VM could have been powered down and closed or whatever */
77 if (FAILED(rc))
78 continue;
79
80 /* note that Pause() will simply return a failure if the VM is
81 * in an inappropriate state */
82 rc = console->Pause();
83 if (FAILED(rc))
84 continue;
85
86 /* save the control to un-pause the VM later */
87 mConsoles.push_back(console);
88 }
89
90 LogFunc (("Suspended %d VMs\n", mConsoles.size()));
91
92 break;
93 }
94
95 case HostPowerEvent_Resume:
96 {
97 LogFunc (("RESUME\n"));
98
99 size_t resumed = 0;
100
101 /* go through VMs we paused on Suspend */
102 for (size_t i = 0; i < mConsoles.size(); ++i)
103 {
104 /* note that Resume() will simply return a failure if the VM is
105 * in an inappropriate state (it will also fail if the VM has
106 * been somehow closed by this time already so that the
107 * console reference we have is dead) */
108 rc = mConsoles[i]->Resume();
109 if (FAILED(rc))
110 continue;
111
112 ++ resumed;
113 }
114
115 LogFunc (("Resumed %d VMs\n", resumed));
116
117#ifdef VBOX_WITH_RESOURCE_USAGE_API
118 /* Resume the performance sampling. */
119 PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
120
121 if (perfcollector)
122 perfcollector->resumeSampling();
123#endif
124
125 mConsoles.clear();
126
127 break;
128 }
129
130 case HostPowerEvent_BatteryLow:
131 {
132 LogFunc (("BATTERY LOW\n"));
133
134 mVirtualBox->getOpenedMachines(machines, &controls);
135
136 size_t saved = 0;
137
138 /* save running VMs */
139 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
140 it != controls.end();
141 ++it)
142 {
143 ComPtr<IInternalSessionControl> pControl = *it;
144 /* get the remote console */
145 ComPtr<IConsole> console;
146 rc = pControl->GetRemoteConsole (console.asOutParam());
147 /* the VM could have been powered down and closed or whatever */
148 if (FAILED(rc))
149 continue;
150
151 ComPtr<IProgress> progress;
152
153 /* note that SaveState() will simply return a failure if the VM
154 * is in an inappropriate state */
155 rc = console->SaveState (progress.asOutParam());
156 if (FAILED(rc))
157 continue;
158
159 /* Wait until the operation has been completed. */
160 LONG iRc;
161 rc = progress->WaitForCompletion(-1);
162 if (SUCCEEDED(rc))
163 progress->COMGETTER(ResultCode) (&iRc);
164 rc = iRc;
165
166 AssertMsg (SUCCEEDED(rc), ("SaveState WaitForCompletion "
167 "failed with %Rhrc (%#08X)\n", rc, rc));
168
169 if (SUCCEEDED(rc))
170 ++ saved;
171 }
172
173 LogFunc (("Saved %d VMs\n", saved));
174
175 break;
176 }
177 }
178}
179/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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