OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "content/browser/renderer_host/pepper/pepper_renderer_connection.h" | 5 #include "content/browser/renderer_host/pepper/pepper_renderer_connection.h" |
6 | 6 |
7 #include "base/bind.h" | |
8 #include "base/memory/ref_counted.h" | |
7 #include "content/browser/browser_child_process_host_impl.h" | 9 #include "content/browser/browser_child_process_host_impl.h" |
8 #include "content/browser/ppapi_plugin_process_host.h" | 10 #include "content/browser/ppapi_plugin_process_host.h" |
9 #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h" | 11 #include "content/browser/renderer_host/pepper/browser_ppapi_host_impl.h" |
10 #include "content/common/pepper_renderer_instance_data.h" | 12 #include "content/common/pepper_renderer_instance_data.h" |
11 #include "content/common/view_messages.h" | 13 #include "content/common/view_messages.h" |
12 #include "content/browser/renderer_host/pepper/pepper_file_ref_host.h" | 14 #include "content/browser/renderer_host/pepper/pepper_file_ref_host.h" |
13 #include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h " | 15 #include "content/browser/renderer_host/pepper/pepper_file_system_browser_host.h " |
14 #include "content/public/browser/content_browser_client.h" | 16 #include "content/public/browser/content_browser_client.h" |
15 #include "content/public/common/content_client.h" | 17 #include "content/public/common/content_client.h" |
16 #include "ipc/ipc_message_macros.h" | 18 #include "ipc/ipc_message_macros.h" |
17 #include "ppapi/host/resource_host.h" | 19 #include "ppapi/host/resource_host.h" |
18 #include "ppapi/proxy/ppapi_message_utils.h" | 20 #include "ppapi/proxy/ppapi_message_utils.h" |
19 #include "ppapi/proxy/ppapi_messages.h" | 21 #include "ppapi/proxy/ppapi_messages.h" |
20 #include "ppapi/proxy/ppapi_message_utils.h" | 22 #include "ppapi/proxy/ppapi_message_utils.h" |
21 #include "ppapi/proxy/resource_message_params.h" | 23 #include "ppapi/proxy/resource_message_params.h" |
22 | 24 |
23 namespace content { | 25 namespace content { |
24 | 26 |
27 namespace { | |
28 | |
29 // Responsible for creating the pending resource hosts, holding their IDs until | |
30 // all of them have been created for a single message, and sending the reply to | |
31 // say that the hosts have been created. | |
32 class PendingHostCreator | |
33 : public base::RefCounted<PendingHostCreator> { | |
34 public: | |
35 PendingHostCreator(BrowserPpapiHostImpl* host, | |
36 BrowserMessageFilter* connection, | |
37 int routing_id, | |
38 int sequence_id, | |
39 size_t nested_msgs_size); | |
40 | |
41 // Adds the given resource host as a pending one. The host is remembered as | |
42 // host number |index|, and will ultimately be sent to the plugin to be | |
43 // attached to a real resource. | |
44 void AddPendingResourceHost( | |
45 size_t index, | |
46 scoped_ptr<ppapi::host::ResourceHost> resource_host); | |
47 | |
48 private: | |
49 friend class base::RefCounted<PendingHostCreator>; | |
50 | |
51 // When the last reference to this class is released, all of the resource | |
52 // hosts would have been added. This destructor sends the message to the | |
53 // plugin to tell it to attach real hosts to all of the pending hosts that | |
54 // have been added by this object. | |
55 ~PendingHostCreator(); | |
56 | |
57 BrowserPpapiHostImpl* host_; | |
58 BrowserMessageFilter* connection_; | |
59 int routing_id_; | |
60 int sequence_id_; | |
61 std::vector<int> pending_resource_host_ids_; | |
62 }; | |
63 | |
64 PendingHostCreator::PendingHostCreator(BrowserPpapiHostImpl* host, | |
65 BrowserMessageFilter* connection, | |
66 int routing_id, | |
67 int sequence_id, | |
68 size_t nested_msgs_size) | |
69 : host_(host), | |
70 connection_(connection), | |
71 routing_id_(routing_id), | |
72 sequence_id_(sequence_id), | |
73 pending_resource_host_ids_(nested_msgs_size, 0) {} | |
74 | |
75 void PendingHostCreator::AddPendingResourceHost( | |
76 size_t index, | |
77 scoped_ptr<ppapi::host::ResourceHost> resource_host) { | |
78 pending_resource_host_ids_[index] = | |
79 host_->GetPpapiHost()->AddPendingResourceHost(resource_host.Pass()); | |
80 } | |
81 | |
82 PendingHostCreator::~PendingHostCreator() { | |
83 connection_->Send(new PpapiHostMsg_CreateResourceHostsFromHostReply( | |
84 routing_id_, sequence_id_, pending_resource_host_ids_)); | |
85 } | |
86 | |
87 } // namespace | |
88 | |
25 PepperRendererConnection::PepperRendererConnection(int render_process_id) | 89 PepperRendererConnection::PepperRendererConnection(int render_process_id) |
26 : render_process_id_(render_process_id) { | 90 : render_process_id_(render_process_id) { |
27 // Only give the renderer permission for stable APIs. | 91 // Only give the renderer permission for stable APIs. |
28 in_process_host_.reset(new BrowserPpapiHostImpl(this, | 92 in_process_host_.reset(new BrowserPpapiHostImpl(this, |
29 ppapi::PpapiPermissions(), | 93 ppapi::PpapiPermissions(), |
30 "", | 94 "", |
31 base::FilePath(), | 95 base::FilePath(), |
32 base::FilePath(), | 96 base::FilePath(), |
33 true /* in_process */, | 97 true /* in_process */, |
34 false /* external_plugin */)); | 98 false /* external_plugin */)); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
87 } | 151 } |
88 | 152 |
89 void PepperRendererConnection::OnMsgCreateResourceHostsFromHost( | 153 void PepperRendererConnection::OnMsgCreateResourceHostsFromHost( |
90 int routing_id, | 154 int routing_id, |
91 int child_process_id, | 155 int child_process_id, |
92 const ppapi::proxy::ResourceMessageCallParams& params, | 156 const ppapi::proxy::ResourceMessageCallParams& params, |
93 PP_Instance instance, | 157 PP_Instance instance, |
94 const std::vector<IPC::Message>& nested_msgs) { | 158 const std::vector<IPC::Message>& nested_msgs) { |
95 BrowserPpapiHostImpl* host = GetHostForChildProcess(child_process_id); | 159 BrowserPpapiHostImpl* host = GetHostForChildProcess(child_process_id); |
96 | 160 |
97 std::vector<int> pending_resource_host_ids(nested_msgs.size(), 0); | |
98 if (!host) { | 161 if (!host) { |
99 DLOG(ERROR) << "Invalid plugin process ID."; | 162 DLOG(ERROR) << "Invalid plugin process ID."; |
yzshen1
2013/11/02 00:54:01
please directly return, then we don't have to have
Matt Giuca
2013/11/02 01:05:21
Done.
| |
100 } else { | 163 } else { |
164 scoped_refptr<PendingHostCreator> creator = new PendingHostCreator( | |
165 host, this, routing_id, params.sequence(), nested_msgs.size()); | |
101 for (size_t i = 0; i < nested_msgs.size(); ++i) { | 166 for (size_t i = 0; i < nested_msgs.size(); ++i) { |
102 const IPC::Message& nested_msg = nested_msgs[i]; | 167 const IPC::Message& nested_msg = nested_msgs[i]; |
103 scoped_ptr<ppapi::host::ResourceHost> resource_host; | 168 scoped_ptr<ppapi::host::ResourceHost> resource_host; |
104 if (host->IsValidInstance(instance)) { | 169 if (host->IsValidInstance(instance)) { |
105 if (nested_msg.type() == PpapiHostMsg_FileRef_CreateExternal::ID) { | 170 if (nested_msg.type() == PpapiHostMsg_FileRef_CreateExternal::ID) { |
106 // FileRef_CreateExternal is only permitted from the renderer. Because | 171 // FileRef_CreateExternal is only permitted from the renderer. Because |
107 // of this, we handle this message here and not in | 172 // of this, we handle this message here and not in |
108 // content_browser_pepper_host_factory.cc. | 173 // content_browser_pepper_host_factory.cc. |
109 base::FilePath external_path; | 174 base::FilePath external_path; |
110 if (ppapi::UnpackMessage<PpapiHostMsg_FileRef_CreateExternal>( | 175 if (ppapi::UnpackMessage<PpapiHostMsg_FileRef_CreateExternal>( |
111 nested_msg, &external_path)) { | 176 nested_msg, &external_path)) { |
112 resource_host.reset(new PepperFileRefHost( | 177 resource_host.reset(new PepperFileRefHost( |
113 host, instance, params.pp_resource(), external_path)); | 178 host, instance, params.pp_resource(), external_path)); |
114 } | 179 } |
115 } else if (nested_msg.type() == | 180 } else if (nested_msg.type() == |
116 PpapiHostMsg_FileSystem_CreateFromRenderer::ID) { | 181 PpapiHostMsg_FileSystem_CreateFromRenderer::ID) { |
117 // Similarly, FileSystem_CreateFromRenderer is only permitted from the | 182 // Similarly, FileSystem_CreateFromRenderer is only permitted from the |
118 // renderer. | 183 // renderer. |
119 std::string root_url; | 184 std::string root_url; |
120 PP_FileSystemType file_system_type; | 185 PP_FileSystemType file_system_type; |
121 if (ppapi::UnpackMessage<PpapiHostMsg_FileSystem_CreateFromRenderer>( | 186 if (ppapi::UnpackMessage<PpapiHostMsg_FileSystem_CreateFromRenderer>( |
122 nested_msg, &root_url, &file_system_type)) { | 187 nested_msg, &root_url, &file_system_type)) { |
123 resource_host.reset( | 188 PepperFileSystemBrowserHost* browser_host = |
124 new PepperFileSystemBrowserHost(host, | 189 new PepperFileSystemBrowserHost(host, |
125 instance, | 190 instance, |
126 params.pp_resource(), | 191 params.pp_resource(), |
127 GURL(root_url), | 192 file_system_type); |
128 file_system_type)); | 193 resource_host.reset(browser_host); |
194 // Open the file system resource host. This is an asynchronous | |
195 // operation, and we must only add the pending resource host and | |
196 // send the message once it completes. | |
197 browser_host->OpenExisting( | |
198 GURL(root_url), | |
199 base::Bind( | |
200 &PendingHostCreator::AddPendingResourceHost, | |
201 creator, | |
202 i, | |
203 base::Passed(&resource_host))); | |
204 // Do not fall through; the fall-through case adds the pending | |
205 // resource host to the list. We must do this asynchronously. | |
206 continue; | |
129 } | 207 } |
130 } | 208 } |
131 } | 209 } |
132 | 210 |
133 if (!resource_host.get()) { | 211 if (!resource_host.get()) { |
134 resource_host = host->GetPpapiHost()->CreateResourceHost( | 212 resource_host = host->GetPpapiHost()->CreateResourceHost( |
135 params, instance, nested_msg); | 213 params, instance, nested_msg); |
136 } | 214 } |
137 | 215 |
138 if (resource_host.get()) { | 216 if (resource_host.get()) |
139 pending_resource_host_ids[i] = | 217 creator->AddPendingResourceHost(i, resource_host.Pass()); |
140 host->GetPpapiHost()->AddPendingResourceHost(resource_host.Pass()); | |
141 } | |
142 } | 218 } |
143 } | 219 } |
144 | 220 |
145 Send(new PpapiHostMsg_CreateResourceHostsFromHostReply( | 221 // Note: All of the pending host IDs that were added as part of this |
146 routing_id, params.sequence(), pending_resource_host_ids)); | 222 // operation will automatically be sent to the plugin when |creator| is |
223 // released. This may happen immediately, or (if there are asynchronous | |
224 // requests to create resource hosts), once all of them complete. | |
147 } | 225 } |
148 | 226 |
149 void PepperRendererConnection::OnMsgDidCreateInProcessInstance( | 227 void PepperRendererConnection::OnMsgDidCreateInProcessInstance( |
150 PP_Instance instance, | 228 PP_Instance instance, |
151 const PepperRendererInstanceData& instance_data) { | 229 const PepperRendererInstanceData& instance_data) { |
152 PepperRendererInstanceData data = instance_data; | 230 PepperRendererInstanceData data = instance_data; |
153 data.render_process_id = render_process_id_; | 231 data.render_process_id = render_process_id_; |
154 in_process_host_->AddInstance(instance, data); | 232 in_process_host_->AddInstance(instance, data); |
155 } | 233 } |
156 | 234 |
157 void PepperRendererConnection::OnMsgDidDeleteInProcessInstance( | 235 void PepperRendererConnection::OnMsgDidDeleteInProcessInstance( |
158 PP_Instance instance) { | 236 PP_Instance instance) { |
159 in_process_host_->DeleteInstance(instance); | 237 in_process_host_->DeleteInstance(instance); |
160 } | 238 } |
161 | 239 |
162 } // namespace content | 240 } // namespace content |
OLD | NEW |