prune resources in MemoryCache
[chromium-blink-merge.git] / ppapi / proxy / broker_dispatcher.cc
blob7187852ba26d86068e67a31e0666833daff6bf45
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 "ppapi/proxy/broker_dispatcher.h"
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"
12 namespace ppapi {
13 namespace proxy {
15 BrokerDispatcher::BrokerDispatcher(PP_ConnectInstance_Func connect_instance)
16 : connect_instance_(connect_instance) {
19 BrokerDispatcher::~BrokerDispatcher() {
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);
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;
41 return false;
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);
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);
68 BrokerHostDispatcher::BrokerHostDispatcher()
69 : BrokerDispatcher(NULL) {
72 void BrokerHostDispatcher::OnChannelError() {
73 DVLOG(1) << "BrokerHostDispatcher::OnChannelError()";
74 BrokerDispatcher::OnChannelError(); // Stop using the channel.
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());
81 BrokerSideDispatcher::BrokerSideDispatcher(
82 PP_ConnectInstance_Func connect_instance)
83 : BrokerDispatcher(connect_instance) {
86 void BrokerSideDispatcher::OnChannelError() {
87 DVLOG(1) << "BrokerSideDispatcher::OnChannelError()";
88 BrokerDispatcher::OnChannelError();
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;
98 } // namespace proxy
99 } // namespace ppapi