dispatcher.cc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright 2013 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. #include "mojo/core/dispatcher.h"
  5. #include "base/logging.h"
  6. #include "base/no_destructor.h"
  7. #include "base/threading/thread_local.h"
  8. #include "mojo/core/configuration.h"
  9. #include "mojo/core/data_pipe_consumer_dispatcher.h"
  10. #include "mojo/core/data_pipe_producer_dispatcher.h"
  11. #include "mojo/core/message_pipe_dispatcher.h"
  12. #include "mojo/core/platform_handle_dispatcher.h"
  13. #include "mojo/core/ports/event.h"
  14. #include "mojo/core/shared_buffer_dispatcher.h"
  15. namespace mojo {
  16. namespace core {
  17. namespace {
  18. base::ThreadLocalBoolean& IsExtractingHandlesFromMessage() {
  19. static base::NoDestructor<base::ThreadLocalBoolean> flag;
  20. return *flag;
  21. }
  22. } // namespace
  23. Dispatcher::DispatcherInTransit::DispatcherInTransit() = default;
  24. Dispatcher::DispatcherInTransit::DispatcherInTransit(
  25. const DispatcherInTransit& other) = default;
  26. Dispatcher::DispatcherInTransit::~DispatcherInTransit() = default;
  27. // static
  28. void Dispatcher::SetExtractingHandlesFromMessage(bool extracting) {
  29. IsExtractingHandlesFromMessage().Set(extracting);
  30. }
  31. // static
  32. void Dispatcher::AssertNotExtractingHandlesFromMessage() {
  33. DCHECK(!IsExtractingHandlesFromMessage().Get());
  34. }
  35. MojoResult Dispatcher::WatchDispatcher(scoped_refptr<Dispatcher> dispatcher,
  36. MojoHandleSignals signals,
  37. MojoTriggerCondition condition,
  38. uintptr_t context) {
  39. return MOJO_RESULT_INVALID_ARGUMENT;
  40. }
  41. MojoResult Dispatcher::CancelWatch(uintptr_t context) {
  42. return MOJO_RESULT_INVALID_ARGUMENT;
  43. }
  44. MojoResult Dispatcher::Arm(uint32_t* num_blocking_events,
  45. MojoTrapEvent* blocking_events) {
  46. return MOJO_RESULT_INVALID_ARGUMENT;
  47. }
  48. MojoResult Dispatcher::WriteMessage(
  49. std::unique_ptr<ports::UserMessageEvent> message) {
  50. return MOJO_RESULT_INVALID_ARGUMENT;
  51. }
  52. MojoResult Dispatcher::ReadMessage(
  53. std::unique_ptr<ports::UserMessageEvent>* message) {
  54. return MOJO_RESULT_INVALID_ARGUMENT;
  55. }
  56. MojoResult Dispatcher::DuplicateBufferHandle(
  57. const MojoDuplicateBufferHandleOptions* options,
  58. scoped_refptr<Dispatcher>* new_dispatcher) {
  59. return MOJO_RESULT_INVALID_ARGUMENT;
  60. }
  61. MojoResult Dispatcher::MapBuffer(
  62. uint64_t offset,
  63. uint64_t num_bytes,
  64. std::unique_ptr<PlatformSharedMemoryMapping>* mapping) {
  65. return MOJO_RESULT_INVALID_ARGUMENT;
  66. }
  67. MojoResult Dispatcher::GetBufferInfo(MojoSharedBufferInfo* info) {
  68. return MOJO_RESULT_INVALID_ARGUMENT;
  69. }
  70. MojoResult Dispatcher::ReadData(const MojoReadDataOptions& options,
  71. void* elements,
  72. uint32_t* num_bytes) {
  73. return MOJO_RESULT_INVALID_ARGUMENT;
  74. }
  75. MojoResult Dispatcher::BeginReadData(const void** buffer,
  76. uint32_t* buffer_num_bytes) {
  77. return MOJO_RESULT_INVALID_ARGUMENT;
  78. }
  79. MojoResult Dispatcher::EndReadData(uint32_t num_bytes_read) {
  80. return MOJO_RESULT_INVALID_ARGUMENT;
  81. }
  82. MojoResult Dispatcher::WriteData(const void* elements,
  83. uint32_t* num_bytes,
  84. const MojoWriteDataOptions& options) {
  85. return MOJO_RESULT_INVALID_ARGUMENT;
  86. }
  87. MojoResult Dispatcher::BeginWriteData(void** buffer,
  88. uint32_t* buffer_num_bytes) {
  89. return MOJO_RESULT_INVALID_ARGUMENT;
  90. }
  91. MojoResult Dispatcher::EndWriteData(uint32_t num_bytes_written) {
  92. return MOJO_RESULT_INVALID_ARGUMENT;
  93. }
  94. MojoResult Dispatcher::AttachMessagePipe(base::StringPiece name,
  95. ports::PortRef remote_peer_port) {
  96. return MOJO_RESULT_INVALID_ARGUMENT;
  97. }
  98. MojoResult Dispatcher::ExtractMessagePipe(base::StringPiece name,
  99. MojoHandle* message_pipe_handle) {
  100. return MOJO_RESULT_INVALID_ARGUMENT;
  101. }
  102. MojoResult Dispatcher::SetQuota(MojoQuotaType type, uint64_t limit) {
  103. return MOJO_RESULT_INVALID_ARGUMENT;
  104. }
  105. MojoResult Dispatcher::QueryQuota(MojoQuotaType type,
  106. uint64_t* limit,
  107. uint64_t* usage) {
  108. return MOJO_RESULT_INVALID_ARGUMENT;
  109. }
  110. HandleSignalsState Dispatcher::GetHandleSignalsState() const {
  111. return HandleSignalsState();
  112. }
  113. MojoResult Dispatcher::AddWatcherRef(
  114. const scoped_refptr<WatcherDispatcher>& watcher,
  115. uintptr_t context) {
  116. return MOJO_RESULT_INVALID_ARGUMENT;
  117. }
  118. MojoResult Dispatcher::RemoveWatcherRef(WatcherDispatcher* watcher,
  119. uintptr_t context) {
  120. return MOJO_RESULT_INVALID_ARGUMENT;
  121. }
  122. void Dispatcher::StartSerialize(uint32_t* num_bytes,
  123. uint32_t* num_ports,
  124. uint32_t* num_platform_handles) {
  125. *num_bytes = 0;
  126. *num_ports = 0;
  127. *num_platform_handles = 0;
  128. }
  129. bool Dispatcher::EndSerialize(void* destination,
  130. ports::PortName* ports,
  131. PlatformHandle* handles) {
  132. LOG(ERROR) << "Attempting to serialize a non-transferrable dispatcher.";
  133. return true;
  134. }
  135. bool Dispatcher::BeginTransit() {
  136. return true;
  137. }
  138. void Dispatcher::CompleteTransitAndClose() {}
  139. void Dispatcher::CancelTransit() {}
  140. // static
  141. scoped_refptr<Dispatcher> Dispatcher::Deserialize(
  142. Type type,
  143. const void* bytes,
  144. size_t num_bytes,
  145. const ports::PortName* ports,
  146. size_t num_ports,
  147. PlatformHandle* platform_handles,
  148. size_t num_platform_handles) {
  149. switch (type) {
  150. case Type::MESSAGE_PIPE:
  151. return MessagePipeDispatcher::Deserialize(bytes, num_bytes, ports,
  152. num_ports, platform_handles,
  153. num_platform_handles);
  154. case Type::SHARED_BUFFER:
  155. return SharedBufferDispatcher::Deserialize(bytes, num_bytes, ports,
  156. num_ports, platform_handles,
  157. num_platform_handles);
  158. case Type::DATA_PIPE_CONSUMER:
  159. return DataPipeConsumerDispatcher::Deserialize(
  160. bytes, num_bytes, ports, num_ports, platform_handles,
  161. num_platform_handles);
  162. case Type::DATA_PIPE_PRODUCER:
  163. return DataPipeProducerDispatcher::Deserialize(
  164. bytes, num_bytes, ports, num_ports, platform_handles,
  165. num_platform_handles);
  166. case Type::PLATFORM_HANDLE:
  167. return PlatformHandleDispatcher::Deserialize(bytes, num_bytes, ports,
  168. num_ports, platform_handles,
  169. num_platform_handles);
  170. default:
  171. LOG(ERROR) << "Deserializing invalid dispatcher type.";
  172. return nullptr;
  173. }
  174. }
  175. Dispatcher::Dispatcher() = default;
  176. Dispatcher::~Dispatcher() = default;
  177. } // namespace core
  178. } // namespace mojo