Chromium Code Reviews| Index: ppapi/host/ppapi_host.cc |
| diff --git a/ppapi/host/ppapi_host.cc b/ppapi/host/ppapi_host.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a38f5fe8f88131c291782ee7ad67fcffd468c8c4 |
| --- /dev/null |
| +++ b/ppapi/host/ppapi_host.cc |
| @@ -0,0 +1,127 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "ppapi/host/ppapi_host.h" |
| + |
| +#include "base/logging.h" |
| +#include "ppapi/c/pp_errors.h" |
| +#include "ppapi/host/host_factory.h" |
| +#include "ppapi/host/host_message_context.h" |
| +#include "ppapi/host/resource_host.h" |
| +#include "ppapi/proxy/ppapi_messages.h" |
| +#include "ppapi/proxy/resource_message_params.h" |
| +#include "ppapi/shared_impl/host_resource.h" |
| + |
| +namespace ppapi { |
| +namespace host { |
| + |
| +namespace { |
| + |
| +// Put a cap on the maximum number of resources so we don't explode of the |
|
dmichael (off chromium)
2012/06/22 16:56:49
nit: explode of->explode if
|
| +// renderer starts spamming us. |
| +const size_t kMaxResourcesPerPlugin = 16384; |
|
dmichael (off chromium)
2012/06/22 16:56:49
optional suggestion: 1 << 14 might look less arbit
|
| + |
| +} // namespace |
| + |
| +PpapiHost::PpapiHost(IPC::Message::Sender* sender, |
| + HostFactory* host_factory) |
| + : sender_(sender), |
| + host_factory_(host_factory) { |
| +} |
| + |
| +PpapiHost::~PpapiHost() { |
| +} |
| + |
| +bool PpapiHost::Send(IPC::Message* msg) { |
| + return sender_->Send(msg); |
| +} |
| + |
| +bool PpapiHost::OnMessageReceived(const IPC::Message& msg) { |
| + bool handled = true; |
| + IPC_BEGIN_MESSAGE_MAP(PpapiHost, msg) |
| + IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCall, |
| + OnHostMsgResourceCall) |
| + IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCreated, |
| + OnHostMsgResourceCreated) |
| + IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceDestroyed, |
| + OnHostMsgResourceDestroyed) |
| + IPC_MESSAGE_UNHANDLED(handled = false) |
| + IPC_END_MESSAGE_MAP() |
| + return handled; |
| +} |
| + |
| +void PpapiHost::SendReply(const proxy::ResourceMessageReplyParams& params, |
| + const IPC::Message& msg) { |
| + Send(new PpapiPluginMsg_ResourceReply(params, msg)); |
| +} |
| + |
| +void PpapiHost::OnHostMsgResourceCall( |
| + const proxy::ResourceMessageCallParams& params, |
| + const IPC::Message& nested_msg) { |
| + HostMessageContext context(params); |
| + proxy::ResourceMessageReplyParams reply_params(params.pp_resource(), |
| + params.sequence()); |
| + |
| + ResourceHost* resource_host = GetResourceHost(params.pp_resource()); |
| + if (resource_host) { |
| + reply_params.set_result(resource_host->OnResourceMessageReceived( |
| + nested_msg, &context)); |
| + |
| + // Sanity check the resource handler. |
| + if (reply_params.result() == PP_OK_COMPLETIONPENDING) { |
| + // Message handler should have only returned a pending result if a |
| + // response will be sent to the plugin. |
| + DCHECK(params.has_callback()); |
| + |
| + // Message handler should not have written a message to be returned if |
| + // completion is pending. |
| + DCHECK(context.reply_msg.type() == 0); |
| + } else if (!params.has_callback()) { |
| + // When no response is required, the message handler should not have |
| + // written a message to be returned. |
| + DCHECK(context.reply_msg.type() == 0); |
| + } |
| + } else { |
| + reply_params.set_result(PP_ERROR_BADRESOURCE); |
| + } |
| + |
| + if (params.has_callback() && reply_params.result() != PP_OK_COMPLETIONPENDING) |
| + SendReply(reply_params, context.reply_msg); |
| +} |
| + |
| +void PpapiHost::OnHostMsgResourceCreated( |
| + const proxy::ResourceMessageCallParams& params, |
| + PP_Instance instance, |
| + const IPC::Message& nested_msg) { |
| + if (resources_.size() >= kMaxResourcesPerPlugin) |
| + return; |
| + |
| + scoped_ptr<ResourceHost> resource_host( |
| + host_factory_->CreateMessageReceived(this, params, instance, |
| + nested_msg)); |
| + if (!resource_host.get()) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + |
| + resources_[params.pp_resource()] = |
| + linked_ptr<ResourceHost>(resource_host.release()); |
| +} |
| + |
| +void PpapiHost::OnHostMsgResourceDestroyed(PP_Resource resource) { |
| + ResourceMap::iterator found = resources_.find(resource); |
| + if (found == resources_.end()) { |
| + NOTREACHED(); |
| + return; |
| + } |
| + resources_.erase(found); |
| +} |
| + |
| +ResourceHost* PpapiHost::GetResourceHost(PP_Resource resource) { |
| + ResourceMap::iterator found = resources_.find(resource); |
| + return found == resources_.end() ? NULL : found->second.get(); |
| +} |
| + |
| +} // namespace host |
| +} // namespace ppapi |