1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox interface to host's power notification service
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
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 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
18 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
19 | * additional information or have any questions.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 | /*******************************************************************************
|
---|
24 | * Header Files *
|
---|
25 | *******************************************************************************/
|
---|
26 |
|
---|
27 | #include "HostPower.h"
|
---|
28 | #include "Logging.h"
|
---|
29 |
|
---|
30 | #include <VBox/com/ptr.h>
|
---|
31 |
|
---|
32 | #include "VirtualBoxImpl.h"
|
---|
33 |
|
---|
34 | #include <iprt/mem.h>
|
---|
35 |
|
---|
36 | HostPowerService::HostPowerService (VirtualBox *aVirtualBox)
|
---|
37 | {
|
---|
38 | Assert(aVirtualBox != NULL);
|
---|
39 | mVirtualBox = aVirtualBox;
|
---|
40 | }
|
---|
41 |
|
---|
42 | HostPowerService::~HostPowerService()
|
---|
43 | {
|
---|
44 | }
|
---|
45 |
|
---|
46 | void HostPowerService::notify(HostPowerEvent aEvent)
|
---|
47 | {
|
---|
48 | VirtualBox::SessionMachineList machines;
|
---|
49 | VirtualBox::InternalControlList controls;
|
---|
50 |
|
---|
51 | HRESULT rc = S_OK;
|
---|
52 |
|
---|
53 | switch (aEvent)
|
---|
54 | {
|
---|
55 | case HostPowerEvent_Suspend:
|
---|
56 | {
|
---|
57 | LogFunc (("SUSPEND\n"));
|
---|
58 |
|
---|
59 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
60 | /* Suspend performance sampling to avoid unnecessary callbacks due to jumps in time. */
|
---|
61 | PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
|
---|
62 |
|
---|
63 | if (perfcollector)
|
---|
64 | perfcollector->suspendSampling();
|
---|
65 | #endif
|
---|
66 | mVirtualBox->getOpenedMachines(machines, &controls);
|
---|
67 |
|
---|
68 | /* pause running VMs */
|
---|
69 | for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
|
---|
70 | it != controls.end();
|
---|
71 | ++it)
|
---|
72 | {
|
---|
73 | ComPtr<IInternalSessionControl> pControl = *it;
|
---|
74 |
|
---|
75 | /* get the remote console */
|
---|
76 | ComPtr<IConsole> console;
|
---|
77 | rc = pControl->GetRemoteConsole(console.asOutParam());
|
---|
78 | /* the VM could have been powered down and closed or whatever */
|
---|
79 | if (FAILED(rc))
|
---|
80 | continue;
|
---|
81 |
|
---|
82 | /* note that Pause() will simply return a failure if the VM is
|
---|
83 | * in an inappropriate state */
|
---|
84 | rc = console->Pause();
|
---|
85 | if (FAILED(rc))
|
---|
86 | continue;
|
---|
87 |
|
---|
88 | /* save the control to un-pause the VM later */
|
---|
89 | mConsoles.push_back(console);
|
---|
90 | }
|
---|
91 |
|
---|
92 | LogFunc (("Suspended %d VMs\n", mConsoles.size()));
|
---|
93 |
|
---|
94 | break;
|
---|
95 | }
|
---|
96 |
|
---|
97 | case HostPowerEvent_Resume:
|
---|
98 | {
|
---|
99 | LogFunc (("RESUME\n"));
|
---|
100 |
|
---|
101 | size_t resumed = 0;
|
---|
102 |
|
---|
103 | /* go through VMs we paused on Suspend */
|
---|
104 | for (size_t i = 0; i < mConsoles.size(); ++i)
|
---|
105 | {
|
---|
106 | /* note that Resume() will simply return a failure if the VM is
|
---|
107 | * in an inappropriate state (it will also fail if the VM has
|
---|
108 | * been somehow closed by this time already so that the
|
---|
109 | * console reference we have is dead) */
|
---|
110 | rc = mConsoles[i]->Resume();
|
---|
111 | if (FAILED(rc))
|
---|
112 | continue;
|
---|
113 |
|
---|
114 | ++ resumed;
|
---|
115 | }
|
---|
116 |
|
---|
117 | LogFunc (("Resumed %d VMs\n", resumed));
|
---|
118 |
|
---|
119 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
120 | /* Resume the performance sampling. */
|
---|
121 | PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
|
---|
122 |
|
---|
123 | if (perfcollector)
|
---|
124 | perfcollector->resumeSampling();
|
---|
125 | #endif
|
---|
126 |
|
---|
127 | mConsoles.clear();
|
---|
128 |
|
---|
129 | break;
|
---|
130 | }
|
---|
131 |
|
---|
132 | case HostPowerEvent_BatteryLow:
|
---|
133 | {
|
---|
134 | LogFunc (("BATTERY LOW\n"));
|
---|
135 |
|
---|
136 | mVirtualBox->getOpenedMachines(machines, &controls);
|
---|
137 |
|
---|
138 | size_t saved = 0;
|
---|
139 |
|
---|
140 | /* save running VMs */
|
---|
141 | for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
|
---|
142 | it != controls.end();
|
---|
143 | ++it)
|
---|
144 | {
|
---|
145 | ComPtr<IInternalSessionControl> pControl = *it;
|
---|
146 | /* get the remote console */
|
---|
147 | ComPtr<IConsole> console;
|
---|
148 | rc = pControl->GetRemoteConsole (console.asOutParam());
|
---|
149 | /* the VM could have been powered down and closed or whatever */
|
---|
150 | if (FAILED(rc))
|
---|
151 | continue;
|
---|
152 |
|
---|
153 | ComPtr<IProgress> progress;
|
---|
154 |
|
---|
155 | /* note that SaveState() will simply return a failure if the VM
|
---|
156 | * is in an inappropriate state */
|
---|
157 | rc = console->SaveState (progress.asOutParam());
|
---|
158 | if (FAILED(rc))
|
---|
159 | continue;
|
---|
160 |
|
---|
161 | /* Wait until the operation has been completed. */
|
---|
162 | LONG iRc;
|
---|
163 | rc = progress->WaitForCompletion(-1);
|
---|
164 | if (SUCCEEDED(rc))
|
---|
165 | progress->COMGETTER(ResultCode) (&iRc);
|
---|
166 | rc = iRc;
|
---|
167 |
|
---|
168 | AssertMsg (SUCCEEDED(rc), ("SaveState WaitForCompletion "
|
---|
169 | "failed with %Rhrc (%#08X)\n", rc, rc));
|
---|
170 |
|
---|
171 | if (SUCCEEDED(rc))
|
---|
172 | ++ saved;
|
---|
173 | }
|
---|
174 |
|
---|
175 | LogFunc (("Saved %d VMs\n", saved));
|
---|
176 |
|
---|
177 | break;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | }
|
---|
181 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|