summaryrefslogtreecommitdiffstats
path: root/content/renderer/v8_value_converter.h
blob: 6c3c6a924424e3323d8318dd2783fa6bcb87c620 (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
// Copyright (c) 2011 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 CHROME_RENDERER_V8_VALUE_CONVERTER_H_
#define CHROME_RENDERER_V8_VALUE_CONVERTER_H_

#include "v8/include/v8.h"

namespace base {
class DictionaryValue;
class ListValue;
class Value;
}

// Converts between v8::Value (JavaScript values in the v8 heap) and Chrome's
// values (from base/values.h). Lists and dictionaries are converted
// recursively.
//
// Only the JSON types (null, boolean, string, number, array, and object) are
// supported by default. Additional types can be allowed with e.g.
// set_allow_date(), set_allow_regexp().
class V8ValueConverter {
 public:
  V8ValueConverter();

  bool allow_undefined() const { return allow_undefined_; }
  void set_allow_undefined(bool val) { allow_undefined_ = val; }

  bool allow_date() const { return allow_date_; }
  void set_allow_date(bool val) { allow_date_ = val; }

  bool allow_regexp() const { return allow_regexp_; }
  void set_allow_regexp(bool val) { allow_regexp_ = val; }

  // Converts Value to v8::Value. Unsupported types are replaced with null.
  // If an array or object throws while setting a value, that property or item
  // is skipped, leaving a hole in the case of arrays.
  v8::Handle<v8::Value> ToV8Value(base::Value* value,
                                  v8::Handle<v8::Context> context) const;

  // Converts v8::Value to Value. Unsupported types are replaced with null.
  // If an array or object throws while getting a value, that property or item
  // is replaced with null.
  base::Value* FromV8Value(v8::Handle<v8::Value> value,
                           v8::Handle<v8::Context> context) const;

 private:
  v8::Handle<v8::Value> ToV8ValueImpl(base::Value* value) const;
  v8::Handle<v8::Value> ToV8Array(base::ListValue* list) const;
  v8::Handle<v8::Value> ToV8Object(base::DictionaryValue* dictionary) const;

  base::Value* FromV8ValueImpl(v8::Handle<v8::Value> value) const;
  base::ListValue* FromV8Array(v8::Handle<v8::Array> array) const;
  base::DictionaryValue* FromV8Object(v8::Handle<v8::Object> object) const;

  // If true, we will convert undefined JavaScript values to null.
  bool allow_undefined_;

  // If true, we will convert Date JavaScript objects to doubles.
  bool allow_date_;

  // If true, we will convet RegExp JavaScript objects to string.
  bool allow_regexp_;
};

#endif // CHROME_RENDERER_V8_VALUE_CONVERTER_H_