[content shell] implement testRunner.overridePreference
[chromium-blink-merge.git] / content / common / child_process.cc
blobf0dfdc5db41736bcf83c05e2650c5a9c7d3b40d1
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 #include "content/common/child_process.h"
7 #if defined(OS_POSIX) && !defined(OS_ANDROID)
8 #include <signal.h> // For SigUSR1Handler below.
9 #endif
11 #include "base/message_loop.h"
12 #include "base/metrics/statistics_recorder.h"
13 #include "base/process_util.h"
14 #include "base/string_number_conversions.h"
15 #include "base/threading/thread.h"
16 #include "base/utf_string_conversions.h"
17 #include "content/common/child_thread.h"
19 #if defined(OS_ANDROID)
20 #include "base/debug/debugger.h"
21 #endif
23 #if defined(OS_POSIX) && !defined(OS_ANDROID)
24 static void SigUSR1Handler(int signal) { }
25 #endif
27 namespace content {
29 ChildProcess* ChildProcess::child_process_;
31 ChildProcess::ChildProcess()
32 : ref_count_(0),
33 shutdown_event_(true, false),
34 io_thread_("Chrome_ChildIOThread") {
35 DCHECK(!child_process_);
36 child_process_ = this;
38 base::StatisticsRecorder::Initialize();
40 // We can't recover from failing to start the IO thread.
41 CHECK(io_thread_.StartWithOptions(
42 base::Thread::Options(MessageLoop::TYPE_IO, 0)));
45 ChildProcess::~ChildProcess() {
46 DCHECK(child_process_ == this);
48 // Signal this event before destroying the child process. That way all
49 // background threads can cleanup.
50 // For example, in the renderer the RenderThread instances will be able to
51 // notice shutdown before the render process begins waiting for them to exit.
52 shutdown_event_.Signal();
54 // Kill the main thread object before nulling child_process_, since
55 // destruction code might depend on it.
56 main_thread_.reset();
58 child_process_ = NULL;
61 ChildThread* ChildProcess::main_thread() {
62 return main_thread_.get();
65 void ChildProcess::set_main_thread(ChildThread* thread) {
66 main_thread_.reset(thread);
69 void ChildProcess::AddRefProcess() {
70 DCHECK(!main_thread_.get() || // null in unittests.
71 MessageLoop::current() == main_thread_->message_loop());
72 ref_count_++;
75 void ChildProcess::ReleaseProcess() {
76 DCHECK(!main_thread_.get() || // null in unittests.
77 MessageLoop::current() == main_thread_->message_loop());
78 DCHECK(ref_count_);
79 DCHECK(child_process_);
80 if (--ref_count_)
81 return;
83 if (main_thread_.get()) // null in unittests.
84 main_thread_->OnProcessFinalRelease();
87 base::WaitableEvent* ChildProcess::GetShutDownEvent() {
88 DCHECK(child_process_);
89 return &child_process_->shutdown_event_;
92 void ChildProcess::WaitForDebugger(const std::string& label) {
93 #if defined(OS_WIN)
94 #if defined(GOOGLE_CHROME_BUILD)
95 std::string title = "Google Chrome";
96 #else // CHROMIUM_BUILD
97 std::string title = "Chromium";
98 #endif // CHROMIUM_BUILD
99 title += " ";
100 title += label; // makes attaching to process easier
101 std::string message = label;
102 message += " starting with pid: ";
103 message += base::IntToString(base::GetCurrentProcId());
104 ::MessageBox(NULL, UTF8ToWide(message).c_str(), UTF8ToWide(title).c_str(),
105 MB_OK | MB_SETFOREGROUND);
106 #elif defined(OS_POSIX)
107 #if defined(OS_ANDROID)
108 LOG(ERROR) << label << " waiting for GDB.";
109 // Wait 24 hours for a debugger to be attached to the current process.
110 base::debug::WaitForDebugger(24 * 60 * 60, false);
111 #else
112 // TODO(playmobil): In the long term, overriding this flag doesn't seem
113 // right, either use our own flag or open a dialog we can use.
114 // This is just to ease debugging in the interim.
115 LOG(ERROR) << label
116 << " ("
117 << getpid()
118 << ") paused waiting for debugger to attach @ pid";
119 // Install a signal handler so that pause can be woken.
120 struct sigaction sa;
121 memset(&sa, 0, sizeof(sa));
122 sa.sa_handler = SigUSR1Handler;
123 sigaction(SIGUSR1, &sa, NULL);
125 pause();
126 #endif // defined(OS_ANDROID)
127 #endif // defined(OS_POSIX)
130 } // namespace content