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