| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/edk/embedder/embedder.h" | 5 #include "mojo/edk/embedder/embedder.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/location.h" | 11 #include "base/location.h" |
| 12 #include "base/logging.h" | 12 #include "base/logging.h" |
| 13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/rand_util.h" | 14 #include "base/rand_util.h" |
| 15 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "base/task_runner.h" | 16 #include "base/task_runner.h" |
| 17 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 18 #include "mojo/edk/embedder/embedder_internal.h" | 18 #include "mojo/edk/embedder/embedder_internal.h" |
| 19 #include "mojo/edk/embedder/entrypoints.h" | 19 #include "mojo/edk/embedder/entrypoints.h" |
| 20 #include "mojo/edk/embedder/platform_channel_pair.h" | 20 #include "mojo/edk/embedder/platform_channel_pair.h" |
| 21 #include "mojo/edk/embedder/process_delegate.h" | 21 #include "mojo/edk/embedder/process_delegate.h" |
| 22 #include "mojo/edk/system/core.h" | 22 #include "mojo/edk/system/core.h" |
| 23 | 23 |
| 24 #if !defined(OS_NACL) | 24 #if !defined(OS_NACL) |
| 25 #include "base/debug/stack_trace.h" |
| 25 #include "crypto/random.h" | 26 #include "crypto/random.h" |
| 26 #endif | 27 #endif |
| 27 | 28 |
| 28 namespace mojo { | 29 namespace mojo { |
| 29 namespace edk { | 30 namespace edk { |
| 30 | |
| 31 class Core; | |
| 32 class PlatformSupport; | |
| 33 | |
| 34 namespace internal { | 31 namespace internal { |
| 35 | 32 |
| 36 Core* g_core; | 33 Core* g_core = nullptr; |
| 37 ProcessDelegate* g_process_delegate; | 34 ProcessDelegate* g_process_delegate = nullptr; |
| 38 | |
| 39 Core* GetCore() { return g_core; } | |
| 40 | 35 |
| 41 } // namespace internal | 36 } // namespace internal |
| 42 | 37 |
| 43 void SetMaxMessageSize(size_t bytes) { | 38 void SetMaxMessageSize(size_t bytes) { |
| 44 } | 39 } |
| 45 | 40 |
| 46 void ChildProcessLaunched(base::ProcessHandle child_process, | 41 void ChildProcessLaunched(base::ProcessHandle child_process, |
| 47 ScopedPlatformHandle server_pipe, | 42 ScopedPlatformHandle server_pipe, |
| 48 const std::string& child_token) { | 43 const std::string& child_token) { |
| 49 ChildProcessLaunched(child_process, std::move(server_pipe), | 44 ChildProcessLaunched(child_process, std::move(server_pipe), |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 91 | 86 |
| 92 void ClosePeerConnection(const std::string& peer_token) { | 87 void ClosePeerConnection(const std::string& peer_token) { |
| 93 CHECK(internal::g_process_delegate); | 88 CHECK(internal::g_process_delegate); |
| 94 return internal::g_core->ClosePeerConnection(peer_token); | 89 return internal::g_core->ClosePeerConnection(peer_token); |
| 95 } | 90 } |
| 96 | 91 |
| 97 void Init() { | 92 void Init() { |
| 98 MojoSystemThunks thunks = MakeSystemThunks(); | 93 MojoSystemThunks thunks = MakeSystemThunks(); |
| 99 size_t expected_size = MojoEmbedderSetSystemThunks(&thunks); | 94 size_t expected_size = MojoEmbedderSetSystemThunks(&thunks); |
| 100 DCHECK_EQ(expected_size, sizeof(thunks)); | 95 DCHECK_EQ(expected_size, sizeof(thunks)); |
| 101 | 96 #if defined(OS_NACL) || 1 |
| 97 DCHECK(!internal::g_core) << "No nacl stack trace."; |
| 98 #else |
| 99 static std::string prev_stack; |
| 100 DCHECK(!internal::g_core) << prev_stack << "\nEndOfPrevStack"; |
| 101 prev_stack = base::debug::StackTrace().ToString(); |
| 102 #endif |
| 102 internal::g_core = new Core(); | 103 internal::g_core = new Core(); |
| 103 } | 104 } |
| 104 | 105 |
| 105 void SetDefaultProcessErrorCallback(const ProcessErrorCallback& callback) { | 106 void SetDefaultProcessErrorCallback(const ProcessErrorCallback& callback) { |
| 106 internal::g_core->SetDefaultProcessErrorCallback(callback); | 107 internal::g_core->SetDefaultProcessErrorCallback(callback); |
| 107 } | 108 } |
| 108 | 109 |
| 109 MojoResult CreatePlatformHandleWrapper( | 110 MojoResult CreatePlatformHandleWrapper( |
| 110 ScopedPlatformHandle platform_handle, | 111 ScopedPlatformHandle platform_handle, |
| 111 MojoHandle* platform_handle_wrapper_handle) { | 112 MojoHandle* platform_handle_wrapper_handle) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 133 base::SharedMemoryHandle* shared_memory_handle, | 134 base::SharedMemoryHandle* shared_memory_handle, |
| 134 size_t* num_bytes, | 135 size_t* num_bytes, |
| 135 bool* read_only) { | 136 bool* read_only) { |
| 136 return internal::g_core->PassSharedMemoryHandle( | 137 return internal::g_core->PassSharedMemoryHandle( |
| 137 mojo_handle, shared_memory_handle, num_bytes, read_only); | 138 mojo_handle, shared_memory_handle, num_bytes, read_only); |
| 138 } | 139 } |
| 139 | 140 |
| 140 void InitIPCSupport(ProcessDelegate* process_delegate, | 141 void InitIPCSupport(ProcessDelegate* process_delegate, |
| 141 scoped_refptr<base::TaskRunner> io_thread_task_runner) { | 142 scoped_refptr<base::TaskRunner> io_thread_task_runner) { |
| 142 CHECK(internal::g_core); | 143 CHECK(internal::g_core); |
| 144 #if defined(OS_NACL) || 1 |
| 145 DCHECK(!internal::g_process_delegate) << "No stack trace on NaCl"; |
| 146 #else |
| 147 static std::string prev_stack; |
| 148 DCHECK(!internal::g_process_delegate) << prev_stack << "\nEndOfPrevStack"; |
| 149 prev_stack = base::debug::StackTrace().ToString(); |
| 150 #endif |
| 143 internal::g_core->SetIOTaskRunner(io_thread_task_runner); | 151 internal::g_core->SetIOTaskRunner(io_thread_task_runner); |
| 144 internal::g_process_delegate = process_delegate; | 152 internal::g_process_delegate = process_delegate; |
| 145 } | 153 } |
| 146 | 154 |
| 147 void ShutdownIPCSupport() { | 155 void ShutdownIPCSupport() { |
| 148 CHECK(internal::g_process_delegate); | 156 CHECK(internal::g_process_delegate); |
| 149 CHECK(internal::g_core); | 157 CHECK(internal::g_core); |
| 150 internal::g_core->RequestShutdown( | 158 internal::g_core->RequestShutdown( |
| 151 base::Bind(&ProcessDelegate::OnShutdownComplete, | 159 base::Bind(&ProcessDelegate::OnShutdownComplete, |
| 152 base::Unretained(internal::g_process_delegate))); | 160 base::Unretained(internal::g_process_delegate))); |
| 161 internal::g_process_delegate = nullptr; |
| 153 } | 162 } |
| 154 | 163 |
| 155 #if defined(OS_MACOSX) && !defined(OS_IOS) | 164 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 156 void SetMachPortProvider(base::PortProvider* port_provider) { | 165 void SetMachPortProvider(base::PortProvider* port_provider) { |
| 157 DCHECK(port_provider); | 166 DCHECK(port_provider); |
| 158 internal::g_core->SetMachPortProvider(port_provider); | 167 internal::g_core->SetMachPortProvider(port_provider); |
| 159 } | 168 } |
| 160 #endif | 169 #endif |
| 161 | 170 |
| 162 ScopedMessagePipeHandle CreateMessagePipe( | 171 ScopedMessagePipeHandle CreateMessagePipe( |
| 163 ScopedPlatformHandle platform_handle) { | 172 ScopedPlatformHandle platform_handle) { |
| 164 CHECK(internal::g_process_delegate); | 173 CHECK(internal::g_core); |
| 165 return internal::g_core->CreateMessagePipe(std::move(platform_handle)); | 174 return internal::g_core->CreateMessagePipe(std::move(platform_handle)); |
| 166 } | 175 } |
| 167 | 176 |
| 168 ScopedMessagePipeHandle CreateParentMessagePipe( | 177 ScopedMessagePipeHandle CreateParentMessagePipe( |
| 169 const std::string& token, const std::string& child_token) { | 178 const std::string& token, const std::string& child_token) { |
| 170 CHECK(internal::g_process_delegate); | 179 CHECK(internal::g_core); |
| 171 return internal::g_core->CreateParentMessagePipe(token, child_token); | 180 return internal::g_core->CreateParentMessagePipe(token, child_token); |
| 172 } | 181 } |
| 173 | 182 |
| 174 ScopedMessagePipeHandle CreateChildMessagePipe(const std::string& token) { | 183 ScopedMessagePipeHandle CreateChildMessagePipe(const std::string& token) { |
| 175 CHECK(internal::g_process_delegate); | 184 CHECK(internal::g_core); |
| 176 return internal::g_core->CreateChildMessagePipe(token); | 185 return internal::g_core->CreateChildMessagePipe(token); |
| 177 } | 186 } |
| 178 | 187 |
| 179 std::string GenerateRandomToken() { | 188 std::string GenerateRandomToken() { |
| 180 char random_bytes[16]; | 189 char random_bytes[16]; |
| 181 #if defined(OS_NACL) | 190 #if defined(OS_NACL) |
| 182 // Not secure. For NaCl only! | 191 // Not secure. For NaCl only! |
| 183 base::RandBytes(random_bytes, 16); | 192 base::RandBytes(random_bytes, 16); |
| 184 #else | 193 #else |
| 185 crypto::RandBytes(random_bytes, 16); | 194 crypto::RandBytes(random_bytes, 16); |
| 186 #endif | 195 #endif |
| 187 return base::HexEncode(random_bytes, 16); | 196 return base::HexEncode(random_bytes, 16); |
| 188 } | 197 } |
| 189 | 198 |
| 190 MojoResult SetProperty(MojoPropertyType type, const void* value) { | 199 MojoResult SetProperty(MojoPropertyType type, const void* value) { |
| 191 CHECK(internal::g_core); | 200 CHECK(internal::g_core); |
| 192 return internal::g_core->SetProperty(type, value); | 201 return internal::g_core->SetProperty(type, value); |
| 193 } | 202 } |
| 194 | 203 |
| 195 } // namespace edk | 204 } // namespace edk |
| 196 } // namespace mojo | 205 } // namespace mojo |
| OLD | NEW |