crash_handler_host_linux.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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.
4
5#ifndef CHROME_BROWSER_CRASH_HANDLER_HOST_LINUX_H_
6#define CHROME_BROWSER_CRASH_HANDLER_HOST_LINUX_H_
7#pragma once
8
9#include <string>
10
11#include "base/message_loop.h"
12#include "base/scoped_ptr.h"
13
14template <typename T> struct DefaultSingletonTraits;
15
16namespace base {
17class Thread;
18}
19
20// This is the base class for singleton objects which crash dump renderers and
21// plugins on Linux. We perform the crash dump from the browser because it
22// allows us to be outside the sandbox.
23//
24// PluginCrashHandlerHostLinux and RendererCrashHandlerHostLinux are singletons
25// that handle plugin and renderer crashes, respectively.
26//
27// Processes signal that they need to be dumped by sending a datagram over a
28// UNIX domain socket. All processes of the same type share the client end of
29// this socket which is installed in their descriptor table before exec.
30class CrashHandlerHostLinux : public MessageLoopForIO::Watcher,
31                              public MessageLoop::DestructionObserver {
32 public:
33  // Get the file descriptor which processes should be given in order to signal
34  // crashes to the browser.
35  int GetDeathSignalSocket() const {
36    return process_socket_;
37  }
38
39  // MessagePumbLibevent::Watcher impl:
40  virtual void OnFileCanWriteWithoutBlocking(int fd);
41  virtual void OnFileCanReadWithoutBlocking(int fd);
42
43  // MessageLoop::DestructionObserver impl:
44  virtual void WillDestroyCurrentMessageLoop();
45
46#if defined(USE_LINUX_BREAKPAD)
47  // Whether we are shutting down or not.
48  bool IsShuttingDown() const;
49#endif
50
51 protected:
52  CrashHandlerHostLinux();
53  virtual ~CrashHandlerHostLinux();
54
55#if defined(USE_LINUX_BREAKPAD)
56  // Only called in concrete subclasses.
57  void InitCrashUploaderThread();
58
59  std::string process_type_;
60#endif
61
62 private:
63  void Init();
64
65#if defined(USE_LINUX_BREAKPAD)
66  // This is here on purpose to make CrashHandlerHostLinux abstract.
67  virtual void SetProcessType() = 0;
68#endif
69
70  int process_socket_;
71  int browser_socket_;
72
73#if defined(USE_LINUX_BREAKPAD)
74  MessageLoopForIO::FileDescriptorWatcher file_descriptor_watcher_;
75  scoped_ptr<base::Thread> uploader_thread_;
76  bool shutting_down_;
77#endif
78
79  DISALLOW_COPY_AND_ASSIGN(CrashHandlerHostLinux);
80};
81
82class PluginCrashHandlerHostLinux : public CrashHandlerHostLinux {
83 public:
84  // Returns the singleton instance.
85  static PluginCrashHandlerHostLinux* GetInstance();
86
87 private:
88  friend struct DefaultSingletonTraits<PluginCrashHandlerHostLinux>;
89  PluginCrashHandlerHostLinux();
90  virtual ~PluginCrashHandlerHostLinux();
91
92#if defined(USE_LINUX_BREAKPAD)
93  virtual void SetProcessType();
94#endif
95
96  DISALLOW_COPY_AND_ASSIGN(PluginCrashHandlerHostLinux);
97};
98
99class RendererCrashHandlerHostLinux : public CrashHandlerHostLinux {
100 public:
101  // Returns the singleton instance.
102  static RendererCrashHandlerHostLinux* GetInstance();
103
104 private:
105  friend struct DefaultSingletonTraits<RendererCrashHandlerHostLinux>;
106  RendererCrashHandlerHostLinux();
107  virtual ~RendererCrashHandlerHostLinux();
108
109#if defined(USE_LINUX_BREAKPAD)
110  virtual void SetProcessType();
111#endif
112
113  DISALLOW_COPY_AND_ASSIGN(RendererCrashHandlerHostLinux);
114};
115
116#endif  // CHROME_BROWSER_CRASH_HANDLER_HOST_LINUX_H_
117