FetchPriority.cpp (3000B)
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 http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/FetchPriority.h" 8 9 #include "mozilla/Assertions.h" 10 #include "mozilla/Logging.h" 11 #include "mozilla/dom/RequestBinding.h" 12 #include "nsISupportsPriority.h" 13 #include "nsStringFwd.h" 14 15 namespace mozilla::dom { 16 17 FetchPriority ToFetchPriority(RequestPriority aRequestPriority) { 18 switch (aRequestPriority) { 19 case RequestPriority::High: { 20 return FetchPriority::High; 21 } 22 case RequestPriority::Low: { 23 return FetchPriority::Low; 24 } 25 case RequestPriority::Auto: { 26 return FetchPriority::Auto; 27 } 28 default: { 29 MOZ_ASSERT_UNREACHABLE(); 30 return FetchPriority::Auto; 31 } 32 } 33 } 34 35 #ifdef DEBUG 36 constexpr auto kPriorityHighest = "PRIORITY_HIGHEST"_ns; 37 constexpr auto kPriorityHigh = "PRIORITY_HIGH"_ns; 38 constexpr auto kPriorityNormal = "PRIORITY_NORMAL"_ns; 39 constexpr auto kPriorityLow = "PRIORITY_LOW"_ns; 40 constexpr auto kPriorityLowest = "PRIORITY_LOWEST"_ns; 41 constexpr auto kPriorityUnknown = "UNKNOWN"_ns; 42 43 /** 44 * See <nsISupportsPriority.idl>. 45 */ 46 static void SupportsPriorityToString(int32_t aSupportsPriority, 47 nsACString& aResult) { 48 switch (aSupportsPriority) { 49 case nsISupportsPriority::PRIORITY_HIGHEST: { 50 aResult = kPriorityHighest; 51 break; 52 } 53 case nsISupportsPriority::PRIORITY_HIGH: { 54 aResult = kPriorityHigh; 55 break; 56 } 57 case nsISupportsPriority::PRIORITY_NORMAL: { 58 aResult = kPriorityNormal; 59 break; 60 } 61 case nsISupportsPriority::PRIORITY_LOW: { 62 aResult = kPriorityLow; 63 break; 64 } 65 case nsISupportsPriority::PRIORITY_LOWEST: { 66 aResult = kPriorityLowest; 67 break; 68 } 69 default: { 70 aResult = kPriorityUnknown; 71 break; 72 } 73 } 74 } 75 76 static const char* ToString(FetchPriority aFetchPriority) { 77 switch (aFetchPriority) { 78 case FetchPriority::Auto: { 79 return kFetchPriorityAttributeValueAuto; 80 } 81 case FetchPriority::Low: { 82 return kFetchPriorityAttributeValueLow; 83 } 84 case FetchPriority::High: { 85 return kFetchPriorityAttributeValueHigh; 86 } 87 default: { 88 MOZ_ASSERT_UNREACHABLE(); 89 return kFetchPriorityAttributeValueAuto; 90 } 91 } 92 } 93 #endif // DEBUG 94 95 void LogPriorityMapping(LazyLogModule& aLazyLogModule, 96 FetchPriority aFetchPriority, 97 int32_t aSupportsPriority) { 98 #ifdef DEBUG 99 nsDependentCString supportsPriority; 100 SupportsPriorityToString(aSupportsPriority, supportsPriority); 101 MOZ_LOG(aLazyLogModule, LogLevel::Debug, 102 ("Mapping priority: %s -> %s", ToString(aFetchPriority), 103 supportsPriority.get())); 104 #endif // DEBUG 105 } 106 } // namespace mozilla::dom