AndroidProcessPriority.cpp (2322B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 6 7 #include "Hal.h" 8 9 #include "mozilla/java/GeckoProcessManagerWrappers.h" 10 #include "mozilla/java/GeckoProcessTypeWrappers.h" 11 #include "mozilla/java/ServiceAllocatorWrappers.h" 12 13 using namespace mozilla::hal; 14 15 /** 16 * Bucket the Gecko HAL process priority level into one of the three Android 17 * priority levels. 18 */ 19 static mozilla::java::ServiceAllocator::PriorityLevel::LocalRef 20 ToJavaPriorityLevel(const ProcessPriority aPriority) { 21 if (aPriority >= PROCESS_PRIORITY_FOREGROUND) { 22 return mozilla::java::ServiceAllocator::PriorityLevel::FOREGROUND(); 23 } else if (aPriority <= PROCESS_PRIORITY_PREALLOC && 24 aPriority >= PROCESS_PRIORITY_BACKGROUND_PERCEIVABLE) { 25 return mozilla::java::ServiceAllocator::PriorityLevel::BACKGROUND(); 26 } 27 28 return mozilla::java::ServiceAllocator::PriorityLevel::IDLE(); 29 } 30 31 namespace mozilla { 32 namespace hal_impl { 33 34 void SetProcessPriority(int aPid, ProcessPriority aPriority) { 35 if (aPriority == PROCESS_PRIORITY_PARENT_PROCESS) { 36 // This is the parent process itself, which we do not control. 37 return; 38 } 39 40 const int32_t intPriority = static_cast<int32_t>(aPriority); 41 if (intPriority < 0 || intPriority >= NUM_PROCESS_PRIORITY) { 42 return; 43 } 44 45 auto contentProcType = java::GeckoProcessType::CONTENT(); 46 auto selector = 47 java::GeckoProcessManager::Selector::New(contentProcType, aPid); 48 auto priorityLevel = ToJavaPriorityLevel(aPriority); 49 50 // To Android, a lower-valued integer is a higher relative priority. 51 // We take the integer value of the enum and subtract it from the value 52 // of the highest Gecko priority level to obtain a 0-based indicator of 53 // the relative priority within the Java PriorityLevel. 54 const int32_t relativeImportance = 55 (static_cast<int32_t>(NUM_PROCESS_PRIORITY) - 1) - intPriority; 56 57 java::GeckoProcessManager::SetProcessPriority(selector, priorityLevel, 58 relativeImportance); 59 } 60 61 } // namespace hal_impl 62 } // namespace mozilla