v8_value_converter.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2013 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #ifndef ELECTRON_SHELL_COMMON_V8_VALUE_CONVERTER_H_
  5. #define ELECTRON_SHELL_COMMON_V8_VALUE_CONVERTER_H_
  6. #include <memory>
  7. #include "base/compiler_specific.h"
  8. #include "v8/include/v8.h"
  9. namespace base {
  10. class DictionaryValue;
  11. class ListValue;
  12. class Value;
  13. } // namespace base
  14. namespace electron {
  15. class V8ValueConverter {
  16. public:
  17. V8ValueConverter();
  18. // disable copy
  19. V8ValueConverter(const V8ValueConverter&) = delete;
  20. V8ValueConverter& operator=(const V8ValueConverter&) = delete;
  21. void SetRegExpAllowed(bool val);
  22. void SetFunctionAllowed(bool val);
  23. void SetStripNullFromObjects(bool val);
  24. v8::Local<v8::Value> ToV8Value(const base::Value* value,
  25. v8::Local<v8::Context> context) const;
  26. std::unique_ptr<base::Value> FromV8Value(
  27. v8::Local<v8::Value> value,
  28. v8::Local<v8::Context> context) const;
  29. private:
  30. class FromV8ValueState;
  31. class ScopedUniquenessGuard;
  32. v8::Local<v8::Value> ToV8ValueImpl(v8::Isolate* isolate,
  33. const base::Value* value) const;
  34. v8::Local<v8::Value> ToV8Array(v8::Isolate* isolate,
  35. const base::ListValue* list) const;
  36. v8::Local<v8::Value> ToV8Object(
  37. v8::Isolate* isolate,
  38. const base::DictionaryValue* dictionary) const;
  39. v8::Local<v8::Value> ToArrayBuffer(v8::Isolate* isolate,
  40. const base::Value* value) const;
  41. std::unique_ptr<base::Value> FromV8ValueImpl(FromV8ValueState* state,
  42. v8::Local<v8::Value> value,
  43. v8::Isolate* isolate) const;
  44. std::unique_ptr<base::Value> FromV8Array(v8::Local<v8::Array> array,
  45. FromV8ValueState* state,
  46. v8::Isolate* isolate) const;
  47. std::unique_ptr<base::Value> FromNodeBuffer(v8::Local<v8::Value> value,
  48. FromV8ValueState* state,
  49. v8::Isolate* isolate) const;
  50. std::unique_ptr<base::Value> FromV8Object(v8::Local<v8::Object> object,
  51. FromV8ValueState* state,
  52. v8::Isolate* isolate) const;
  53. // If true, we will convert RegExp JavaScript objects to string.
  54. bool reg_exp_allowed_ = false;
  55. // If true, we will convert Function JavaScript objects to dictionaries.
  56. bool function_allowed_ = false;
  57. // If true, undefined and null values are ignored when converting v8 objects
  58. // into Values.
  59. bool strip_null_from_objects_ = false;
  60. };
  61. } // namespace electron
  62. #endif // ELECTRON_SHELL_COMMON_V8_VALUE_CONVERTER_H_