| Index: mojo/edk/embedder/embedder.cc
|
| diff --git a/mojo/edk/embedder/embedder.cc b/mojo/edk/embedder/embedder.cc
|
| index 4353f9b3ce51e507a0ced57ac383f01bab976aa5..9f27a407c3bf3a17af95eca3fa66da52b0012c6a 100644
|
| --- a/mojo/edk/embedder/embedder.cc
|
| +++ b/mojo/edk/embedder/embedder.cc
|
| @@ -4,167 +4,159 @@
|
|
|
| #include "mojo/edk/embedder/embedder.h"
|
|
|
| -#include <stddef.h>
|
| #include <stdint.h>
|
|
|
| -#include <utility>
|
| -
|
| -#include "base/atomicops.h"
|
| #include "base/bind.h"
|
| -#include "base/bind_helpers.h"
|
| -#include "base/command_line.h"
|
| #include "base/location.h"
|
| #include "base/logging.h"
|
| -#include "base/memory/scoped_ptr.h"
|
| -#include "base/message_loop/message_loop.h"
|
| +#include "base/memory/ref_counted.h"
|
| +#include "base/strings/string_number_conversions.h"
|
| #include "base/task_runner.h"
|
| +#include "base/thread_task_runner_handle.h"
|
| +#include "crypto/random.h"
|
| #include "mojo/edk/embedder/embedder_internal.h"
|
| #include "mojo/edk/embedder/platform_channel_pair.h"
|
| #include "mojo/edk/embedder/process_delegate.h"
|
| #include "mojo/edk/embedder/simple_platform_support.h"
|
| -#include "mojo/edk/system/broker_state.h"
|
| -#include "mojo/edk/system/child_broker.h"
|
| -#include "mojo/edk/system/child_broker_host.h"
|
| -#include "mojo/edk/system/configuration.h"
|
| #include "mojo/edk/system/core.h"
|
| -#include "mojo/edk/system/message_pipe_dispatcher.h"
|
| -#include "mojo/edk/system/platform_handle_dispatcher.h"
|
|
|
| namespace mojo {
|
| namespace edk {
|
|
|
| -namespace internal {
|
| +class Core;
|
| +class PlatformSupport;
|
|
|
| -// Declared in embedder_internal.h.
|
| -Broker* g_broker = nullptr;
|
| -PlatformSupport* g_platform_support = nullptr;
|
| -Core* g_core = nullptr;
|
| +namespace internal {
|
|
|
| +Core* g_core;
|
| +base::TaskRunner* g_io_thread_task_runner;
|
| +PlatformSupport* g_platform_support;
|
| ProcessDelegate* g_process_delegate;
|
| -base::TaskRunner* g_io_thread_task_runner = nullptr;
|
|
|
| -Core* GetCore() {
|
| - return g_core;
|
| -}
|
| +// This is used to help negotiate message pipe connections over arbitrary
|
| +// platform channels. The embedder needs to know which end of the pipe it's on
|
| +// so it can do the right thing.
|
| +//
|
| +// TODO: Remove this when people stop using mojo::embedder::CreateChannel()
|
| +// and thus mojo::edk::CreateMessagePipe(ScopedPlatformHandle).
|
| +bool g_is_parent_process = true;
|
| +
|
| +Core* GetCore() { return g_core; }
|
|
|
| } // namespace internal
|
|
|
| void SetMaxMessageSize(size_t bytes) {
|
| - GetMutableConfiguration()->max_message_num_bytes = bytes;
|
| }
|
|
|
| void PreInitializeParentProcess() {
|
| - BrokerState::GetInstance();
|
| }
|
|
|
| void PreInitializeChildProcess() {
|
| - ChildBroker::GetInstance();
|
| + internal::g_is_parent_process = false;
|
| }
|
|
|
| ScopedPlatformHandle ChildProcessLaunched(base::ProcessHandle child_process) {
|
| - PlatformChannelPair token_channel;
|
| - new ChildBrokerHost(child_process, token_channel.PassServerHandle());
|
| - return token_channel.PassClientHandle();
|
| + PlatformChannelPair channel;
|
| + ChildProcessLaunched(child_process, channel.PassServerHandle());
|
| + return channel.PassClientHandle();
|
| }
|
|
|
| void ChildProcessLaunched(base::ProcessHandle child_process,
|
| ScopedPlatformHandle server_pipe) {
|
| - new ChildBrokerHost(child_process, std::move(server_pipe));
|
| + CHECK(internal::g_core);
|
| + internal::g_core->AddChild(child_process, std::move(server_pipe));
|
| }
|
|
|
| void SetParentPipeHandle(ScopedPlatformHandle pipe) {
|
| - ChildBroker::GetInstance()->SetChildBrokerHostHandle(std::move(pipe));
|
| + CHECK(internal::g_core);
|
| + internal::g_is_parent_process = false;
|
| + internal::g_core->InitChild(std::move(pipe));
|
| }
|
|
|
| void Init() {
|
| - const base::CommandLine& command_line =
|
| - *base::CommandLine::ForCurrentProcess();
|
| - if (command_line.HasSwitch("use-new-edk") && !internal::g_broker)
|
| - BrokerState::GetInstance();
|
| -
|
| - DCHECK(!internal::g_platform_support);
|
| + internal::g_core = new Core();
|
| internal::g_platform_support = new SimplePlatformSupport();
|
| -
|
| - DCHECK(!internal::g_core);
|
| - internal::g_core = new Core(internal::g_platform_support);
|
| }
|
|
|
| MojoResult AsyncWait(MojoHandle handle,
|
| MojoHandleSignals signals,
|
| const base::Callback<void(MojoResult)>& callback) {
|
| + CHECK(internal::g_core);
|
| return internal::g_core->AsyncWait(handle, signals, callback);
|
| }
|
|
|
| MojoResult CreatePlatformHandleWrapper(
|
| ScopedPlatformHandle platform_handle,
|
| MojoHandle* platform_handle_wrapper_handle) {
|
| - DCHECK(platform_handle_wrapper_handle);
|
| -
|
| - scoped_refptr<Dispatcher> dispatcher =
|
| - PlatformHandleDispatcher::Create(std::move(platform_handle));
|
| -
|
| - DCHECK(internal::g_core);
|
| - MojoHandle h = internal::g_core->AddDispatcher(dispatcher);
|
| - if (h == MOJO_HANDLE_INVALID) {
|
| - LOG(ERROR) << "Handle table full";
|
| - dispatcher->Close();
|
| - return MOJO_RESULT_RESOURCE_EXHAUSTED;
|
| - }
|
| -
|
| - *platform_handle_wrapper_handle = h;
|
| - return MOJO_RESULT_OK;
|
| + return internal::g_core->CreatePlatformHandleWrapper(
|
| + std::move(platform_handle), platform_handle_wrapper_handle);
|
| }
|
|
|
| MojoResult PassWrappedPlatformHandle(MojoHandle platform_handle_wrapper_handle,
|
| ScopedPlatformHandle* platform_handle) {
|
| - DCHECK(platform_handle);
|
| -
|
| - DCHECK(internal::g_core);
|
| - scoped_refptr<Dispatcher> dispatcher(
|
| - internal::g_core->GetDispatcher(platform_handle_wrapper_handle));
|
| - if (!dispatcher)
|
| - return MOJO_RESULT_INVALID_ARGUMENT;
|
| -
|
| - if (dispatcher->GetType() != Dispatcher::Type::PLATFORM_HANDLE)
|
| - return MOJO_RESULT_INVALID_ARGUMENT;
|
| -
|
| - *platform_handle = static_cast<PlatformHandleDispatcher*>(dispatcher.get())
|
| - ->PassPlatformHandle();
|
| - return MOJO_RESULT_OK;
|
| + return internal::g_core->PassWrappedPlatformHandle(
|
| + platform_handle_wrapper_handle, platform_handle);
|
| }
|
|
|
| void InitIPCSupport(ProcessDelegate* process_delegate,
|
| scoped_refptr<base::TaskRunner> io_thread_task_runner) {
|
| - // |Init()| must have already been called.
|
| - DCHECK(internal::g_core);
|
| - internal::g_process_delegate = process_delegate;
|
| + CHECK(internal::g_core);
|
| + CHECK(!internal::g_io_thread_task_runner);
|
| internal::g_io_thread_task_runner = io_thread_task_runner.get();
|
| + internal::g_io_thread_task_runner->AddRef();
|
| +
|
| + internal::g_core->SetIOTaskRunner(io_thread_task_runner);
|
| + internal::g_process_delegate = process_delegate;
|
| }
|
|
|
| void ShutdownIPCSupportOnIOThread() {
|
| }
|
|
|
| void ShutdownIPCSupport() {
|
| - // TODO(jam): remove ProcessDelegate from new EDK once the old EDK is gone.
|
| - internal::g_process_delegate->OnShutdownComplete();
|
| + CHECK(internal::g_process_delegate);
|
| + CHECK(internal::g_core);
|
| + internal::g_core->RequestShutdown(
|
| + base::Bind(&ProcessDelegate::OnShutdownComplete,
|
| + base::Unretained(internal::g_process_delegate)));
|
| }
|
|
|
| ScopedMessagePipeHandle CreateMessagePipe(
|
| ScopedPlatformHandle platform_handle) {
|
| - MojoCreateMessagePipeOptions options = {
|
| - static_cast<uint32_t>(sizeof(MojoCreateMessagePipeOptions)),
|
| - MOJO_CREATE_MESSAGE_PIPE_OPTIONS_FLAG_TRANSFERABLE};
|
| - scoped_refptr<MessagePipeDispatcher> dispatcher =
|
| - MessagePipeDispatcher::Create(options);
|
| -
|
| - ScopedMessagePipeHandle rv(
|
| - MessagePipeHandle(internal::g_core->AddDispatcher(dispatcher)));
|
| - CHECK(rv.is_valid());
|
| - dispatcher->Init(std::move(platform_handle), nullptr, 0, nullptr, 0, nullptr,
|
| - nullptr);
|
| - // TODO(vtl): The |.Pass()| below is only needed due to an MSVS bug; remove it
|
| - // once that's fixed.
|
| - return rv;
|
| + NOTREACHED();
|
| + return ScopedMessagePipeHandle();
|
| +}
|
| +
|
| +void CreateMessagePipe(
|
| + ScopedPlatformHandle platform_handle,
|
| + const base::Callback<void(ScopedMessagePipeHandle)>& callback) {
|
| + DCHECK(internal::g_core);
|
| + if (internal::g_is_parent_process) {
|
| + internal::g_core->CreateParentMessagePipe(std::move(platform_handle),
|
| + callback);
|
| + } else {
|
| + internal::g_core->CreateChildMessagePipe(std::move(platform_handle),
|
| + callback);
|
| + }
|
| +}
|
| +
|
| +void CreateParentMessagePipe(
|
| + const std::string& token,
|
| + const base::Callback<void(ScopedMessagePipeHandle)>& callback) {
|
| + DCHECK(internal::g_core);
|
| + internal::g_core->CreateParentMessagePipe(token, callback);
|
| +}
|
| +
|
| +void CreateChildMessagePipe(
|
| + const std::string& token,
|
| + const base::Callback<void(ScopedMessagePipeHandle)>& callback) {
|
| + DCHECK(internal::g_core);
|
| + internal::g_core->CreateChildMessagePipe(token, callback);
|
| +}
|
| +
|
| +std::string GenerateRandomToken() {
|
| + char random_bytes[16];
|
| + crypto::RandBytes(random_bytes, 16);
|
| + return base::HexEncode(random_bytes, 16);
|
| }
|
|
|
| } // namespace edk
|
|
|