summaryrefslogtreecommitdiffstats
path: root/chromium/content/renderer/pepper/v8_var_converter.h
blob: 835a8e4820945052bd205fdb76a18110809b046d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CONTENT_RENDERER_PEPPER_V8_VAR_CONVERTER_H_
#define CONTENT_RENDERER_PEPPER_V8_VAR_CONVERTER_H_

#include <memory>

#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "content/common/content_export.h"
#include "ppapi/c/pp_instance.h"
#include "ppapi/c/pp_var.h"
#include "ppapi/shared_impl/scoped_pp_var.h"
#include "v8/include/v8.h"

namespace content {

class ResourceConverter;

class CONTENT_EXPORT V8VarConverter {
 public:
  // Whether or not to allow converting object vars. If they are not allowed
  // and they are passed in, conversion will fail.
  enum AllowObjectVars {
    kDisallowObjectVars,
    kAllowObjectVars
  };
  V8VarConverter(PP_Instance instance, AllowObjectVars object_vars_allowed);

  // Constructor for testing.
  V8VarConverter(PP_Instance instance,
                 std::unique_ptr<ResourceConverter> resource_converter);
  ~V8VarConverter();

  // Converts the given PP_Var to a v8::Value. True is returned upon success.
  bool ToV8Value(const PP_Var& var,
                 v8::Local<v8::Context> context,
                 v8::Local<v8::Value>* result);

  struct VarResult {
   public:
    VarResult() : completed_synchronously(false), success(false) {}

    // True if the conversion completed synchronously and the callback will not
    // be called.
    bool completed_synchronously;

    // True if the conversion was successful. Only valid if
    // |completed_synchronously| is true.
    bool success;

    // The result if the conversion was successful. Only valid if
    // |completed_synchronously| and |success| are true.
    ppapi::ScopedPPVar var;
  };

  // Converts the given v8::Value to a PP_Var. Every PP_Var in the reference
  // graph in the result will have a refcount equal to the number of references
  // to it in the graph. The root of the result will have one additional
  // reference. The callback is run when conversion is complete with the
  // resulting var and a bool indicating success or failure. Conversion may be
  // asynchronous because converting some resources may result in communication
  // across IPC. |context| is guaranteed to only be used synchronously. If
  // the conversion can occur synchronously, |callback| will not be run,
  // otherwise it will be run.
  VarResult FromV8Value(
      v8::Local<v8::Value> val,
      v8::Local<v8::Context> context,
      const base::Callback<void(const ppapi::ScopedPPVar&, bool)>& callback);
  bool FromV8ValueSync(v8::Local<v8::Value> val,
                       v8::Local<v8::Context> context,
                       ppapi::ScopedPPVar* result_var);
 private:
  // Returns true on success, false on failure.
  bool FromV8ValueInternal(v8::Local<v8::Value> val,
                           v8::Local<v8::Context> context,
                           ppapi::ScopedPPVar* result_var);

  PP_Instance instance_;

  // Whether or not to support conversion to PP_VARTYPE_OBJECT.
  AllowObjectVars object_vars_allowed_;

  // The converter to use for converting V8 vars to resources.
  std::unique_ptr<ResourceConverter> resource_converter_;

  DISALLOW_COPY_AND_ASSIGN(V8VarConverter);
};

}  // namespace content

#endif  // CONTENT_RENDERER_PEPPER_V8_VAR_CONVERTER_H_