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