blob: 30cee185aad16a20e386e64ead7f650ad728a87f [file] [log] [blame]
[email protected]e2614c62011-04-16 22:12:451// 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]2b657fd2011-04-18 16:00:477#include "base/sync_socket.h"
8#include "ppapi/c/pp_errors.h"
[email protected]e2614c62011-04-16 22:12:459#include "ppapi/proxy/ppapi_messages.h"
10
[email protected]4d2efd22011-08-18 21:58:0211namespace ppapi {
[email protected]e2614c62011-04-16 22:12:4512namespace proxy {
13
[email protected]2b657fd2011-04-18 16:00:4714namespace {
15
16int32_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]e2614c62011-04-16 22:12:4528BrokerDispatcher::BrokerDispatcher(base::ProcessHandle remote_process_handle,
29 PP_ConnectInstance_Func connect_instance)
[email protected]2b657fd2011-04-18 16:00:4730 : ProxyChannel(remote_process_handle),
31 connect_instance_(connect_instance) {
[email protected]e2614c62011-04-16 22:12:4532}
33
34BrokerDispatcher::~BrokerDispatcher() {
35}
36
37bool 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
44bool BrokerDispatcher::OnMessageReceived(const IPC::Message& msg) {
45 // Control messages.
46 if (msg.routing_id() == MSG_ROUTING_CONTROL) {
47 bool handled = true;
[email protected]e2614c62011-04-16 22:12:4548 IPC_BEGIN_MESSAGE_MAP(BrokerDispatcher, msg)
[email protected]2b657fd2011-04-18 16:00:4749 IPC_MESSAGE_HANDLER(PpapiMsg_ConnectToPlugin, OnMsgConnectToPlugin)
[email protected]e2614c62011-04-16 22:12:4550 IPC_MESSAGE_UNHANDLED(handled = false)
51 IPC_END_MESSAGE_MAP()
[email protected]e2614c62011-04-16 22:12:4552 return handled;
53 }
54 return false;
55}
56
[email protected]2b657fd2011-04-18 16:00:4757// Transfers ownership of the handle to the broker module.
58void BrokerDispatcher::OnMsgConnectToPlugin(
59 PP_Instance instance,
[email protected]6990e5d2011-09-30 18:10:5760 IPC::PlatformFileForTransit handle,
61 int32_t* result) {
[email protected]2b657fd2011-04-18 16:00:4762 if (handle == IPC::InvalidPlatformFileForTransit()) {
[email protected]6990e5d2011-09-30 18:10:5763 *result = PP_ERROR_FAILED;
[email protected]2b657fd2011-04-18 16:00:4764 } else {
65 base::SyncSocket::Handle socket_handle =
66 IPC::PlatformFileForTransitToPlatformFile(handle);
67
68 if (connect_instance_) {
[email protected]6990e5d2011-09-30 18:10:5769 *result = connect_instance_(instance, PlatformFileToInt(socket_handle));
[email protected]2b657fd2011-04-18 16:00:4770 } else {
[email protected]6990e5d2011-09-30 18:10:5771 *result = PP_ERROR_FAILED;
[email protected]2b657fd2011-04-18 16:00:4772 // 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]2b657fd2011-04-18 16:00:4778}
79
80BrokerHostDispatcher::BrokerHostDispatcher(
81 base::ProcessHandle remote_process_handle)
82 : BrokerDispatcher(remote_process_handle, NULL) {
83}
84
[email protected]4b417e52011-04-18 22:51:0885void BrokerHostDispatcher::OnChannelError() {
[email protected]1977348dd2011-10-26 22:27:4186 DVLOG(1) << "BrokerHostDispatcher::OnChannelError()";
[email protected]4b417e52011-04-18 22:51:0887 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
94BrokerSideDispatcher::BrokerSideDispatcher(
95 base::ProcessHandle remote_process_handle,
96 PP_ConnectInstance_Func connect_instance)
97 : BrokerDispatcher(remote_process_handle, connect_instance) {
98}
99
100void BrokerSideDispatcher::OnChannelError() {
[email protected]1977348dd2011-10-26 22:27:41101 DVLOG(1) << "BrokerSideDispatcher::OnChannelError()";
[email protected]4b417e52011-04-18 22:51:08102 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]e2614c62011-04-16 22:12:45112} // namespace proxy
[email protected]4d2efd22011-08-18 21:58:02113} // namespace ppapi