1// Copyright 2018 The Chromium Embedded Framework Authors. Portions Copyright 2// 2018 the Chromium Authors. All rights reserved. Use of this source code is 3// governed by a BSD-style license that can be found in the LICENSE file. 4 5#include <mach-o/dyld.h> 6#include <stdio.h> 7 8#include <memory> 9 10#include "sandbox/mac/seatbelt_exec.h" 11 12#include "include/cef_sandbox_mac.h" 13 14void* cef_sandbox_initialize(int argc, char** argv) { 15 uint32_t exec_path_size = 0; 16 int rv = _NSGetExecutablePath(NULL, &exec_path_size); 17 if (rv != -1) { 18 return NULL; 19 } 20 21 std::unique_ptr<char[]> exec_path(new char[exec_path_size]); 22 rv = _NSGetExecutablePath(exec_path.get(), &exec_path_size); 23 if (rv != 0) { 24 return NULL; 25 } 26 27 sandbox::SeatbeltExecServer::CreateFromArgumentsResult seatbelt = 28 sandbox::SeatbeltExecServer::CreateFromArguments(exec_path.get(), argc, 29 argv); 30 if (seatbelt.sandbox_required) { 31 if (!seatbelt.server) { 32 fprintf(stderr, "Failed to create the seatbelt sandbox server.\n"); 33 return NULL; 34 } 35 if (!seatbelt.server->InitializeSandbox()) { 36 fprintf(stderr, "Failed to initialize the sandbox.\n"); 37 return NULL; 38 } 39 } 40 41 auto* copy = new sandbox::SeatbeltExecServer::CreateFromArgumentsResult(); 42 copy->sandbox_required = seatbelt.sandbox_required; 43 copy->server.swap(seatbelt.server); 44 return copy; 45} 46 47void cef_sandbox_destroy(void* sandbox_context) { 48 delete static_cast<sandbox::SeatbeltExecServer::CreateFromArgumentsResult*>( 49 sandbox_context); 50} 51 52CefScopedSandboxContext::CefScopedSandboxContext() : sandbox_context_(NULL) {} 53 54CefScopedSandboxContext::~CefScopedSandboxContext() { 55 if (sandbox_context_) { 56 cef_sandbox_destroy(sandbox_context_); 57 } 58} 59 60bool CefScopedSandboxContext::Initialize(int argc, char** argv) { 61 if (sandbox_context_) 62 return false; 63 sandbox_context_ = cef_sandbox_initialize(argc, argv); 64 return !!sandbox_context_; 65} 66