summaryrefslogtreecommitdiffstats
path: root/js/xpconnect/src/XPCSelfHostedShmem.h
blob: 0fcb1ed05c016cf3bf414248117bfbb08bc64097 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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 xpcselfhostedshmem_h___
#define xpcselfhostedshmem_h___

#include "base/shared_memory.h"
#include "mozilla/UniquePtr.h"
#include "mozilla/UniquePtrExtensions.h"
#include "mozilla/Span.h"
#include "mozilla/StaticPtr.h"
#include "nsIMemoryReporter.h"
#include "nsIObserver.h"
#include "nsIThread.h"

namespace xpc {

// This class is a singleton which holds a file-mapping of the Self Hosted
// Stencil XDR, to be shared by the parent process with all the child processes.
//
// It is either initialized by the parent process by monitoring when the Self
// hosted stencil is produced, or by the content process by reading a shared
// memory-mapped file.
class SelfHostedShmem final : public nsIMemoryReporter {
 public:
  NS_DECL_THREADSAFE_ISUPPORTS
  NS_DECL_NSIMEMORYREPORTER

  // NOTE: This type is identical to JS::SelfHostedCache, but we repeat it to
  // avoid including JS engine API in ipc code.
  using ContentType = mozilla::Span<const uint8_t>;

  static SelfHostedShmem& GetSingleton();

  // Initialize this singleton with the content of the Self-hosted Stencil XDR.
  // This will be used to initialize the shared memory which would hold a copy.
  //
  // This function is not thread-safe and should be call at most once and from
  // the main thread.
  void InitFromParent(ContentType aXdr);

  // Initialize this singleton with the content coming from the parent process,
  // using a file handle which maps to the memory pages of the parent process.
  //
  // This function is not thread-safe and should be call at most once and from
  // the main thread.
  [[nodiscard]] bool InitFromChild(base::SharedMemoryHandle aHandle,
                                   size_t aLen);

  // Return a span over the read-only XDR content of the self-hosted stencil.
  ContentType Content() const;

  // Return the file handle which is under which the content is mapped.
  const mozilla::UniqueFileHandle& Handle() const;

  // Register this class to the memory reporter service.
  void InitMemoryReporter();

  // Unregister this class from the memory reporter service, and delete the
  // memory associated with the shared memory. As the memory is borrowed by the
  // JS engine, this function should be called after JS_Shutdown.
  //
  // NOTE: This is not using the Observer service which would call Shutdown
  // while JS code might still be running during shutdown process.
  static void Shutdown();

 private:
  SelfHostedShmem() = default;
  ~SelfHostedShmem() = default;

  static mozilla::StaticRefPtr<SelfHostedShmem> sSelfHostedXdr;

  // read-only file Handle used to transfer from the parent process to content
  // processes.
  mozilla::UniqueFileHandle mHandle;

  // Shared memory used by JS runtime initialization.
  mozilla::UniquePtr<base::SharedMemory> mMem;

  // Length of the content within the shared memory.
  size_t mLen = 0;
};

}  // namespace xpc

#endif  // !xpcselfhostedshmem_h___