OLD | NEW |
(Empty) | |
| 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/host/ppapi_host.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "ppapi/c/pp_errors.h" |
| 9 #include "ppapi/host/host_factory.h" |
| 10 #include "ppapi/host/host_message_context.h" |
| 11 #include "ppapi/host/resource_host.h" |
| 12 #include "ppapi/proxy/ppapi_messages.h" |
| 13 #include "ppapi/proxy/resource_message_params.h" |
| 14 #include "ppapi/shared_impl/host_resource.h" |
| 15 |
| 16 namespace ppapi { |
| 17 namespace host { |
| 18 |
| 19 namespace { |
| 20 |
| 21 // Put a cap on the maximum number of resources so we don't explode if the |
| 22 // renderer starts spamming us. |
| 23 const size_t kMaxResourcesPerPlugin = 1 << 14; |
| 24 |
| 25 } // namespace |
| 26 |
| 27 PpapiHost::PpapiHost(IPC::Message::Sender* sender, |
| 28 HostFactory* host_factory) |
| 29 : sender_(sender), |
| 30 host_factory_(host_factory) { |
| 31 } |
| 32 |
| 33 PpapiHost::~PpapiHost() { |
| 34 } |
| 35 |
| 36 bool PpapiHost::Send(IPC::Message* msg) { |
| 37 return sender_->Send(msg); |
| 38 } |
| 39 |
| 40 bool PpapiHost::OnMessageReceived(const IPC::Message& msg) { |
| 41 bool handled = true; |
| 42 IPC_BEGIN_MESSAGE_MAP(PpapiHost, msg) |
| 43 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCall, |
| 44 OnHostMsgResourceCall) |
| 45 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCreated, |
| 46 OnHostMsgResourceCreated) |
| 47 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceDestroyed, |
| 48 OnHostMsgResourceDestroyed) |
| 49 IPC_MESSAGE_UNHANDLED(handled = false) |
| 50 IPC_END_MESSAGE_MAP() |
| 51 return handled; |
| 52 } |
| 53 |
| 54 void PpapiHost::SendReply(const proxy::ResourceMessageReplyParams& params, |
| 55 const IPC::Message& msg) { |
| 56 Send(new PpapiPluginMsg_ResourceReply(params, msg)); |
| 57 } |
| 58 |
| 59 void PpapiHost::OnHostMsgResourceCall( |
| 60 const proxy::ResourceMessageCallParams& params, |
| 61 const IPC::Message& nested_msg) { |
| 62 HostMessageContext context(params); |
| 63 proxy::ResourceMessageReplyParams reply_params(params.pp_resource(), |
| 64 params.sequence()); |
| 65 |
| 66 ResourceHost* resource_host = GetResourceHost(params.pp_resource()); |
| 67 if (resource_host) { |
| 68 reply_params.set_result(resource_host->OnResourceMessageReceived( |
| 69 nested_msg, &context)); |
| 70 |
| 71 // Sanity check the resource handler. |
| 72 if (reply_params.result() == PP_OK_COMPLETIONPENDING) { |
| 73 // Message handler should have only returned a pending result if a |
| 74 // response will be sent to the plugin. |
| 75 DCHECK(params.has_callback()); |
| 76 |
| 77 // Message handler should not have written a message to be returned if |
| 78 // completion is pending. |
| 79 DCHECK(context.reply_msg.type() == 0); |
| 80 } else if (!params.has_callback()) { |
| 81 // When no response is required, the message handler should not have |
| 82 // written a message to be returned. |
| 83 DCHECK(context.reply_msg.type() == 0); |
| 84 } |
| 85 } else { |
| 86 reply_params.set_result(PP_ERROR_BADRESOURCE); |
| 87 } |
| 88 |
| 89 if (params.has_callback() && reply_params.result() != PP_OK_COMPLETIONPENDING) |
| 90 SendReply(reply_params, context.reply_msg); |
| 91 } |
| 92 |
| 93 void PpapiHost::OnHostMsgResourceCreated( |
| 94 const proxy::ResourceMessageCallParams& params, |
| 95 PP_Instance instance, |
| 96 const IPC::Message& nested_msg) { |
| 97 if (resources_.size() >= kMaxResourcesPerPlugin) |
| 98 return; |
| 99 |
| 100 scoped_ptr<ResourceHost> resource_host( |
| 101 host_factory_->CreateResourceHost(this, params, instance, |
| 102 nested_msg)); |
| 103 if (!resource_host.get()) { |
| 104 NOTREACHED(); |
| 105 return; |
| 106 } |
| 107 |
| 108 resources_[params.pp_resource()] = |
| 109 linked_ptr<ResourceHost>(resource_host.release()); |
| 110 } |
| 111 |
| 112 void PpapiHost::OnHostMsgResourceDestroyed(PP_Resource resource) { |
| 113 ResourceMap::iterator found = resources_.find(resource); |
| 114 if (found == resources_.end()) { |
| 115 NOTREACHED(); |
| 116 return; |
| 117 } |
| 118 resources_.erase(found); |
| 119 } |
| 120 |
| 121 ResourceHost* PpapiHost::GetResourceHost(PP_Resource resource) { |
| 122 ResourceMap::iterator found = resources_.find(resource); |
| 123 return found == resources_.end() ? NULL : found->second.get(); |
| 124 } |
| 125 |
| 126 } // namespace host |
| 127 } // namespace ppapi |
OLD | NEW |