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/renderer/pepper/resource_converter.h" | 5 #include "content/renderer/pepper/resource_converter.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" |
8 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
9 #include "content/public/renderer/renderer_ppapi_host.h" | |
10 #include "content/renderer/pepper/pepper_file_system_host.h" | 10 #include "content/renderer/pepper/pepper_file_system_host.h" |
11 #include "content/renderer/pepper/pepper_media_stream_audio_track_host.h" | 11 #include "content/renderer/pepper/pepper_media_stream_audio_track_host.h" |
12 #include "content/renderer/pepper/pepper_media_stream_video_track_host.h" | 12 #include "content/renderer/pepper/pepper_media_stream_video_track_host.h" |
| 13 #include "content/renderer/pepper/renderer_ppapi_host_impl.h" |
13 #include "ipc/ipc_message.h" | 14 #include "ipc/ipc_message.h" |
14 #include "ppapi/host/ppapi_host.h" | 15 #include "ppapi/host/ppapi_host.h" |
15 #include "ppapi/host/resource_host.h" | 16 #include "ppapi/host/resource_host.h" |
16 #include "ppapi/proxy/ppapi_messages.h" | 17 #include "ppapi/proxy/ppapi_messages.h" |
17 #include "ppapi/shared_impl/resource_var.h" | 18 #include "ppapi/shared_impl/resource_var.h" |
18 #include "ppapi/shared_impl/scoped_pp_var.h" | 19 #include "ppapi/shared_impl/scoped_pp_var.h" |
19 #include "storage/common/fileapi/file_system_util.h" | 20 #include "storage/common/fileapi/file_system_util.h" |
20 #include "third_party/WebKit/public/platform/WebFileSystem.h" | 21 #include "third_party/WebKit/public/platform/WebFileSystem.h" |
21 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" | 22 #include "third_party/WebKit/public/platform/WebMediaStreamSource.h" |
22 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" | 23 #include "third_party/WebKit/public/platform/WebMediaStreamTrack.h" |
23 #include "third_party/WebKit/public/web/WebDOMFileSystem.h" | 24 #include "third_party/WebKit/public/web/WebDOMFileSystem.h" |
24 #include "third_party/WebKit/public/web/WebDOMMediaStreamTrack.h" | 25 #include "third_party/WebKit/public/web/WebDOMMediaStreamTrack.h" |
25 #include "third_party/WebKit/public/web/WebLocalFrame.h" | 26 #include "third_party/WebKit/public/web/WebLocalFrame.h" |
26 | 27 |
27 using ppapi::ResourceVar; | 28 using ppapi::ResourceVar; |
28 | 29 |
29 namespace content { | 30 namespace content { |
30 namespace { | 31 namespace { |
31 | 32 |
32 void FlushComplete( | |
33 const base::Callback<void(bool)>& callback, | |
34 const std::vector<scoped_refptr<content::HostResourceVar> >& browser_vars, | |
35 const std::vector<int>& pending_host_ids) { | |
36 CHECK(browser_vars.size() == pending_host_ids.size()); | |
37 for (size_t i = 0; i < browser_vars.size(); ++i) { | |
38 browser_vars[i]->set_pending_browser_host_id(pending_host_ids[i]); | |
39 } | |
40 callback.Run(true); | |
41 } | |
42 | |
43 // Converts a blink::WebFileSystem::Type to a PP_FileSystemType. | 33 // Converts a blink::WebFileSystem::Type to a PP_FileSystemType. |
44 PP_FileSystemType WebFileSystemTypeToPPAPI(blink::WebFileSystem::Type type) { | 34 PP_FileSystemType WebFileSystemTypeToPPAPI(blink::WebFileSystem::Type type) { |
45 switch (type) { | 35 switch (type) { |
46 case blink::WebFileSystem::TypeTemporary: | 36 case blink::WebFileSystem::TypeTemporary: |
47 return PP_FILESYSTEMTYPE_LOCALTEMPORARY; | 37 return PP_FILESYSTEMTYPE_LOCALTEMPORARY; |
48 case blink::WebFileSystem::TypePersistent: | 38 case blink::WebFileSystem::TypePersistent: |
49 return PP_FILESYSTEMTYPE_LOCALPERSISTENT; | 39 return PP_FILESYSTEMTYPE_LOCALPERSISTENT; |
50 case blink::WebFileSystem::TypeIsolated: | 40 case blink::WebFileSystem::TypeIsolated: |
51 return PP_FILESYSTEMTYPE_ISOLATED; | 41 return PP_FILESYSTEMTYPE_ISOLATED; |
52 case blink::WebFileSystem::TypeExternal: | 42 case blink::WebFileSystem::TypeExternal: |
(...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 } | 181 } |
192 #endif | 182 #endif |
193 return false; | 183 return false; |
194 } | 184 } |
195 | 185 |
196 } // namespace | 186 } // namespace |
197 | 187 |
198 ResourceConverter::~ResourceConverter() {} | 188 ResourceConverter::~ResourceConverter() {} |
199 | 189 |
200 ResourceConverterImpl::ResourceConverterImpl(PP_Instance instance) | 190 ResourceConverterImpl::ResourceConverterImpl(PP_Instance instance) |
201 : instance_(instance) {} | 191 : instance_(instance), |
| 192 weak_ptr_factory_(this) {} |
202 | 193 |
203 ResourceConverterImpl::~ResourceConverterImpl() { | 194 ResourceConverterImpl::~ResourceConverterImpl() { |
204 // Verify Flush() was called. | 195 // Verify Flush() was called. |
205 DCHECK(browser_host_create_messages_.empty()); | 196 DCHECK(browser_host_create_messages_.empty()); |
206 DCHECK(browser_vars_.empty()); | 197 DCHECK(browser_vars_.empty()); |
207 } | 198 } |
208 | 199 |
209 bool ResourceConverterImpl::FromV8Value(v8::Handle<v8::Object> val, | 200 bool ResourceConverterImpl::FromV8Value(v8::Handle<v8::Object> val, |
210 v8::Handle<v8::Context> context, | 201 v8::Handle<v8::Context> context, |
211 PP_Var* result, | 202 PP_Var* result, |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 void ResourceConverterImpl::Reset() { | 260 void ResourceConverterImpl::Reset() { |
270 browser_host_create_messages_.clear(); | 261 browser_host_create_messages_.clear(); |
271 browser_vars_.clear(); | 262 browser_vars_.clear(); |
272 } | 263 } |
273 | 264 |
274 bool ResourceConverterImpl::NeedsFlush() { | 265 bool ResourceConverterImpl::NeedsFlush() { |
275 return !browser_host_create_messages_.empty(); | 266 return !browser_host_create_messages_.empty(); |
276 } | 267 } |
277 | 268 |
278 void ResourceConverterImpl::Flush(const base::Callback<void(bool)>& callback) { | 269 void ResourceConverterImpl::Flush(const base::Callback<void(bool)>& callback) { |
279 RendererPpapiHost::GetForPPInstance(instance_)->CreateBrowserResourceHosts( | 270 pending_conversions_.push(PendingConversion()); |
280 instance_, | 271 pending_conversions_.back().callback = callback; |
281 browser_host_create_messages_, | 272 pending_conversions_.back().browser_vars.swap(browser_vars_); |
282 base::Bind(&FlushComplete, callback, browser_vars_)); | 273 pending_conversions_.back().sequence_num = |
| 274 RendererPpapiHost::GetForPPInstance(instance_)-> |
| 275 CreateBrowserResourceHosts( |
| 276 instance_, |
| 277 browser_host_create_messages_, |
| 278 base::Bind(&ResourceConverterImpl::FlushComplete, |
| 279 weak_ptr_factory_.GetWeakPtr())); |
| 280 |
283 browser_host_create_messages_.clear(); | 281 browser_host_create_messages_.clear(); |
284 browser_vars_.clear(); | 282 } |
| 283 |
| 284 void ResourceConverterImpl::FlushSync() { |
| 285 // Send a request to the browser. We depend here on the fact that the async |
| 286 // message generated by Flush will arrive before our synchronous request for |
| 287 // all pending hosts. |
| 288 // |
| 289 // We don't need a response for this, since we'll get the result back in the |
| 290 // response to calling GetAllPendingBrowserHosts, so use a null callback. |
| 291 base::Callback<void(bool)> null_callback; |
| 292 Flush(null_callback); |
| 293 |
| 294 std::vector<ppapi::proxy::CompletedBrowserResourceHosts> completed_hosts; |
| 295 RendererPpapiHostImpl* host_impl = static_cast<RendererPpapiHostImpl*>( |
| 296 RendererPpapiHost::GetForPPInstance(instance_)); |
| 297 host_impl->GetAllPendingBrowserHosts(instance_, &completed_hosts); |
| 298 for (const auto& completed_host_set : completed_hosts) |
| 299 FlushComplete(completed_host_set); |
285 } | 300 } |
286 | 301 |
287 bool ResourceConverterImpl::ToV8Value(const PP_Var& var, | 302 bool ResourceConverterImpl::ToV8Value(const PP_Var& var, |
288 v8::Handle<v8::Context> context, | 303 v8::Handle<v8::Context> context, |
289 v8::Handle<v8::Value>* result) { | 304 v8::Handle<v8::Value>* result) { |
290 DCHECK(var.type == PP_VARTYPE_RESOURCE); | 305 DCHECK(var.type == PP_VARTYPE_RESOURCE); |
291 | 306 |
292 ResourceVar* resource = ResourceVar::FromPPVar(var); | 307 ResourceVar* resource = ResourceVar::FromPPVar(var); |
293 if (!resource) { | 308 if (!resource) { |
294 NOTREACHED(); | 309 NOTREACHED(); |
(...skipping 30 matching lines...) Expand all Loading... |
325 static_cast<content::PepperMediaStreamVideoTrackHost*>(resource_host), | 340 static_cast<content::PepperMediaStreamVideoTrackHost*>(resource_host), |
326 context, | 341 context, |
327 result); | 342 result); |
328 } else { | 343 } else { |
329 LOG(ERROR) << "The type of resource #" << resource_id | 344 LOG(ERROR) << "The type of resource #" << resource_id |
330 << " cannot be converted to a JavaScript object."; | 345 << " cannot be converted to a JavaScript object."; |
331 return false; | 346 return false; |
332 } | 347 } |
333 } | 348 } |
334 | 349 |
| 350 ResourceConverterImpl::PendingConversion::PendingConversion() {} |
| 351 ResourceConverterImpl::PendingConversion::~PendingConversion() {} |
| 352 |
335 scoped_refptr<HostResourceVar> ResourceConverterImpl::CreateResourceVar( | 353 scoped_refptr<HostResourceVar> ResourceConverterImpl::CreateResourceVar( |
336 int pending_renderer_id, | 354 int pending_renderer_id, |
337 const IPC::Message& create_message) { | 355 const IPC::Message& create_message) { |
338 return new HostResourceVar(pending_renderer_id, create_message); | 356 return new HostResourceVar(pending_renderer_id, create_message); |
339 } | 357 } |
340 | 358 |
341 scoped_refptr<HostResourceVar> | 359 scoped_refptr<HostResourceVar> |
342 ResourceConverterImpl::CreateResourceVarWithBrowserHost( | 360 ResourceConverterImpl::CreateResourceVarWithBrowserHost( |
343 int pending_renderer_id, | 361 int pending_renderer_id, |
344 const IPC::Message& create_message, | 362 const IPC::Message& create_message, |
345 const IPC::Message& browser_host_create_message) { | 363 const IPC::Message& browser_host_create_message) { |
346 scoped_refptr<HostResourceVar> result = | 364 scoped_refptr<HostResourceVar> result = |
347 CreateResourceVar(pending_renderer_id, create_message); | 365 CreateResourceVar(pending_renderer_id, create_message); |
348 browser_host_create_messages_.push_back(browser_host_create_message); | 366 browser_host_create_messages_.push_back(browser_host_create_message); |
349 browser_vars_.push_back(result); | 367 browser_vars_.push_back(result); |
350 return result; | 368 return result; |
351 } | 369 } |
352 | 370 |
| 371 void ResourceConverterImpl::FlushComplete( |
| 372 const ppapi::proxy::CompletedBrowserResourceHosts& hosts) { |
| 373 if (pending_conversions_.empty() || |
| 374 hosts.sequence_id < pending_conversions_.front().sequence_num) { |
| 375 // We must have already processed this result, so we can ignore it. |
| 376 return; |
| 377 } |
| 378 const PendingConversion& conversion = pending_conversions_.front(); |
| 379 CHECK(conversion.browser_vars.size() == hosts.host_ids.size()); |
| 380 for (size_t i = 0; i < conversion.browser_vars.size(); ++i) { |
| 381 conversion.browser_vars[i]->set_pending_browser_host_id( |
| 382 hosts.host_ids[i]); |
| 383 } |
| 384 if (!conversion.callback.is_null()) |
| 385 conversion.callback.Run(true); |
| 386 pending_conversions_.pop(); |
| 387 } |
| 388 |
353 } // namespace content | 389 } // namespace content |
OLD | NEW |