Re-subimission of https://codereview.chromium.org/1041213003/
[chromium-blink-merge.git] / content / child / child_process.h
blobcf3173f7132c4ee64b7b242bd059c4ac6b0798f4
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_CHILD_CHILD_PROCESS_H_
6 #define CONTENT_CHILD_CHILD_PROCESS_H_
8 #include "base/basictypes.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/synchronization/waitable_event.h"
11 #include "base/threading/thread.h"
12 #include "content/common/content_export.h"
14 namespace content {
15 class ChildThreadImpl;
17 // Base class for child processes of the browser process (i.e. renderer and
18 // plugin host). This is a singleton object for each child process.
20 // During process shutdown the following sequence of actions happens in
21 // order.
23 // 1. ChildProcess::~ChildProcess() is called.
24 // 2. Shutdown event is fired. Background threads should stop.
25 // 3. ChildThreadImpl::Shutdown() is called. ChildThread is also deleted.
26 // 4. IO thread is stopped.
27 // 5. Main message loop exits.
28 // 6. Child process is now fully stopped.
30 // Note: IO thread outlives the ChildThreadImpl object.
31 class CONTENT_EXPORT ChildProcess {
32 public:
33 // Child processes should have an object that derives from this class.
34 // Normally you would immediately call set_main_thread after construction.
35 ChildProcess();
36 virtual ~ChildProcess();
38 // May be NULL if the main thread hasn't been set explicitly.
39 ChildThreadImpl* main_thread();
41 // Sets the object associated with the main thread of this process.
42 // Takes ownership of the pointer.
43 void set_main_thread(ChildThreadImpl* thread);
45 base::MessageLoop* io_message_loop() { return io_thread_.message_loop(); }
46 base::MessageLoopProxy* io_message_loop_proxy() {
47 return io_thread_.message_loop_proxy().get();
50 // A global event object that is signalled when the main thread's message
51 // loop exits. This gives background threads a way to observe the main
52 // thread shutting down. This can be useful when a background thread is
53 // waiting for some information from the browser process. If the browser
54 // process goes away prematurely, the background thread can at least notice
55 // the child processes's main thread exiting to determine that it should give
56 // up waiting.
57 // For example, see the renderer code used to implement GetCookies().
58 base::WaitableEvent* GetShutDownEvent();
60 // These are used for ref-counting the child process. The process shuts
61 // itself down when the ref count reaches 0.
62 // For example, in the renderer process, generally each tab managed by this
63 // process will hold a reference to the process, and release when closed.
64 void AddRefProcess();
65 void ReleaseProcess();
67 // Getter for the one ChildProcess object for this process. Can only be called
68 // on the main thread.
69 static ChildProcess* current();
71 static void WaitForDebugger(const std::string& label);
72 private:
73 int ref_count_;
75 // An event that will be signalled when we shutdown.
76 base::WaitableEvent shutdown_event_;
78 // The thread that handles IO events.
79 base::Thread io_thread_;
81 // NOTE: make sure that main_thread_ is listed after shutdown_event_, since
82 // it depends on it (indirectly through IPC::SyncChannel). Same for
83 // io_thread_.
84 scoped_ptr<ChildThreadImpl> main_thread_;
86 DISALLOW_COPY_AND_ASSIGN(ChildProcess);
89 } // namespace content
91 #endif // CONTENT_CHILD_CHILD_PROCESS_H_