| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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/core.h" | 5 #include "mojo/edk/system/core.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 #include "mojo/edk/system/waiter.h" | 37 #include "mojo/edk/system/waiter.h" |
| 38 | 38 |
| 39 namespace mojo { | 39 namespace mojo { |
| 40 namespace edk { | 40 namespace edk { |
| 41 | 41 |
| 42 namespace { | 42 namespace { |
| 43 | 43 |
| 44 // This is an unnecessarily large limit that is relatively easy to enforce. | 44 // This is an unnecessarily large limit that is relatively easy to enforce. |
| 45 const uint32_t kMaxHandlesPerMessage = 1024 * 1024; | 45 const uint32_t kMaxHandlesPerMessage = 1024 * 1024; |
| 46 | 46 |
| 47 void OnPortConnected( | 47 // TODO: Maybe we could negotiate a debugging pipe ID for cross-process pipes |
| 48 Core* core, | 48 // too; for now we just use a constant. This only affects bootstrap pipes. |
| 49 int endpoint, | 49 const uint64_t kUnknownPipeIdForDebug = 0x7f7f7f7f7f7f7f7fUL; |
| 50 const base::Callback<void(ScopedMessagePipeHandle)>& callback, | |
| 51 const ports::PortRef& port) { | |
| 52 // TODO: Maybe we could negotiate a pipe ID for cross-process pipes too; | |
| 53 // for now we just use 0x7F7F7F7F7F7F7F7F. In practice these are used for | |
| 54 // bootstrap and aren't passed around, so tracking them is less important. | |
| 55 MojoHandle handle = core->AddDispatcher( | |
| 56 new MessagePipeDispatcher(core->GetNodeController(), port, | |
| 57 0x7f7f7f7f7f7f7f7fUL, endpoint)); | |
| 58 callback.Run(ScopedMessagePipeHandle(MessagePipeHandle(handle))); | |
| 59 } | |
| 60 | 50 |
| 61 } // namespace | 51 } // namespace |
| 62 | 52 |
| 63 Core::Core() {} | 53 Core::Core() {} |
| 64 | 54 |
| 65 Core::~Core() { | 55 Core::~Core() { |
| 66 if (node_controller_ && node_controller_->io_task_runner()) { | 56 if (node_controller_ && node_controller_->io_task_runner()) { |
| 67 // If this races with IO thread shutdown the callback will be dropped and | 57 // If this races with IO thread shutdown the callback will be dropped and |
| 68 // the NodeController will be shutdown on this thread anyway, which is also | 58 // the NodeController will be shutdown on this thread anyway, which is also |
| 69 // just fine. | 59 // just fine. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 if (base::ThreadTaskRunnerHandle::IsSet()) { | 143 if (base::ThreadTaskRunnerHandle::IsSet()) { |
| 154 on_shutdown = base::Bind(base::IgnoreResult(&base::TaskRunner::PostTask), | 144 on_shutdown = base::Bind(base::IgnoreResult(&base::TaskRunner::PostTask), |
| 155 base::ThreadTaskRunnerHandle::Get(), | 145 base::ThreadTaskRunnerHandle::Get(), |
| 156 FROM_HERE, callback); | 146 FROM_HERE, callback); |
| 157 } else { | 147 } else { |
| 158 on_shutdown = callback; | 148 on_shutdown = callback; |
| 159 } | 149 } |
| 160 GetNodeController()->RequestShutdown(on_shutdown); | 150 GetNodeController()->RequestShutdown(on_shutdown); |
| 161 } | 151 } |
| 162 | 152 |
| 163 void Core::CreateMessagePipe( | 153 ScopedMessagePipeHandle Core::CreateMessagePipe( |
| 164 ScopedPlatformHandle platform_handle, | 154 ScopedPlatformHandle platform_handle) { |
| 165 const base::Callback<void(ScopedMessagePipeHandle)>& callback) { | 155 ports::PortRef port0, port1; |
| 166 ports::PortRef port; | 156 GetNodeController()->node()->CreatePortPair(&port0, &port1); |
| 167 GetNodeController()->node()->CreateUninitializedPort(&port); | 157 MojoHandle handle = AddDispatcher( |
| 158 new MessagePipeDispatcher(GetNodeController(), port0, |
| 159 kUnknownPipeIdForDebug, 0)); |
| 168 RemoteMessagePipeBootstrap::Create( | 160 RemoteMessagePipeBootstrap::Create( |
| 169 GetNodeController(), std::move(platform_handle), port, | 161 GetNodeController(), std::move(platform_handle), port1); |
| 170 base::Bind(&OnPortConnected, base::Unretained(this), 0, callback, port)); | 162 return ScopedMessagePipeHandle(MessagePipeHandle(handle)); |
| 171 } | 163 } |
| 172 | 164 |
| 173 void Core::CreateParentMessagePipe( | 165 ScopedMessagePipeHandle Core::CreateParentMessagePipe( |
| 174 const std::string& token, | 166 const std::string& token) { |
| 175 const base::Callback<void(ScopedMessagePipeHandle)>& callback) { | 167 ports::PortRef port0, port1; |
| 176 GetNodeController()->ReservePort( | 168 GetNodeController()->node()->CreatePortPair(&port0, &port1); |
| 177 token, | 169 MojoHandle handle = AddDispatcher( |
| 178 base::Bind(&OnPortConnected, base::Unretained(this), 0, callback)); | 170 new MessagePipeDispatcher(GetNodeController(), port0, |
| 171 kUnknownPipeIdForDebug, 0)); |
| 172 GetNodeController()->ReservePort(token, port1); |
| 173 return ScopedMessagePipeHandle(MessagePipeHandle(handle)); |
| 179 } | 174 } |
| 180 | 175 |
| 181 void Core::CreateChildMessagePipe( | 176 ScopedMessagePipeHandle Core::CreateChildMessagePipe(const std::string& token) { |
| 182 const std::string& token, | 177 ports::PortRef port0, port1; |
| 183 const base::Callback<void(ScopedMessagePipeHandle)>& callback) { | 178 GetNodeController()->node()->CreatePortPair(&port0, &port1); |
| 184 ports::PortRef port; | 179 MojoHandle handle = AddDispatcher( |
| 185 GetNodeController()->node()->CreateUninitializedPort(&port); | 180 new MessagePipeDispatcher(GetNodeController(), port0, |
| 186 GetNodeController()->ConnectToParentPort( | 181 kUnknownPipeIdForDebug, 1)); |
| 187 port, token, | 182 GetNodeController()->MergePortIntoParent(token, port1); |
| 188 base::Bind(&OnPortConnected, base::Unretained(this), 1, callback, port)); | 183 return ScopedMessagePipeHandle(MessagePipeHandle(handle)); |
| 189 } | 184 } |
| 190 | 185 |
| 191 MojoResult Core::AsyncWait(MojoHandle handle, | 186 MojoResult Core::AsyncWait(MojoHandle handle, |
| 192 MojoHandleSignals signals, | 187 MojoHandleSignals signals, |
| 193 const base::Callback<void(MojoResult)>& callback) { | 188 const base::Callback<void(MojoResult)>& callback) { |
| 194 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handle); | 189 scoped_refptr<Dispatcher> dispatcher = GetDispatcher(handle); |
| 195 DCHECK(dispatcher); | 190 DCHECK(dispatcher); |
| 196 | 191 |
| 197 scoped_ptr<AsyncWaiter> waiter = make_scoped_ptr(new AsyncWaiter(callback)); | 192 scoped_ptr<AsyncWaiter> waiter = make_scoped_ptr(new AsyncWaiter(callback)); |
| 198 MojoResult rv = dispatcher->AddAwakable(waiter.get(), signals, 0, nullptr); | 193 MojoResult rv = dispatcher->AddAwakable(waiter.get(), signals, 0, nullptr); |
| (...skipping 505 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 704 scoped_ptr<NodeController> node_controller) { | 699 scoped_ptr<NodeController> node_controller) { |
| 705 // It's OK to leak this reference. At this point we know the IO loop is still | 700 // It's OK to leak this reference. At this point we know the IO loop is still |
| 706 // running, and we know the NodeController will observe its eventual | 701 // running, and we know the NodeController will observe its eventual |
| 707 // destruction. This tells the NodeController to delete itself when that | 702 // destruction. This tells the NodeController to delete itself when that |
| 708 // happens. | 703 // happens. |
| 709 node_controller.release()->DestroyOnIOThreadShutdown(); | 704 node_controller.release()->DestroyOnIOThreadShutdown(); |
| 710 } | 705 } |
| 711 | 706 |
| 712 } // namespace edk | 707 } // namespace edk |
| 713 } // namespace mojo | 708 } // namespace mojo |
| OLD | NEW |