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 3 // found in the LICENSE file. 4 5 #ifndef CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H 6 #define CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H 7 8 #include <vector> 9 10 #include "base/basictypes.h" 11 #include "base/callback.h" 12 #include "base/compiler_specific.h" 13 #include "base/memory/ref_counted.h" 14 #include "content/common/content_export.h" 15 #include "content/renderer/pepper/host_resource_var.h" 16 #include "ppapi/c/pp_instance.h" 17 #include "ppapi/c/pp_var.h" 18 #include "v8/include/v8.h" 19 20 namespace IPC { 21 class Message; 22 } 23 24 namespace ppapi { 25 class ScopedPPVar; 26 } 27 28 namespace content { 29 30 class RendererPpapiHost; 31 32 // This class is responsible for converting V8 vars to Pepper resources. 33 class CONTENT_EXPORT ResourceConverter { 34 public: 35 virtual ~ResourceConverter(); 36 37 // Flush() must be called before any vars created by the ResourceConverter 38 // are valid. It handles creating any resource hosts that need to be created. 39 virtual void Flush(const base::Callback<void(bool)>& callback) = 0; 40 41 // Attempts to convert a V8 object to a PP_Var with type PP_VARTYPE_RESOURCE. 42 // On success, writes the resulting var to |result|, sets |was_resource| to 43 // true and returns true. If |val| is not a resource, sets |was_resource| to 44 // false and returns true. If an error occurs, returns false. 45 virtual bool FromV8Value(v8::Handle<v8::Object> val, 46 v8::Handle<v8::Context> context, 47 PP_Var* result, 48 bool* was_resource) = 0; 49 }; 50 51 class ResourceConverterImpl : public ResourceConverter { 52 public: 53 ResourceConverterImpl(PP_Instance instance, RendererPpapiHost* host); 54 virtual ~ResourceConverterImpl(); 55 56 // ResourceConverter overrides. 57 virtual void Flush(const base::Callback<void(bool)>& callback) OVERRIDE; 58 virtual bool FromV8Value(v8::Handle<v8::Object> val, 59 v8::Handle<v8::Context> context, 60 PP_Var* result, 61 bool* was_resource) OVERRIDE; 62 63 private: 64 // Creates a resource var with the given |pending_renderer_id| and 65 // |create_message| to be sent to the plugin. 66 scoped_refptr<HostResourceVar> CreateResourceVar( 67 int pending_renderer_id, 68 const IPC::Message& create_message); 69 // Creates a resource var with the given |pending_renderer_id| and 70 // |create_message| to be sent to the plugin. Also sends 71 // |browser_host_create_message| to the browser, and asynchronously stores the 72 // resulting browser host ID in the newly created var. 73 scoped_refptr<HostResourceVar> CreateResourceVarWithBrowserHost( 74 int pending_renderer_id, 75 const IPC::Message& create_message, 76 const IPC::Message& browser_host_create_message); 77 78 // The instance this ResourceConverter is associated with. 79 PP_Instance instance_; 80 // The RendererPpapiHost to use to create browser hosts. 81 RendererPpapiHost* host_; 82 83 // A list of the messages to create the browser hosts. This is a parallel 84 // array to |browser_vars|. It is kept as a parallel array so that it can be 85 // conveniently passed to |CreateBrowserResourceHosts|. 86 std::vector<IPC::Message> browser_host_create_messages_; 87 // A list of the resource vars associated with browser hosts. 88 std::vector<scoped_refptr<HostResourceVar> > browser_vars; 89 90 DISALLOW_COPY_AND_ASSIGN(ResourceConverterImpl); 91 }; 92 93 } // namespace content 94 #endif // CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H 95