1 // Copyright (c) 2011 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/sandbox_init_mac.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "content/common/sandbox_mac.h"
11 #include "content/public/common/content_switches.h"
12 #include "content/public/common/sandbox_init.h"
16 bool InitializeSandbox(int sandbox_type
, const base::FilePath
& allowed_dir
) {
17 // Warm up APIs before turning on the sandbox.
18 Sandbox::SandboxWarmup(sandbox_type
);
20 // Actually sandbox the process.
21 return Sandbox::EnableSandbox(sandbox_type
, allowed_dir
);
24 // Fill in |sandbox_type| and |allowed_dir| based on the command line, returns
25 // false if the current process type doesn't need to be sandboxed or if the
26 // sandbox was disabled from the command line.
27 bool GetSandboxTypeFromCommandLine(int* sandbox_type
,
28 base::FilePath
* allowed_dir
) {
33 *allowed_dir
= base::FilePath(); // Empty by default.
35 const base::CommandLine
& command_line
=
36 *base::CommandLine::ForCurrentProcess();
37 if (command_line
.HasSwitch(switches::kNoSandbox
))
40 std::string process_type
=
41 command_line
.GetSwitchValueASCII(switches::kProcessType
);
42 if (process_type
.empty()) {
43 // Browser process isn't sandboxed.
45 } else if (process_type
== switches::kRendererProcess
) {
46 *sandbox_type
= SANDBOX_TYPE_RENDERER
;
47 } else if (process_type
== switches::kUtilityProcess
) {
48 // Utility process sandbox.
49 *sandbox_type
= SANDBOX_TYPE_UTILITY
;
51 command_line
.GetSwitchValuePath(switches::kUtilityProcessAllowedDir
);
52 } else if (process_type
== switches::kGpuProcess
) {
53 if (command_line
.HasSwitch(switches::kDisableGpuSandbox
))
55 *sandbox_type
= SANDBOX_TYPE_GPU
;
56 } else if ((process_type
== switches::kPluginProcess
) ||
57 (process_type
== switches::kPpapiBrokerProcess
)) {
59 } else if (process_type
== switches::kPpapiPluginProcess
) {
60 *sandbox_type
= SANDBOX_TYPE_PPAPI
;
62 // This is a process which we don't know about, i.e. an embedder-defined
63 // process. If the embedder wants it sandboxed, they have a chance to return
64 // the sandbox profile in ContentClient::GetSandboxProfileForSandboxType.
70 bool InitializeSandbox() {
72 base::FilePath allowed_dir
;
73 if (!GetSandboxTypeFromCommandLine(&sandbox_type
, &allowed_dir
))
75 return InitializeSandbox(sandbox_type
, allowed_dir
);
78 bool BrokerDuplicateSharedMemoryHandle(
79 const base::SharedMemoryHandle
& source_handle
,
80 base::ProcessId target_process_id
,
81 base::SharedMemoryHandle
* target_handle
) {
82 *target_handle
= base::SharedMemory::DuplicateHandle(source_handle
);
83 return base::SharedMemory::IsHandleValid(*target_handle
);
86 } // namespace content