Bug 1883706: part 3) Implement `createHTML`, `createScript` and `createScriptURL...
[gecko.git] / hal / windows / WindowsProcessPriority.cpp
blobfe9b78133b32bb62f086776eef67bde24d1e393e
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "Hal.h"
6 #include "HalLog.h"
7 #include "nsWindowsHelpers.h" // for nsAutoHandle and nsModuleHandle
8 #include "mozilla/StaticPrefs_dom.h"
10 #include <windows.h>
12 using namespace mozilla::hal;
14 namespace mozilla {
15 namespace hal_impl {
17 void SetProcessPriority(int aPid, ProcessPriority aPriority) {
18 HAL_LOG("WindowsProcessPriority - SetProcessPriority(%d, %s)\n", aPid,
19 ProcessPriorityToString(aPriority));
21 nsAutoHandle processHandle(
22 ::OpenProcess(PROCESS_SET_INFORMATION, FALSE, aPid));
23 if (processHandle) {
24 DWORD priority = NORMAL_PRIORITY_CLASS;
25 if (aPriority == PROCESS_PRIORITY_BACKGROUND) {
26 priority = IDLE_PRIORITY_CLASS;
27 } else if (aPriority == PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE) {
28 priority = BELOW_NORMAL_PRIORITY_CLASS;
31 if (::SetPriorityClass(processHandle, priority)) {
32 HAL_LOG("WindowsProcessPriority - priority set to %d for pid %d\n",
33 aPriority, aPid);
36 // Set the process into or out of EcoQoS.
37 static bool alreadyInitialized = false;
38 static decltype(::SetProcessInformation)* setProcessInformation = nullptr;
39 if (!alreadyInitialized) {
40 if (aPriority == PROCESS_PRIORITY_PARENT_PROCESS ||
41 !StaticPrefs::dom_ipc_processPriorityManager_backgroundUsesEcoQoS()) {
42 return;
45 alreadyInitialized = true;
46 // SetProcessInformation only exists on Windows 8 and later.
47 nsModuleHandle module(LoadLibrary(L"Kernel32.dll"));
48 if (module) {
49 setProcessInformation =
50 (decltype(::SetProcessInformation)*)GetProcAddress(
51 module, "SetProcessInformation");
54 if (!setProcessInformation) {
55 return;
58 PROCESS_POWER_THROTTLING_STATE PowerThrottling;
59 RtlZeroMemory(&PowerThrottling, sizeof(PowerThrottling));
60 PowerThrottling.Version = PROCESS_POWER_THROTTLING_CURRENT_VERSION;
61 PowerThrottling.ControlMask = PROCESS_POWER_THROTTLING_EXECUTION_SPEED;
62 PowerThrottling.StateMask =
63 (aPriority == PROCESS_PRIORITY_BACKGROUND) &&
64 StaticPrefs::
65 dom_ipc_processPriorityManager_backgroundUsesEcoQoS()
66 ? PROCESS_POWER_THROTTLING_EXECUTION_SPEED
67 : 0;
68 if (setProcessInformation(processHandle, ProcessPowerThrottling,
69 &PowerThrottling, sizeof(PowerThrottling))) {
70 HAL_LOG("SetProcessInformation(%d, %s)\n", aPid,
71 aPriority == PROCESS_PRIORITY_BACKGROUND ? "eco" : "normal");
72 } else {
73 HAL_LOG("SetProcessInformation failed for %d\n", aPid);
78 } // namespace hal_impl
79 } // namespace mozilla