v8_value_converter.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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 SHELL_COMMON_V8_VALUE_CONVERTER_H_
  5. #define SHELL_COMMON_V8_VALUE_CONVERTER_H_
  6. #include <memory>
  7. #include "base/compiler_specific.h"
  8. #include "base/macros.h"
  9. #include "v8/include/v8.h"
  10. namespace base {
  11. class DictionaryValue;
  12. class ListValue;
  13. class Value;
  14. } // namespace base
  15. namespace electron {
  16. class V8ValueConverter {
  17. public:
  18. V8ValueConverter();
  19. void SetRegExpAllowed(bool val);
  20. void SetFunctionAllowed(bool val);
  21. void SetStripNullFromObjects(bool val);
  22. v8::Local<v8::Value> ToV8Value(const base::Value* value,
  23. v8::Local<v8::Context> context) const;
  24. std::unique_ptr<base::Value> FromV8Value(
  25. v8::Local<v8::Value> value,
  26. v8::Local<v8::Context> context) const;
  27. private:
  28. class FromV8ValueState;
  29. class ScopedUniquenessGuard;
  30. v8::Local<v8::Value> ToV8ValueImpl(v8::Isolate* isolate,
  31. const base::Value* value) const;
  32. v8::Local<v8::Value> ToV8Array(v8::Isolate* isolate,
  33. const base::ListValue* list) const;
  34. v8::Local<v8::Value> ToV8Object(
  35. v8::Isolate* isolate,
  36. const base::DictionaryValue* dictionary) const;
  37. v8::Local<v8::Value> ToArrayBuffer(v8::Isolate* isolate,
  38. const base::Value* value) const;
  39. std::unique_ptr<base::Value> FromV8ValueImpl(FromV8ValueState* state,
  40. v8::Local<v8::Value> value,
  41. v8::Isolate* isolate) const;
  42. std::unique_ptr<base::Value> FromV8Array(v8::Local<v8::Array> array,
  43. FromV8ValueState* state,
  44. v8::Isolate* isolate) const;
  45. std::unique_ptr<base::Value> FromNodeBuffer(v8::Local<v8::Value> value,
  46. FromV8ValueState* state,
  47. v8::Isolate* isolate) const;
  48. std::unique_ptr<base::Value> FromV8Object(v8::Local<v8::Object> object,
  49. FromV8ValueState* state,
  50. v8::Isolate* isolate) const;
  51. // If true, we will convert RegExp JavaScript objects to string.
  52. bool reg_exp_allowed_ = false;
  53. // If true, we will convert Function JavaScript objects to dictionaries.
  54. bool function_allowed_ = false;
  55. // If true, undefined and null values are ignored when converting v8 objects
  56. // into Values.
  57. bool strip_null_from_objects_ = false;
  58. DISALLOW_COPY_AND_ASSIGN(V8ValueConverter);
  59. };
  60. } // namespace electron
  61. #endif // SHELL_COMMON_V8_VALUE_CONVERTER_H_