OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/proxy/plugin_message_filter.h" | 5 #include "ppapi/proxy/plugin_message_filter.h" |
6 | 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
7 #include "ppapi/proxy/ppapi_messages.h" | 9 #include "ppapi/proxy/ppapi_messages.h" |
| 10 #include "ppapi/proxy/resource_message_params.h" |
| 11 #include "ppapi/proxy/resource_reply_thread_registrar.h" |
| 12 #include "ppapi/shared_impl/ppapi_globals.h" |
| 13 #include "ppapi/shared_impl/proxy_lock.h" |
| 14 #include "ppapi/shared_impl/resource.h" |
| 15 #include "ppapi/shared_impl/resource_tracker.h" |
8 | 16 |
9 namespace ppapi { | 17 namespace ppapi { |
10 namespace proxy { | 18 namespace proxy { |
11 | 19 |
12 PluginMessageFilter::PluginMessageFilter( | 20 PluginMessageFilter::PluginMessageFilter( |
13 std::set<PP_Instance>* seen_instance_ids) | 21 std::set<PP_Instance>* seen_instance_ids, |
| 22 scoped_refptr<ResourceReplyThreadRegistrar> registrar) |
14 : seen_instance_ids_(seen_instance_ids), | 23 : seen_instance_ids_(seen_instance_ids), |
| 24 resource_reply_thread_registrar_(registrar), |
15 channel_(NULL) { | 25 channel_(NULL) { |
16 } | 26 } |
17 | 27 |
18 PluginMessageFilter::~PluginMessageFilter() { | 28 PluginMessageFilter::~PluginMessageFilter() { |
19 } | 29 } |
20 | 30 |
21 void PluginMessageFilter::OnFilterAdded(IPC::Channel* channel) { | 31 void PluginMessageFilter::OnFilterAdded(IPC::Channel* channel) { |
22 channel_ = channel; | 32 channel_ = channel; |
23 } | 33 } |
24 | 34 |
25 void PluginMessageFilter::OnFilterRemoved() { | 35 void PluginMessageFilter::OnFilterRemoved() { |
26 channel_ = NULL; | 36 channel_ = NULL; |
27 } | 37 } |
28 | 38 |
29 bool PluginMessageFilter::OnMessageReceived(const IPC::Message& message) { | 39 bool PluginMessageFilter::OnMessageReceived(const IPC::Message& message) { |
30 bool handled = true; | 40 bool handled = true; |
31 IPC_BEGIN_MESSAGE_MAP(PluginMessageFilter, message) | 41 IPC_BEGIN_MESSAGE_MAP(PluginMessageFilter, message) |
32 IPC_MESSAGE_HANDLER(PpapiMsg_ReserveInstanceId, OnMsgReserveInstanceId) | 42 IPC_MESSAGE_HANDLER(PpapiMsg_ReserveInstanceId, OnMsgReserveInstanceId) |
| 43 IPC_MESSAGE_HANDLER(PpapiPluginMsg_ResourceReply, OnMsgResourceReply) |
33 IPC_MESSAGE_UNHANDLED(handled = false) | 44 IPC_MESSAGE_UNHANDLED(handled = false) |
34 IPC_END_MESSAGE_MAP() | 45 IPC_END_MESSAGE_MAP() |
35 return handled; | 46 return handled; |
36 } | 47 } |
37 | 48 |
38 bool PluginMessageFilter::Send(IPC::Message* msg) { | 49 bool PluginMessageFilter::Send(IPC::Message* msg) { |
39 if (channel_) | 50 if (channel_) |
40 return channel_->Send(msg); | 51 return channel_->Send(msg); |
41 delete msg; | 52 delete msg; |
42 return false; | 53 return false; |
43 } | 54 } |
44 | 55 |
| 56 // static |
| 57 void PluginMessageFilter::DispatchResourceReplyForTest( |
| 58 const ResourceMessageReplyParams& reply_params, |
| 59 const IPC::Message& nested_msg) { |
| 60 DispatchResourceReply(reply_params, nested_msg); |
| 61 } |
| 62 |
45 void PluginMessageFilter::OnMsgReserveInstanceId(PP_Instance instance, | 63 void PluginMessageFilter::OnMsgReserveInstanceId(PP_Instance instance, |
46 bool* usable) { | 64 bool* usable) { |
| 65 // If |seen_instance_ids_| is set to NULL, we are not supposed to see this |
| 66 // message. |
| 67 CHECK(seen_instance_ids_); |
47 // See the message definition for how this works. | 68 // See the message definition for how this works. |
48 if (seen_instance_ids_->find(instance) != seen_instance_ids_->end()) { | 69 if (seen_instance_ids_->find(instance) != seen_instance_ids_->end()) { |
49 // Instance ID already seen, reject it. | 70 // Instance ID already seen, reject it. |
50 *usable = false; | 71 *usable = false; |
51 return; | 72 return; |
52 } | 73 } |
53 | 74 |
54 // This instance ID is new so we can return that it's usable and mark it as | 75 // This instance ID is new so we can return that it's usable and mark it as |
55 // used for future reference. | 76 // used for future reference. |
56 seen_instance_ids_->insert(instance); | 77 seen_instance_ids_->insert(instance); |
57 *usable = true; | 78 *usable = true; |
58 } | 79 } |
59 | 80 |
| 81 void PluginMessageFilter::OnMsgResourceReply( |
| 82 const ResourceMessageReplyParams& reply_params, |
| 83 const IPC::Message& nested_msg) { |
| 84 scoped_refptr<base::MessageLoopProxy> target = |
| 85 resource_reply_thread_registrar_->GetTargetThreadAndUnregister( |
| 86 reply_params.pp_resource(), reply_params.sequence()); |
| 87 |
| 88 target->PostTask( |
| 89 FROM_HERE, |
| 90 base::Bind(&DispatchResourceReply, reply_params, nested_msg)); |
| 91 } |
| 92 |
| 93 // static |
| 94 void PluginMessageFilter::DispatchResourceReply( |
| 95 const ResourceMessageReplyParams& reply_params, |
| 96 const IPC::Message& nested_msg) { |
| 97 ProxyAutoLock lock; |
| 98 Resource* resource = PpapiGlobals::Get()->GetResourceTracker()->GetResource( |
| 99 reply_params.pp_resource()); |
| 100 if (!resource) { |
| 101 DVLOG_IF(1, reply_params.sequence() != 0) |
| 102 << "Pepper resource reply message received but the resource doesn't " |
| 103 "exist (probably has been destroyed)."; |
| 104 return; |
| 105 } |
| 106 resource->OnReplyReceived(reply_params, nested_msg); |
| 107 } |
| 108 |
60 } // namespace proxy | 109 } // namespace proxy |
61 } // namespace ppapi | 110 } // namespace ppapi |
OLD | NEW |