summaryrefslogtreecommitdiffstats
path: root/netwerk/protocol/about/nsAboutProtocolUtils.h
blob: ef508dce2e2c7fb7154a8c513e3d673032b3cfb6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef nsAboutProtocolUtils_h
#define nsAboutProtocolUtils_h

#include "nsIURI.h"
#include "nsString.h"
#include "nsReadableUtils.h"
#include "nsIAboutModule.h"
#include "nsServiceManagerUtils.h"
#include "prtime.h"

[[nodiscard]] inline nsresult NS_GetAboutModuleName(nsIURI* aAboutURI,
                                                    nsCString& aModule) {
  NS_ASSERTION(aAboutURI->SchemeIs("about"),
               "should be used only on about: URIs");

  nsresult rv = aAboutURI->GetPathQueryRef(aModule);
  NS_ENSURE_SUCCESS(rv, rv);

  int32_t f = aModule.FindCharInSet("#?"_ns);
  if (f != kNotFound) {
    aModule.Truncate(f);
  }

  // convert to lowercase, as all about: modules are lowercase
  ToLowerCase(aModule);
  return NS_OK;
}

inline nsresult NS_GetAboutModule(nsIURI* aAboutURI, nsIAboutModule** aModule) {
  MOZ_ASSERT(aAboutURI, "Must have URI");

  nsAutoCString contractID;
  nsresult rv = NS_GetAboutModuleName(aAboutURI, contractID);
  if (NS_FAILED(rv)) return rv;

  // look up a handler to deal with "what"
  contractID.InsertLiteral(NS_ABOUT_MODULE_CONTRACTID_PREFIX, 0);

  return CallGetService(contractID.get(), aModule);
}

inline PRTime SecondsToPRTime(uint32_t t_sec) {
  return (PRTime)t_sec * PR_USEC_PER_SEC;
}

inline void PrintTimeString(char* buf, uint32_t bufsize, uint32_t t_sec) {
  PRExplodedTime et;
  PRTime t_usec = SecondsToPRTime(t_sec);
  PR_ExplodeTime(t_usec, PR_LocalTimeParameters, &et);
  PR_FormatTime(buf, bufsize, "%Y-%m-%d %H:%M:%S", &et);
}

#endif