Roll src/third_party/WebKit 6c137a7:4aace46 (svn 202008:202009)
[chromium-blink-merge.git] / ppapi / proxy / plugin_message_filter.cc
bloba2994c52885c6fe0f16361760ea194f545214bd4
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"
7 #include "base/bind.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"
20 namespace ppapi {
21 namespace proxy {
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),
28 sender_(NULL) {
31 PluginMessageFilter::~PluginMessageFilter() {
34 void PluginMessageFilter::OnFilterAdded(IPC::Sender* sender) {
35 sender_ = sender;
38 void PluginMessageFilter::OnFilterRemoved() {
39 sender_ = NULL;
42 bool PluginMessageFilter::OnMessageReceived(const IPC::Message& message) {
43 bool handled = true;
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)
48 IPC_END_MESSAGE_MAP()
49 return handled;
52 bool PluginMessageFilter::Send(IPC::Message* msg) {
53 if (sender_)
54 return sender_->Send(msg);
55 delete msg;
56 return false;
59 void PluginMessageFilter::AddResourceMessageFilter(
60 const scoped_refptr<ResourceMessageFilter>& filter) {
61 resource_filters_.push_back(filter);
64 // static
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,
72 bool* usable) {
73 // If |seen_instance_ids_| is set to NULL, we are not supposed to see this
74 // message.
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.
79 *usable = false;
80 return;
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);
86 *usable = true;
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))
94 return;
96 scoped_refptr<base::SingleThreadTaskRunner> target =
97 resource_reply_thread_registrar_->GetTargetThread(reply_params,
98 nested_msg);
99 target->PostTask(
100 FROM_HERE, base::Bind(&DispatchResourceReply, reply_params, nested_msg));
103 // static
104 void PluginMessageFilter::DispatchResourceReply(
105 const ResourceMessageReplyParams& reply_params,
106 const IPC::Message& nested_msg) {
107 ProxyAutoLock lock;
108 Resource* resource = PpapiGlobals::Get()->GetResourceTracker()->GetResource(
109 reply_params.pp_resource());
110 if (!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).";
114 return;
116 resource->OnReplyReceived(reply_params, nested_msg);
119 } // namespace proxy
120 } // namespace ppapi