CefV8Value類實現定JavaScript數據類型、數組、對象


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調用按照字符串名稱或者索引來設置獲取值。

自己開發了一個股票智能分析軟件,功能很強大,需要的點擊下面的鏈接獲取:

https://www.cnblogs.com/bclshuai/p/11380657.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM