15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 80529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch#include "base/callback.h" 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "content/common/content_export.h" 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "v8/include/v8.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Value; 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content { 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Converts between v8::Value (JavaScript values in the v8 heap) and Chrome's 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// values (from base/values.h). Lists and dictionaries are converted 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// recursively. 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The JSON types (null, boolean, string, number, array, and object) as well as 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// binary values are supported. For binary values, we convert to WebKit 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ArrayBuffers, and support converting from an ArrayBuffer or any of the 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ArrayBufferView subclasses (Uint8Array, etc.). 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class CONTENT_EXPORT V8ValueConverter { 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 28c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // Extends the default behaviour of V8ValueConverter. 29c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch class CONTENT_EXPORT Strategy { 30c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch public: 310529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch typedef base::Callback<base::Value*( 320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch v8::Handle<v8::Value>, v8::Isolate* isolate)> FromV8ValueCallback; 330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 34c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch virtual ~Strategy() {} 350529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 36c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // If false is returned, V8ValueConverter proceeds with the default 37c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // behavior. 380529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Use |callback| to convert any child values, as this will retain 390529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // the ValueConverter's internal checks for depth and cycles. 40c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch virtual bool FromV8Object(v8::Handle<v8::Object> value, 41a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::Value** out, 420529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch v8::Isolate* isolate, 435c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu const FromV8ValueCallback& callback) const; 440529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch 45c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // If false is returned, V8ValueConverter proceeds with the default 46c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // behavior. 470529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // Use |callback| to convert any child values, as this will retain 480529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch // the ValueConverter's internal checks for depth and cycles. 49c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch virtual bool FromV8Array(v8::Handle<v8::Array> value, 50a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::Value** out, 510529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch v8::Isolate* isolate, 525c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu const FromV8ValueCallback& callback) const; 535c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 545c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // If false is returned, V8ValueConverter proceeds with the default 555c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // behavior. v8::Object is passed as ArrayBuffer and ArrayBufferView 565c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // classes are siblings. 575c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu virtual bool FromV8ArrayBuffer(v8::Handle<v8::Object> value, 58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) base::Value** out, 59cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) v8::Isolate* isolate) const; 605c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 615c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // If false is returned, V8ValueConverter proceeds with the default 625c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // behavior. This allows to intercept "non-finite" values and do something 635c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // with them. 645c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu virtual bool FromV8Number(v8::Handle<v8::Number> value, 655c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu base::Value** out) const; 665c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu 675c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // If false is returned, V8ValueConverter proceeds with the default 685c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu // behavior. 695c02ac1a9c1b504631c0a3d2b6e737b5d738bae1Bo Liu virtual bool FromV8Undefined(base::Value** out) const; 70c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch }; 71c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static V8ValueConverter* create(); 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~V8ValueConverter() {} 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If true, Date objects are converted into DoubleValues with the number of 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // seconds since Unix epoch. 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Otherwise they are converted into DictionaryValues with whatever additional 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // properties has been set on them. 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetDateAllowed(bool val) = 0; 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If true, RegExp objects are converted into StringValues with the regular 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // expression between / and /, for example "/ab?c/". 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Otherwise they are converted into DictionaryValues with whatever additional 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // properties has been set on them. 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetRegExpAllowed(bool val) = 0; 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If true, Function objects are converted into DictionaryValues with whatever 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // additional properties has been set on them. 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Otherwise they are treated as unsupported, see FromV8Value. 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetFunctionAllowed(bool val) = 0; 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // If true, null values are stripped from objects. This is often useful when 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // converting arguments to extension APIs. 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void SetStripNullFromObjects(bool val) = 0; 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 100c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch // Extend default behavior of V8ValueConverter. 101c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch virtual void SetStrategy(Strategy* strategy) = 0; 102c2db58bd994c04d98e4ee2cd7565b71548655fe3Ben Murdoch 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Converts a base::Value to a v8::Value. 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Unsupported types are replaced with null. If an array or object throws 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // while setting a value, that property or item is skipped, leaving a hole in 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the case of arrays. 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual v8::Handle<v8::Value> ToV8Value( 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const base::Value* value, 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) v8::Handle<v8::Context> context) const = 0; 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Converts a v8::Value to base::Value. 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Unsupported types (unless explicitly configured) are not converted, so 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // this method may return NULL -- the exception is when converting arrays, 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // where unsupported types are converted to Value(TYPE_NULL). 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Likewise, if an object throws while converting a property it will not be 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // converted, whereas if an array throws while converting an item it will be 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // converted to Value(TYPE_NULL). 1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual base::Value* FromV8Value(v8::Handle<v8::Value> value, 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) v8::Handle<v8::Context> context) const = 0; 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace content 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CONTENT_PUBLIC_RENDERER_V8_VALUE_CONVERTER_H_ 128