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