• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Reset the state of the resource converter.
38   virtual void Reset() = 0;
39 
40   // Returns true if Flush() needs to be called before using any vars created
41   // by the resource converter.
42   virtual bool NeedsFlush() = 0;
43 
44   // If NeedsFlush() is true then Flush() must be called before any vars created
45   // by the ResourceConverter are valid. It handles creating any resource hosts
46   // that need to be created. |callback| will always be called asynchronously.
47   virtual void Flush(const base::Callback<void(bool)>& callback) = 0;
48 
49   // Attempts to convert a V8 object to a PP_Var with type PP_VARTYPE_RESOURCE.
50   // On success, writes the resulting var to |result|, sets |was_resource| to
51   // true and returns true. If |val| is not a resource, sets |was_resource| to
52   // false and returns true. If an error occurs, returns false.
53   virtual bool FromV8Value(v8::Handle<v8::Object> val,
54                            v8::Handle<v8::Context> context,
55                            PP_Var* result,
56                            bool* was_resource) = 0;
57 
58   // Attempts to convert a PP_Var to a V8 object. |var| must have type
59   // PP_VARTYPE_RESOURCE. On success, writes the resulting value to |result| and
60   // returns true. If an error occurs, returns false.
61   virtual bool ToV8Value(const PP_Var& var,
62                          v8::Handle<v8::Context> context,
63                          v8::Handle<v8::Value>* result) = 0;
64 };
65 
66 class ResourceConverterImpl : public ResourceConverter {
67  public:
68   ResourceConverterImpl(PP_Instance instance, RendererPpapiHost* host);
69   virtual ~ResourceConverterImpl();
70 
71   // ResourceConverter overrides.
72   virtual void Reset() OVERRIDE;
73   virtual bool NeedsFlush() OVERRIDE;
74   virtual void Flush(const base::Callback<void(bool)>& callback) OVERRIDE;
75   virtual bool FromV8Value(v8::Handle<v8::Object> val,
76                            v8::Handle<v8::Context> context,
77                            PP_Var* result,
78                            bool* was_resource) OVERRIDE;
79   virtual bool ToV8Value(const PP_Var& var,
80                          v8::Handle<v8::Context> context,
81                          v8::Handle<v8::Value>* result) OVERRIDE;
82 
83  private:
84   // Creates a resource var with the given |pending_renderer_id| and
85   // |create_message| to be sent to the plugin.
86   scoped_refptr<HostResourceVar> CreateResourceVar(
87       int pending_renderer_id,
88       const IPC::Message& create_message);
89   // Creates a resource var with the given |pending_renderer_id| and
90   // |create_message| to be sent to the plugin. Also sends
91   // |browser_host_create_message| to the browser, and asynchronously stores the
92   // resulting browser host ID in the newly created var.
93   scoped_refptr<HostResourceVar> CreateResourceVarWithBrowserHost(
94       int pending_renderer_id,
95       const IPC::Message& create_message,
96       const IPC::Message& browser_host_create_message);
97 
98   // The instance this ResourceConverter is associated with.
99   PP_Instance instance_;
100   // The RendererPpapiHost to use to create browser hosts.
101   RendererPpapiHost* host_;
102 
103   // A list of the messages to create the browser hosts. This is a parallel
104   // array to |browser_vars|. It is kept as a parallel array so that it can be
105   // conveniently passed to |CreateBrowserResourceHosts|.
106   std::vector<IPC::Message> browser_host_create_messages_;
107   // A list of the resource vars associated with browser hosts.
108   std::vector<scoped_refptr<HostResourceVar> > browser_vars_;
109 
110   DISALLOW_COPY_AND_ASSIGN(ResourceConverterImpl);
111 };
112 
113 }  // namespace content
114 #endif  // CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H
115