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