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 | mVirtualBox->getOpenedMachinesAndControls (machines, controls);
|
---|
58 |
|
---|
59 | /* pause running VMs */
|
---|
60 | for (size_t i = 0; i < controls.size(); ++ i)
|
---|
61 | {
|
---|
62 | /* get the remote console */
|
---|
63 | ComPtr <IConsole> console;
|
---|
64 | rc = controls [i]->GetRemoteConsole (console.asOutParam());
|
---|
65 | /* the VM could have been powered down and closed or whatever */
|
---|
66 | if (FAILED (rc))
|
---|
67 | continue;
|
---|
68 |
|
---|
69 | /* note that Pause() will simply return a failure if the VM is
|
---|
70 | * in an inappropriate state */
|
---|
71 | rc = console->Pause();
|
---|
72 | if (FAILED (rc))
|
---|
73 | continue;
|
---|
74 |
|
---|
75 | /* save the control to un-pause the VM later */
|
---|
76 | mConsoles.push_back (console);
|
---|
77 | }
|
---|
78 |
|
---|
79 | LogFunc (("Suspended %d VMs\n", mConsoles.size()));
|
---|
80 |
|
---|
81 | break;
|
---|
82 | }
|
---|
83 |
|
---|
84 | case HostPowerEvent_Resume:
|
---|
85 | {
|
---|
86 | LogFunc (("RESUME\n"));
|
---|
87 |
|
---|
88 | size_t resumed = 0;
|
---|
89 |
|
---|
90 | /* go through VMs we paused on Suspend */
|
---|
91 | for (size_t i = 0; i < mConsoles.size(); ++ i)
|
---|
92 | {
|
---|
93 | /* note that Resume() will simply return a failure if the VM is
|
---|
94 | * in an inappropriate state (it will also fail if the VM has
|
---|
95 | * been somehow closed by this time already so that the
|
---|
96 | * console reference we have is dead) */
|
---|
97 | rc = mConsoles [i]->Resume();
|
---|
98 | if (FAILED (rc))
|
---|
99 | continue;
|
---|
100 |
|
---|
101 | ++ resumed;
|
---|
102 | }
|
---|
103 |
|
---|
104 | LogFunc (("Resumed %d VMs\n", resumed));
|
---|
105 |
|
---|
106 | mConsoles.clear();
|
---|
107 |
|
---|
108 | break;
|
---|
109 | }
|
---|
110 |
|
---|
111 | case HostPowerEvent_BatteryLow:
|
---|
112 | {
|
---|
113 | LogFunc (("BATTERY LOW\n"));
|
---|
114 |
|
---|
115 | mVirtualBox->getOpenedMachinesAndControls (machines, controls);
|
---|
116 |
|
---|
117 | size_t saved = 0;
|
---|
118 |
|
---|
119 | /* save running VMs */
|
---|
120 | for (size_t i = 0; i < controls.size(); ++ i)
|
---|
121 | {
|
---|
122 | /* get the remote console */
|
---|
123 | ComPtr <IConsole> console;
|
---|
124 | rc = controls [i]->GetRemoteConsole (console.asOutParam());
|
---|
125 | /* the VM could have been powered down and closed or whatever */
|
---|
126 | if (FAILED (rc))
|
---|
127 | continue;
|
---|
128 |
|
---|
129 | ComPtr<IProgress> progress;
|
---|
130 |
|
---|
131 | /* note that SaveState() will simply return a failure if the VM
|
---|
132 | * is in an inappropriate state */
|
---|
133 | rc = console->SaveState (progress.asOutParam());
|
---|
134 | if (FAILED (rc))
|
---|
135 | continue;
|
---|
136 |
|
---|
137 | /* Wait until the operation has been completed. */
|
---|
138 | rc = progress->WaitForCompletion(-1);
|
---|
139 | if (SUCCEEDED (rc))
|
---|
140 | progress->COMGETTER(ResultCode) (&rc);
|
---|
141 |
|
---|
142 | AssertMsg (SUCCEEDED (rc), ("SaveState WaitForCompletion "
|
---|
143 | "failed with %Rhrc (%#08X)\n", rc, rc));
|
---|
144 |
|
---|
145 | if (SUCCEEDED (rc))
|
---|
146 | ++ saved;
|
---|
147 | }
|
---|
148 |
|
---|
149 | LogFunc (("Saved %d VMs\n", saved));
|
---|
150 |
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | }
|
---|