1// Copyright 2014 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#include "content/browser/bootstrap_sandbox_mac.h"
6
7#include "base/logging.h"
8#include "base/mac/mac_util.h"
9#include "base/memory/scoped_ptr.h"
10#include "base/memory/singleton.h"
11#include "content/common/sandbox_init_mac.h"
12#include "content/public/browser/browser_child_process_observer.h"
13#include "content/public/browser/child_process_data.h"
14#include "content/public/common/sandbox_type_mac.h"
15#include "sandbox/mac/bootstrap_sandbox.h"
16
17namespace content {
18
19namespace {
20
21// This class is responsible for creating the BootstrapSandbox global
22// singleton, as well as registering all associated policies with it.
23class BootstrapSandboxPolicy : public BrowserChildProcessObserver {
24 public:
25  static BootstrapSandboxPolicy* GetInstance();
26
27  sandbox::BootstrapSandbox* sandbox() const {
28    return sandbox_.get();
29  }
30
31  // BrowserChildProcessObserver:
32  virtual void BrowserChildProcessHostDisconnected(
33      const ChildProcessData& data) OVERRIDE;
34  virtual void BrowserChildProcessCrashed(
35      const ChildProcessData& data) OVERRIDE;
36
37 private:
38  friend struct DefaultSingletonTraits<BootstrapSandboxPolicy>;
39  BootstrapSandboxPolicy();
40  virtual ~BootstrapSandboxPolicy();
41
42  void RegisterSandboxPolicies();
43
44  scoped_ptr<sandbox::BootstrapSandbox> sandbox_;
45};
46
47BootstrapSandboxPolicy* BootstrapSandboxPolicy::GetInstance() {
48  return Singleton<BootstrapSandboxPolicy>::get();
49}
50
51void BootstrapSandboxPolicy::BrowserChildProcessHostDisconnected(
52      const ChildProcessData& data) {
53  sandbox()->ChildDied(data.handle);
54}
55
56void BootstrapSandboxPolicy::BrowserChildProcessCrashed(
57      const ChildProcessData& data) {
58  sandbox()->ChildDied(data.handle);
59}
60
61BootstrapSandboxPolicy::BootstrapSandboxPolicy()
62    : sandbox_(sandbox::BootstrapSandbox::Create()) {
63  CHECK(sandbox_.get());
64  BrowserChildProcessObserver::Add(this);
65  RegisterSandboxPolicies();
66}
67
68BootstrapSandboxPolicy::~BootstrapSandboxPolicy() {
69  BrowserChildProcessObserver::Remove(this);
70}
71
72void BootstrapSandboxPolicy::RegisterSandboxPolicies() {
73}
74
75}  // namespace
76
77bool ShouldEnableBootstrapSandbox() {
78  return base::mac::IsOSMountainLionOrEarlier() ||
79         base::mac::IsOSMavericks();
80}
81
82sandbox::BootstrapSandbox* GetBootstrapSandbox() {
83  return BootstrapSandboxPolicy::GetInstance()->sandbox();
84}
85
86}  // namespace content
87