1 // Copyright (c) 2011 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/plugin_message_filter.h"
8 #include "base/logging.h"
9 #include "ipc/ipc_channel.h"
10 #include "ppapi/proxy/ppapi_messages.h"
11 #include "ppapi/proxy/resource_message_params.h"
12 #include "ppapi/proxy/resource_reply_thread_registrar.h"
13 #include "ppapi/shared_impl/ppapi_globals.h"
14 #include "ppapi/shared_impl/proxy_lock.h"
15 #include "ppapi/shared_impl/resource.h"
16 #include "ppapi/shared_impl/resource_tracker.h"
21 PluginMessageFilter::PluginMessageFilter(
22 std::set
<PP_Instance
>* seen_instance_ids
,
23 scoped_refptr
<ResourceReplyThreadRegistrar
> registrar
)
24 : seen_instance_ids_(seen_instance_ids
),
25 resource_reply_thread_registrar_(registrar
),
29 PluginMessageFilter::~PluginMessageFilter() {
32 void PluginMessageFilter::OnFilterAdded(IPC::Sender
* sender
) {
36 void PluginMessageFilter::OnFilterRemoved() {
40 bool PluginMessageFilter::OnMessageReceived(const IPC::Message
& message
) {
42 IPC_BEGIN_MESSAGE_MAP(PluginMessageFilter
, message
)
43 IPC_MESSAGE_HANDLER(PpapiMsg_ReserveInstanceId
, OnMsgReserveInstanceId
)
44 IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply
, OnMsgResourceReply
)
45 IPC_MESSAGE_UNHANDLED(handled
= false)
50 bool PluginMessageFilter::Send(IPC::Message
* msg
) {
52 return sender_
->Send(msg
);
57 void PluginMessageFilter::AddResourceMessageFilter(
58 const scoped_refptr
<ResourceMessageFilter
>& filter
) {
59 resource_filters_
.push_back(filter
);
63 void PluginMessageFilter::DispatchResourceReplyForTest(
64 const ResourceMessageReplyParams
& reply_params
,
65 const IPC::Message
& nested_msg
) {
66 DispatchResourceReply(reply_params
, nested_msg
);
69 void PluginMessageFilter::OnMsgReserveInstanceId(PP_Instance instance
,
71 // If |seen_instance_ids_| is set to NULL, we are not supposed to see this
73 CHECK(seen_instance_ids_
);
74 // See the message definition for how this works.
75 if (seen_instance_ids_
->find(instance
) != seen_instance_ids_
->end()) {
76 // Instance ID already seen, reject it.
81 // This instance ID is new so we can return that it's usable and mark it as
82 // used for future reference.
83 seen_instance_ids_
->insert(instance
);
87 void PluginMessageFilter::OnMsgResourceReply(
88 const ResourceMessageReplyParams
& reply_params
,
89 const IPC::Message
& nested_msg
) {
90 for (const auto& filter_ptr
: resource_filters_
) {
91 if (filter_ptr
->OnResourceReplyReceived(reply_params
, nested_msg
))
94 scoped_refptr
<base::MessageLoopProxy
> target
=
95 resource_reply_thread_registrar_
->GetTargetThread(reply_params
,
98 FROM_HERE
, base::Bind(&DispatchResourceReply
, reply_params
, nested_msg
));
102 void PluginMessageFilter::DispatchResourceReply(
103 const ResourceMessageReplyParams
& reply_params
,
104 const IPC::Message
& nested_msg
) {
106 Resource
* resource
= PpapiGlobals::Get()->GetResourceTracker()->GetResource(
107 reply_params
.pp_resource());
109 DVLOG_IF(1, reply_params
.sequence() != 0)
110 << "Pepper resource reply message received but the resource doesn't "
111 "exist (probably has been destroyed).";
114 resource
->OnReplyReceived(reply_params
, nested_msg
);