[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "ppapi/proxy/plugin_resource.h" |
| 6 | |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 7 | #include <limits> |
| 8 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 9 | #include "ppapi/proxy/ppapi_messages.h" |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 10 | |
| 11 | namespace ppapi { |
| 12 | namespace proxy { |
| 13 | |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 14 | PluginResource::PluginResource(Connection connection, PP_Instance instance) |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 15 | : Resource(OBJECT_IS_PROXY, instance), |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 16 | connection_(connection), |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 17 | next_sequence_number_(1), |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 18 | sent_create_to_browser_(false), |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 19 | sent_create_to_renderer_(false) { |
| 20 | } |
| 21 | |
| 22 | PluginResource::~PluginResource() { |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 23 | if (sent_create_to_browser_) { |
| 24 | connection_.browser_sender->Send( |
| 25 | new PpapiHostMsg_ResourceDestroyed(pp_resource())); |
| 26 | } |
| 27 | if (sent_create_to_renderer_) { |
| 28 | connection_.renderer_sender->Send( |
| 29 | new PpapiHostMsg_ResourceDestroyed(pp_resource())); |
| 30 | } |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 31 | } |
| 32 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 33 | void PluginResource::OnReplyReceived( |
| 34 | const proxy::ResourceMessageReplyParams& params, |
| 35 | const IPC::Message& msg) { |
[email protected] | 278f5eb9 | 2013-03-27 00:26:18 | [diff] [blame] | 36 | TRACE_EVENT2("ppapi proxy", "PluginResource::OnReplyReceived", |
| 37 | "Class", IPC_MESSAGE_ID_CLASS(msg.type()), |
| 38 | "Line", IPC_MESSAGE_ID_LINE(msg.type())); |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 39 | // Grab the callback for the reply sequence number and run it with |msg|. |
| 40 | CallbackMap::iterator it = callbacks_.find(params.sequence()); |
| 41 | if (it == callbacks_.end()) { |
| 42 | DCHECK(false) << "Callback does not exist for an expected sequence number."; |
| 43 | } else { |
| 44 | scoped_refptr<PluginResourceCallbackBase> callback = it->second; |
| 45 | callbacks_.erase(it); |
| 46 | callback->Run(params, msg); |
| 47 | } |
| 48 | } |
| 49 | |
[email protected] | 28df6a0 | 2012-11-08 07:29:45 | [diff] [blame] | 50 | void PluginResource::NotifyLastPluginRefWasDeleted() { |
| 51 | Resource::NotifyLastPluginRefWasDeleted(); |
| 52 | |
| 53 | // The callbacks may hold referrences to this object. Normally, we will get |
| 54 | // reply messages from the host side and remove them. However, it is possible |
| 55 | // that some replies from the host never arrive, e.g., the corresponding |
| 56 | // renderer crashes. In that case, we have to clean up the callbacks, |
| 57 | // otherwise this object will live forever. |
| 58 | callbacks_.clear(); |
| 59 | } |
| 60 | |
| 61 | void PluginResource::NotifyInstanceWasDeleted() { |
| 62 | Resource::NotifyInstanceWasDeleted(); |
| 63 | |
| 64 | // Please see comments in NotifyLastPluginRefWasDeleted() about why we must |
| 65 | // clean up the callbacks. |
| 66 | // It is possible that NotifyLastPluginRefWasDeleted() is never called for a |
| 67 | // resource. For example, those singleton-style resources such as |
| 68 | // GamepadResource never expose references to the plugin and thus won't |
| 69 | // receive a NotifyLastPluginRefWasDeleted() call. For those resources, we |
| 70 | // need to clean up callbacks when the instance goes away. |
| 71 | callbacks_.clear(); |
| 72 | } |
| 73 | |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 74 | void PluginResource::SendCreate(Destination dest, const IPC::Message& msg) { |
[email protected] | 278f5eb9 | 2013-03-27 00:26:18 | [diff] [blame] | 75 | TRACE_EVENT2("ppapi proxy", "PluginResource::SendCreate", |
| 76 | "Class", IPC_MESSAGE_ID_CLASS(msg.type()), |
| 77 | "Line", IPC_MESSAGE_ID_LINE(msg.type())); |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 78 | if (dest == RENDERER) { |
| 79 | DCHECK(!sent_create_to_renderer_); |
| 80 | sent_create_to_renderer_ = true; |
| 81 | } else { |
| 82 | DCHECK(!sent_create_to_browser_); |
| 83 | sent_create_to_browser_ = true; |
| 84 | } |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 85 | ResourceMessageCallParams params(pp_resource(), GetNextSequence()); |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 86 | GetSender(dest)->Send( |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 87 | new PpapiHostMsg_ResourceCreated(params, pp_instance(), msg)); |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 88 | } |
| 89 | |
[email protected] | db70c13 | 2012-12-05 00:41:20 | [diff] [blame] | 90 | void PluginResource::AttachToPendingHost(Destination dest, |
| 91 | int pending_host_id) { |
| 92 | // Connecting to a pending host is a replacement for "create". |
| 93 | if (dest == RENDERER) { |
| 94 | DCHECK(!sent_create_to_renderer_); |
| 95 | sent_create_to_renderer_ = true; |
| 96 | } else { |
| 97 | DCHECK(!sent_create_to_browser_); |
| 98 | sent_create_to_browser_ = true; |
| 99 | } |
| 100 | GetSender(dest)->Send( |
| 101 | new PpapiHostMsg_AttachToPendingHost(pp_resource(), pending_host_id)); |
| 102 | } |
| 103 | |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 104 | void PluginResource::Post(Destination dest, const IPC::Message& msg) { |
[email protected] | 278f5eb9 | 2013-03-27 00:26:18 | [diff] [blame] | 105 | TRACE_EVENT2("ppapi proxy", "PluginResource::Post", |
| 106 | "Class", IPC_MESSAGE_ID_CLASS(msg.type()), |
| 107 | "Line", IPC_MESSAGE_ID_LINE(msg.type())); |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 108 | ResourceMessageCallParams params(pp_resource(), GetNextSequence()); |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 109 | SendResourceCall(dest, params, msg); |
[email protected] | 93df81e | 2012-08-10 22:22:46 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 112 | bool PluginResource::SendResourceCall( |
[email protected] | 9164da3 | 2012-10-16 03:40:57 | [diff] [blame] | 113 | Destination dest, |
[email protected] | e1f5c9b | 2012-10-04 00:07:44 | [diff] [blame] | 114 | const ResourceMessageCallParams& call_params, |
| 115 | const IPC::Message& nested_msg) { |
[email protected] | 0d586af | 2013-07-31 21:58:06 | [diff] [blame] | 116 | // For in-process plugins, we need to send the routing ID with the request. |
| 117 | // The browser then uses that routing ID when sending the reply so it will be |
[email protected] | fb44cb0a | 2013-12-04 00:45:55 | [diff] [blame^] | 118 | // routed back to the correct RenderFrameImpl. |
[email protected] | 0d586af | 2013-07-31 21:58:06 | [diff] [blame] | 119 | if (dest == BROWSER && connection_.in_process) { |
| 120 | return GetSender(dest)->Send(new PpapiHostMsg_InProcessResourceCall( |
| 121 | connection_.browser_sender_routing_id, |
| 122 | call_params, |
| 123 | nested_msg)); |
| 124 | } else { |
| 125 | return GetSender(dest)->Send( |
| 126 | new PpapiHostMsg_ResourceCall(call_params, nested_msg)); |
| 127 | } |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 128 | } |
| 129 | |
[email protected] | 0c92b0d | 2012-12-08 00:46:23 | [diff] [blame] | 130 | int32_t PluginResource::GenericSyncCall( |
| 131 | Destination dest, |
| 132 | const IPC::Message& msg, |
| 133 | IPC::Message* reply, |
| 134 | ResourceMessageReplyParams* reply_params) { |
[email protected] | 278f5eb9 | 2013-03-27 00:26:18 | [diff] [blame] | 135 | TRACE_EVENT2("ppapi proxy", "PluginResource::GenericSyncCall", |
| 136 | "Class", IPC_MESSAGE_ID_CLASS(msg.type()), |
| 137 | "Line", IPC_MESSAGE_ID_LINE(msg.type())); |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 138 | ResourceMessageCallParams params(pp_resource(), GetNextSequence()); |
[email protected] | ff44fc1 | 2012-10-03 00:52:16 | [diff] [blame] | 139 | params.set_has_callback(); |
[email protected] | 5878693 | 2012-10-13 10:16:08 | [diff] [blame] | 140 | bool success = GetSender(dest)->Send(new PpapiHostMsg_ResourceSyncCall( |
[email protected] | 0c92b0d | 2012-12-08 00:46:23 | [diff] [blame] | 141 | params, msg, reply_params, reply)); |
[email protected] | ff44fc1 | 2012-10-03 00:52:16 | [diff] [blame] | 142 | if (success) |
[email protected] | 0c92b0d | 2012-12-08 00:46:23 | [diff] [blame] | 143 | return reply_params->result(); |
[email protected] | ff44fc1 | 2012-10-03 00:52:16 | [diff] [blame] | 144 | return PP_ERROR_FAILED; |
| 145 | } |
| 146 | |
[email protected] | d84e999 | 2012-11-08 22:13:28 | [diff] [blame] | 147 | int32_t PluginResource::GetNextSequence() { |
| 148 | // Return the value with wraparound, making sure we don't make a sequence |
| 149 | // number with a 0 ID. Note that signed wraparound is undefined in C++ so we |
| 150 | // manually check. |
| 151 | int32_t ret = next_sequence_number_; |
| 152 | if (next_sequence_number_ == std::numeric_limits<int32_t>::max()) |
| 153 | next_sequence_number_ = 1; // Skip 0 which is invalid. |
| 154 | else |
| 155 | next_sequence_number_++; |
| 156 | return ret; |
| 157 | } |
| 158 | |
[email protected] | eccf8031 | 2012-07-14 15:43:42 | [diff] [blame] | 159 | } // namespace proxy |
| 160 | } // namespace ppapi |