3.2 CefV8Value类实现定JavaScript数据类型
3.2.1 一般对象
3.2.2 CEF实现带access的JavaScript对象
3.2.3 CEF实现带拦截器CefV8Interceptor的JavaScript对象
3.2 CefV8Value类实现定JavaScript数据类型
上面的实例中我们已经用到CefV8Value类的CreateString("My Value!")创建字符串和CreateFunction创建了函数。这个类还可以创建, null, bool, int, double, date string等数据类型,都有对应的函数。static CefRefPtr<CefV8Value> CreateArray(int length)创建JavaScript数组。下面详细讲解下给JavaScript提供创建对象。accessor就是给JavaScript提供调用的set和get方法设置或获取属性。interceptor是拦截器,拦截set和get方法。
///
// Create a new CefV8Value object of type object with optional accessor and/or
// interceptor. This method should only be called from within the scope of a
// CefRenderProcessHandler, CefV8Handler or CefV8Accessor callback, or in
// combination with calling Enter() and Exit() on a stored CefV8Context
// reference.
///
/*--cef(optional_param=accessor, optional_param=interceptor)--*/
static CefRefPtr<CefV8Value> CreateObject(
CefRefPtr<CefV8Accessor> accessor,
CefRefPtr<CefV8Interceptor> interceptor);
3.2.1 一般对象
如果不需要给JavaScript提供get和set方法,可以直接创建对象。
CefRefPtr<CefV8Value> obj = CefV8Value::CreateObject(NULL);
给对象设置属性名称为myval,属性值为My String;字符串类型。
virtual bool SetValue(int index, CefRefPtr<CefV8Value> value) = 0;
obj->SetValue("myval", CefV8Value::CreateString("My String!"));
3.2.2 CEF实现带access的JavaScript对象
如果要提供set和get方法,需要提供access实现类。
(1)实现access类
class MyV8Accessor : public CefV8Accessor {
public:
MyV8Accessor() {}
virtual bool Get(const CefString& name,
const CefRefPtr<CefV8Value> object,
CefRefPtr<CefV8Value>& retval,
CefString& exception) OVERRIDE {
if (name == "myval") {
// Return the value.
retval = CefV8Value::CreateString(myval_);
return true;
}
// Value does not exist.
return false;
}
virtual bool Set(const CefString& name,
const CefRefPtr<CefV8Value> object,
const CefRefPtr<CefV8Value> value,
CefString& exception) OVERRIDE {
if (name == "myval") {
if (value->IsString()) {
// Store the value.
myval_ = value->GetStringValue();
} else {
// Throw an exception.
exception = "Invalid value type";
}
return true;
}
// Value does not exist.
return false;
}
// Variable used for storing the value.
CefString myval_;
// Provide the reference counting implementation for this class.
IMPLEMENT_REFCOUNTING(MyV8Accessor);
};
(2)使用SetValue函数设置对象属性采用access方式设置和获取数据。把对象设置到窗口对象中。
virtual bool SetValue(const CefString& key,
AccessControl settings,
PropertyAttribute attribute) = 0;
typedef enum {
V8_ACCESS_CONTROL_DEFAULT = 0,
V8_ACCESS_CONTROL_ALL_CAN_READ = 1,
V8_ACCESS_CONTROL_ALL_CAN_WRITE = 1 << 1,
V8_ACCESS_CONTROL_PROHIBITS_OVERWRITING = 1 << 2
} cef_v8_accesscontrol_t;
///
// V8 property attribute values.
///
typedef enum {
V8_PROPERTY_ATTRIBUTE_NONE = 0, // Writeable, Enumerable,
// Configurable
V8_PROPERTY_ATTRIBUTE_READONLY = 1 << 0, // Not writeable
V8_PROPERTY_ATTRIBUTE_DONTENUM = 1 << 1, // Not enumerable
V8_PROPERTY_ATTRIBUTE_DONTDELETE = 1 << 2 // Not configurable
} cef_v8_propertyattribute_t;
obj->SetValue("myval", V8_ACCESS_CONTROL_DEFAULT,
V8_PROPERTY_ATTRIBUTE_NONE);
(3)JavaScript中调用set和get方法,就可以设置和获取属性值。
3.2.3 CEF实现带拦截器CefV8Interceptor的JavaScript对象
拦截器CefV8Interceptor和Access的区别是,除了可以用字符串来映射属性,还可以用index索引来映射属性。其定义如下
// Interface that should be implemented to handle V8 interceptor calls. The
// methods of this class will be called on the thread associated with the V8
// interceptor. Interceptor's named property handlers (with first argument of
// type CefString) are called when object is indexed by string. Indexed property
// handlers (with first argument of type int) are called when object is indexed
// by integer.
///
/*--cef(source=client,no_debugct_check)--*/
class CefV8Interceptor : public virtual CefBaseRefCounted {
public:
///
// Handle retrieval of the interceptor value identified by |name|. |object| is
// the receiver ('this' object) of the interceptor. If retrieval succeeds, set
// |retval| to the return value. If the requested value does not exist, don't
// set either |retval| or |exception|. If retrieval fails, set |exception| to
// the exception that will be thrown. If the property has an associated
// accessor, it will be called only if you don't set |retval|.
// Return true if interceptor retrieval was handled, false otherwise.
///
/*--cef(capi_name=get_byname)--*/
virtual bool Get(const CefString& name,
const CefRefPtr<CefV8Value> object,
CefRefPtr<CefV8Value>& retval,
CefString& exception) = 0;
///
// Handle retrieval of the interceptor value identified by |index|. |object|
// is the receiver ('this' object) of the interceptor. If retrieval succeeds,
// set |retval| to the return value. If the requested value does not exist,
// don't set either |retval| or |exception|. If retrieval fails, set
// |exception| to the exception that will be thrown.
// Return true if interceptor retrieval was handled, false otherwise.
///
/*--cef(capi_name=get_byindex,index_param=index)--*/
virtual bool Get(int index,
const CefRefPtr<CefV8Value> object,
CefRefPtr<CefV8Value>& retval,
CefString& exception) = 0;
///
// Handle assignment of the interceptor value identified by |name|. |object|
// is the receiver ('this' object) of the interceptor. |value| is the new
// value being assigned to the interceptor. If assignment fails, set
// |exception| to the exception that will be thrown. This setter will always
// be called, even when the property has an associated accessor.
// Return true if interceptor assignment was handled, false otherwise.
///
/*--cef(capi_name=set_byname)--*/
virtual bool Set(const CefString& name,
const CefRefPtr<CefV8Value> object,
const CefRefPtr<CefV8Value> value,
CefString& exception) = 0;
///
// Handle assignment of the interceptor value identified by |index|. |object|
// is the receiver ('this' object) of the interceptor. |value| is the new
// value being assigned to the interceptor. If assignment fails, set
// |exception| to the exception that will be thrown.
// Return true if interceptor assignment was handled, false otherwise.
///
/*--cef(capi_name=set_byindex,index_param=index)--*/
virtual bool Set(int index,
const CefRefPtr<CefV8Value> object,
const CefRefPtr<CefV8Value> value,
CefString& exception) = 0;
};
实例步骤如下
(1)实现Interceptor类
class Interceptor : public CefV8Interceptor {
public:
Interceptor() {}
virtual bool Get(const CefString& name,
const CefRefPtr<CefV8Value> object,
CefRefPtr<CefV8Value>& retval,
CefString& exception) OVERRIDE {
return true;
}
virtual bool Get(int index,
const CefRefPtr<CefV8Value> object,
CefRefPtr<CefV8Value>& retval,
CefString& exception) OVERRIDE {
return true;
}
virtual bool Set(const CefString& name,
const CefRefPtr<CefV8Value> object,
const CefRefPtr<CefV8Value> value,
CefString& exception) OVERRIDE {
return true;
}
virtual bool Set(int index,
const CefRefPtr<CefV8Value> object,
const CefRefPtr<CefV8Value> value,
CefString& exception) OVERRIDE {
return true;
}
IMPLEMENT_REFCOUNTING(Interceptor);
};
(2)创建Interceptor对象和JavaScript对象
CefRefPtr<CefV8Interceptor> interceptor = new Interceptor();
PERF_ITERATIONS_START()
CefRefPtr<CefV8Value> value = CefV8Value::CreateObject(nullptr, interceptor);
(3)SetValue()函设置到窗口对象中(待验证)
(4)JavaScript调用按照字符串名称或者索引来设置获取值。
自己开发了一个股票智能分析软件,功能很强大,需要的点击下面的链接获取: