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