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