1 // Copyright (c) 2012 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.
5 #include "content/renderer/pepper/pepper_broker.h"
7 #include "build/build_config.h"
8 #include "content/renderer/pepper/pepper_proxy_channel_delegate_impl.h"
9 #include "content/renderer/pepper/plugin_module.h"
10 #include "content/renderer/pepper/ppb_broker_impl.h"
11 #include "content/renderer/pepper/renderer_restrict_dispatch_group.h"
12 #include "ipc/ipc_channel_handle.h"
13 #include "ppapi/proxy/broker_dispatcher.h"
14 #include "ppapi/proxy/ppapi_messages.h"
15 #include "ppapi/shared_impl/platform_file.h"
25 base::SyncSocket::Handle DuplicateHandle(base::SyncSocket::Handle handle) {
26 base::SyncSocket::Handle out_handle = base::kInvalidPlatformFileValue;
28 DWORD options = DUPLICATE_SAME_ACCESS;
29 if (!::DuplicateHandle(::GetCurrentProcess(),
31 ::GetCurrentProcess(),
36 out_handle = base::kInvalidPlatformFileValue;
38 #elif defined(OS_POSIX)
39 // If asked to close the source, we can simply re-use the source fd instead of
40 // dup()ing and close()ing.
41 out_handle = ::dup(handle);
43 #error Not implemented.
50 PepperBrokerDispatcherWrapper::PepperBrokerDispatcherWrapper() {
53 PepperBrokerDispatcherWrapper::~PepperBrokerDispatcherWrapper() {
56 bool PepperBrokerDispatcherWrapper::Init(
57 base::ProcessId broker_pid,
58 const IPC::ChannelHandle& channel_handle) {
59 if (channel_handle.name.empty())
63 DCHECK_NE(-1, channel_handle.socket.fd);
64 if (channel_handle.socket.fd == -1)
68 dispatcher_delegate_.reset(new PepperProxyChannelDelegateImpl);
70 new ppapi::proxy::BrokerHostDispatcher());
72 if (!dispatcher_->InitBrokerWithChannel(dispatcher_delegate_.get(),
77 dispatcher_delegate_.reset();
80 dispatcher_->channel()->SetRestrictDispatchChannelGroup(
81 kRendererRestrictDispatchGroup_Pepper);
85 // Does not take ownership of the local pipe.
86 int32_t PepperBrokerDispatcherWrapper::SendHandleToBroker(
88 base::SyncSocket::Handle handle) {
89 IPC::PlatformFileForTransit foreign_socket_handle =
90 dispatcher_->ShareHandleWithRemote(handle, false);
91 if (foreign_socket_handle == IPC::InvalidPlatformFileForTransit())
92 return PP_ERROR_FAILED;
95 if (!dispatcher_->Send(
96 new PpapiMsg_ConnectToPlugin(instance, foreign_socket_handle, &result))) {
97 // The plugin did not receive the handle, so it must be closed.
98 // The easiest way to clean it up is to just put it in an object
99 // and then close it. This failure case is not performance critical.
100 // The handle could still leak if Send succeeded but the IPC later failed.
101 base::SyncSocket temp_socket(
102 IPC::PlatformFileForTransitToPlatformFile(foreign_socket_handle));
103 return PP_ERROR_FAILED;
109 PepperBroker::PepperBroker(PluginModule* plugin_module)
110 : plugin_module_(plugin_module) {
111 DCHECK(plugin_module_);
113 plugin_module_->SetBroker(this);
116 PepperBroker::~PepperBroker() {
117 ReportFailureToClients(PP_ERROR_ABORTED);
118 plugin_module_->SetBroker(NULL);
119 plugin_module_ = NULL;
122 // If the channel is not ready, queue the connection.
123 void PepperBroker::AddPendingConnect(PPB_Broker_Impl* client) {
124 DCHECK(pending_connects_.find(client) == pending_connects_.end())
125 << "Connect was already called for this client";
127 // Ensure this object and the associated broker exist as long as the
128 // client exists. There is a corresponding Release() call in Disconnect(),
129 // which is called when the PPB_Broker_Impl is destroyed. The only other
130 // possible reference is in pending_connect_broker_, which only holds a
131 // transient reference. This ensures the broker is available as long as the
132 // plugin needs it and allows the plugin to release the broker when it is no
136 pending_connects_[client].client = client->AsWeakPtr();
139 void PepperBroker::Disconnect(PPB_Broker_Impl* client) {
140 // Remove the pending connect if one exists. This class will not call client's
142 pending_connects_.erase(client);
144 // TODO(ddorwin): Send message disconnect message using dispatcher_.
146 // Release the reference added in Connect().
147 // This must be the last statement because it may delete this object.
151 void PepperBroker::OnBrokerChannelConnected(
152 base::ProcessId broker_pid,
153 const IPC::ChannelHandle& channel_handle) {
154 scoped_ptr<PepperBrokerDispatcherWrapper> dispatcher(
155 new PepperBrokerDispatcherWrapper);
156 if (!dispatcher->Init(broker_pid, channel_handle)) {
157 ReportFailureToClients(PP_ERROR_FAILED);
161 dispatcher_.reset(dispatcher.release());
163 // Process all pending channel requests from the plugins.
164 for (ClientMap::iterator i = pending_connects_.begin();
165 i != pending_connects_.end();) {
166 base::WeakPtr<PPB_Broker_Impl>& weak_ptr = i->second.client;
167 if (!i->second.is_authorized) {
173 ConnectPluginToBroker(weak_ptr.get());
175 pending_connects_.erase(i++);
179 void PepperBroker::OnBrokerPermissionResult(PPB_Broker_Impl* client,
181 ClientMap::iterator entry = pending_connects_.find(client);
182 if (entry == pending_connects_.end())
185 if (!entry->second.client.get()) {
186 // Client has gone away.
187 pending_connects_.erase(entry);
193 client->BrokerConnected(
194 ppapi::PlatformFileToInt(base::kInvalidPlatformFileValue),
196 pending_connects_.erase(entry);
201 ConnectPluginToBroker(client);
202 pending_connects_.erase(entry);
206 // Mark the request as authorized, continue waiting for the broker
208 DCHECK(!entry->second.is_authorized);
209 entry->second.is_authorized = true;
212 PepperBroker::PendingConnection::PendingConnection() : is_authorized(false) {
215 PepperBroker::PendingConnection::~PendingConnection() {
218 void PepperBroker::ReportFailureToClients(int error_code) {
219 DCHECK_NE(PP_OK, error_code);
220 for (ClientMap::iterator i = pending_connects_.begin();
221 i != pending_connects_.end(); ++i) {
222 base::WeakPtr<PPB_Broker_Impl>& weak_ptr = i->second.client;
223 if (weak_ptr.get()) {
224 weak_ptr->BrokerConnected(
225 ppapi::PlatformFileToInt(base::kInvalidPlatformFileValue),
229 pending_connects_.clear();
232 void PepperBroker::ConnectPluginToBroker(PPB_Broker_Impl* client) {
233 base::SyncSocket::Handle plugin_handle = base::kInvalidPlatformFileValue;
234 int32_t result = PP_OK;
236 // The socket objects will be deleted when this function exits, closing the
237 // handles. Any uses of the socket must duplicate them.
238 scoped_ptr<base::SyncSocket> broker_socket(new base::SyncSocket());
239 scoped_ptr<base::SyncSocket> plugin_socket(new base::SyncSocket());
240 if (base::SyncSocket::CreatePair(broker_socket.get(), plugin_socket.get())) {
241 result = dispatcher_->SendHandleToBroker(client->pp_instance(),
242 broker_socket->handle());
244 // If the broker has its pipe handle, duplicate the plugin's handle.
245 // Otherwise, the plugin's handle will be automatically closed.
247 plugin_handle = DuplicateHandle(plugin_socket->handle());
249 result = PP_ERROR_FAILED;
252 // TOOD(ddorwin): Change the IPC to asynchronous: Queue an object containing
253 // client and plugin_socket.release(), then return.
254 // That message handler will then call client->BrokerConnected() with the
255 // saved pipe handle.
256 // Temporarily, just call back.
257 client->BrokerConnected(ppapi::PlatformFileToInt(plugin_handle), result);
260 } // namespace content