1 /* -*- Mode: C++; tab-width: 8; 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 file,
5  * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #ifndef mozilla_PreallocatedProcessManager_h
8 #define mozilla_PreallocatedProcessManager_h
9 
10 #include "base/basictypes.h"
11 #include "nsCOMPtr.h"
12 #include "nsIObserver.h"
13 
14 namespace mozilla {
15 namespace dom {
16 class ContentParent;
17 } // namespace dom
18 
19 /**
20  * This class manages a ContentParent that it starts up ahead of any particular
21  * need.  You can then call Take() to get this process and use it.  Since we
22  * already started it up, it should be ready for use faster than if you'd
23  * created the process when you needed it.
24  *
25  * This class watches the dom.ipc.processPrelaunch.enabled pref.  If it changes
26  * from false to true, it preallocates a process.  If it changes from true to
27  * false, it kills the preallocated process, if any.
28  *
29  * We don't expect this pref to flip between true and false in production, but
30  * flipping the pref is important for tests.
31  *
32  * The static methods here are implemented by forwarding calls on to a
33  * PreallocatedProcessManagerImpl singleton class, so if you add a new static
34  * method here, you'll need to write a corresponding public method on the
35  * singleton.
36  */
37 class PreallocatedProcessManager final
38 {
39   typedef mozilla::dom::ContentParent ContentParent;
40 
41 public:
42   /**
43    * Create a process after a delay.  We wait for a period of time (specified
44    * by the dom.ipc.processPrelaunch.delayMs pref), then wait for this process
45    * to go idle, then allocate the new process.
46    *
47    * If the dom.ipc.processPrelaunch.enabled pref is false, or if we already
48    * have a preallocated process, this function does nothing.
49    */
50   static void AllocateAfterDelay();
51 
52   /**
53    * Create a process once this process goes idle.
54    *
55    * If the dom.ipc.processPrelaunch.enabled pref is false, or if we already
56    * have a preallocated process, this function does nothing.
57    */
58   static void AllocateOnIdle();
59 
60   /**
61    * Create a process right now.
62    *
63    * If the dom.ipc.processPrelaunch.enabled pref is false, or if we already
64    * have a preallocated process, this function does nothing.
65    */
66   static void AllocateNow();
67 
68   /**
69    * Take the preallocated process, if we have one.  If we don't have one, this
70    * returns null.
71    *
72    * If you call Take() twice in a row, the second call is guaranteed to return
73    * null.
74    *
75    * After you Take() the preallocated process, you need to call one of the
76    * Allocate* functions (or change the dom.ipc.processPrelaunch pref from
77    * false to true) before we'll create a new process.
78    */
79   static already_AddRefed<ContentParent> Take();
80 
81 private:
82   PreallocatedProcessManager();
83   DISALLOW_EVIL_CONSTRUCTORS(PreallocatedProcessManager);
84 };
85 
86 } // namespace mozilla
87 
88 #endif // defined mozilla_PreallocatedProcessManager_h
89