| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "ppapi/host/ppapi_host.h" | 5 #include "ppapi/host/ppapi_host.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "ppapi/c/pp_errors.h" | 8 #include "ppapi/c/pp_errors.h" |
| 9 #include "ppapi/host/host_factory.h" | 9 #include "ppapi/host/host_factory.h" |
| 10 #include "ppapi/host/host_message_context.h" | 10 #include "ppapi/host/host_message_context.h" |
| 11 #include "ppapi/host/instance_message_filter.h" |
| 11 #include "ppapi/host/resource_host.h" | 12 #include "ppapi/host/resource_host.h" |
| 12 #include "ppapi/proxy/ppapi_messages.h" | 13 #include "ppapi/proxy/ppapi_messages.h" |
| 13 #include "ppapi/proxy/resource_message_params.h" | 14 #include "ppapi/proxy/resource_message_params.h" |
| 14 #include "ppapi/shared_impl/host_resource.h" | 15 #include "ppapi/shared_impl/host_resource.h" |
| 15 | 16 |
| 16 namespace ppapi { | 17 namespace ppapi { |
| 17 namespace host { | 18 namespace host { |
| 18 | 19 |
| 19 namespace { | 20 namespace { |
| 20 | 21 |
| 21 // Put a cap on the maximum number of resources so we don't explode if the | 22 // Put a cap on the maximum number of resources so we don't explode if the |
| 22 // renderer starts spamming us. | 23 // renderer starts spamming us. |
| 23 const size_t kMaxResourcesPerPlugin = 1 << 14; | 24 const size_t kMaxResourcesPerPlugin = 1 << 14; |
| 24 | 25 |
| 25 } // namespace | 26 } // namespace |
| 26 | 27 |
| 27 PpapiHost::PpapiHost(IPC::Sender* sender, | 28 PpapiHost::PpapiHost(IPC::Sender* sender, |
| 28 HostFactory* host_factory, | 29 HostFactory* host_factory, |
| 29 const PpapiPermissions& perms) | 30 const PpapiPermissions& perms) |
| 30 : sender_(sender), | 31 : sender_(sender), |
| 31 host_factory_(host_factory), | 32 host_factory_(host_factory), |
| 32 permissions_(perms) { | 33 permissions_(perms) { |
| 33 } | 34 } |
| 34 | 35 |
| 35 PpapiHost::~PpapiHost() { | 36 PpapiHost::~PpapiHost() { |
| 37 FOR_EACH_OBSERVER(InstanceMessageFilter, |
| 38 instance_message_filters_, |
| 39 CallPpapiHostDestroyed()); |
| 36 } | 40 } |
| 37 | 41 |
| 38 bool PpapiHost::Send(IPC::Message* msg) { | 42 bool PpapiHost::Send(IPC::Message* msg) { |
| 39 return sender_->Send(msg); | 43 return sender_->Send(msg); |
| 40 } | 44 } |
| 41 | 45 |
| 42 bool PpapiHost::OnMessageReceived(const IPC::Message& msg) { | 46 bool PpapiHost::OnMessageReceived(const IPC::Message& msg) { |
| 43 bool handled = true; | 47 bool handled = true; |
| 44 IPC_BEGIN_MESSAGE_MAP(PpapiHost, msg) | 48 IPC_BEGIN_MESSAGE_MAP(PpapiHost, msg) |
| 45 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCall, | 49 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCall, |
| 46 OnHostMsgResourceCall) | 50 OnHostMsgResourceCall) |
| 47 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCreated, | 51 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceCreated, |
| 48 OnHostMsgResourceCreated) | 52 OnHostMsgResourceCreated) |
| 49 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceDestroyed, | 53 IPC_MESSAGE_HANDLER(PpapiHostMsg_ResourceDestroyed, |
| 50 OnHostMsgResourceDestroyed) | 54 OnHostMsgResourceDestroyed) |
| 51 IPC_MESSAGE_UNHANDLED(handled = false) | 55 IPC_MESSAGE_UNHANDLED(handled = false) |
| 52 IPC_END_MESSAGE_MAP() | 56 IPC_END_MESSAGE_MAP() |
| 57 |
| 58 if (!handled) { |
| 59 // Custom implementation of FOR_EACH_OBSERVER to allow for filter |
| 60 // functionality. |
| 61 if (instance_message_filters_.might_have_observers()) { |
| 62 ObserverListBase<InstanceMessageFilter>::Iterator it( |
| 63 instance_message_filters_); |
| 64 InstanceMessageFilter* obs; |
| 65 while ((obs = it.GetNext()) != NULL) { |
| 66 if (obs->OnInstanceMessageReceived(msg)) { |
| 67 handled = true; |
| 68 break; |
| 69 } |
| 70 } |
| 71 } |
| 72 } |
| 73 |
| 53 return handled; | 74 return handled; |
| 54 } | 75 } |
| 55 | 76 |
| 56 void PpapiHost::SendReply(const proxy::ResourceMessageReplyParams& params, | 77 void PpapiHost::SendReply(const proxy::ResourceMessageReplyParams& params, |
| 57 const IPC::Message& msg) { | 78 const IPC::Message& msg) { |
| 58 Send(new PpapiPluginMsg_ResourceReply(params, msg)); | 79 Send(new PpapiPluginMsg_ResourceReply(params, msg)); |
| 59 } | 80 } |
| 60 | 81 |
| 82 |
| 83 void PpapiHost::AddInstanceMessageFilter(InstanceMessageFilter* filter) { |
| 84 instance_message_filters_.AddObserver(filter); |
| 85 } |
| 86 |
| 87 void PpapiHost::RemoveInstanceMessageFilter(InstanceMessageFilter* filter) { |
| 88 instance_message_filters_.RemoveObserver(filter); |
| 89 } |
| 90 |
| 61 void PpapiHost::OnHostMsgResourceCall( | 91 void PpapiHost::OnHostMsgResourceCall( |
| 62 const proxy::ResourceMessageCallParams& params, | 92 const proxy::ResourceMessageCallParams& params, |
| 63 const IPC::Message& nested_msg) { | 93 const IPC::Message& nested_msg) { |
| 64 HostMessageContext context(params); | 94 HostMessageContext context(params); |
| 65 proxy::ResourceMessageReplyParams reply_params(params.pp_resource(), | 95 proxy::ResourceMessageReplyParams reply_params(params.pp_resource(), |
| 66 params.sequence()); | 96 params.sequence()); |
| 67 | 97 |
| 68 ResourceHost* resource_host = GetResourceHost(params.pp_resource()); | 98 ResourceHost* resource_host = GetResourceHost(params.pp_resource()); |
| 69 if (resource_host) { | 99 if (resource_host) { |
| 70 reply_params.set_result(resource_host->OnResourceMessageReceived( | 100 reply_params.set_result(resource_host->OnResourceMessageReceived( |
| (...skipping 22 matching lines...) Expand all Loading... |
| 93 } | 123 } |
| 94 | 124 |
| 95 void PpapiHost::OnHostMsgResourceCreated( | 125 void PpapiHost::OnHostMsgResourceCreated( |
| 96 const proxy::ResourceMessageCallParams& params, | 126 const proxy::ResourceMessageCallParams& params, |
| 97 PP_Instance instance, | 127 PP_Instance instance, |
| 98 const IPC::Message& nested_msg) { | 128 const IPC::Message& nested_msg) { |
| 99 if (resources_.size() >= kMaxResourcesPerPlugin) | 129 if (resources_.size() >= kMaxResourcesPerPlugin) |
| 100 return; | 130 return; |
| 101 | 131 |
| 102 scoped_ptr<ResourceHost> resource_host( | 132 scoped_ptr<ResourceHost> resource_host( |
| 103 host_factory_->CreateResourceHost(this, params, instance, | 133 host_factory_->CreateResourceHost(this, params, instance, nested_msg)); |
| 104 nested_msg)); | |
| 105 if (!resource_host.get()) { | 134 if (!resource_host.get()) { |
| 106 NOTREACHED(); | 135 NOTREACHED(); |
| 107 return; | 136 return; |
| 108 } | 137 } |
| 109 | 138 |
| 110 resources_[params.pp_resource()] = | 139 resources_[params.pp_resource()] = |
| 111 linked_ptr<ResourceHost>(resource_host.release()); | 140 linked_ptr<ResourceHost>(resource_host.release()); |
| 112 } | 141 } |
| 113 | 142 |
| 114 void PpapiHost::OnHostMsgResourceDestroyed(PP_Resource resource) { | 143 void PpapiHost::OnHostMsgResourceDestroyed(PP_Resource resource) { |
| 115 ResourceMap::iterator found = resources_.find(resource); | 144 ResourceMap::iterator found = resources_.find(resource); |
| 116 if (found == resources_.end()) { | 145 if (found == resources_.end()) { |
| 117 NOTREACHED(); | 146 NOTREACHED(); |
| 118 return; | 147 return; |
| 119 } | 148 } |
| 120 resources_.erase(found); | 149 resources_.erase(found); |
| 121 } | 150 } |
| 122 | 151 |
| 123 ResourceHost* PpapiHost::GetResourceHost(PP_Resource resource) { | 152 ResourceHost* PpapiHost::GetResourceHost(PP_Resource resource) { |
| 124 ResourceMap::iterator found = resources_.find(resource); | 153 ResourceMap::iterator found = resources_.find(resource); |
| 125 return found == resources_.end() ? NULL : found->second.get(); | 154 return found == resources_.end() ? NULL : found->second.get(); |
| 126 } | 155 } |
| 127 | 156 |
| 128 } // namespace host | 157 } // namespace host |
| 129 } // namespace ppapi | 158 } // namespace ppapi |
| OLD | NEW |