VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/HostPower.cpp@ 50216

Last change on this file since 50216 was 50216, checked in by vboxsync, 11 years ago

Main/HostPower: s/HostPower\/SavestateOnBatteryLow/VBoxInternal2\/SavestateOnBatteryLow/ + handle runtime changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/** @file
2 *
3 * VirtualBox interface to host's power notification service
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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#include "MachineImpl.h"
30
31#include <iprt/mem.h>
32#include <iprt/cpp/utils.h>
33
34HostPowerService::HostPowerService(VirtualBox *aVirtualBox)
35{
36 AssertPtr(aVirtualBox);
37 mVirtualBox = aVirtualBox;
38}
39
40HostPowerService::~HostPowerService()
41{
42}
43
44void HostPowerService::notify(Reason_T aReason)
45{
46 SessionMachinesList machines;
47 VirtualBox::InternalControlList controls;
48
49 HRESULT rc = S_OK;
50
51 switch (aReason)
52 {
53 case Reason_HostSuspend:
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 /* PauseWithReason() will simply return a failure if
74 * the VM is in an inappropriate state */
75 rc = pControl->PauseWithReason(Reason_HostSuspend);
76 if (FAILED(rc))
77 continue;
78
79 /* save the control to un-pause the VM later */
80 mSessionControls.push_back(pControl);
81 }
82
83 LogFunc(("Suspended %d VMs\n", mSessionControls.size()));
84
85 break;
86 }
87
88 case Reason_HostResume:
89 {
90 LogFunc(("RESUME\n"));
91
92 size_t resumed = 0;
93
94 /* go through VMs we paused on Suspend */
95 for (size_t i = 0; i < mSessionControls.size(); ++i)
96 {
97 /* note that Resume() will simply return a failure if the VM is
98 * in an inappropriate state (it will also fail if the VM has
99 * been somehow closed by this time already so that the
100 * console reference we have is dead) */
101 rc = mSessionControls[i]->ResumeWithReason(Reason_HostResume);
102 if (FAILED(rc))
103 continue;
104
105 ++resumed;
106 }
107
108 LogFunc(("Resumed %d VMs\n", resumed));
109
110#ifdef VBOX_WITH_RESOURCE_USAGE_API
111 /* Resume the performance sampling. */
112 PerformanceCollector *perfcollector = mVirtualBox->performanceCollector();
113
114 if (perfcollector)
115 perfcollector->resumeSampling();
116#endif
117
118 mSessionControls.clear();
119
120 break;
121 }
122
123 case Reason_HostBatteryLow:
124 {
125 Bstr value;
126 HRESULT hrc = mVirtualBox->GetExtraData(Bstr("VBoxInternal2/SavestateOnBatteryLow").raw(),
127 value.asOutParam());
128 if ( SUCCEEDED(hrc)
129 && value != "0")
130 {
131 LogFunc(("BATTERY LOW -- savestate running VMs\n"));
132
133 mVirtualBox->getOpenedMachines(machines, &controls);
134 size_t saved = 0;
135
136 /* save running VMs */
137 for (VirtualBox::InternalControlList::const_iterator it = controls.begin();
138 it != controls.end();
139 ++it)
140 {
141 ComPtr<IInternalSessionControl> pControl = *it;
142 ComPtr<IProgress> progress;
143
144 /* note that SaveStateWithReason() will simply return a failure
145 * if the VM is in an inappropriate state */
146 rc = pControl->SaveStateWithReason(Reason_HostBatteryLow, progress.asOutParam());
147 if (FAILED(rc))
148 continue;
149
150 /* Wait until the operation has been completed. */
151 rc = progress->WaitForCompletion(-1);
152 if (SUCCEEDED(rc))
153 {
154 LONG iRc;
155 progress->COMGETTER(ResultCode)(&iRc);
156 rc = iRc;
157 }
158
159 AssertMsg(SUCCEEDED(rc), ("SaveState WaitForCompletion failed with %Rhrc (%#08X)\n", rc, rc));
160
161 if (SUCCEEDED(rc))
162 ++saved;
163 }
164 LogFunc(("Saved %d VMs\n", saved));
165 }
166 else
167 {
168 LogFunc(("BATTERY LOW -- no action\n"));
169 }
170
171 break;
172 }
173
174 default:
175 /* nothing */;
176 }
177}
178/* vi: set tabstop=4 shiftwidth=4 expandtab: */
Note: See TracBrowser for help on using the repository browser.

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