[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 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 "ppapi/proxy/broker_dispatcher.h" |
| 6 | |
[email protected] | 2b657fd | 2011-04-18 16:00:47 | [diff] [blame] | 7 | #include "base/sync_socket.h" |
| 8 | #include "ppapi/c/pp_errors.h" |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 9 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | 5a77eca | 2011-11-23 23:46:46 | [diff] [blame] | 10 | #include "ppapi/shared_impl/platform_file.h" |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 11 | |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 12 | namespace ppapi { |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 13 | namespace proxy { |
| 14 | |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 15 | BrokerDispatcher::BrokerDispatcher(PP_ConnectInstance_Func connect_instance) |
| 16 | : connect_instance_(connect_instance) { |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 17 | } |
| 18 | |
| 19 | BrokerDispatcher::~BrokerDispatcher() { |
| 20 | } |
| 21 | |
| 22 | bool BrokerDispatcher::InitBrokerWithChannel( |
| 23 | ProxyChannel::Delegate* delegate, |
[email protected] | 108fd34 | 2013-01-04 20:46:54 | [diff] [blame] | 24 | base::ProcessId peer_pid, |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 25 | const IPC::ChannelHandle& channel_handle, |
| 26 | bool is_client) { |
[email protected] | f7b7eb7c | 2014-02-27 23:54:15 | [diff] [blame] | 27 | return ProxyChannel::InitWithChannel(delegate, peer_pid, channel_handle, |
| 28 | is_client); |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 29 | } |
| 30 | |
| 31 | bool BrokerDispatcher::OnMessageReceived(const IPC::Message& msg) { |
| 32 | // Control messages. |
| 33 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 34 | bool handled = true; |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 35 | IPC_BEGIN_MESSAGE_MAP(BrokerDispatcher, msg) |
[email protected] | 2b657fd | 2011-04-18 16:00:47 | [diff] [blame] | 36 | IPC_MESSAGE_HANDLER(PpapiMsg_ConnectToPlugin, OnMsgConnectToPlugin) |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 37 | IPC_MESSAGE_UNHANDLED(handled = false) |
| 38 | IPC_END_MESSAGE_MAP() |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 39 | return handled; |
| 40 | } |
| 41 | return false; |
| 42 | } |
| 43 | |
[email protected] | 2b657fd | 2011-04-18 16:00:47 | [diff] [blame] | 44 | // Transfers ownership of the handle to the broker module. |
| 45 | void BrokerDispatcher::OnMsgConnectToPlugin( |
| 46 | PP_Instance instance, |
[email protected] | 6990e5d | 2011-09-30 18:10:57 | [diff] [blame] | 47 | IPC::PlatformFileForTransit handle, |
| 48 | int32_t* result) { |
[email protected] | 2b657fd | 2011-04-18 16:00:47 | [diff] [blame] | 49 | if (handle == IPC::InvalidPlatformFileForTransit()) { |
[email protected] | 6990e5d | 2011-09-30 18:10:57 | [diff] [blame] | 50 | *result = PP_ERROR_FAILED; |
[email protected] | 2b657fd | 2011-04-18 16:00:47 | [diff] [blame] | 51 | } else { |
| 52 | base::SyncSocket::Handle socket_handle = |
| 53 | IPC::PlatformFileForTransitToPlatformFile(handle); |
| 54 | |
| 55 | if (connect_instance_) { |
[email protected] | 5a77eca | 2011-11-23 23:46:46 | [diff] [blame] | 56 | *result = connect_instance_(instance, |
| 57 | ppapi::PlatformFileToInt(socket_handle)); |
[email protected] | 2b657fd | 2011-04-18 16:00:47 | [diff] [blame] | 58 | } else { |
[email protected] | 6990e5d | 2011-09-30 18:10:57 | [diff] [blame] | 59 | *result = PP_ERROR_FAILED; |
[email protected] | 2b657fd | 2011-04-18 16:00:47 | [diff] [blame] | 60 | // Close the handle since there is no other owner. |
| 61 | // The easiest way to clean it up is to just put it in an object |
| 62 | // and then close them. This failure case is not performance critical. |
| 63 | base::SyncSocket temp_socket(socket_handle); |
| 64 | } |
| 65 | } |
[email protected] | 2b657fd | 2011-04-18 16:00:47 | [diff] [blame] | 66 | } |
| 67 | |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 68 | BrokerHostDispatcher::BrokerHostDispatcher() |
| 69 | : BrokerDispatcher(NULL) { |
[email protected] | 2b657fd | 2011-04-18 16:00:47 | [diff] [blame] | 70 | } |
| 71 | |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 72 | void BrokerHostDispatcher::OnChannelError() { |
[email protected] | 1977348dd | 2011-10-26 22:27:41 | [diff] [blame] | 73 | DVLOG(1) << "BrokerHostDispatcher::OnChannelError()"; |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 74 | BrokerDispatcher::OnChannelError(); // Stop using the channel. |
| 75 | |
| 76 | // Tell the host about the crash so it can clean up and display notification. |
| 77 | // TODO(ddorwin): Add BrokerCrashed() to PPB_Proxy_Private and call it. |
| 78 | // ppb_proxy_->BrokerCrashed(pp_module()); |
| 79 | } |
| 80 | |
| 81 | BrokerSideDispatcher::BrokerSideDispatcher( |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 82 | PP_ConnectInstance_Func connect_instance) |
[email protected] | f0ecb55 | 2012-05-11 22:09:11 | [diff] [blame] | 83 | : BrokerDispatcher(connect_instance) { |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | void BrokerSideDispatcher::OnChannelError() { |
[email protected] | 1977348dd | 2011-10-26 22:27:41 | [diff] [blame] | 87 | DVLOG(1) << "BrokerSideDispatcher::OnChannelError()"; |
[email protected] | 4b417e5 | 2011-04-18 22:51:08 | [diff] [blame] | 88 | BrokerDispatcher::OnChannelError(); |
| 89 | |
| 90 | // The renderer has crashed or exited. This channel and all instances |
| 91 | // associated with it are no longer valid. |
| 92 | // TODO(ddorwin): This causes the broker process to exit, which may not be |
| 93 | // desirable in some use cases. |
| 94 | delete this; |
| 95 | } |
| 96 | |
| 97 | |
[email protected] | e2614c6 | 2011-04-16 22:12:45 | [diff] [blame] | 98 | } // namespace proxy |
[email protected] | 4d2efd2 | 2011-08-18 21:58:02 | [diff] [blame] | 99 | } // namespace ppapi |