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 <iprt/mem.h>
|
---|
33 |
|
---|
34 | HostPowerService::HostPowerService (VirtualBox *aVirtualBox)
|
---|
35 | {
|
---|
36 | Assert (aVirtualBox != NULL);
|
---|
37 | mVirtualBox = aVirtualBox;
|
---|
38 | }
|
---|
39 |
|
---|
40 | HostPowerService::~HostPowerService()
|
---|
41 | {
|
---|
42 | }
|
---|
43 |
|
---|
44 | void HostPowerService::notify (HostPowerEvent aEvent)
|
---|
45 | {
|
---|
46 | VirtualBox::SessionMachineVector machines;
|
---|
47 | VirtualBox::InternalControlVector 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->getOpenedMachinesAndControls (machines, controls);
|
---|
65 |
|
---|
66 | /* pause running VMs */
|
---|
67 | for (size_t i = 0; i < controls.size(); ++ i)
|
---|
68 | {
|
---|
69 | /* get the remote console */
|
---|
70 | ComPtr<IConsole> console;
|
---|
71 | rc = controls [i]->GetRemoteConsole (console.asOutParam());
|
---|
72 | /* the VM could have been powered down and closed or whatever */
|
---|
73 | if (FAILED (rc))
|
---|
74 | continue;
|
---|
75 |
|
---|
76 | /* note that Pause() will simply return a failure if the VM is
|
---|
77 | * in an inappropriate state */
|
---|
78 | rc = console->Pause();
|
---|
79 | if (FAILED (rc))
|
---|
80 | continue;
|
---|
81 |
|
---|
82 | /* save the control to un-pause the VM later */
|
---|
83 | mConsoles.push_back (console);
|
---|
84 | }
|
---|
85 |
|
---|
86 | LogFunc (("Suspended %d VMs\n", mConsoles.size()));
|
---|
87 |
|
---|
88 | break;
|
---|
89 | }
|
---|
90 |
|
---|
91 | case HostPowerEvent_Resume:
|
---|
92 | {
|
---|
93 | LogFunc (("RESUME\n"));
|
---|
94 |
|
---|
95 | size_t resumed = 0;
|
---|
96 |
|
---|
97 | /* go through VMs we paused on Suspend */
|
---|
98 | for (size_t i = 0; i < mConsoles.size(); ++ i)
|
---|
99 | {
|
---|
100 | /* note that Resume() will simply return a failure if the VM is
|
---|
101 | * in an inappropriate state (it will also fail if the VM has
|
---|
102 | * been somehow closed by this time already so that the
|
---|
103 | * console reference we have is dead) */
|
---|
104 | rc = mConsoles [i]->Resume();
|
---|
105 | if (FAILED (rc))
|
---|
106 | continue;
|
---|
107 |
|
---|
108 | ++ resumed;
|
---|
109 | }
|
---|
110 |
|
---|
111 | LogFunc (("Resumed %d VMs\n", resumed));
|
---|
112 |
|
---|
113 | #ifdef VBOX_WITH_RESOURCE_USAGE_API
|
---|
114 | /* Resume the performance sampling. */
|
---|
115 | PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
|
---|
116 |
|
---|
117 | if (perfcollector)
|
---|
118 | perfcollector->resumeSampling();
|
---|
119 | #endif
|
---|
120 |
|
---|
121 | mConsoles.clear();
|
---|
122 |
|
---|
123 | break;
|
---|
124 | }
|
---|
125 |
|
---|
126 | case HostPowerEvent_BatteryLow:
|
---|
127 | {
|
---|
128 | LogFunc (("BATTERY LOW\n"));
|
---|
129 |
|
---|
130 | mVirtualBox->getOpenedMachinesAndControls (machines, controls);
|
---|
131 |
|
---|
132 | size_t saved = 0;
|
---|
133 |
|
---|
134 | /* save running VMs */
|
---|
135 | for (size_t i = 0; i < controls.size(); ++ i)
|
---|
136 | {
|
---|
137 | /* get the remote console */
|
---|
138 | ComPtr<IConsole> console;
|
---|
139 | rc = controls [i]->GetRemoteConsole (console.asOutParam());
|
---|
140 | /* the VM could have been powered down and closed or whatever */
|
---|
141 | if (FAILED (rc))
|
---|
142 | continue;
|
---|
143 |
|
---|
144 | ComPtr<IProgress> progress;
|
---|
145 |
|
---|
146 | /* note that SaveState() will simply return a failure if the VM
|
---|
147 | * is in an inappropriate state */
|
---|
148 | rc = console->SaveState (progress.asOutParam());
|
---|
149 | if (FAILED (rc))
|
---|
150 | continue;
|
---|
151 |
|
---|
152 | /* Wait until the operation has been completed. */
|
---|
153 | LONG iRc;
|
---|
154 | rc = progress->WaitForCompletion(-1);
|
---|
155 | if (SUCCEEDED(rc))
|
---|
156 | progress->COMGETTER(ResultCode) (&iRc);
|
---|
157 | rc = iRc;
|
---|
158 |
|
---|
159 | AssertMsg (SUCCEEDED(rc), ("SaveState WaitForCompletion "
|
---|
160 | "failed with %Rhrc (%#08X)\n", rc, rc));
|
---|
161 |
|
---|
162 | if (SUCCEEDED(rc))
|
---|
163 | ++ saved;
|
---|
164 | }
|
---|
165 |
|
---|
166 | LogFunc (("Saved %d VMs\n", saved));
|
---|
167 |
|
---|
168 | break;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | /* vi: set tabstop=4 shiftwidth=4 expandtab: */
|
---|