- add sources.
[platform/framework/web/crosswalk.git] / src / ppapi / proxy / broker_dispatcher.cc
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.
4
5 #include "ppapi/proxy/broker_dispatcher.h"
6
7 #include "base/sync_socket.h"
8 #include "ppapi/c/pp_errors.h"
9 #include "ppapi/proxy/ppapi_messages.h"
10 #include "ppapi/shared_impl/platform_file.h"
11
12 namespace ppapi {
13 namespace proxy {
14
15 BrokerDispatcher::BrokerDispatcher(PP_ConnectInstance_Func connect_instance)
16     : connect_instance_(connect_instance) {
17 }
18
19 BrokerDispatcher::~BrokerDispatcher() {
20 }
21
22 bool BrokerDispatcher::InitBrokerWithChannel(
23     ProxyChannel::Delegate* delegate,
24     base::ProcessId peer_pid,
25     const IPC::ChannelHandle& channel_handle,
26     bool is_client) {
27   return ProxyChannel::InitWithChannel(delegate, peer_pid, channel_handle,
28                                        is_client);
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;
35     IPC_BEGIN_MESSAGE_MAP(BrokerDispatcher, msg)
36       IPC_MESSAGE_HANDLER(PpapiMsg_ConnectToPlugin, OnMsgConnectToPlugin)
37       IPC_MESSAGE_UNHANDLED(handled = false)
38     IPC_END_MESSAGE_MAP()
39     return handled;
40   }
41   return false;
42 }
43
44 // Transfers ownership of the handle to the broker module.
45 void BrokerDispatcher::OnMsgConnectToPlugin(
46     PP_Instance instance,
47     IPC::PlatformFileForTransit handle,
48     int32_t* result) {
49   if (handle == IPC::InvalidPlatformFileForTransit()) {
50     *result = PP_ERROR_FAILED;
51   } else {
52     base::SyncSocket::Handle socket_handle =
53         IPC::PlatformFileForTransitToPlatformFile(handle);
54
55     if (connect_instance_) {
56       *result = connect_instance_(instance,
57                                   ppapi::PlatformFileToInt(socket_handle));
58     } else {
59       *result = PP_ERROR_FAILED;
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   }
66 }
67
68 BrokerHostDispatcher::BrokerHostDispatcher()
69     : BrokerDispatcher(NULL) {
70 }
71
72 void BrokerHostDispatcher::OnChannelError() {
73   DVLOG(1) << "BrokerHostDispatcher::OnChannelError()";
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(
82     PP_ConnectInstance_Func connect_instance)
83     : BrokerDispatcher(connect_instance) {
84 }
85
86 void BrokerSideDispatcher::OnChannelError() {
87   DVLOG(1) << "BrokerSideDispatcher::OnChannelError()";
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
98 }  // namespace proxy
99 }  // namespace ppapi