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/location.h"
9 #include "base/logging.h"
10 #include "base/single_thread_task_runner.h"
11 #include "ipc/ipc_channel.h"
12 #include "ppapi/proxy/ppapi_messages.h"
13 #include "ppapi/proxy/resource_message_params.h"
14 #include "ppapi/proxy/resource_reply_thread_registrar.h"
15 #include "ppapi/shared_impl/ppapi_globals.h"
16 #include "ppapi/shared_impl/proxy_lock.h"
17 #include "ppapi/shared_impl/resource.h"
18 #include "ppapi/shared_impl/resource_tracker.h"
23 PluginMessageFilter::PluginMessageFilter(
24 std::set
<PP_Instance
>* seen_instance_ids
,
25 scoped_refptr
<ResourceReplyThreadRegistrar
> registrar
)
26 : seen_instance_ids_(seen_instance_ids
),
27 resource_reply_thread_registrar_(registrar
),
31 PluginMessageFilter::~PluginMessageFilter() {
34 void PluginMessageFilter::OnFilterAdded(IPC::Sender
* sender
) {
38 void PluginMessageFilter::OnFilterRemoved() {
42 bool PluginMessageFilter::OnMessageReceived(const IPC::Message
& message
) {
44 IPC_BEGIN_MESSAGE_MAP(PluginMessageFilter
, message
)
45 IPC_MESSAGE_HANDLER(PpapiMsg_ReserveInstanceId
, OnMsgReserveInstanceId
)
46 IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply
, OnMsgResourceReply
)
47 IPC_MESSAGE_UNHANDLED(handled
= false)
52 bool PluginMessageFilter::Send(IPC::Message
* msg
) {
54 return sender_
->Send(msg
);
59 void PluginMessageFilter::AddResourceMessageFilter(
60 const scoped_refptr
<ResourceMessageFilter
>& filter
) {
61 resource_filters_
.push_back(filter
);
65 void PluginMessageFilter::DispatchResourceReplyForTest(
66 const ResourceMessageReplyParams
& reply_params
,
67 const IPC::Message
& nested_msg
) {
68 DispatchResourceReply(reply_params
, nested_msg
);
71 void PluginMessageFilter::OnMsgReserveInstanceId(PP_Instance instance
,
73 // If |seen_instance_ids_| is set to NULL, we are not supposed to see this
75 CHECK(seen_instance_ids_
);
76 // See the message definition for how this works.
77 if (seen_instance_ids_
->find(instance
) != seen_instance_ids_
->end()) {
78 // Instance ID already seen, reject it.
83 // This instance ID is new so we can return that it's usable and mark it as
84 // used for future reference.
85 seen_instance_ids_
->insert(instance
);
89 void PluginMessageFilter::OnMsgResourceReply(
90 const ResourceMessageReplyParams
& reply_params
,
91 const IPC::Message
& nested_msg
) {
92 for (const auto& filter_ptr
: resource_filters_
) {
93 if (filter_ptr
->OnResourceReplyReceived(reply_params
, nested_msg
))
96 scoped_refptr
<base::SingleThreadTaskRunner
> target
=
97 resource_reply_thread_registrar_
->GetTargetThread(reply_params
,
100 FROM_HERE
, base::Bind(&DispatchResourceReply
, reply_params
, nested_msg
));
104 void PluginMessageFilter::DispatchResourceReply(
105 const ResourceMessageReplyParams
& reply_params
,
106 const IPC::Message
& nested_msg
) {
108 Resource
* resource
= PpapiGlobals::Get()->GetResourceTracker()->GetResource(
109 reply_params
.pp_resource());
111 DVLOG_IF(1, reply_params
.sequence() != 0)
112 << "Pepper resource reply message received but the resource doesn't "
113 "exist (probably has been destroyed).";
116 resource
->OnReplyReceived(reply_params
, nested_msg
);