diff options
author | davemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-14 20:21:40 +0000 |
---|---|---|
committer | davemoore@chromium.org <davemoore@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-04-14 20:21:40 +0000 |
commit | cf28cd1ed8bc378ba1260a94897ab8c9aecbc894 (patch) | |
tree | e288c75db8a5ff448240913eee0839ef630dd1f9 /mojo | |
parent | d2202e2a3ea471caeab836e7f215bfdc20f23f2f (diff) | |
download | chromium_src-cf28cd1ed8bc378ba1260a94897ab8c9aecbc894.zip chromium_src-cf28cd1ed8bc378ba1260a94897ab8c9aecbc894.tar.gz chromium_src-cf28cd1ed8bc378ba1260a94897ab8c9aecbc894.tar.bz2 |
Make mojo_system static and mojo_system_impl a component, never use both
BUG=
R=viettrungluu@chromium.org
Review URL: https://codereview.chromium.org/231353002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@263717 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo')
29 files changed, 695 insertions, 516 deletions
diff --git a/mojo/embedder/embedder.cc b/mojo/embedder/embedder.cc index db8d930..7698160 100644 --- a/mojo/embedder/embedder.cc +++ b/mojo/embedder/embedder.cc @@ -9,7 +9,8 @@ #include "base/logging.h" #include "base/memory/scoped_ptr.h" #include "mojo/system/channel.h" -#include "mojo/system/core_impl.h" +#include "mojo/system/core.h" +#include "mojo/system/entrypoints.h" #include "mojo/system/message_pipe.h" #include "mojo/system/message_pipe_dispatcher.h" #include "mojo/system/raw_channel.h" @@ -17,6 +18,10 @@ namespace mojo { namespace embedder { +void Init() { + system::entrypoints::SetCore(new system::Core()); +} + struct ChannelInfo { scoped_refptr<system::Channel> channel; }; @@ -55,10 +60,6 @@ static void CreateChannelOnIOThread( } } -void Init() { - Core::Init(new system::CoreImpl()); -} - ScopedMessagePipeHandle CreateChannel( ScopedPlatformHandle platform_handle, scoped_refptr<base::TaskRunner> io_thread_task_runner, @@ -70,10 +71,10 @@ ScopedMessagePipeHandle CreateChannel( scoped_refptr<system::MessagePipe> > remote_message_pipe = system::MessagePipeDispatcher::CreateRemoteMessagePipe(); - system::CoreImpl* core_impl = static_cast<system::CoreImpl*>(Core::Get()); - DCHECK(core_impl); + system::Core* core = system::entrypoints::GetCore(); + DCHECK(core); ScopedMessagePipeHandle rv( - MessagePipeHandle(core_impl->AddDispatcher(remote_message_pipe.first))); + MessagePipeHandle(core->AddDispatcher(remote_message_pipe.first))); // TODO(vtl): Do we properly handle the failure case here? if (rv.is_valid()) { io_thread_task_runner->PostTask(FROM_HERE, diff --git a/mojo/embedder/test_embedder.cc b/mojo/embedder/test_embedder.cc index 855d1da..049855d 100644 --- a/mojo/embedder/test_embedder.cc +++ b/mojo/embedder/test_embedder.cc @@ -6,7 +6,8 @@ #include "base/logging.h" #include "base/macros.h" -#include "mojo/system/core_impl.h" +#include "mojo/system/core.h" +#include "mojo/system/entrypoints.h" #include "mojo/system/handle_table.h" namespace mojo { @@ -14,7 +15,7 @@ namespace mojo { namespace system { namespace internal { -bool ShutdownCheckNoLeaks(CoreImpl* core_impl) { +bool ShutdownCheckNoLeaks(Core* core_impl) { // No point in taking the lock. const HandleTable::HandleToEntryMap& handle_to_entry_map = core_impl->handle_table_.handle_to_entry_map_; @@ -38,12 +39,12 @@ namespace embedder { namespace test { bool Shutdown() { - system::CoreImpl* core_impl = static_cast<system::CoreImpl*>(Core::Get()); - CHECK(core_impl); - Core::Reset(); + system::Core* core = system::entrypoints::GetCore(); + CHECK(core); + system::entrypoints::SetCore(NULL); - bool rv = system::internal::ShutdownCheckNoLeaks(core_impl); - delete core_impl; + bool rv = system::internal::ShutdownCheckNoLeaks(core); + delete core; return rv; } diff --git a/mojo/mojo.gyp b/mojo/mojo.gyp index c721cfa..27a2efe 100644 --- a/mojo/mojo.gyp +++ b/mojo/mojo.gyp @@ -36,6 +36,7 @@ 'mojo_js', 'mojo_js_unittests', 'mojo_message_generator', + 'mojo_native_viewport_service', 'mojo_pepper_container_app', 'mojo_public_test_utils', 'mojo_public_bindings_unittests', @@ -70,7 +71,6 @@ '../base/base.gyp:base', '../base/base.gyp:test_support_base', '../testing/gtest.gyp:gtest', - 'mojo_system', 'mojo_system_impl', 'mojo_test_support', 'mojo_test_support_impl', @@ -84,7 +84,6 @@ 'type': 'static_library', 'dependencies': [ '../base/base.gyp:test_support_base', - 'mojo_system', 'mojo_system_impl', 'mojo_test_support', 'mojo_test_support_impl', @@ -97,12 +96,12 @@ 'target_name': 'mojo_system_impl', 'type': '<(component)', 'dependencies': [ - 'mojo_system', '../base/base.gyp:base', '../base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', ], 'defines': [ 'MOJO_SYSTEM_IMPL_IMPLEMENTATION', + 'MOJO_SYSTEM_IMPLEMENTATION', ], 'sources': [ 'embedder/embedder.cc', @@ -119,8 +118,8 @@ 'system/channel.cc', 'system/channel.h', 'system/constants.h', - 'system/core_impl.cc', - 'system/core_impl.h', + 'system/core.cc', + 'system/core.h', 'system/data_pipe.cc', 'system/data_pipe.h', 'system/data_pipe_consumer_dispatcher.cc', @@ -129,6 +128,7 @@ 'system/data_pipe_producer_dispatcher.h', 'system/dispatcher.cc', 'system/dispatcher.h', + 'system/entrypoints.cc', 'system/handle_table.cc', 'system/handle_table.h', 'system/local_data_pipe.cc', @@ -179,16 +179,16 @@ 'target_name': 'mojo_system_unittests', 'type': 'executable', 'dependencies': [ + '../base/base.gyp:base', '../base/base.gyp:run_all_unittests', '../testing/gtest.gyp:gtest', 'mojo_common_test_support', - 'mojo_system', 'mojo_system_impl', ], 'sources': [ 'embedder/embedder_unittest.cc', 'embedder/platform_channel_pair_posix_unittest.cc', - 'system/core_impl_unittest.cc', + 'system/core_unittest.cc', 'system/core_test_base.cc', 'system/core_test_base.h', 'system/data_pipe_unittest.cc', @@ -223,6 +223,7 @@ 'mojo_gles2', 'mojo_gles2_bindings', 'mojo_environment_chromium', + 'mojo_system_impl', ], 'defines': [ 'MOJO_GLES2_IMPL_IMPLEMENTATION', @@ -257,7 +258,11 @@ 'dependencies': [ '../base/base.gyp:base', '../base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', - 'mojo_system', + 'mojo_system_impl', + ], + 'export_dependent_settings': [ + '../base/third_party/dynamic_annotations/dynamic_annotations.gyp:dynamic_annotations', + 'mojo_system_impl', ], 'sources': [ 'common/common_type_converters.cc', @@ -280,7 +285,6 @@ '../base/base.gyp:base', '../base/base.gyp:test_support_base', '../testing/gtest.gyp:gtest', - 'mojo_system', 'mojo_system_impl', ], 'sources': [ @@ -304,8 +308,6 @@ 'mojo_common_test_support', 'mojo_public_test_utils', 'mojo_run_all_unittests', - 'mojo_system', - 'mojo_system_impl', ], 'sources': [ 'common/common_type_converters_unittest.cc', @@ -368,6 +370,7 @@ 'mojo_common_lib', 'mojo_environment_chromium', 'mojo_shell_bindings', + 'mojo_system_impl', ], 'sources': [ 'service_manager/service_loader.h', @@ -405,7 +408,6 @@ 'mojo_gles2_impl', 'mojo_service_manager', 'mojo_shell_bindings', - 'mojo_system', 'mojo_system_impl', 'mojo_native_viewport_service', 'mojo_spy', @@ -466,7 +468,6 @@ 'mojo_environment_chromium', 'mojo_service_manager', 'mojo_shell_lib', - 'mojo_system', 'mojo_system_impl', ], 'sources': [ @@ -486,7 +487,6 @@ 'mojo_run_all_unittests', 'mojo_service_manager', 'mojo_shell_client', - 'mojo_system', ], 'variables': { 'mojom_base_output_dir': 'mojo', @@ -505,13 +505,11 @@ '../gin/gin.gyp:gin', '../v8/tools/gyp/v8.gyp:v8', 'mojo_common_lib', - 'mojo_system', ], 'export_dependent_settings': [ '../base/base.gyp:base', '../gin/gin.gyp:gin', 'mojo_common_lib', - 'mojo_system', ], 'sources': [ 'bindings/js/core.cc', @@ -546,7 +544,6 @@ 'mojo_bindings', 'mojo_common_lib', 'mojo_environment_chromium', - 'mojo_system', 'mojo_system_impl', ], 'sources': [ diff --git a/mojo/mojo_apps.gypi b/mojo/mojo_apps.gypi index 934d36a..62d904d 100644 --- a/mojo/mojo_apps.gypi +++ b/mojo/mojo_apps.gypi @@ -14,7 +14,6 @@ 'mojo_gles2_bindings', 'mojo_js_bindings_lib', 'mojo_native_viewport_bindings', - 'mojo_system', ], 'export_dependent_settings': [ '../base/base.gyp:base', @@ -23,7 +22,6 @@ 'mojo_gles2', 'mojo_gles2_bindings', 'mojo_native_viewport_bindings', - 'mojo_system', ], 'sources': [ 'apps/js/mojo_runner_delegate.cc', @@ -56,6 +54,7 @@ 'type': 'shared_library', 'dependencies': [ 'mojo_js_lib', + 'mojo_system_impl', ], 'sources': [ 'apps/js/main.cc', diff --git a/mojo/mojo_examples.gypi b/mojo/mojo_examples.gypi index 15b4f619..590285f 100644 --- a/mojo/mojo_examples.gypi +++ b/mojo/mojo_examples.gypi @@ -44,7 +44,6 @@ '../skia/skia.gyp:skia', '../gpu/gpu.gyp:gles2_implementation', 'mojo_gles2', - 'mojo_system', ], 'sources': [ 'examples/compositor_app/mojo_context_provider.cc', @@ -65,7 +64,7 @@ 'mojo_gles2', 'mojo_native_viewport_bindings', 'mojo_shell_client', - 'mojo_system', + 'mojo_system_impl', ], 'sources': [ 'examples/compositor_app/compositor_app.cc', @@ -94,7 +93,7 @@ 'mojo_gles2', 'mojo_native_viewport_bindings', 'mojo_shell_client', - 'mojo_system', + 'mojo_system_impl', ], 'defines': [ # We don't really want to export. We could change how @@ -215,7 +214,7 @@ 'mojo_environment_chromium', 'mojo_gles2', 'mojo_shell_client', - 'mojo_system', + 'mojo_system_impl' ], 'sources': [ 'examples/aura_demo/aura_demo.cc', @@ -240,11 +239,9 @@ 'includes': [ 'public/tools/bindings/mojom_bindings_generator.gypi' ], 'export_dependent_settings': [ 'mojo_bindings', - 'mojo_system', ], 'dependencies': [ 'mojo_bindings', - 'mojo_system', ], }, { @@ -266,7 +263,7 @@ 'mojo_gles2', 'mojo_launcher_bindings', 'mojo_shell_client', - 'mojo_system', + 'mojo_system_impl', ], 'sources': [ 'examples/launcher/launcher.cc', @@ -291,11 +288,9 @@ 'includes': [ 'public/tools/bindings/mojom_bindings_generator.gypi' ], 'export_dependent_settings': [ 'mojo_bindings', - 'mojo_system', ], 'dependencies': [ 'mojo_bindings', - 'mojo_system', ], }, { @@ -309,7 +304,7 @@ 'mojo_launcher_bindings', 'mojo_native_viewport_bindings', 'mojo_shell_client', - 'mojo_system', + 'mojo_system_impl', 'mojo_view_manager_bindings', ], 'sources': [ diff --git a/mojo/mojo_public.gypi b/mojo/mojo_public.gypi index 261170a..15eccb0 100644 --- a/mojo/mojo_public.gypi +++ b/mojo/mojo_public.gypi @@ -2,7 +2,7 @@ 'targets': [ { 'target_name': 'mojo_system', - 'type': '<(component)', + 'type': 'static_library', 'defines': [ 'MOJO_SYSTEM_IMPLEMENTATION', ], @@ -19,22 +19,8 @@ 'public/c/system/core.h', 'public/c/system/macros.h', 'public/c/system/system_export.h', - 'public/system/core_private.cc', - 'public/system/core_private.h', - ], - 'conditions': [ - ['OS=="mac"', { - 'xcode_settings': { - # Make it a run-path dependent library. - 'DYLIB_INSTALL_NAME_BASE': '@rpath', - }, - 'direct_dependent_settings': { - 'xcode_settings': { - # Look for run-path dependent libraries in the loader's directory. - 'LD_RUNPATH_SEARCH_PATHS': [ '@loader_path/.', ], - }, - }, - }], + 'public/platform/native/system_thunks.cc', + 'public/platform/native/system_thunks.h', ], }, { @@ -68,13 +54,7 @@ ['OS=="mac"', { 'xcode_settings': { # Make it a run-path dependent library. - 'DYLIB_INSTALL_NAME_BASE': '@rpath', - }, - 'direct_dependent_settings': { - 'xcode_settings': { - # Look for run-path dependent libraries in the loader's directory. - 'LD_RUNPATH_SEARCH_PATHS': [ '@loader_path/.', ], - }, + 'DYLIB_INSTALL_NAME_BASE': '@loader_path', }, }], ], @@ -103,13 +83,7 @@ ['OS=="mac"', { 'xcode_settings': { # Make it a run-path dependent library. - 'DYLIB_INSTALL_NAME_BASE': '@rpath', - }, - 'direct_dependent_settings': { - 'xcode_settings': { - # Look for run-path dependent libraries in the loader's directory. - 'LD_RUNPATH_SEARCH_PATHS': [ '@loader_path/.', ], - }, + 'DYLIB_INSTALL_NAME_BASE': '@loader_path', }, }], ], @@ -120,7 +94,6 @@ 'dependencies': [ '../base/base.gyp:base', '../testing/gtest.gyp:gtest', - 'mojo_system', 'mojo_test_support', ], 'sources': [ @@ -139,7 +112,6 @@ 'mojo_public_test_utils', 'mojo_run_all_unittests', 'mojo_sample_service', - 'mojo_system', 'mojo_utility', ], 'sources': [ @@ -171,7 +143,6 @@ 'mojo_environment_standalone', 'mojo_public_test_utils', 'mojo_run_all_unittests', - 'mojo_system', 'mojo_utility', ], 'sources': [ @@ -187,7 +158,6 @@ 'mojo_bindings', 'mojo_public_test_utils', 'mojo_run_all_unittests', - 'mojo_system', ], 'sources': [ 'public/c/system/tests/core_unittest.cc', @@ -206,7 +176,6 @@ 'mojo_bindings', 'mojo_public_test_utils', 'mojo_run_all_unittests', - 'mojo_system', 'mojo_utility', ], 'sources': [ @@ -232,7 +201,6 @@ '../testing/gtest.gyp:gtest', 'mojo_public_test_utils', 'mojo_run_all_perftests', - 'mojo_system', 'mojo_utility', ], 'sources': [ @@ -301,11 +269,9 @@ 'includes': [ 'public/tools/bindings/mojom_bindings_generator.gypi' ], 'export_dependent_settings': [ 'mojo_bindings', - 'mojo_system', ], 'dependencies': [ 'mojo_bindings', - 'mojo_system', ], }, { @@ -366,7 +332,6 @@ 'includes': [ 'public/tools/bindings/mojom_bindings_generator.gypi' ], 'dependencies': [ 'mojo_bindings', - 'mojo_system', ], 'export_dependent_settings': [ 'mojo_bindings', diff --git a/mojo/mojo_services.gypi b/mojo/mojo_services.gypi index f2106a7..208f14b 100644 --- a/mojo/mojo_services.gypi +++ b/mojo/mojo_services.gypi @@ -16,12 +16,10 @@ 'includes': [ 'public/tools/bindings/mojom_bindings_generator.gypi' ], 'export_dependent_settings': [ 'mojo_bindings', - 'mojo_system', ], 'dependencies': [ '../gpu/gpu.gyp:command_buffer_common', 'mojo_bindings', - 'mojo_system', ], }, { @@ -55,16 +53,14 @@ 'includes': [ 'public/tools/bindings/mojom_bindings_generator.gypi' ], 'export_dependent_settings': [ 'mojo_bindings', - 'mojo_system', ], 'dependencies': [ 'mojo_bindings', - 'mojo_system', ], }, { 'target_name': 'mojo_native_viewport_service', - 'type': '<(component)', + 'type': 'shared_library', 'dependencies': [ '../base/base.gyp:base', '../ui/events/events.gyp:events', @@ -75,6 +71,7 @@ 'mojo_gles2_service', 'mojo_native_viewport_bindings', 'mojo_shell_client', + 'mojo_system_impl', ], 'defines': [ 'MOJO_NATIVE_VIEWPORT_IMPLEMENTATION', diff --git a/mojo/public/platform/native/system_thunks.cc b/mojo/public/platform/native/system_thunks.cc new file mode 100644 index 0000000..bd82b00 --- /dev/null +++ b/mojo/public/platform/native/system_thunks.cc @@ -0,0 +1,168 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "mojo/public/platform/native/system_thunks.h" + +#include <assert.h> + +extern "C" { + +static MojoSystemThunks g_thunks = {0}; + +MojoTimeTicks MojoGetTimeTicksNow() { + assert(g_thunks.GetTimeTicksNow); + return g_thunks.GetTimeTicksNow(); +} + +MojoResult MojoClose(MojoHandle handle) { + assert(g_thunks.Close); + return g_thunks.Close(handle); +} + +MojoResult MojoWait(MojoHandle handle, + MojoWaitFlags flags, + MojoDeadline deadline) { + assert(g_thunks.Wait); + return g_thunks.Wait(handle, flags, deadline); +} + +MojoResult MojoWaitMany(const MojoHandle* handles, + const MojoWaitFlags* flags, + uint32_t num_handles, + MojoDeadline deadline) { + assert(g_thunks.WaitMany); + return g_thunks.WaitMany(handles, flags, num_handles, deadline); +} + +MojoResult MojoCreateMessagePipe(MojoHandle* message_pipe_handle0, + MojoHandle* message_pipe_handle1) { + assert(g_thunks.CreateMessagePipe); + return g_thunks.CreateMessagePipe(message_pipe_handle0, message_pipe_handle1); +} + +MojoResult MojoWriteMessage(MojoHandle message_pipe_handle, + const void* bytes, + uint32_t num_bytes, + const MojoHandle* handles, + uint32_t num_handles, + MojoWriteMessageFlags flags) { + assert(g_thunks.WriteMessage); + return g_thunks.WriteMessage(message_pipe_handle, bytes, num_bytes, handles, + num_handles, flags); +} + +MojoResult MojoReadMessage(MojoHandle message_pipe_handle, + void* bytes, + uint32_t* num_bytes, + MojoHandle* handles, + uint32_t* num_handles, + MojoReadMessageFlags flags) { + assert(g_thunks.ReadMessage); + return g_thunks.ReadMessage(message_pipe_handle, bytes, num_bytes, handles, + num_handles, flags); +} + +MojoResult MojoCreateDataPipe(const MojoCreateDataPipeOptions* options, + MojoHandle* data_pipe_producer_handle, + MojoHandle* data_pipe_consumer_handle) { + assert(g_thunks.CreateDataPipe); + return g_thunks.CreateDataPipe(options, data_pipe_producer_handle, + data_pipe_consumer_handle); +} + +MojoResult MojoWriteData(MojoHandle data_pipe_producer_handle, + const void* elements, + uint32_t* num_elements, + MojoWriteDataFlags flags) { + assert(g_thunks.WriteData); + return g_thunks.WriteData(data_pipe_producer_handle, elements, num_elements, + flags); +} + +MojoResult MojoBeginWriteData(MojoHandle data_pipe_producer_handle, + void** buffer, + uint32_t* buffer_num_elements, + MojoWriteDataFlags flags) { + assert(g_thunks.BeginWriteData); + return g_thunks.BeginWriteData(data_pipe_producer_handle, buffer, + buffer_num_elements, flags); +} + +MojoResult MojoEndWriteData(MojoHandle data_pipe_producer_handle, + uint32_t num_elements_written) { + assert(g_thunks.EndWriteData); + return g_thunks.EndWriteData(data_pipe_producer_handle, num_elements_written); +} + +MojoResult MojoReadData(MojoHandle data_pipe_consumer_handle, + void* elements, + uint32_t* num_elements, + MojoReadDataFlags flags) { + assert(g_thunks.ReadData); + return g_thunks.ReadData(data_pipe_consumer_handle, elements, num_elements, + flags); +} + +MojoResult MojoBeginReadData(MojoHandle data_pipe_consumer_handle, + const void** buffer, + uint32_t* buffer_num_elements, + MojoReadDataFlags flags) { + assert(g_thunks.BeginReadData); + return g_thunks.BeginReadData(data_pipe_consumer_handle, buffer, + buffer_num_elements, flags); +} + +MojoResult MojoEndReadData(MojoHandle data_pipe_consumer_handle, + uint32_t num_elements_read) { + assert(g_thunks.EndReadData); + return g_thunks.EndReadData(data_pipe_consumer_handle, num_elements_read); +} + +MojoResult MojoCreateSharedBuffer( + const struct MojoCreateSharedBufferOptions* options, + uint64_t num_bytes, + MojoHandle* shared_buffer_handle) { + assert(g_thunks.CreateSharedBuffer); + return g_thunks.CreateSharedBuffer(options, num_bytes, shared_buffer_handle); +} + +MojoResult MojoDuplicateBufferHandle( + MojoHandle buffer_handle, + const struct MojoDuplicateBufferHandleOptions* options, + MojoHandle* new_buffer_handle) { + assert(g_thunks.DuplicateBufferHandle); + return g_thunks.DuplicateBufferHandle(buffer_handle, options, + new_buffer_handle); +} + +MojoResult MojoMapBuffer(MojoHandle buffer_handle, + uint64_t offset, + uint64_t num_bytes, + void** buffer, + MojoMapBufferFlags flags) { + assert(g_thunks.MapBuffer); + return g_thunks.MapBuffer(buffer_handle, offset, num_bytes, buffer, flags); +} + +MojoResult MojoUnmapBuffer(void* buffer) { + assert(g_thunks.UnmapBuffer); + return g_thunks.UnmapBuffer(buffer); +} + +// Call this function by looking +// Always export this api. +#if defined(WIN32) +#define THUNK_EXPORT __declspec(dllexport) +#else +#define THUNK_EXPORT __attribute__((visibility("default"))) +#endif + +extern "C" THUNK_EXPORT size_t MojoSetSystemThunks( + const MojoSystemThunks* system_thunks) { + if (system_thunks->size >= sizeof(g_thunks)) + g_thunks = *system_thunks; + return sizeof(g_thunks); +} + +} // extern "C" diff --git a/mojo/public/platform/native/system_thunks.h b/mojo/public/platform/native/system_thunks.h new file mode 100644 index 0000000..9c366e5 --- /dev/null +++ b/mojo/public/platform/native/system_thunks.h @@ -0,0 +1,136 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_ +#define MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_ + +#include <stddef.h> + +#include "mojo/public/c/system/core.h" + +// The embedder needs to bind the basic Mojo Core functions of a DSO to those of +// the embedder when loading a DSO that is dependent on mojo_system. +// The typical usage would look like: +// base::ScopedNativeLibrary app_library( +// base::LoadNativeLibrary(app_path_, &error)); +// typedef MojoResult (*MojoSetSystemThunksFn)(MojoSystemThunks*); +// MojoSetSystemThunksFn mojo_set_system_thunks_fn = +// reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer( +// "MojoSetSystemThunks")); +// MojoSystemThunks system_thunks = MojoMakeSystemThunks(); +// size_t expected_size = mojo_set_system_thunks_fn(&system_thunks); +// if (expected_size > sizeof(MojoSystemThunks)) { +// LOG(ERROR) +// << "Invalid DSO. Expected MojoSystemThunks size: " +// << expected_size; +// break; +// } + +// Structure used to bind the basic Mojo Core functions of a DSO to those of +// the embedder. +// This is the ABI between the embedder and the DSO. It can only have new +// functions added to the end. No other changes are supported. +#pragma pack(push, 8) +struct MojoSystemThunks { + size_t size; // Should be set to sizeof(MojoSystemThunks). + MojoTimeTicks (*GetTimeTicksNow)(); + MojoResult (*Close)(MojoHandle handle); + MojoResult (*Wait)(MojoHandle handle, + MojoWaitFlags flags, + MojoDeadline deadline); + MojoResult (*WaitMany)(const MojoHandle* handles, + const MojoWaitFlags* flags, + uint32_t num_handles, + MojoDeadline deadline); + MojoResult (*CreateMessagePipe)(MojoHandle* message_pipe_handle0, + MojoHandle* message_pipe_handle1); + MojoResult (*WriteMessage)(MojoHandle message_pipe_handle, + const void* bytes, + uint32_t num_bytes, + const MojoHandle* handles, + uint32_t num_handles, + MojoWriteMessageFlags flags); + MojoResult (*ReadMessage)(MojoHandle message_pipe_handle, + void* bytes, + uint32_t* num_bytes, + MojoHandle* handles, + uint32_t* num_handles, + MojoReadMessageFlags flags); + MojoResult (*CreateDataPipe)(const MojoCreateDataPipeOptions* options, + MojoHandle* data_pipe_producer_handle, + MojoHandle* data_pipe_consumer_handle); + MojoResult (*WriteData)(MojoHandle data_pipe_producer_handle, + const void* elements, + uint32_t* num_elements, + MojoWriteDataFlags flags); + MojoResult (*BeginWriteData)(MojoHandle data_pipe_producer_handle, + void** buffer, + uint32_t* buffer_num_elements, + MojoWriteDataFlags flags); + MojoResult (*EndWriteData)(MojoHandle data_pipe_producer_handle, + uint32_t num_elements_written); + MojoResult (*ReadData)(MojoHandle data_pipe_consumer_handle, + void* elements, + uint32_t* num_elements, + MojoReadDataFlags flags); + MojoResult (*BeginReadData)(MojoHandle data_pipe_consumer_handle, + const void** buffer, + uint32_t* buffer_num_elements, + MojoReadDataFlags flags); + MojoResult (*EndReadData)(MojoHandle data_pipe_consumer_handle, + uint32_t num_elements_read); + MojoResult (*CreateSharedBuffer)( + const MojoCreateSharedBufferOptions* options, + uint64_t num_bytes, + MojoHandle* shared_buffer_handle); + MojoResult (*DuplicateBufferHandle)( + MojoHandle buffer_handle, + const MojoDuplicateBufferHandleOptions* options, + MojoHandle* new_buffer_handle); + MojoResult (*MapBuffer)(MojoHandle buffer_handle, + uint64_t offset, + uint64_t num_bytes, + void** buffer, + MojoMapBufferFlags flags); + MojoResult (*UnmapBuffer)(void* buffer); +}; +#pragma pack(pop) + +// Intended to be called from the embedder. Returns a |MojoCore| initialized +// to contain pointers to each of the embedder's MojoCore functions. +inline MojoSystemThunks MojoMakeSystemThunks() { + MojoSystemThunks system_thunks = { + sizeof(MojoSystemThunks), + MojoGetTimeTicksNow, + MojoClose, + MojoWait, + MojoWaitMany, + MojoCreateMessagePipe, + MojoWriteMessage, + MojoReadMessage, + MojoCreateDataPipe, + MojoWriteData, + MojoBeginWriteData, + MojoEndWriteData, + MojoReadData, + MojoBeginReadData, + MojoEndReadData, + MojoCreateSharedBuffer, + MojoDuplicateBufferHandle, + MojoMapBuffer, + MojoUnmapBuffer + }; + return system_thunks; +} + +// Use this type for the function found by dynamically discovering it in +// a DSO linked with mojo_system. For example: +// MojoSetSystemThunksFn mojo_set_system_thunks_fn = +// reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer( +// "MojoSetSystemThunks")); +// The expected size of |system_thunks} is returned. +// The contents of |system_thunks| are copied. +typedef size_t (*MojoSetSystemThunksFn)(const MojoSystemThunks* system_thunks); + +#endif // MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_ diff --git a/mojo/public/system/core_private.h b/mojo/public/system/core_private.h deleted file mode 100644 index 057bfca..0000000 --- a/mojo/public/system/core_private.h +++ /dev/null @@ -1,87 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_SYSTEM_CORE_PRIVATE_H_ -#define MOJO_PUBLIC_SYSTEM_CORE_PRIVATE_H_ - -#include "mojo/public/c/system/core.h" - -namespace mojo { - -// Implementors of the core APIs can use this interface to install their -// implementation into the mojo_system dynamic library. Mojo clients should not -// call these functions directly. -class MOJO_SYSTEM_EXPORT Core { - public: - virtual ~Core(); - - static void Init(Core* core); - static Core* Get(); - static void Reset(); - - virtual MojoTimeTicks GetTimeTicksNow() = 0; - virtual MojoResult Close(MojoHandle handle) = 0; - virtual MojoResult Wait(MojoHandle handle, - MojoWaitFlags flags, - MojoDeadline deadline) = 0; - virtual MojoResult WaitMany(const MojoHandle* handles, - const MojoWaitFlags* flags, - uint32_t num_handles, - MojoDeadline deadline) = 0; - virtual MojoResult CreateMessagePipe(MojoHandle* message_pipe_handle0, - MojoHandle* message_pipe_handle1) = 0; - virtual MojoResult WriteMessage(MojoHandle message_pipe_handle, - const void* bytes, - uint32_t num_bytes, - const MojoHandle* handles, - uint32_t num_handles, - MojoWriteMessageFlags flags) = 0; - virtual MojoResult ReadMessage(MojoHandle message_pipe_handle, - void* bytes, - uint32_t* num_bytes, - MojoHandle* handles, - uint32_t* num_handles, - MojoReadMessageFlags flags) = 0; - virtual MojoResult CreateDataPipe(const MojoCreateDataPipeOptions* options, - MojoHandle* data_pipe_producer_handle, - MojoHandle* data_pipe_consumer_handle) = 0; - virtual MojoResult WriteData(MojoHandle data_pipe_producer_handle, - const void* elements, - uint32_t* num_elements, - MojoWriteDataFlags flags) = 0; - virtual MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle, - void** buffer, - uint32_t* buffer_num_elements, - MojoWriteDataFlags flags) = 0; - virtual MojoResult EndWriteData(MojoHandle data_pipe_producer_handle, - uint32_t num_elements_written) = 0; - virtual MojoResult ReadData(MojoHandle data_pipe_consumer_handle, - void* elements, - uint32_t* num_elements, - MojoReadDataFlags flags) = 0; - virtual MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle, - const void** buffer, - uint32_t* buffer_num_elements, - MojoReadDataFlags flags) = 0; - virtual MojoResult EndReadData(MojoHandle data_pipe_consumer_handle, - uint32_t num_elements_read) = 0; - virtual MojoResult CreateSharedBuffer( - const MojoCreateSharedBufferOptions* options, - uint64_t num_bytes, - MojoHandle* shared_buffer_handle) = 0; - virtual MojoResult DuplicateBufferHandle( - MojoHandle buffer_handle, - const MojoDuplicateBufferHandleOptions* options, - MojoHandle* new_buffer_handle) = 0; - virtual MojoResult MapBuffer(MojoHandle buffer_handle, - uint64_t offset, - uint64_t num_bytes, - void** buffer, - MojoMapBufferFlags flags) = 0; - virtual MojoResult UnmapBuffer(void* buffer) = 0; -}; - -} // namespace mojo - -#endif // MOJO_PUBLIC_SYSTEM_CORE_PRIVATE_H_ diff --git a/mojo/shell/in_process_dynamic_service_runner.cc b/mojo/shell/in_process_dynamic_service_runner.cc index 9030d59..6604180 100644 --- a/mojo/shell/in_process_dynamic_service_runner.cc +++ b/mojo/shell/in_process_dynamic_service_runner.cc @@ -11,6 +11,7 @@ #include "base/logging.h" #include "base/message_loop/message_loop_proxy.h" #include "base/scoped_native_library.h" +#include "mojo/public/platform/native/system_thunks.h" namespace mojo { namespace shell { @@ -63,6 +64,20 @@ void InProcessDynamicServiceRunner::Run() { break; } + MojoSetSystemThunksFn mojo_set_system_thunks_fn = + reinterpret_cast<MojoSetSystemThunksFn>(app_library.GetFunctionPointer( + "MojoSetSystemThunks")); + if (mojo_set_system_thunks_fn) { + MojoSystemThunks system_thunks = MojoMakeSystemThunks(); + size_t expected_size = mojo_set_system_thunks_fn(&system_thunks); + if (expected_size > sizeof(MojoSystemThunks)) { + LOG(ERROR) + << "Invalid DSO. Expected MojoSystemThunks size: " + << expected_size; + break; + } + } + typedef MojoResult (*MojoMainFunction)(MojoHandle); MojoMainFunction main_function = reinterpret_cast<MojoMainFunction>( app_library.GetFunctionPointer("MojoMain")); diff --git a/mojo/system/core_impl.cc b/mojo/system/core.cc index 12c1be5..dd8fb57 100644 --- a/mojo/system/core_impl.cc +++ b/mojo/system/core.cc @@ -2,12 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/system/core_impl.h" +#include "mojo/system/core.h" #include <vector> #include "base/logging.h" #include "base/time/time.h" +#include "mojo/public/c/system/core.h" #include "mojo/system/constants.h" #include "mojo/system/data_pipe.h" #include "mojo/system/data_pipe_consumer_dispatcher.h" @@ -26,11 +27,11 @@ namespace system { // Implementation notes // -// Mojo primitives are implemented by the singleton |CoreImpl| object. Most +// Mojo primitives are implemented by the singleton |Core| object. Most // calls are for a "primary" handle (the first argument). -// |CoreImpl::GetDispatcher()| is used to look up a |Dispatcher| object for a +// |Core::GetDispatcher()| is used to look up a |Dispatcher| object for a // given handle. That object implements most primitives for that object. The -// wait primitives are not attached to objects and are implemented by |CoreImpl| +// wait primitives are not attached to objects and are implemented by |Core| // itself. // // Some objects have multiple handles associated to them, e.g., message pipes @@ -73,39 +74,37 @@ namespace system { // - Locks at the "INF" level may not have any locks taken while they are // held. -CoreImpl::HandleTableEntry::HandleTableEntry() +Core::HandleTableEntry::HandleTableEntry() : busy(false) { } -CoreImpl::HandleTableEntry::HandleTableEntry( +Core::HandleTableEntry::HandleTableEntry( const scoped_refptr<Dispatcher>& dispatcher) : dispatcher(dispatcher), busy(false) { } -CoreImpl::HandleTableEntry::~HandleTableEntry() { +Core::HandleTableEntry::~HandleTableEntry() { DCHECK(!busy); } -CoreImpl::CoreImpl() { +Core::Core() { } -CoreImpl::~CoreImpl() { - // This should usually not be reached (the singleton lives forever), except in - // tests. +Core::~Core() { } -MojoHandle CoreImpl::AddDispatcher( +MojoHandle Core::AddDispatcher( const scoped_refptr<Dispatcher>& dispatcher) { base::AutoLock locker(handle_table_lock_); return handle_table_.AddDispatcher(dispatcher); } -MojoTimeTicks CoreImpl::GetTimeTicksNow() { +MojoTimeTicks Core::GetTimeTicksNow() { return base::TimeTicks::Now().ToInternalValue(); } -MojoResult CoreImpl::Close(MojoHandle handle) { +MojoResult Core::Close(MojoHandle handle) { if (handle == MOJO_HANDLE_INVALID) return MOJO_RESULT_INVALID_ARGUMENT; @@ -125,16 +124,16 @@ MojoResult CoreImpl::Close(MojoHandle handle) { return dispatcher->Close(); } -MojoResult CoreImpl::Wait(MojoHandle handle, - MojoWaitFlags flags, - MojoDeadline deadline) { +MojoResult Core::Wait(MojoHandle handle, + MojoWaitFlags flags, + MojoDeadline deadline) { return WaitManyInternal(&handle, &flags, 1, deadline); } -MojoResult CoreImpl::WaitMany(const MojoHandle* handles, - const MojoWaitFlags* flags, - uint32_t num_handles, - MojoDeadline deadline) { +MojoResult Core::WaitMany(const MojoHandle* handles, + const MojoWaitFlags* flags, + uint32_t num_handles, + MojoDeadline deadline) { if (!VerifyUserPointer<MojoHandle>(handles, num_handles)) return MOJO_RESULT_INVALID_ARGUMENT; if (!VerifyUserPointer<MojoWaitFlags>(flags, num_handles)) @@ -146,8 +145,8 @@ MojoResult CoreImpl::WaitMany(const MojoHandle* handles, return WaitManyInternal(handles, flags, num_handles, deadline); } -MojoResult CoreImpl::CreateMessagePipe(MojoHandle* message_pipe_handle0, - MojoHandle* message_pipe_handle1) { +MojoResult Core::CreateMessagePipe(MojoHandle* message_pipe_handle0, + MojoHandle* message_pipe_handle1) { if (!VerifyUserPointer<MojoHandle>(message_pipe_handle0, 1)) return MOJO_RESULT_INVALID_ARGUMENT; if (!VerifyUserPointer<MojoHandle>(message_pipe_handle1, 1)) @@ -185,12 +184,12 @@ MojoResult CoreImpl::CreateMessagePipe(MojoHandle* message_pipe_handle0, // isn't done, in the in-process case, calls on the old handle may complete // after the the message has been received and a new handle created (and // possibly even after calls have been made on the new handle). -MojoResult CoreImpl::WriteMessage(MojoHandle message_pipe_handle, - const void* bytes, - uint32_t num_bytes, - const MojoHandle* handles, - uint32_t num_handles, - MojoWriteMessageFlags flags) { +MojoResult Core::WriteMessage(MojoHandle message_pipe_handle, + const void* bytes, + uint32_t num_bytes, + const MojoHandle* handles, + uint32_t num_handles, + MojoWriteMessageFlags flags) { scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); if (!dispatcher.get()) return MOJO_RESULT_INVALID_ARGUMENT; @@ -249,12 +248,12 @@ MojoResult CoreImpl::WriteMessage(MojoHandle message_pipe_handle, return rv; } -MojoResult CoreImpl::ReadMessage(MojoHandle message_pipe_handle, - void* bytes, - uint32_t* num_bytes, - MojoHandle* handles, - uint32_t* num_handles, - MojoReadMessageFlags flags) { +MojoResult Core::ReadMessage(MojoHandle message_pipe_handle, + void* bytes, + uint32_t* num_bytes, + MojoHandle* handles, + uint32_t* num_handles, + MojoReadMessageFlags flags) { scoped_refptr<Dispatcher> dispatcher(GetDispatcher(message_pipe_handle)); if (!dispatcher.get()) return MOJO_RESULT_INVALID_ARGUMENT; @@ -298,9 +297,9 @@ MojoResult CoreImpl::ReadMessage(MojoHandle message_pipe_handle, return rv; } -MojoResult CoreImpl::CreateDataPipe(const MojoCreateDataPipeOptions* options, - MojoHandle* data_pipe_producer_handle, - MojoHandle* data_pipe_consumer_handle) { +MojoResult Core::CreateDataPipe(const MojoCreateDataPipeOptions* options, + MojoHandle* data_pipe_producer_handle, + MojoHandle* data_pipe_consumer_handle) { if (options) { // The |struct_size| field must be valid to read. if (!VerifyUserPointer<uint32_t>(&options->struct_size, 1)) @@ -348,10 +347,10 @@ MojoResult CoreImpl::CreateDataPipe(const MojoCreateDataPipeOptions* options, return MOJO_RESULT_OK; } -MojoResult CoreImpl::WriteData(MojoHandle data_pipe_producer_handle, - const void* elements, - uint32_t* num_bytes, - MojoWriteDataFlags flags) { +MojoResult Core::WriteData(MojoHandle data_pipe_producer_handle, + const void* elements, + uint32_t* num_bytes, + MojoWriteDataFlags flags) { scoped_refptr<Dispatcher> dispatcher( GetDispatcher(data_pipe_producer_handle)); if (!dispatcher.get()) @@ -360,10 +359,10 @@ MojoResult CoreImpl::WriteData(MojoHandle data_pipe_producer_handle, return dispatcher->WriteData(elements, num_bytes, flags); } -MojoResult CoreImpl::BeginWriteData(MojoHandle data_pipe_producer_handle, - void** buffer, - uint32_t* buffer_num_bytes, - MojoWriteDataFlags flags) { +MojoResult Core::BeginWriteData(MojoHandle data_pipe_producer_handle, + void** buffer, + uint32_t* buffer_num_bytes, + MojoWriteDataFlags flags) { scoped_refptr<Dispatcher> dispatcher( GetDispatcher(data_pipe_producer_handle)); if (!dispatcher.get()) @@ -372,8 +371,8 @@ MojoResult CoreImpl::BeginWriteData(MojoHandle data_pipe_producer_handle, return dispatcher->BeginWriteData(buffer, buffer_num_bytes, flags); } -MojoResult CoreImpl::EndWriteData(MojoHandle data_pipe_producer_handle, - uint32_t num_bytes_written) { +MojoResult Core::EndWriteData(MojoHandle data_pipe_producer_handle, + uint32_t num_bytes_written) { scoped_refptr<Dispatcher> dispatcher( GetDispatcher(data_pipe_producer_handle)); if (!dispatcher.get()) @@ -382,10 +381,10 @@ MojoResult CoreImpl::EndWriteData(MojoHandle data_pipe_producer_handle, return dispatcher->EndWriteData(num_bytes_written); } -MojoResult CoreImpl::ReadData(MojoHandle data_pipe_consumer_handle, - void* elements, - uint32_t* num_bytes, - MojoReadDataFlags flags) { +MojoResult Core::ReadData(MojoHandle data_pipe_consumer_handle, + void* elements, + uint32_t* num_bytes, + MojoReadDataFlags flags) { scoped_refptr<Dispatcher> dispatcher( GetDispatcher(data_pipe_consumer_handle)); if (!dispatcher.get()) @@ -394,10 +393,10 @@ MojoResult CoreImpl::ReadData(MojoHandle data_pipe_consumer_handle, return dispatcher->ReadData(elements, num_bytes, flags); } -MojoResult CoreImpl::BeginReadData(MojoHandle data_pipe_consumer_handle, - const void** buffer, - uint32_t* buffer_num_bytes, - MojoReadDataFlags flags) { +MojoResult Core::BeginReadData(MojoHandle data_pipe_consumer_handle, + const void** buffer, + uint32_t* buffer_num_bytes, + MojoReadDataFlags flags) { scoped_refptr<Dispatcher> dispatcher( GetDispatcher(data_pipe_consumer_handle)); if (!dispatcher.get()) @@ -406,8 +405,8 @@ MojoResult CoreImpl::BeginReadData(MojoHandle data_pipe_consumer_handle, return dispatcher->BeginReadData(buffer, buffer_num_bytes, flags); } -MojoResult CoreImpl::EndReadData(MojoHandle data_pipe_consumer_handle, - uint32_t num_bytes_read) { +MojoResult Core::EndReadData(MojoHandle data_pipe_consumer_handle, + uint32_t num_bytes_read) { scoped_refptr<Dispatcher> dispatcher( GetDispatcher(data_pipe_consumer_handle)); if (!dispatcher.get()) @@ -416,7 +415,7 @@ MojoResult CoreImpl::EndReadData(MojoHandle data_pipe_consumer_handle, return dispatcher->EndReadData(num_bytes_read); } -MojoResult CoreImpl::CreateSharedBuffer( +MojoResult Core::CreateSharedBuffer( const MojoCreateSharedBufferOptions* options, uint64_t num_bytes, MojoHandle* shared_buffer_handle) { @@ -456,7 +455,7 @@ MojoResult CoreImpl::CreateSharedBuffer( return MOJO_RESULT_OK; } -MojoResult CoreImpl::DuplicateBufferHandle( +MojoResult Core::DuplicateBufferHandle( MojoHandle buffer_handle, const MojoDuplicateBufferHandleOptions* options, MojoHandle* new_buffer_handle) { @@ -485,11 +484,11 @@ MojoResult CoreImpl::DuplicateBufferHandle( return MOJO_RESULT_OK; } -MojoResult CoreImpl::MapBuffer(MojoHandle buffer_handle, - uint64_t offset, - uint64_t num_bytes, - void** buffer, - MojoMapBufferFlags flags) { +MojoResult Core::MapBuffer(MojoHandle buffer_handle, + uint64_t offset, + uint64_t num_bytes, + void** buffer, + MojoMapBufferFlags flags) { scoped_refptr<Dispatcher> dispatcher(GetDispatcher(buffer_handle)); if (!dispatcher.get()) return MOJO_RESULT_INVALID_ARGUMENT; @@ -515,12 +514,12 @@ MojoResult CoreImpl::MapBuffer(MojoHandle buffer_handle, return MOJO_RESULT_OK; } -MojoResult CoreImpl::UnmapBuffer(void* buffer) { +MojoResult Core::UnmapBuffer(void* buffer) { base::AutoLock locker(mapping_table_lock_); return mapping_table_.RemoveMapping(buffer); } -scoped_refptr<Dispatcher> CoreImpl::GetDispatcher(MojoHandle handle) { +scoped_refptr<Dispatcher> Core::GetDispatcher(MojoHandle handle) { if (handle == MOJO_HANDLE_INVALID) return NULL; @@ -532,10 +531,10 @@ scoped_refptr<Dispatcher> CoreImpl::GetDispatcher(MojoHandle handle) { // different flags may be specified. // TODO(vtl): This incurs a performance cost in |RemoveWaiter()|. Analyze this // more carefully and address it if necessary. -MojoResult CoreImpl::WaitManyInternal(const MojoHandle* handles, - const MojoWaitFlags* flags, - uint32_t num_handles, - MojoDeadline deadline) { +MojoResult Core::WaitManyInternal(const MojoHandle* handles, + const MojoWaitFlags* flags, + uint32_t num_handles, + MojoDeadline deadline) { DCHECK_GT(num_handles, 0u); std::vector<scoped_refptr<Dispatcher> > dispatchers; diff --git a/mojo/system/core.h b/mojo/system/core.h new file mode 100644 index 0000000..5d3a1cf --- /dev/null +++ b/mojo/system/core.h @@ -0,0 +1,151 @@ +// Copyright 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_SYSTEM_CORE_H_ +#define MOJO_SYSTEM_CORE_H_ + +#include "base/basictypes.h" +#include "base/compiler_specific.h" +#include "base/memory/ref_counted.h" +#include "base/synchronization/lock.h" +#include "mojo/system/handle_table.h" +#include "mojo/system/mapping_table.h" +#include "mojo/system/system_impl_export.h" + +namespace mojo { +namespace system { + +class Dispatcher; + +// |Core| is an object that implements the Mojo system calls. All public methods +// are thread-safe. +class MOJO_SYSTEM_IMPL_EXPORT Core { + public: + // These methods are only to be used by via the embedder API. + Core(); + virtual ~Core(); + MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); + + // System calls implementation. + MojoTimeTicks GetTimeTicksNow(); + MojoResult Close(MojoHandle handle); + MojoResult Wait(MojoHandle handle, + MojoWaitFlags flags, + MojoDeadline deadline); + MojoResult WaitMany(const MojoHandle* handles, + const MojoWaitFlags* flags, + uint32_t num_handles, + MojoDeadline deadline); + MojoResult CreateMessagePipe( + MojoHandle* message_pipe_handle0, + MojoHandle* message_pipe_handle1); + MojoResult WriteMessage(MojoHandle message_pipe_handle, + const void* bytes, + uint32_t num_bytes, + const MojoHandle* handles, + uint32_t num_handles, + MojoWriteMessageFlags flags); + MojoResult ReadMessage(MojoHandle message_pipe_handle, + void* bytes, + uint32_t* num_bytes, + MojoHandle* handles, + uint32_t* num_handles, + MojoReadMessageFlags flags); + MojoResult CreateDataPipe( + const MojoCreateDataPipeOptions* options, + MojoHandle* data_pipe_producer_handle, + MojoHandle* data_pipe_consumer_handle); + MojoResult WriteData(MojoHandle data_pipe_producer_handle, + const void* elements, + uint32_t* num_bytes, + MojoWriteDataFlags flags); + MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle, + void** buffer, + uint32_t* buffer_num_bytes, + MojoWriteDataFlags flags); + MojoResult EndWriteData(MojoHandle data_pipe_producer_handle, + uint32_t num_bytes_written); + MojoResult ReadData(MojoHandle data_pipe_consumer_handle, + void* elements, + uint32_t* num_bytes, + MojoReadDataFlags flags); + MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle, + const void** buffer, + uint32_t* buffer_num_bytes, + MojoReadDataFlags flags); + MojoResult EndReadData(MojoHandle data_pipe_consumer_handle, + uint32_t num_bytes_read); + MojoResult CreateSharedBuffer( + const MojoCreateSharedBufferOptions* options, + uint64_t num_bytes, + MojoHandle* shared_buffer_handle); + MojoResult DuplicateBufferHandle( + MojoHandle buffer_handle, + const MojoDuplicateBufferHandleOptions* options, + MojoHandle* new_buffer_handle); + MojoResult MapBuffer(MojoHandle buffer_handle, + uint64_t offset, + uint64_t num_bytes, + void** buffer, + MojoMapBufferFlags flags); + MojoResult UnmapBuffer(void* buffer); + + private: + friend bool internal::ShutdownCheckNoLeaks(Core*); + // The |busy| member is used only to deal with functions (in particular + // |WriteMessage()|) that want to hold on to a dispatcher and later remove it + // from the handle table, without holding on to the handle table lock. + // + // For example, if |WriteMessage()| is called with a handle to be sent, (under + // the handle table lock) it must first check that that handle is not busy (if + // it is busy, then it fails with |MOJO_RESULT_BUSY|) and then marks it as + // busy. To avoid deadlock, it should also try to acquire the locks for all + // the dispatchers for the handles that it is sending (and fail with + // |MOJO_RESULT_BUSY| if the attempt fails). At this point, it can release the + // handle table lock. + // + // If |Close()| is simultaneously called on that handle, it too checks if the + // handle is marked busy. If it is, it fails (with |MOJO_RESULT_BUSY|). This + // prevents |WriteMessage()| from sending a handle that has been closed (or + // learning about this too late). + struct HandleTableEntry { + HandleTableEntry(); + explicit HandleTableEntry(const scoped_refptr<Dispatcher>& dispatcher); + ~HandleTableEntry(); + + scoped_refptr<Dispatcher> dispatcher; + bool busy; + }; + typedef base::hash_map<MojoHandle, HandleTableEntry> HandleTableMap; + + // Looks up the dispatcher for the given handle. Returns null if the handle is + // invalid. + scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); + + // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic + // validation of arguments. + MojoResult WaitManyInternal(const MojoHandle* handles, + const MojoWaitFlags* flags, + uint32_t num_handles, + MojoDeadline deadline); + + // --------------------------------------------------------------------------- + + // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we + // had them). + base::Lock handle_table_lock_; // Protects |handle_table_|. + HandleTable handle_table_; + + base::Lock mapping_table_lock_; // Protects |mapping_table_|. + MappingTable mapping_table_; + + // --------------------------------------------------------------------------- + + DISALLOW_COPY_AND_ASSIGN(Core); +}; + +} // namespace system +} // namespace mojo + +#endif // MOJO_SYSTEM_CORE_H_ diff --git a/mojo/system/core_impl.h b/mojo/system/core_impl.h deleted file mode 100644 index e0cbd1e..0000000 --- a/mojo/system/core_impl.h +++ /dev/null @@ -1,153 +0,0 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_SYSTEM_CORE_IMPL_H_ -#define MOJO_SYSTEM_CORE_IMPL_H_ - -#include "base/basictypes.h" -#include "base/compiler_specific.h" -#include "base/memory/ref_counted.h" -#include "base/synchronization/lock.h" -#include "mojo/public/system/core_private.h" -#include "mojo/system/handle_table.h" -#include "mojo/system/mapping_table.h" -#include "mojo/system/system_impl_export.h" - -namespace mojo { -namespace system { - -class Dispatcher; - -// |CoreImpl| is a singleton object that implements the Mojo system calls. All -// public methods are thread-safe. -class MOJO_SYSTEM_IMPL_EXPORT CoreImpl : public Core { - public: - // These methods are only to be used by via the embedder API. - CoreImpl(); - virtual ~CoreImpl(); - MojoHandle AddDispatcher(const scoped_refptr<Dispatcher>& dispatcher); - - // |CorePrivate| implementation: - virtual MojoTimeTicks GetTimeTicksNow() OVERRIDE; - virtual MojoResult Close(MojoHandle handle) OVERRIDE; - virtual MojoResult Wait(MojoHandle handle, - MojoWaitFlags flags, - MojoDeadline deadline) OVERRIDE; - virtual MojoResult WaitMany(const MojoHandle* handles, - const MojoWaitFlags* flags, - uint32_t num_handles, - MojoDeadline deadline) OVERRIDE; - virtual MojoResult CreateMessagePipe( - MojoHandle* message_pipe_handle0, - MojoHandle* message_pipe_handle1) OVERRIDE; - virtual MojoResult WriteMessage(MojoHandle message_pipe_handle, - const void* bytes, - uint32_t num_bytes, - const MojoHandle* handles, - uint32_t num_handles, - MojoWriteMessageFlags flags) OVERRIDE; - virtual MojoResult ReadMessage(MojoHandle message_pipe_handle, - void* bytes, - uint32_t* num_bytes, - MojoHandle* handles, - uint32_t* num_handles, - MojoReadMessageFlags flags) OVERRIDE; - virtual MojoResult CreateDataPipe( - const MojoCreateDataPipeOptions* options, - MojoHandle* data_pipe_producer_handle, - MojoHandle* data_pipe_consumer_handle) OVERRIDE; - virtual MojoResult WriteData(MojoHandle data_pipe_producer_handle, - const void* elements, - uint32_t* num_bytes, - MojoWriteDataFlags flags) OVERRIDE; - virtual MojoResult BeginWriteData(MojoHandle data_pipe_producer_handle, - void** buffer, - uint32_t* buffer_num_bytes, - MojoWriteDataFlags flags) OVERRIDE; - virtual MojoResult EndWriteData(MojoHandle data_pipe_producer_handle, - uint32_t num_bytes_written) OVERRIDE; - virtual MojoResult ReadData(MojoHandle data_pipe_consumer_handle, - void* elements, - uint32_t* num_bytes, - MojoReadDataFlags flags) OVERRIDE; - virtual MojoResult BeginReadData(MojoHandle data_pipe_consumer_handle, - const void** buffer, - uint32_t* buffer_num_bytes, - MojoReadDataFlags flags) OVERRIDE; - virtual MojoResult EndReadData(MojoHandle data_pipe_consumer_handle, - uint32_t num_bytes_read) OVERRIDE; - virtual MojoResult CreateSharedBuffer( - const MojoCreateSharedBufferOptions* options, - uint64_t num_bytes, - MojoHandle* shared_buffer_handle) OVERRIDE; - virtual MojoResult DuplicateBufferHandle( - MojoHandle buffer_handle, - const MojoDuplicateBufferHandleOptions* options, - MojoHandle* new_buffer_handle) OVERRIDE; - virtual MojoResult MapBuffer(MojoHandle buffer_handle, - uint64_t offset, - uint64_t num_bytes, - void** buffer, - MojoMapBufferFlags flags) OVERRIDE; - virtual MojoResult UnmapBuffer(void* buffer) OVERRIDE; - - private: - friend bool internal::ShutdownCheckNoLeaks(CoreImpl*); - - // The |busy| member is used only to deal with functions (in particular - // |WriteMessage()|) that want to hold on to a dispatcher and later remove it - // from the handle table, without holding on to the handle table lock. - // - // For example, if |WriteMessage()| is called with a handle to be sent, (under - // the handle table lock) it must first check that that handle is not busy (if - // it is busy, then it fails with |MOJO_RESULT_BUSY|) and then marks it as - // busy. To avoid deadlock, it should also try to acquire the locks for all - // the dispatchers for the handles that it is sending (and fail with - // |MOJO_RESULT_BUSY| if the attempt fails). At this point, it can release the - // handle table lock. - // - // If |Close()| is simultaneously called on that handle, it too checks if the - // handle is marked busy. If it is, it fails (with |MOJO_RESULT_BUSY|). This - // prevents |WriteMessage()| from sending a handle that has been closed (or - // learning about this too late). - struct HandleTableEntry { - HandleTableEntry(); - explicit HandleTableEntry(const scoped_refptr<Dispatcher>& dispatcher); - ~HandleTableEntry(); - - scoped_refptr<Dispatcher> dispatcher; - bool busy; - }; - typedef base::hash_map<MojoHandle, HandleTableEntry> HandleTableMap; - - // Looks up the dispatcher for the given handle. Returns null if the handle is - // invalid. - scoped_refptr<Dispatcher> GetDispatcher(MojoHandle handle); - - // Internal implementation of |Wait()| and |WaitMany()|; doesn't do basic - // validation of arguments. - MojoResult WaitManyInternal(const MojoHandle* handles, - const MojoWaitFlags* flags, - uint32_t num_handles, - MojoDeadline deadline); - - // --------------------------------------------------------------------------- - - // TODO(vtl): |handle_table_lock_| should be a reader-writer lock (if only we - // had them). - base::Lock handle_table_lock_; // Protects |handle_table_|. - HandleTable handle_table_; - - base::Lock mapping_table_lock_; // Protects |mapping_table_|. - MappingTable mapping_table_; - - // --------------------------------------------------------------------------- - - DISALLOW_COPY_AND_ASSIGN(CoreImpl); -}; - -} // namespace system -} // namespace mojo - -#endif // MOJO_SYSTEM_CORE_IMPL_H_ diff --git a/mojo/system/core_test_base.cc b/mojo/system/core_test_base.cc index 89b8fb3..29c3c5c 100644 --- a/mojo/system/core_test_base.cc +++ b/mojo/system/core_test_base.cc @@ -10,7 +10,7 @@ #include "base/logging.h" #include "base/memory/ref_counted.h" #include "mojo/system/constants.h" -#include "mojo/system/core_impl.h" +#include "mojo/system/core.h" #include "mojo/system/dispatcher.h" #include "mojo/system/memory.h" @@ -168,7 +168,7 @@ CoreTestBase::~CoreTestBase() { } void CoreTestBase::SetUp() { - core_ = new CoreImpl(); + core_ = new Core(); } void CoreTestBase::TearDown() { diff --git a/mojo/system/core_test_base.h b/mojo/system/core_test_base.h index beb985b..b725938 100644 --- a/mojo/system/core_test_base.h +++ b/mojo/system/core_test_base.h @@ -14,7 +14,7 @@ namespace mojo { namespace system { -class CoreImpl; +class Core; namespace test { @@ -34,10 +34,10 @@ class CoreTestBase : public testing::Test { // |info| must remain alive until the returned handle is closed. MojoHandle CreateMockHandle(MockHandleInfo* info); - CoreImpl* core() { return core_; } + Core* core() { return core_; } private: - CoreImpl* core_; + Core* core_; DISALLOW_COPY_AND_ASSIGN(CoreTestBase); }; diff --git a/mojo/system/core_impl_unittest.cc b/mojo/system/core_unittest.cc index 93dc860..8fab233 100644 --- a/mojo/system/core_impl_unittest.cc +++ b/mojo/system/core_unittest.cc @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/system/core_impl.h" +#include "mojo/system/core.h" #include <limits> @@ -15,9 +15,9 @@ namespace mojo { namespace system { namespace { -typedef test::CoreTestBase CoreImplTest; +typedef test::CoreTestBase CoreTest; -TEST_F(CoreImplTest, GetTimeTicksNow) { +TEST_F(CoreTest, GetTimeTicksNow) { const MojoTimeTicks start = core()->GetTimeTicksNow(); EXPECT_NE(static_cast<MojoTimeTicks>(0), start) << "GetTimeTicksNow should return nonzero value"; @@ -28,7 +28,7 @@ TEST_F(CoreImplTest, GetTimeTicksNow) { << "Sleeping should result in increasing time ticks"; } -TEST_F(CoreImplTest, Basic) { +TEST_F(CoreTest, Basic) { MockHandleInfo info; EXPECT_EQ(0u, info.GetCtorCallCount()); @@ -120,7 +120,7 @@ TEST_F(CoreImplTest, Basic) { EXPECT_EQ(0u, info.GetRemoveWaiterCallCount()); } -TEST_F(CoreImplTest, InvalidArguments) { +TEST_F(CoreTest, InvalidArguments) { // |Close()|: { EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->Close(MOJO_HANDLE_INVALID)); @@ -195,7 +195,7 @@ TEST_F(CoreImplTest, InvalidArguments) { } // |WriteMessage()|: - // Only check arguments checked by |CoreImpl|, namely |handle|, |handles|, and + // Only check arguments checked by |Core|, namely |handle|, |handles|, and // |num_handles|. { EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, @@ -210,7 +210,7 @@ TEST_F(CoreImplTest, InvalidArguments) { EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->WriteMessage(h, NULL, 0, NULL, 1, MOJO_WRITE_MESSAGE_FLAG_NONE)); - // Checked by |CoreImpl|, shouldn't go through to the dispatcher. + // Checked by |Core|, shouldn't go through to the dispatcher. EXPECT_EQ(0u, info.GetWriteMessageCallCount()); // Huge handle count (implausibly big on some systems -- more than can be @@ -289,7 +289,7 @@ TEST_F(CoreImplTest, InvalidArguments) { } // |ReadMessage()|: - // Only check arguments checked by |CoreImpl|, namely |handle|, |handles|, and + // Only check arguments checked by |Core|, namely |handle|, |handles|, and // |num_handles|. { EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, @@ -303,7 +303,7 @@ TEST_F(CoreImplTest, InvalidArguments) { EXPECT_EQ(MOJO_RESULT_INVALID_ARGUMENT, core()->ReadMessage(h, NULL, NULL, NULL, &handle_count, MOJO_READ_MESSAGE_FLAG_NONE)); - // Checked by |CoreImpl|, shouldn't go through to the dispatcher. + // Checked by |Core|, shouldn't go through to the dispatcher. EXPECT_EQ(0u, info.GetReadMessageCallCount()); // Okay. @@ -311,7 +311,7 @@ TEST_F(CoreImplTest, InvalidArguments) { EXPECT_EQ(MOJO_RESULT_OK, core()->ReadMessage(h, NULL, NULL, NULL, &handle_count, MOJO_READ_MESSAGE_FLAG_NONE)); - // Checked by |CoreImpl|, shouldn't go through to the dispatcher. + // Checked by |Core|, shouldn't go through to the dispatcher. EXPECT_EQ(1u, info.GetReadMessageCallCount()); EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h)); @@ -322,7 +322,7 @@ TEST_F(CoreImplTest, InvalidArguments) { // - including |WaitMany()| with the same handle more than once (with // same/different flags) -TEST_F(CoreImplTest, MessagePipe) { +TEST_F(CoreTest, MessagePipe) { MojoHandle h[2]; EXPECT_EQ(MOJO_RESULT_OK, core()->CreateMessagePipe(&h[0], &h[1])); @@ -424,7 +424,7 @@ TEST_F(CoreImplTest, MessagePipe) { } // Tests passing a message pipe handle. -TEST_F(CoreImplTest, MessagePipeBasicLocalHandlePassing1) { +TEST_F(CoreTest, MessagePipeBasicLocalHandlePassing1) { const char kHello[] = "hello"; const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello)); const char kWorld[] = "world!!!"; @@ -551,7 +551,7 @@ TEST_F(CoreImplTest, MessagePipeBasicLocalHandlePassing1) { EXPECT_EQ(MOJO_RESULT_OK, core()->Close(h_received)); } -TEST_F(CoreImplTest, DataPipe) { +TEST_F(CoreTest, DataPipe) { MojoHandle ph, ch; // p is for producer and c is for consumer. EXPECT_EQ(MOJO_RESULT_OK, core()->CreateDataPipe(NULL, &ph, &ch)); @@ -673,7 +673,7 @@ TEST_F(CoreImplTest, DataPipe) { } // Tests passing data pipe producer and consumer handles. -TEST_F(CoreImplTest, MessagePipeBasicLocalHandlePassing2) { +TEST_F(CoreTest, MessagePipeBasicLocalHandlePassing2) { const char kHello[] = "hello"; const uint32_t kHelloSize = static_cast<uint32_t>(sizeof(kHello)); const char kWorld[] = "world!!!"; diff --git a/mojo/system/data_pipe.h b/mojo/system/data_pipe.h index 8d6fcaf..9abd81e 100644 --- a/mojo/system/data_pipe.h +++ b/mojo/system/data_pipe.h @@ -19,7 +19,7 @@ class Waiter; class WaiterList; // |DataPipe| is a base class for secondary objects implementing data pipes, -// similar to |MessagePipe| (see the explanatory comment in core_impl.cc). It is +// similar to |MessagePipe| (see the explanatory comment in core.cc). It is // typically owned by the dispatcher(s) corresponding to the local endpoints. // Its subclasses implement the three cases: local producer and consumer, local // producer and remote consumer, and remote producer and local consumer. This diff --git a/mojo/system/dispatcher.h b/mojo/system/dispatcher.h index 5bb6b3e..504e33a 100644 --- a/mojo/system/dispatcher.h +++ b/mojo/system/dispatcher.h @@ -22,7 +22,7 @@ namespace mojo { namespace system { class Channel; -class CoreImpl; +class Core; class Dispatcher; class DispatcherTransport; class HandleTable; @@ -135,9 +135,9 @@ class MOJO_SYSTEM_IMPL_EXPORT Dispatcher : // has been called. class HandleTableAccess { private: - friend class CoreImpl; + friend class Core; friend class HandleTable; - // Tests also need this, to avoid needing |CoreImpl|. + // Tests also need this, to avoid needing |Core|. friend DispatcherTransport test::DispatcherTryStartTransport(Dispatcher*); // This must be called under the handle table lock and only if the handle diff --git a/mojo/public/system/core_private.cc b/mojo/system/entrypoints.cc index bd9d6b8..096fd02 100644 --- a/mojo/public/system/core_private.cc +++ b/mojo/system/entrypoints.cc @@ -1,30 +1,41 @@ -// Copyright 2013 The Chromium Authors. All rights reserved. +// Copyright 2014 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include "mojo/public/system/core_private.h" +#include "mojo/system/core.h" -#include <assert.h> -#include <stddef.h> +static mojo::system::Core* g_core = NULL; -static mojo::Core* g_core = NULL; +namespace mojo { +namespace system { +namespace entrypoints { + +void SetCore(Core* core) { + g_core = core; +} + +Core* GetCore() { + return g_core; +} + +} // namespace entrypoints +} // namepace system +} // namespace mojo +// Definitions of the system functions. extern "C" { MojoTimeTicks MojoGetTimeTicksNow() { - assert(g_core); return g_core->GetTimeTicksNow(); } MojoResult MojoClose(MojoHandle handle) { - assert(g_core); return g_core->Close(handle); } MojoResult MojoWait(MojoHandle handle, MojoWaitFlags flags, MojoDeadline deadline) { - assert(g_core); return g_core->Wait(handle, flags, deadline); } @@ -32,13 +43,11 @@ MojoResult MojoWaitMany(const MojoHandle* handles, const MojoWaitFlags* flags, uint32_t num_handles, MojoDeadline deadline) { - assert(g_core); return g_core->WaitMany(handles, flags, num_handles, deadline); } MojoResult MojoCreateMessagePipe(MojoHandle* message_pipe_handle0, MojoHandle* message_pipe_handle1) { - assert(g_core); return g_core->CreateMessagePipe(message_pipe_handle0, message_pipe_handle1); } @@ -48,51 +57,45 @@ MojoResult MojoWriteMessage(MojoHandle message_pipe_handle, const MojoHandle* handles, uint32_t num_handles, MojoWriteMessageFlags flags) { - assert(g_core); - return g_core->WriteMessage(message_pipe_handle, bytes, num_bytes, handles, - num_handles, flags); + return g_core->WriteMessage( + message_pipe_handle, bytes, num_bytes, handles, num_handles, flags); } MojoResult MojoReadMessage(MojoHandle message_pipe_handle, void* bytes, uint32_t* num_bytes, MojoHandle* handles, - uint32_t* num_handles, + uint32_t* num_handles, MojoReadMessageFlags flags) { - assert(g_core); - return g_core->ReadMessage(message_pipe_handle, bytes, num_bytes, handles, - num_handles, flags); + return g_core->ReadMessage( + message_pipe_handle, bytes, num_bytes, handles, num_handles, flags); } MojoResult MojoCreateDataPipe(const MojoCreateDataPipeOptions* options, MojoHandle* data_pipe_producer_handle, MojoHandle* data_pipe_consumer_handle) { - assert(g_core); - return g_core->CreateDataPipe(options, data_pipe_producer_handle, - data_pipe_consumer_handle); + return g_core->CreateDataPipe( + options, data_pipe_producer_handle, data_pipe_consumer_handle); } MojoResult MojoWriteData(MojoHandle data_pipe_producer_handle, const void* elements, uint32_t* num_elements, MojoWriteDataFlags flags) { - assert(g_core); - return g_core->WriteData(data_pipe_producer_handle, elements, num_elements, - flags); + return g_core->WriteData( + data_pipe_producer_handle, elements, num_elements, flags); } MojoResult MojoBeginWriteData(MojoHandle data_pipe_producer_handle, void** buffer, uint32_t* buffer_num_elements, MojoWriteDataFlags flags) { - assert(g_core); - return g_core->BeginWriteData(data_pipe_producer_handle, buffer, - buffer_num_elements, flags); + return g_core->BeginWriteData( + data_pipe_producer_handle, buffer, buffer_num_elements, flags); } MojoResult MojoEndWriteData(MojoHandle data_pipe_producer_handle, uint32_t num_elements_written) { - assert(g_core); return g_core->EndWriteData(data_pipe_producer_handle, num_elements_written); } @@ -100,23 +103,20 @@ MojoResult MojoReadData(MojoHandle data_pipe_consumer_handle, void* elements, uint32_t* num_elements, MojoReadDataFlags flags) { - assert(g_core); - return g_core->ReadData(data_pipe_consumer_handle, elements, num_elements, - flags); + return g_core->ReadData( + data_pipe_consumer_handle, elements, num_elements, flags); } MojoResult MojoBeginReadData(MojoHandle data_pipe_consumer_handle, const void** buffer, uint32_t* buffer_num_elements, MojoReadDataFlags flags) { - assert(g_core); - return g_core->BeginReadData(data_pipe_consumer_handle, buffer, - buffer_num_elements, flags); + return g_core->BeginReadData( + data_pipe_consumer_handle, buffer, buffer_num_elements, flags); } MojoResult MojoEndReadData(MojoHandle data_pipe_consumer_handle, uint32_t num_elements_read) { - assert(g_core); return g_core->EndReadData(data_pipe_consumer_handle, num_elements_read); } @@ -124,7 +124,6 @@ MojoResult MojoCreateSharedBuffer( const struct MojoCreateSharedBufferOptions* options, uint64_t num_bytes, MojoHandle* shared_buffer_handle) { - assert(g_core); return g_core->CreateSharedBuffer(options, num_bytes, shared_buffer_handle); } @@ -132,9 +131,8 @@ MojoResult MojoDuplicateBufferHandle( MojoHandle buffer_handle, const struct MojoDuplicateBufferHandleOptions* options, MojoHandle* new_buffer_handle) { - assert(g_core); - return g_core->DuplicateBufferHandle(buffer_handle, options, - new_buffer_handle); + return g_core->DuplicateBufferHandle( + buffer_handle, options, new_buffer_handle); } MojoResult MojoMapBuffer(MojoHandle buffer_handle, @@ -142,36 +140,11 @@ MojoResult MojoMapBuffer(MojoHandle buffer_handle, uint64_t num_bytes, void** buffer, MojoMapBufferFlags flags) { - assert(g_core); return g_core->MapBuffer(buffer_handle, offset, num_bytes, buffer, flags); } MojoResult MojoUnmapBuffer(void* buffer) { - assert(g_core); return g_core->UnmapBuffer(buffer); } } // extern "C" - -namespace mojo { - -Core::~Core() { -} - -// static -void Core::Init(Core* core) { - assert(!g_core); - g_core = core; -} - -// static -Core* Core::Get() { - return g_core; -} - -// static -void Core::Reset() { - g_core = NULL; -} - -} // namespace mojo diff --git a/mojo/system/entrypoints.h b/mojo/system/entrypoints.h new file mode 100644 index 0000000..65a0363 --- /dev/null +++ b/mojo/system/entrypoints.h @@ -0,0 +1,24 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef MOJO_SYSTEM_ENTRYPOINTS_H +#define MOJO_SYSTEM_ENTRYPOINTS_H + +namespace mojo { +namespace system { + +class Core; + +namespace entrypoints { + +// Sets the instance of Core to be used by system functions. +void SetCore(Core* core); +// Gets the instance of Core to be used by system functions. +Core* GetCore(); + +} // namespace entrypoints +} // namepace system +} // namespace mojo + +#endif // MOJO_SYSTEM_ENTRYPOINTS_H diff --git a/mojo/system/handle_table.cc b/mojo/system/handle_table.cc index 798b610..b863e81 100644 --- a/mojo/system/handle_table.cc +++ b/mojo/system/handle_table.cc @@ -31,7 +31,7 @@ HandleTable::HandleTable() HandleTable::~HandleTable() { // This should usually not be reached (the only instance should be owned by - // the singleton |CoreImpl|, which lives forever), except in tests. + // the singleton |Core|, which lives forever), except in tests. } Dispatcher* HandleTable::GetDispatcher(MojoHandle handle) { diff --git a/mojo/system/handle_table.h b/mojo/system/handle_table.h index 4bcf02c..408fae2 100644 --- a/mojo/system/handle_table.h +++ b/mojo/system/handle_table.h @@ -17,23 +17,23 @@ namespace mojo { namespace system { -class CoreImpl; +class Core; class Dispatcher; class DispatcherTransport; // Test-only function (defined/used in embedder/test_embedder.cc). Declared here // so it can be friended. namespace internal { -bool ShutdownCheckNoLeaks(CoreImpl*); +bool ShutdownCheckNoLeaks(Core*); } -// This class provides the (global) handle table (owned by |CoreImpl|), which -// maps (valid) |MojoHandle|s to |Dispatcher|s. This is abstracted so that, -// e.g., caching may be added. +// This class provides the (global) handle table (owned by |Core|), which maps +// (valid) |MojoHandle|s to |Dispatcher|s. This is abstracted so that, e.g., +// caching may be added. // -// This class is NOT thread-safe; locking is left to |CoreImpl| (since it may -// need to make several changes -- "atomically" or in rapid successsion, in -// which case the extra locking/unlocking would be unnecessary overhead). +// This class is NOT thread-safe; locking is left to |Core| (since it may need +// to make several changes -- "atomically" or in rapid successsion, in which +// case the extra locking/unlocking would be unnecessary overhead). class MOJO_SYSTEM_IMPL_EXPORT HandleTable { public: @@ -44,8 +44,8 @@ class MOJO_SYSTEM_IMPL_EXPORT HandleTable { // |MOJO_HANDLE_INVALID|). Returns null if there's no dispatcher for the given // handle. // WARNING: For efficiency, this returns a dumb pointer. If you're going to - // use the result outside |CoreImpl|'s lock, you MUST take a reference (e.g., - // by storing the result inside a |scoped_refptr|). + // use the result outside |Core|'s lock, you MUST take a reference (e.g., by + // storing the result inside a |scoped_refptr|). Dispatcher* GetDispatcher(MojoHandle handle); // On success, gets the dispatcher for a given handle (which should not be @@ -98,7 +98,7 @@ class MOJO_SYSTEM_IMPL_EXPORT HandleTable { void RestoreBusyHandles(const MojoHandle* handles, uint32_t num_handles); private: - friend bool internal::ShutdownCheckNoLeaks(CoreImpl*); + friend bool internal::ShutdownCheckNoLeaks(Core*); struct Entry { Entry(); diff --git a/mojo/system/mapping_table.cc b/mojo/system/mapping_table.cc index 39b79bc..a6e5bb3 100644 --- a/mojo/system/mapping_table.cc +++ b/mojo/system/mapping_table.cc @@ -16,7 +16,7 @@ MappingTable::MappingTable() { MappingTable::~MappingTable() { // This should usually not be reached (the only instance should be owned by - // the singleton |CoreImpl|, which lives forever), except in tests. + // the singleton |Core|, which lives forever), except in tests. } MojoResult MappingTable::AddMapping( diff --git a/mojo/system/mapping_table.h b/mojo/system/mapping_table.h index bbae097..1b14890 100644 --- a/mojo/system/mapping_table.h +++ b/mojo/system/mapping_table.h @@ -18,20 +18,19 @@ namespace mojo { namespace system { -class CoreImpl; +class Core; class RawSharedBufferMapping; // Test-only function (defined/used in embedder/test_embedder.cc). Declared here // so it can be friended. namespace internal { -bool ShutdownCheckNoLeaks(CoreImpl*); +bool ShutdownCheckNoLeaks(Core*); } -// This class provides the (global) table of memory mappings (owned by -// |CoreImpl|), which maps mapping base addresses to -// |RawSharedBuffer::Mapping|s. +// This class provides the (global) table of memory mappings (owned by |Core|), +// which maps mapping base addresses to |RawSharedBuffer::Mapping|s. // -// This class is NOT thread-safe; locking is left to |CoreImpl|. +// This class is NOT thread-safe; locking is left to |Core|. class MOJO_SYSTEM_IMPL_EXPORT MappingTable { public: MappingTable(); @@ -43,7 +42,7 @@ class MOJO_SYSTEM_IMPL_EXPORT MappingTable { MojoResult RemoveMapping(void* address); private: - friend bool internal::ShutdownCheckNoLeaks(CoreImpl*); + friend bool internal::ShutdownCheckNoLeaks(Core*); typedef base::hash_map<uintptr_t, RawSharedBufferMapping*> AddressToMappingMap; diff --git a/mojo/system/message_pipe.cc b/mojo/system/message_pipe.cc index e450569..6f2e5cb 100644 --- a/mojo/system/message_pipe.cc +++ b/mojo/system/message_pipe.cc @@ -181,7 +181,7 @@ MojoResult MessagePipe::EnqueueMessage( // You're not allowed to send either handle to a message pipe over the // message pipe, so check for this. (The case of trying to write a handle to - // itself is taken care of by |CoreImpl|. That case kind of makes sense, but + // itself is taken care of by |Core|. That case kind of makes sense, but // leads to complications if, e.g., both sides try to do the same thing with // their respective handles simultaneously. The other case, of trying to // write the peer handle to a handle, doesn't make sense -- since no handle @@ -192,8 +192,8 @@ MojoResult MessagePipe::EnqueueMessage( if ((*transports)[i].GetType() == Dispatcher::kTypeMessagePipe) { MessagePipeDispatcherTransport mp_transport((*transports)[i]); if (mp_transport.GetMessagePipe() == this) { - // The other case should have been disallowed by |CoreImpl|. (Note: - // |port| is the peer port of the handle given to |WriteMessage()|.) + // The other case should have been disallowed by |Core|. (Note: |port| + // is the peer port of the handle given to |WriteMessage()|.) DCHECK_EQ(mp_transport.GetPort(), port); return MOJO_RESULT_INVALID_ARGUMENT; } diff --git a/mojo/system/message_pipe.h b/mojo/system/message_pipe.h index de42c44..85cce0a 100644 --- a/mojo/system/message_pipe.h +++ b/mojo/system/message_pipe.h @@ -24,9 +24,8 @@ class Channel; class Waiter; // |MessagePipe| is the secondary object implementing a message pipe (see the -// explanatory comment in core_impl.cc). It is typically owned by the -// dispatcher(s) corresponding to the local endpoints. This class is -// thread-safe. +// explanatory comment in core.cc). It is typically owned by the dispatcher(s) +// corresponding to the local endpoints. This class is thread-safe. class MOJO_SYSTEM_IMPL_EXPORT MessagePipe : public base::RefCountedThreadSafe<MessagePipe> { public: diff --git a/mojo/system/simple_dispatcher.h b/mojo/system/simple_dispatcher.h index d2934fa..15a64d5 100644 --- a/mojo/system/simple_dispatcher.h +++ b/mojo/system/simple_dispatcher.h @@ -17,8 +17,8 @@ namespace system { // A base class for simple dispatchers. "Simple" means that there's a one-to-one // correspondence between handles and dispatchers (see the explanatory comment -// in core_impl.cc). This class implements the standard waiter-signalling -// mechanism in that case. +// in core.cc). This class implements the standard waiter-signalling mechanism +// in that case. class MOJO_SYSTEM_IMPL_EXPORT SimpleDispatcher : public Dispatcher { protected: SimpleDispatcher(); diff --git a/mojo/system/waiter_list.h b/mojo/system/waiter_list.h index 1d35b94..f6a69ed 100644 --- a/mojo/system/waiter_list.h +++ b/mojo/system/waiter_list.h @@ -20,8 +20,8 @@ class Waiter; // handle/|Dispatcher|. There should be a |WaiterList| for each handle that can // be waited on (in any way). In the simple case, the |WaiterList| is owned by // the |Dispatcher|, whereas in more complex cases it is owned by the secondary -// object (see simple_dispatcher.* and the explanatory comment in core_impl.cc). -// This class is thread-unsafe (all concurrent access must be protected by some +// object (see simple_dispatcher.* and the explanatory comment in core.cc). This +// class is thread-unsafe (all concurrent access must be protected by some // lock). class MOJO_SYSTEM_IMPL_EXPORT WaiterList { public: |