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_RENDERER_V8_VALUE_CONVERTER_H_ | 5 #ifndef CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_ |
6 #define CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_ | 6 #define CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_ |
7 | 7 |
8 #include "content/common/content_export.h" | 8 #include "content/common/content_export.h" |
9 #include "v8/include/v8.h" | 9 #include "v8/include/v8.h" |
10 | 10 |
11 namespace base { | 11 namespace base { |
12 class Value; | 12 class Value; |
13 } | 13 } |
14 | 14 |
15 namespace content { | 15 namespace content { |
16 | 16 |
17 // Converts between v8::Value (JavaScript values in the v8 heap) and Chrome's | 17 // Converts between v8::Value (JavaScript values in the v8 heap) and Chrome's |
18 // values (from base/values.h). Lists and dictionaries are converted | 18 // values (from base/values.h). Lists and dictionaries are converted |
19 // recursively. | 19 // recursively. |
20 // | 20 // |
21 // The JSON types (null, boolean, string, number, array, and object) as well as | 21 // The JSON types (null, boolean, string, number, array, and object) as well as |
22 // binary values are supported. For binary values, we convert to WebKit | 22 // binary values are supported. For binary values, we convert to WebKit |
23 // ArrayBuffers, and support converting from an ArrayBuffer or any of the | 23 // ArrayBuffers, and support converting from an ArrayBuffer or any of the |
24 // ArrayBufferView subclasses (Uint8Array, etc.). | 24 // ArrayBufferView subclasses (Uint8Array, etc.). |
25 class CONTENT_EXPORT V8ValueConverter { | 25 class CONTENT_EXPORT V8ValueConverter { |
26 public: | 26 public: |
27 static V8ValueConverter* create(); | 27 // Extends the default behaviour of V8ValueConverter. |
| 28 class CONTENT_EXPORT Strategy { |
| 29 public: |
| 30 // If false is returned, V8ValueConverter proceeds with the default |
| 31 // behavior. |
| 32 virtual bool FromV8Object(v8::Handle<v8::Object> value, |
| 33 base::Value** out) const = 0; |
| 34 // If false is returned, V8ValueConverter proceeds with the default |
| 35 // behavior. |
| 36 virtual bool FromV8Array(v8::Handle<v8::Array> value, |
| 37 base::Value** out) const = 0; |
| 38 }; |
| 39 |
| 40 static V8ValueConverter* create(); |
28 | 41 |
29 virtual ~V8ValueConverter() {} | 42 virtual ~V8ValueConverter() {} |
30 | 43 |
31 // If true, Date objects are converted into DoubleValues with the number of | 44 // If true, Date objects are converted into DoubleValues with the number of |
32 // seconds since Unix epoch. | 45 // seconds since Unix epoch. |
33 // | 46 // |
34 // Otherwise they are converted into DictionaryValues with whatever additional | 47 // Otherwise they are converted into DictionaryValues with whatever additional |
35 // properties has been set on them. | 48 // properties has been set on them. |
36 virtual void SetDateAllowed(bool val) = 0; | 49 virtual void SetDateAllowed(bool val) = 0; |
37 | 50 |
38 // If true, RegExp objects are converted into StringValues with the regular | 51 // If true, RegExp objects are converted into StringValues with the regular |
39 // expression between / and /, for example "/ab?c/". | 52 // expression between / and /, for example "/ab?c/". |
40 // | 53 // |
41 // Otherwise they are converted into DictionaryValues with whatever additional | 54 // Otherwise they are converted into DictionaryValues with whatever additional |
42 // properties has been set on them. | 55 // properties has been set on them. |
43 virtual void SetRegExpAllowed(bool val) = 0; | 56 virtual void SetRegExpAllowed(bool val) = 0; |
44 | 57 |
45 // If true, Function objects are converted into DictionaryValues with whatever | 58 // If true, Function objects are converted into DictionaryValues with whatever |
46 // additional properties has been set on them. | 59 // additional properties has been set on them. |
47 // | 60 // |
48 // Otherwise they are treated as unsupported, see FromV8Value. | 61 // Otherwise they are treated as unsupported, see FromV8Value. |
49 virtual void SetFunctionAllowed(bool val) = 0; | 62 virtual void SetFunctionAllowed(bool val) = 0; |
50 | 63 |
51 // If true, null values are stripped from objects. This is often useful when | 64 // If true, null values are stripped from objects. This is often useful when |
52 // converting arguments to extension APIs. | 65 // converting arguments to extension APIs. |
53 virtual void SetStripNullFromObjects(bool val) = 0; | 66 virtual void SetStripNullFromObjects(bool val) = 0; |
54 | 67 |
| 68 // Extend default behavior of V8ValueConverter. |
| 69 virtual void SetStrategy(Strategy* strategy) = 0; |
| 70 |
55 // Converts a base::Value to a v8::Value. | 71 // Converts a base::Value to a v8::Value. |
56 // | 72 // |
57 // Unsupported types are replaced with null. If an array or object throws | 73 // Unsupported types are replaced with null. If an array or object throws |
58 // while setting a value, that property or item is skipped, leaving a hole in | 74 // while setting a value, that property or item is skipped, leaving a hole in |
59 // the case of arrays. | 75 // the case of arrays. |
60 virtual v8::Handle<v8::Value> ToV8Value( | 76 virtual v8::Handle<v8::Value> ToV8Value( |
61 const base::Value* value, | 77 const base::Value* value, |
62 v8::Handle<v8::Context> context) const = 0; | 78 v8::Handle<v8::Context> context) const = 0; |
63 | 79 |
64 // Converts a v8::Value to base::Value. | 80 // Converts a v8::Value to base::Value. |
65 // | 81 // |
66 // Unsupported types (unless explicitly configured) are not converted, so | 82 // Unsupported types (unless explicitly configured) are not converted, so |
67 // this method may return NULL -- the exception is when converting arrays, | 83 // this method may return NULL -- the exception is when converting arrays, |
68 // where unsupported types are converted to Value(TYPE_NULL). | 84 // where unsupported types are converted to Value(TYPE_NULL). |
69 // | 85 // |
70 // Likewise, if an object throws while converting a property it will not be | 86 // Likewise, if an object throws while converting a property it will not be |
71 // converted, whereas if an array throws while converting an item it will be | 87 // converted, whereas if an array throws while converting an item it will be |
72 // converted to Value(TYPE_NULL). | 88 // converted to Value(TYPE_NULL). |
73 virtual base::Value* FromV8Value(v8::Handle<v8::Value> value, | 89 virtual base::Value* FromV8Value(v8::Handle<v8::Value> value, |
74 v8::Handle<v8::Context> context) const = 0; | 90 v8::Handle<v8::Context> context) const = 0; |
75 }; | 91 }; |
76 | 92 |
77 } // namespace content | 93 } // namespace content |
78 | 94 |
79 #endif // CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_ | 95 #endif // CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_ |
OLD | NEW |