Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / pepper / v8_var_converter.h
blob6e537268c849d6710bcced5dc9021bb755241cc1
1 // Copyright (c) 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_V8_VAR_CONVERTER_H_
6 #define CONTENT_RENDERER_PEPPER_V8_VAR_CONVERTER_H_
8 #include "base/basictypes.h"
9 #include "base/callback.h"
10 #include "base/compiler_specific.h"
11 #include "ppapi/c/pp_instance.h"
12 #include "ppapi/c/pp_var.h"
13 #include "ppapi/shared_impl/scoped_pp_var.h"
14 #include "v8/include/v8.h"
15 #include "content/common/content_export.h"
17 namespace content {
19 class ResourceConverter;
21 class CONTENT_EXPORT V8VarConverter {
22 public:
23 // Whether or not to allow converting object vars. If they are not allowed
24 // and they are passed in, conversion will fail.
25 enum AllowObjectVars {
26 kDisallowObjectVars,
27 kAllowObjectVars
29 V8VarConverter(PP_Instance instance, AllowObjectVars object_vars_allowed);
31 // Constructor for testing.
32 V8VarConverter(PP_Instance instance,
33 scoped_ptr<ResourceConverter> resource_converter);
34 ~V8VarConverter();
36 // Converts the given PP_Var to a v8::Value. True is returned upon success.
37 bool ToV8Value(const PP_Var& var,
38 v8::Local<v8::Context> context,
39 v8::Local<v8::Value>* result);
41 struct VarResult {
42 public:
43 VarResult() : completed_synchronously(false), success(false) {}
45 // True if the conversion completed synchronously and the callback will not
46 // be called.
47 bool completed_synchronously;
49 // True if the conversion was successful. Only valid if
50 // |completed_synchronously| is true.
51 bool success;
53 // The result if the conversion was successful. Only valid if
54 // |completed_synchronously| and |success| are true.
55 ppapi::ScopedPPVar var;
58 // Converts the given v8::Value to a PP_Var. Every PP_Var in the reference
59 // graph in the result will have a refcount equal to the number of references
60 // to it in the graph. The root of the result will have one additional
61 // reference. The callback is run when conversion is complete with the
62 // resulting var and a bool indicating success or failure. Conversion may be
63 // asynchronous because converting some resources may result in communication
64 // across IPC. |context| is guaranteed to only be used synchronously. If
65 // the conversion can occur synchronously, |callback| will not be run,
66 // otherwise it will be run.
67 VarResult FromV8Value(
68 v8::Local<v8::Value> val,
69 v8::Local<v8::Context> context,
70 const base::Callback<void(const ppapi::ScopedPPVar&, bool)>& callback);
71 bool FromV8ValueSync(v8::Local<v8::Value> val,
72 v8::Local<v8::Context> context,
73 ppapi::ScopedPPVar* result_var);
74 private:
75 // Returns true on success, false on failure.
76 bool FromV8ValueInternal(v8::Local<v8::Value> val,
77 v8::Local<v8::Context> context,
78 ppapi::ScopedPPVar* result_var);
80 PP_Instance instance_;
82 // Whether or not to support conversion to PP_VARTYPE_OBJECT.
83 AllowObjectVars object_vars_allowed_;
85 // The converter to use for converting V8 vars to resources.
86 scoped_ptr<ResourceConverter> resource_converter_;
88 DISALLOW_COPY_AND_ASSIGN(V8VarConverter);
91 } // namespace content
93 #endif // CONTENT_RENDERER_PEPPER_V8_VAR_CONVERTER_H_