Bug 1869043 assert that graph set access is main thread only r=padenot
[gecko.git] / netwerk / protocol / about / nsAboutProtocolUtils.h
blobfdc92620f35453bc171e568e613489059563534a
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/. */
5 #ifndef nsAboutProtocolUtils_h
6 #define nsAboutProtocolUtils_h
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"
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");
21 MOZ_TRY(aAboutURI->GetPathQueryRef(aModule));
23 int32_t f = aModule.FindCharInSet("#?"_ns);
24 if (f != kNotFound) {
25 aModule.Truncate(f);
28 // convert to lowercase, as all about: modules are lowercase
29 ToLowerCase(aModule);
30 return NS_OK;
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;
39 return name.EqualsLiteral("blank") || name.EqualsLiteral("srcdoc");
42 inline nsresult NS_GetAboutModule(nsIURI* aAboutURI, nsIAboutModule** aModule) {
43 MOZ_ASSERT(aAboutURI, "Must have URI");
45 nsAutoCString contractID;
46 MOZ_TRY(NS_GetAboutModuleName(aAboutURI, contractID));
48 // look up a handler to deal with "what"
49 contractID.InsertLiteral(NS_ABOUT_MODULE_CONTRACTID_PREFIX, 0);
51 return CallGetService(contractID.get(), aModule);
54 inline PRTime SecondsToPRTime(uint32_t t_sec) {
55 return (PRTime)t_sec * PR_USEC_PER_SEC;
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);
65 #endif