| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CONTENT_PUBLIC_CHILD_V8_VALUE_CONVERTER_H_ | 5 #ifndef CONTENT_PUBLIC_CHILD_V8_VALUE_CONVERTER_H_ |
| 6 #define CONTENT_PUBLIC_CHILD_V8_VALUE_CONVERTER_H_ | 6 #define CONTENT_PUBLIC_CHILD_V8_VALUE_CONVERTER_H_ |
| 7 | 7 |
| 8 #include <memory> |
| 9 |
| 8 #include "base/callback.h" | 10 #include "base/callback.h" |
| 9 #include "content/common/content_export.h" | 11 #include "content/common/content_export.h" |
| 10 #include "v8/include/v8.h" | 12 #include "v8/include/v8.h" |
| 11 | 13 |
| 12 namespace base { | 14 namespace base { |
| 13 class Value; | 15 class Value; |
| 14 } | 16 } |
| 15 | 17 |
| 16 namespace content { | 18 namespace content { |
| 17 | 19 |
| 18 // Converts between v8::Value (JavaScript values in the v8 heap) and Chrome's | 20 // Converts between v8::Value (JavaScript values in the v8 heap) and Chrome's |
| 19 // values (from base/values.h). Lists and dictionaries are converted | 21 // values (from base/values.h). Lists and dictionaries are converted |
| 20 // recursively. | 22 // recursively. |
| 21 // | 23 // |
| 22 // The JSON types (null, boolean, string, number, array, and object) as well as | 24 // The JSON types (null, boolean, string, number, array, and object) as well as |
| 23 // binary values are supported. For binary values, we convert to WebKit | 25 // binary values are supported. For binary values, we convert to WebKit |
| 24 // ArrayBuffers, and support converting from an ArrayBuffer or any of the | 26 // ArrayBuffers, and support converting from an ArrayBuffer or any of the |
| 25 // ArrayBufferView subclasses (Uint8Array, etc.). | 27 // ArrayBufferView subclasses (Uint8Array, etc.). |
| 26 class CONTENT_EXPORT V8ValueConverter { | 28 class CONTENT_EXPORT V8ValueConverter { |
| 27 public: | 29 public: |
| 28 // Extends the default behaviour of V8ValueConverter. | 30 // Extends the default behaviour of V8ValueConverter. |
| 29 class CONTENT_EXPORT Strategy { | 31 class CONTENT_EXPORT Strategy { |
| 30 public: | 32 public: |
| 31 typedef base::Callback<base::Value*( | 33 typedef base::Callback<std::unique_ptr<base::Value>(v8::Local<v8::Value>, |
| 32 v8::Local<v8::Value>, v8::Isolate* isolate)> FromV8ValueCallback; | 34 v8::Isolate* isolate)> |
| 35 FromV8ValueCallback; |
| 33 | 36 |
| 34 virtual ~Strategy() {} | 37 virtual ~Strategy() {} |
| 35 | 38 |
| 36 // If false is returned, V8ValueConverter proceeds with the default | 39 // If false is returned, V8ValueConverter proceeds with the default |
| 37 // behavior. | 40 // behavior. |
| 38 // Use |callback| to convert any child values, as this will retain | 41 // Use |callback| to convert any child values, as this will retain |
| 39 // the ValueConverter's internal checks for depth and cycles. | 42 // the ValueConverter's internal checks for depth and cycles. |
| 40 virtual bool FromV8Object(v8::Local<v8::Object> value, | 43 virtual bool FromV8Object(v8::Local<v8::Object> value, |
| 41 base::Value** out, | 44 std::unique_ptr<base::Value>* out, |
| 42 v8::Isolate* isolate, | 45 v8::Isolate* isolate, |
| 43 const FromV8ValueCallback& callback) const; | 46 const FromV8ValueCallback& callback) const; |
| 44 | 47 |
| 45 // If false is returned, V8ValueConverter proceeds with the default | 48 // If false is returned, V8ValueConverter proceeds with the default |
| 46 // behavior. | 49 // behavior. |
| 47 // Use |callback| to convert any child values, as this will retain | 50 // Use |callback| to convert any child values, as this will retain |
| 48 // the ValueConverter's internal checks for depth and cycles. | 51 // the ValueConverter's internal checks for depth and cycles. |
| 49 virtual bool FromV8Array(v8::Local<v8::Array> value, | 52 virtual bool FromV8Array(v8::Local<v8::Array> value, |
| 50 base::Value** out, | 53 std::unique_ptr<base::Value>* out, |
| 51 v8::Isolate* isolate, | 54 v8::Isolate* isolate, |
| 52 const FromV8ValueCallback& callback) const; | 55 const FromV8ValueCallback& callback) const; |
| 53 | 56 |
| 54 // If false is returned, V8ValueConverter proceeds with the default | 57 // If false is returned, V8ValueConverter proceeds with the default |
| 55 // behavior. v8::Object is passed as ArrayBuffer and ArrayBufferView | 58 // behavior. v8::Object is passed as ArrayBuffer and ArrayBufferView |
| 56 // classes are siblings. | 59 // classes are siblings. |
| 57 virtual bool FromV8ArrayBuffer(v8::Local<v8::Object> value, | 60 virtual bool FromV8ArrayBuffer(v8::Local<v8::Object> value, |
| 58 base::Value** out, | 61 std::unique_ptr<base::Value>* out, |
| 59 v8::Isolate* isolate) const; | 62 v8::Isolate* isolate) const; |
| 60 | 63 |
| 61 // If false is returned, V8ValueConverter proceeds with the default | 64 // If false is returned, V8ValueConverter proceeds with the default |
| 62 // behavior. This allows to intercept "non-finite" values and do something | 65 // behavior. This allows to intercept "non-finite" values and do something |
| 63 // with them. | 66 // with them. |
| 64 virtual bool FromV8Number(v8::Local<v8::Number> value, | 67 virtual bool FromV8Number(v8::Local<v8::Number> value, |
| 65 base::Value** out) const; | 68 std::unique_ptr<base::Value>* out) const; |
| 66 | 69 |
| 67 // If false is returned, V8ValueConverter proceeds with the default | 70 // If false is returned, V8ValueConverter proceeds with the default |
| 68 // behavior. | 71 // behavior. |
| 69 virtual bool FromV8Undefined(base::Value** out) const; | 72 virtual bool FromV8Undefined(std::unique_ptr<base::Value>* out) const; |
| 70 }; | 73 }; |
| 71 | 74 |
| 72 static V8ValueConverter* create(); | 75 static V8ValueConverter* create(); |
| 73 | 76 |
| 74 virtual ~V8ValueConverter() {} | 77 virtual ~V8ValueConverter() {} |
| 75 | 78 |
| 76 // If true, Date objects are converted into DoubleValues with the number of | 79 // If true, Date objects are converted into DoubleValues with the number of |
| 77 // seconds since Unix epoch. | 80 // seconds since Unix epoch. |
| 78 // | 81 // |
| 79 // Otherwise they are converted into DictionaryValues with whatever additional | 82 // Otherwise they are converted into DictionaryValues with whatever additional |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 112 | 115 |
| 113 // Converts a v8::Value to base::Value. | 116 // Converts a v8::Value to base::Value. |
| 114 // | 117 // |
| 115 // Unsupported types (unless explicitly configured) are not converted, so | 118 // Unsupported types (unless explicitly configured) are not converted, so |
| 116 // this method may return NULL -- the exception is when converting arrays, | 119 // this method may return NULL -- the exception is when converting arrays, |
| 117 // where unsupported types are converted to Value(TYPE_NULL). | 120 // where unsupported types are converted to Value(TYPE_NULL). |
| 118 // | 121 // |
| 119 // Likewise, if an object throws while converting a property it will not be | 122 // Likewise, if an object throws while converting a property it will not be |
| 120 // converted, whereas if an array throws while converting an item it will be | 123 // converted, whereas if an array throws while converting an item it will be |
| 121 // converted to Value(TYPE_NULL). | 124 // converted to Value(TYPE_NULL). |
| 122 // TODO(dcheng): This should return a unique_ptr. | 125 virtual std::unique_ptr<base::Value> FromV8Value( |
| 123 virtual base::Value* FromV8Value(v8::Local<v8::Value> value, | 126 v8::Local<v8::Value> value, |
| 124 v8::Local<v8::Context> context) const = 0; | 127 v8::Local<v8::Context> context) const = 0; |
| 125 }; | 128 }; |
| 126 | 129 |
| 127 } // namespace content | 130 } // namespace content |
| 128 | 131 |
| 129 #endif // CONTENT_PUBLIC_CHILD_V8_VALUE_CONVERTER_H_ | 132 #endif // CONTENT_PUBLIC_CHILD_V8_VALUE_CONVERTER_H_ |
| OLD | NEW |