OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "mojo/edk/system/message_pipe_dispatcher.h" | 5 #include "mojo/edk/system/message_pipe_dispatcher.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "mojo/edk/embedder/embedder_internal.h" | 10 #include "mojo/edk/embedder/embedder_internal.h" |
11 #include "mojo/edk/embedder/platform_handle_utils.h" | 11 #include "mojo/edk/embedder/platform_handle_utils.h" |
12 #include "mojo/edk/embedder/platform_shared_buffer.h" | 12 #include "mojo/edk/embedder/platform_shared_buffer.h" |
13 #include "mojo/edk/embedder/platform_support.h" | 13 #include "mojo/edk/embedder/platform_support.h" |
14 #include "mojo/edk/system/configuration.h" | 14 #include "mojo/edk/system/configuration.h" |
15 #include "mojo/edk/system/message_in_transit.h" | 15 #include "mojo/edk/system/message_in_transit.h" |
16 #include "mojo/edk/system/options_validation.h" | 16 #include "mojo/edk/system/options_validation.h" |
17 #include "mojo/edk/system/transport_data.h" | 17 #include "mojo/edk/system/transport_data.h" |
18 | 18 |
19 #if defined(OS_WIN) | 19 #if defined(OS_WIN) |
20 #include "mojo/edk/system/token_serializer_win.h" | 20 #include "mojo/edk/system/token_serializer_win.h" |
21 #endif | 21 #endif |
22 | 22 |
23 namespace mojo { | 23 namespace mojo { |
24 namespace edk { | 24 namespace edk { |
25 | 25 |
26 // TODO(jam): do more tests on using channel on same thread if it supports it ( | |
27 // i.e. with USE_CHROME_EDK and Windows). Also see ipc_channel_mojo.cc | |
28 bool g_use_channel_on_io_thread_only = true; | |
29 | |
30 namespace { | 26 namespace { |
31 | 27 |
32 const size_t kInvalidMessagePipeHandleIndex = static_cast<size_t>(-1); | 28 const size_t kInvalidMessagePipeHandleIndex = static_cast<size_t>(-1); |
33 | 29 |
34 struct MOJO_ALIGNAS(8) SerializedMessagePipeHandleDispatcher { | 30 struct MOJO_ALIGNAS(8) SerializedMessagePipeHandleDispatcher { |
35 // Could be |kInvalidMessagePipeHandleIndex| if the other endpoint of the MP | 31 // Could be |kInvalidMessagePipeHandleIndex| if the other endpoint of the MP |
36 // was closed. | 32 // was closed. |
37 size_t platform_handle_index; | 33 size_t platform_handle_index; |
38 bool write_error; | 34 bool write_error; |
39 | 35 |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 std::vector<int>* serialized_read_fds, | 123 std::vector<int>* serialized_read_fds, |
128 std::vector<int>* serialized_write_fds) { | 124 std::vector<int>* serialized_write_fds) { |
129 if (message_pipe.get().is_valid()) { | 125 if (message_pipe.get().is_valid()) { |
130 channel_ = RawChannel::Create(message_pipe.Pass()); | 126 channel_ = RawChannel::Create(message_pipe.Pass()); |
131 | 127 |
132 // TODO(jam): It's probably cleaner to pass this in Init call. | 128 // TODO(jam): It's probably cleaner to pass this in Init call. |
133 channel_->SetSerializedData( | 129 channel_->SetSerializedData( |
134 serialized_read_buffer, serialized_read_buffer_size, | 130 serialized_read_buffer, serialized_read_buffer_size, |
135 serialized_write_buffer, serialized_write_buffer_size, | 131 serialized_write_buffer, serialized_write_buffer_size, |
136 serialized_read_fds, serialized_write_fds); | 132 serialized_read_fds, serialized_write_fds); |
137 if (g_use_channel_on_io_thread_only) { | 133 internal::g_io_thread_task_runner->PostTask( |
138 internal::g_io_thread_task_runner->PostTask( | 134 FROM_HERE, base::Bind(&MessagePipeDispatcher::InitOnIO, this)); |
139 FROM_HERE, base::Bind(&MessagePipeDispatcher::InitOnIO, this)); | |
140 } else { | |
141 InitOnIO(); | |
142 } | |
143 // TODO(jam): optimize for when running on IO thread? | |
144 } | 135 } |
145 } | 136 } |
146 | 137 |
147 void MessagePipeDispatcher::InitOnIO() { | 138 void MessagePipeDispatcher::InitOnIO() { |
148 base::AutoLock locker(lock()); | 139 base::AutoLock locker(lock()); |
149 calling_init_ = true; | 140 calling_init_ = true; |
150 if (channel_) | 141 if (channel_) |
151 channel_->Init(this); | 142 channel_->Init(this); |
152 calling_init_ = false; | 143 calling_init_ = false; |
153 } | 144 } |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
350 : channel_(nullptr), | 341 : channel_(nullptr), |
351 serialized_(false), | 342 serialized_(false), |
352 serialized_read_fds_length_(0u), | 343 serialized_read_fds_length_(0u), |
353 serialized_write_fds_length_(0u), | 344 serialized_write_fds_length_(0u), |
354 serialized_message_fds_length_(0u), | 345 serialized_message_fds_length_(0u), |
355 calling_init_(false), | 346 calling_init_(false), |
356 write_error_(false) { | 347 write_error_(false) { |
357 } | 348 } |
358 | 349 |
359 MessagePipeDispatcher::~MessagePipeDispatcher() { | 350 MessagePipeDispatcher::~MessagePipeDispatcher() { |
360 // |Close()|/|CloseImplNoLock()| should have taken care of the channel. | 351 // |Close()|/|CloseImplNoLock()| should have taken care of the channel. The |
361 DCHECK(!channel_); | 352 // exception is if they posted a task to run CloseOnIO but the IO thread shut |
| 353 // down and so when it was deleting pending tasks it caused the last reference |
| 354 // to destruct this object. In that case, safe to destroy the channel. |
| 355 if (channel_ && internal::g_io_thread_task_runner->RunsTasksOnCurrentThread()) |
| 356 channel_->Shutdown(); |
| 357 else |
| 358 DCHECK(!channel_); |
362 #if defined(OS_POSIX) | 359 #if defined(OS_POSIX) |
363 ClosePlatformHandles(&serialized_fds_); | 360 ClosePlatformHandles(&serialized_fds_); |
364 #endif | 361 #endif |
365 } | 362 } |
366 | 363 |
367 void MessagePipeDispatcher::CancelAllAwakablesNoLock() { | 364 void MessagePipeDispatcher::CancelAllAwakablesNoLock() { |
368 lock().AssertAcquired(); | 365 lock().AssertAcquired(); |
369 awakable_list_.CancelAll(); | 366 awakable_list_.CancelAll(); |
370 } | 367 } |
371 | 368 |
372 void MessagePipeDispatcher::CloseImplNoLock() { | 369 void MessagePipeDispatcher::CloseImplNoLock() { |
373 lock().AssertAcquired(); | 370 lock().AssertAcquired(); |
374 if (g_use_channel_on_io_thread_only) { | 371 internal::g_io_thread_task_runner->PostTask( |
375 internal::g_io_thread_task_runner->PostTask( | 372 FROM_HERE, base::Bind(&MessagePipeDispatcher::CloseOnIO, this)); |
376 FROM_HERE, base::Bind(&MessagePipeDispatcher::CloseOnIO, this)); | |
377 } else { | |
378 CloseOnIO(); | |
379 } | |
380 } | 373 } |
381 | 374 |
382 void MessagePipeDispatcher::SerializeInternal() { | 375 void MessagePipeDispatcher::SerializeInternal() { |
383 // We need to stop watching handle immediately, even though not on IO thread, | 376 // We need to stop watching handle immediately, even though not on IO thread, |
384 // so that other messages aren't read after this. | 377 // so that other messages aren't read after this. |
385 std::vector<int> serialized_read_fds, serialized_write_fds; | 378 std::vector<int> serialized_read_fds, serialized_write_fds; |
386 if (channel_) { | 379 if (channel_) { |
387 bool write_error = false; | 380 bool write_error = false; |
388 | 381 |
389 serialized_platform_handle_ = channel_->ReleaseHandle( | 382 serialized_platform_handle_ = channel_->ReleaseHandle( |
(...skipping 465 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
855 LOG(WARNING) << "Enqueueing null dispatcher"; | 848 LOG(WARNING) << "Enqueueing null dispatcher"; |
856 dispatchers->push_back(nullptr); | 849 dispatchers->push_back(nullptr); |
857 } | 850 } |
858 } | 851 } |
859 message->SetDispatchers(dispatchers.Pass()); | 852 message->SetDispatchers(dispatchers.Pass()); |
860 return MOJO_RESULT_OK; | 853 return MOJO_RESULT_OK; |
861 } | 854 } |
862 | 855 |
863 } // namespace edk | 856 } // namespace edk |
864 } // namespace mojo | 857 } // namespace mojo |
OLD | NEW |