• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "mojo/edk/embedder/embedder.h"
6 
7 #include <stdint.h>
8 
9 #include "base/bind.h"
10 #include "base/location.h"
11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/rand_util.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/task_runner.h"
16 #include "base/threading/thread_task_runner_handle.h"
17 #include "mojo/edk/embedder/embedder_internal.h"
18 #include "mojo/edk/embedder/entrypoints.h"
19 #include "mojo/edk/embedder/platform_channel_pair.h"
20 #include "mojo/edk/embedder/process_delegate.h"
21 #include "mojo/edk/system/core.h"
22 
23 #if !defined(OS_NACL)
24 #include "crypto/random.h"
25 #endif
26 
27 namespace mojo {
28 namespace edk {
29 
30 class Core;
31 class PlatformSupport;
32 
33 namespace internal {
34 
35 Core* g_core;
36 ProcessDelegate* g_process_delegate;
37 
GetCore()38 Core* GetCore() { return g_core; }
39 
40 }  // namespace internal
41 
SetMaxMessageSize(size_t bytes)42 void SetMaxMessageSize(size_t bytes) {
43 }
44 
ChildProcessLaunched(base::ProcessHandle child_process,ScopedPlatformHandle server_pipe,const std::string & child_token)45 void ChildProcessLaunched(base::ProcessHandle child_process,
46                           ScopedPlatformHandle server_pipe,
47                           const std::string& child_token) {
48   ChildProcessLaunched(child_process, std::move(server_pipe),
49                        child_token, ProcessErrorCallback());
50 }
51 
ChildProcessLaunched(base::ProcessHandle child_process,ScopedPlatformHandle server_pipe,const std::string & child_token,const ProcessErrorCallback & process_error_callback)52 void ChildProcessLaunched(base::ProcessHandle child_process,
53                           ScopedPlatformHandle server_pipe,
54                           const std::string& child_token,
55                           const ProcessErrorCallback& process_error_callback) {
56   CHECK(internal::g_core);
57   internal::g_core->AddChild(child_process, std::move(server_pipe),
58                              child_token, process_error_callback);
59 }
60 
ChildProcessLaunchFailed(const std::string & child_token)61 void ChildProcessLaunchFailed(const std::string& child_token) {
62   CHECK(internal::g_core);
63   internal::g_core->ChildLaunchFailed(child_token);
64 }
65 
SetParentPipeHandle(ScopedPlatformHandle pipe)66 void SetParentPipeHandle(ScopedPlatformHandle pipe) {
67   CHECK(internal::g_core);
68   internal::g_core->InitChild(std::move(pipe));
69 }
70 
SetParentPipeHandleFromCommandLine()71 void SetParentPipeHandleFromCommandLine() {
72   ScopedPlatformHandle platform_channel =
73       PlatformChannelPair::PassClientHandleFromParentProcess(
74           *base::CommandLine::ForCurrentProcess());
75   CHECK(platform_channel.is_valid());
76   SetParentPipeHandle(std::move(platform_channel));
77 }
78 
Init()79 void Init() {
80   MojoSystemThunks thunks = MakeSystemThunks();
81   size_t expected_size = MojoEmbedderSetSystemThunks(&thunks);
82   DCHECK_EQ(expected_size, sizeof(thunks));
83 
84   internal::g_core = new Core();
85 }
86 
CreatePlatformHandleWrapper(ScopedPlatformHandle platform_handle,MojoHandle * platform_handle_wrapper_handle)87 MojoResult CreatePlatformHandleWrapper(
88     ScopedPlatformHandle platform_handle,
89     MojoHandle* platform_handle_wrapper_handle) {
90   return internal::g_core->CreatePlatformHandleWrapper(
91       std::move(platform_handle), platform_handle_wrapper_handle);
92 }
93 
PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle,ScopedPlatformHandle * platform_handle)94 MojoResult PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle,
95                                      ScopedPlatformHandle* platform_handle) {
96   return internal::g_core->PassWrappedPlatformHandle(
97       platform_handle_wrapper_handle, platform_handle);
98 }
99 
CreateSharedBufferWrapper(base::SharedMemoryHandle shared_memory_handle,size_t num_bytes,bool read_only,MojoHandle * mojo_wrapper_handle)100 MojoResult CreateSharedBufferWrapper(
101     base::SharedMemoryHandle shared_memory_handle,
102     size_t num_bytes,
103     bool read_only,
104     MojoHandle* mojo_wrapper_handle) {
105   return internal::g_core->CreateSharedBufferWrapper(
106       shared_memory_handle, num_bytes, read_only, mojo_wrapper_handle);
107 }
108 
PassSharedMemoryHandle(MojoHandle mojo_handle,base::SharedMemoryHandle * shared_memory_handle,size_t * num_bytes,bool * read_only)109 MojoResult PassSharedMemoryHandle(
110     MojoHandle mojo_handle,
111     base::SharedMemoryHandle* shared_memory_handle,
112     size_t* num_bytes,
113     bool* read_only) {
114   return internal::g_core->PassSharedMemoryHandle(
115       mojo_handle, shared_memory_handle, num_bytes, read_only);
116 }
117 
InitIPCSupport(ProcessDelegate * process_delegate,scoped_refptr<base::TaskRunner> io_thread_task_runner)118 void InitIPCSupport(ProcessDelegate* process_delegate,
119                     scoped_refptr<base::TaskRunner> io_thread_task_runner) {
120   CHECK(internal::g_core);
121   internal::g_core->SetIOTaskRunner(io_thread_task_runner);
122   internal::g_process_delegate = process_delegate;
123 }
124 
ShutdownIPCSupport()125 void ShutdownIPCSupport() {
126   CHECK(internal::g_process_delegate);
127   CHECK(internal::g_core);
128   internal::g_core->RequestShutdown(
129       base::Bind(&ProcessDelegate::OnShutdownComplete,
130                  base::Unretained(internal::g_process_delegate)));
131 }
132 
133 #if defined(OS_MACOSX) && !defined(OS_IOS)
SetMachPortProvider(base::PortProvider * port_provider)134 void SetMachPortProvider(base::PortProvider* port_provider) {
135   DCHECK(port_provider);
136   internal::g_core->SetMachPortProvider(port_provider);
137 }
138 #endif
139 
CreateParentMessagePipe(const std::string & token,const std::string & child_token)140 ScopedMessagePipeHandle CreateParentMessagePipe(
141     const std::string& token, const std::string& child_token) {
142   CHECK(internal::g_process_delegate);
143   return internal::g_core->CreateParentMessagePipe(token, child_token);
144 }
145 
CreateChildMessagePipe(const std::string & token)146 ScopedMessagePipeHandle CreateChildMessagePipe(const std::string& token) {
147   CHECK(internal::g_process_delegate);
148   return internal::g_core->CreateChildMessagePipe(token);
149 }
150 
GenerateRandomToken()151 std::string GenerateRandomToken() {
152   char random_bytes[16];
153 #if defined(OS_NACL)
154   // Not secure. For NaCl only!
155   base::RandBytes(random_bytes, 16);
156 #else
157   crypto::RandBytes(random_bytes, 16);
158 #endif
159   return base::HexEncode(random_bytes, 16);
160 }
161 
SetProperty(MojoPropertyType type,const void * value)162 MojoResult SetProperty(MojoPropertyType type, const void* value) {
163   CHECK(internal::g_core);
164   return internal::g_core->SetProperty(type, value);
165 }
166 
167 }  // namespace edk
168 }  // namespace mojo
169