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.
5 #ifndef CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H
6 #define CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H
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"
30 class RendererPpapiHost;
32 // This class is responsible for converting V8 vars to Pepper resources.
33 class CONTENT_EXPORT ResourceConverter {
35 virtual ~ResourceConverter();
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;
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,
48 bool* was_resource) = 0;
51 class ResourceConverterImpl : public ResourceConverter {
53 ResourceConverterImpl(PP_Instance instance, RendererPpapiHost* host);
54 virtual ~ResourceConverterImpl();
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,
61 bool* was_resource) OVERRIDE;
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);
78 // The instance this ResourceConverter is associated with.
79 PP_Instance instance_;
80 // The RendererPpapiHost to use to create browser hosts.
81 RendererPpapiHost* host_;
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;
90 DISALLOW_COPY_AND_ASSIGN(ResourceConverterImpl);
93 } // namespace content
94 #endif // CONTENT_RENDERER_PEPPER_RESOURCE_CONVERTER_H