Bug 1816170 - Disable perftest-on-autoland cron. r=aglavic
[gecko.git] / xpcom / base / RLBoxSandboxPool.h
blob47f389354896288c890063cabad01bd626030352
1 /* -*- Mode: C++; tab-width: 20; 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/. */
7 #ifndef SECURITY_RLBOX_SANDBOX_POOL_H_
8 #define SECURITY_RLBOX_SANDBOX_POOL_H_
10 #include "nsCOMPtr.h"
11 #include "nsITimer.h"
12 #include "nsTArray.h"
13 #include "nsINamed.h"
15 #include "mozilla/Mutex.h"
16 #include "mozilla/rlbox/rlbox_types.hpp"
18 namespace mozilla {
20 class RLBoxSandboxDataBase;
21 class RLBoxSandboxPoolData;
23 // The RLBoxSandboxPool class is used to manage a pool of sandboxes that are
24 // reused -- to save sandbox creation time and memory -- and automatically
25 // destroyed when no longer in used. The sandbox pool is threadsafe and can be
26 // used to share unused sandboxes across a thread pool.
28 // Each sandbox pool manages a particular kind of sandbox (e.g., expat
29 // sandboxes, woff2 sandboxes, etc.); this is largely because different
30 // sandboxes might have different callbacks and attacker assumptions. Hence,
31 // RLBoxSandboxPool is intended to be subclassed for the different kinds of
32 // sandbox pools. Each sandbox pool class needs to implement the
33 // CreateSandboxData() method, which returns a pointer to a RLBoxSandboxDataBase
34 // object. RLBoxSandboxDataBase itself should be subclassed to implement
35 // sandbox-specific details.
36 class RLBoxSandboxPool : public nsITimerCallback, public nsINamed {
37 public:
38 NS_DECL_THREADSAFE_ISUPPORTS
39 NS_DECL_NSITIMERCALLBACK
40 NS_DECL_NSINAMED
42 RLBoxSandboxPool(size_t aDelaySeconds = 10)
43 : mPool(),
44 mDelaySeconds(aDelaySeconds),
45 mMutex("RLBoxSandboxPool::mMutex"){};
47 void Push(UniquePtr<RLBoxSandboxDataBase> sbx);
48 // PopOrCreate returns a sandbox from the pool if the pool is not empty and
49 // tries to mint a new one otherwise. If creating a new sandbox fails, the
50 // function returns a nullptr. The parameter aMinSize is the minimum size of
51 // the sandbox memory.
52 UniquePtr<RLBoxSandboxPoolData> PopOrCreate(uint64_t aMinSize = 0);
54 protected:
55 // CreateSandboxData takes a parameter which is the size of the sandbox memory
56 virtual UniquePtr<RLBoxSandboxDataBase> CreateSandboxData(uint64_t aSize) = 0;
57 virtual ~RLBoxSandboxPool() = default;
59 private:
60 void StartTimer() MOZ_REQUIRES(mMutex);
61 void CancelTimer() MOZ_REQUIRES(mMutex);
63 nsTArray<UniquePtr<RLBoxSandboxDataBase>> mPool MOZ_GUARDED_BY(mMutex);
64 const size_t mDelaySeconds MOZ_GUARDED_BY(mMutex);
65 nsCOMPtr<nsITimer> mTimer MOZ_GUARDED_BY(mMutex);
66 mozilla::Mutex mMutex;
69 // The RLBoxSandboxDataBase class serves as the subclass for all sandbox data
70 // classes, which keep track of the RLBox sandbox and any relevant sandbox data
71 // (e.g., callbacks).
72 class RLBoxSandboxDataBase {
73 public:
74 const uint64_t mSize;
75 explicit RLBoxSandboxDataBase(uint64_t aSize) : mSize(aSize) {}
76 virtual ~RLBoxSandboxDataBase() = default;
79 // This class is used wrap sandbox data objects (RLBoxSandboxDataBase) when they
80 // are popped from sandbox pools. The wrapper destructor pushes the sandbox back
81 // into the pool.
82 class RLBoxSandboxPoolData {
83 public:
84 RLBoxSandboxPoolData(UniquePtr<RLBoxSandboxDataBase> aSbxData,
85 RefPtr<RLBoxSandboxPool> aPool) {
86 mSbxData = std::move(aSbxData);
87 mPool = aPool;
88 MOZ_COUNT_CTOR(RLBoxSandboxPoolData);
91 RLBoxSandboxDataBase* SandboxData() const { return mSbxData.get(); };
93 ~RLBoxSandboxPoolData() {
94 mPool->Push(std::move(mSbxData));
95 MOZ_COUNT_DTOR(RLBoxSandboxPoolData);
98 private:
99 UniquePtr<RLBoxSandboxDataBase> mSbxData;
100 RefPtr<RLBoxSandboxPool> mPool;
103 } // namespace mozilla
105 #endif