| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/browser/mojo/mojo_child_connection.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/macros.h" | |
| 11 #include "content/public/common/mojo_shell_connection.h" | |
| 12 #include "mojo/edk/embedder/embedder.h" | |
| 13 #include "mojo/public/cpp/system/message_pipe.h" | |
| 14 #include "services/shell/public/cpp/connector.h" | |
| 15 #include "services/shell/public/cpp/identity.h" | |
| 16 #include "services/shell/public/cpp/interface_registry.h" | |
| 17 #include "services/shell/public/interfaces/service.mojom.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 void CallBinderOnTaskRunner( | |
| 24 const shell::InterfaceRegistry::Binder& binder, | |
| 25 scoped_refptr<base::SequencedTaskRunner> task_runner, | |
| 26 const std::string& interface_name, | |
| 27 mojo::ScopedMessagePipeHandle request_handle) { | |
| 28 task_runner->PostTask( | |
| 29 FROM_HERE, | |
| 30 base::Bind(binder, interface_name, base::Passed(&request_handle))); | |
| 31 } | |
| 32 | |
| 33 } // namespace | |
| 34 | |
| 35 class MojoChildConnection::IOThreadContext | |
| 36 : public base::RefCountedThreadSafe<IOThreadContext> { | |
| 37 public: | |
| 38 IOThreadContext() {} | |
| 39 | |
| 40 void Initialize(const shell::Identity& child_identity, | |
| 41 shell::Connector* connector, | |
| 42 mojo::ScopedMessagePipeHandle service_pipe, | |
| 43 scoped_refptr<base::SequencedTaskRunner> io_task_runner) { | |
| 44 DCHECK(!io_task_runner_); | |
| 45 io_task_runner_ = io_task_runner; | |
| 46 std::unique_ptr<shell::Connector> io_thread_connector; | |
| 47 if (connector) | |
| 48 io_thread_connector = connector->Clone(); | |
| 49 io_task_runner_->PostTask( | |
| 50 FROM_HERE, | |
| 51 base::Bind(&IOThreadContext::InitializeOnIOThread, this, | |
| 52 child_identity, | |
| 53 base::Passed(&io_thread_connector), | |
| 54 base::Passed(&service_pipe))); | |
| 55 } | |
| 56 | |
| 57 void ShutDown() { | |
| 58 if (!io_task_runner_) | |
| 59 return; | |
| 60 bool posted = io_task_runner_->PostTask( | |
| 61 FROM_HERE, | |
| 62 base::Bind(&IOThreadContext::ShutDownOnIOThread, this)); | |
| 63 DCHECK(posted); | |
| 64 } | |
| 65 | |
| 66 void GetRemoteInterfaceOnIOThread( | |
| 67 const std::string& interface_name, | |
| 68 mojo::ScopedMessagePipeHandle request_handle) { | |
| 69 if (connection_) { | |
| 70 connection_->GetRemoteInterfaces()->GetInterface( | |
| 71 interface_name, std::move(request_handle)); | |
| 72 } | |
| 73 } | |
| 74 | |
| 75 void SetProcessHandle(base::ProcessHandle handle) { | |
| 76 DCHECK(io_task_runner_); | |
| 77 io_task_runner_->PostTask( | |
| 78 FROM_HERE, | |
| 79 base::Bind(&IOThreadContext::SetProcessHandleOnIOThread, this, handle)); | |
| 80 } | |
| 81 | |
| 82 private: | |
| 83 friend class base::RefCountedThreadSafe<IOThreadContext>; | |
| 84 | |
| 85 virtual ~IOThreadContext() {} | |
| 86 | |
| 87 void InitializeOnIOThread( | |
| 88 const shell::Identity& child_identity, | |
| 89 std::unique_ptr<shell::Connector> connector, | |
| 90 mojo::ScopedMessagePipeHandle service_pipe) { | |
| 91 shell::mojom::ServicePtr service; | |
| 92 service.Bind(mojo::InterfacePtrInfo<shell::mojom::Service>( | |
| 93 std::move(service_pipe), 0u)); | |
| 94 shell::mojom::PIDReceiverRequest pid_receiver_request = | |
| 95 mojo::GetProxy(&pid_receiver_); | |
| 96 | |
| 97 shell::Connector::ConnectParams params(child_identity); | |
| 98 params.set_client_process_connection(std::move(service), | |
| 99 std::move(pid_receiver_request)); | |
| 100 | |
| 101 // In some unit testing scenarios a null connector is passed. | |
| 102 if (connector) | |
| 103 connection_ = connector->Connect(¶ms); | |
| 104 } | |
| 105 | |
| 106 void ShutDownOnIOThread() { | |
| 107 connection_.reset(); | |
| 108 pid_receiver_.reset(); | |
| 109 } | |
| 110 | |
| 111 void SetProcessHandleOnIOThread(base::ProcessHandle handle) { | |
| 112 DCHECK(pid_receiver_.is_bound()); | |
| 113 pid_receiver_->SetPID(base::GetProcId(handle)); | |
| 114 pid_receiver_.reset(); | |
| 115 } | |
| 116 | |
| 117 scoped_refptr<base::SequencedTaskRunner> io_task_runner_; | |
| 118 std::unique_ptr<shell::Connection> connection_; | |
| 119 shell::mojom::PIDReceiverPtr pid_receiver_; | |
| 120 | |
| 121 DISALLOW_COPY_AND_ASSIGN(IOThreadContext); | |
| 122 }; | |
| 123 | |
| 124 MojoChildConnection::MojoChildConnection( | |
| 125 const std::string& service_name, | |
| 126 const std::string& instance_id, | |
| 127 const std::string& child_token, | |
| 128 shell::Connector* connector, | |
| 129 scoped_refptr<base::SequencedTaskRunner> io_task_runner) | |
| 130 : context_(new IOThreadContext), | |
| 131 child_identity_(service_name, shell::mojom::kInheritUserID, instance_id), | |
| 132 service_token_(mojo::edk::GenerateRandomToken()), | |
| 133 weak_factory_(this) { | |
| 134 mojo::ScopedMessagePipeHandle service_pipe = | |
| 135 mojo::edk::CreateParentMessagePipe(service_token_, child_token); | |
| 136 | |
| 137 context_ = new IOThreadContext; | |
| 138 context_->Initialize(child_identity_, connector, std::move(service_pipe), | |
| 139 io_task_runner); | |
| 140 remote_interfaces_.Forward( | |
| 141 base::Bind(&CallBinderOnTaskRunner, | |
| 142 base::Bind(&IOThreadContext::GetRemoteInterfaceOnIOThread, | |
| 143 context_), io_task_runner)); | |
| 144 } | |
| 145 | |
| 146 MojoChildConnection::~MojoChildConnection() { | |
| 147 context_->ShutDown(); | |
| 148 } | |
| 149 | |
| 150 void MojoChildConnection::SetProcessHandle(base::ProcessHandle handle) { | |
| 151 context_->SetProcessHandle(handle); | |
| 152 } | |
| 153 | |
| 154 } // namespace content | |
| OLD | NEW |