nsAboutProtocolUtils.h (2009B)
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 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef nsAboutProtocolUtils_h 6 #define nsAboutProtocolUtils_h 7 8 #include "mozilla/Try.h" 9 #include "nsIURI.h" 10 #include "nsString.h" 11 #include "nsReadableUtils.h" 12 #include "nsIAboutModule.h" 13 #include "nsServiceManagerUtils.h" 14 #include "prtime.h" 15 16 [[nodiscard]] inline nsresult NS_GetAboutModuleName(nsIURI* aAboutURI, 17 nsCString& aModule) { 18 NS_ASSERTION(aAboutURI->SchemeIs("about"), 19 "should be used only on about: URIs"); 20 21 MOZ_TRY(aAboutURI->GetPathQueryRef(aModule)); 22 23 int32_t f = aModule.FindCharInSet("#?"_ns); 24 if (f != kNotFound) { 25 aModule.Truncate(f); 26 } 27 28 // convert to lowercase, as all about: modules are lowercase 29 ToLowerCase(aModule); 30 return NS_OK; 31 } 32 33 [[nodiscard]] inline bool NS_IsContentAccessibleAboutURI(nsIURI* aURI) { 34 MOZ_ASSERT(aURI->SchemeIs("about"), "Should be used only on about: URIs"); 35 nsAutoCString name; 36 if (NS_WARN_IF(NS_FAILED(NS_GetAboutModuleName(aURI, name)))) { 37 return true; 38 } 39 return name.EqualsLiteral("blank") || name.EqualsLiteral("srcdoc"); 40 } 41 42 inline nsresult NS_GetAboutModule(nsIURI* aAboutURI, nsIAboutModule** aModule) { 43 MOZ_ASSERT(aAboutURI, "Must have URI"); 44 45 nsAutoCString contractID; 46 MOZ_TRY(NS_GetAboutModuleName(aAboutURI, contractID)); 47 48 // look up a handler to deal with "what" 49 contractID.InsertLiteral(NS_ABOUT_MODULE_CONTRACTID_PREFIX, 0); 50 51 return CallGetService(contractID.get(), aModule); 52 } 53 54 inline PRTime SecondsToPRTime(uint32_t t_sec) { 55 return (PRTime)t_sec * PR_USEC_PER_SEC; 56 } 57 58 inline void PrintTimeString(char* buf, uint32_t bufsize, uint32_t t_sec) { 59 PRExplodedTime et; 60 PRTime t_usec = SecondsToPRTime(t_sec); 61 PR_ExplodeTime(t_usec, PR_LocalTimeParameters, &et); 62 PR_FormatTime(buf, bufsize, "%Y-%m-%d %H:%M:%S", &et); 63 } 64 65 #endif