WebKit之扩展Binding

DOM对象有三种形态

一.寄生于现有对象,单实例

从属于某个全局变量,访问时通过宿主对象完成。如window.object,也可以直接调用object.method。这个方法最为简单,按需分配,并且随着宿主对象释放。参考windowNavigator实现。

1、添加CooperWebObject.h, CooperWebObject.cpp, CooperWebObject.idl文件,简单起见,将这三个文件放到Source/WebCore/page目录下。

a. CooperWebObject.h

[html]  view plain copy
  1. #ifndef CooperWebObject_h  
  2. #define CooperWebObject_h  
  3.   
  4. #include <wtf/Forward.h>  
  5. #include <wtf/HashMap.h>  
  6. #include <wtf/PassRefPtr.h>  
  7. #include <wtf/RefCounted.h>  
  8. #include <wtf/RefPtr.h>  
  9.   
  10. namespace WebCore{  
  11.   
  12.     class CooperWebObject : public RefCounted<CooperWebObject>  
  13.     {  
  14.     public:  
  15.         static PassRefPtr<CooperWebObject> create( )  
  16.         {  
  17.             return adoptRef( new CooperWebObject() );  
  18.         }  
  19.   
  20.         String description() const;  
  21.   
  22.     private:  
  23.         CooperWebObject();  
  24.     };  
  25.   
  26. }//namespace WebCore  
  27.   
  28. #endif  

b. CooprWebObject.cpp

[html]  view plain copy
  1. #include "config.h"  
  2. #include "CooperWebObject.h"  
  3.   
  4. namespace WebCore{  
  5.   
  6.     CooperWebObject::CooperWebObject()  
  7.     {  
  8.         //construction  
  9.     }  
  10.   
  11.     String CooperWebObject::description() const  
  12.     {  
  13.         return "Hello World!";  
  14.     }  
  15.   
  16. }//namespace WebCore  

c. CooperWebObject.idl

[html]  view plain copy
  1. [  
  2.     OmitConstructor  
  3. ] interface CooperWebObject {  
  4.     readonly attribute DOMString description;  
  5. };  

也可以通过idl文件手动去生成相关的文件,JSCooperWebObject.h JSCooperWebOhject.cpp。手动生成的时候需要使用到WebCore\bindings\scripts\generate-bindings.pl 这个脚本,使用的时候还需要一些参数,./generate-bindings.pl ./test/CooperWebObject.idl --generator=JS --outputDir=../js/  详细的参数说明参考 generate-bindings.pl 中的注释。WebKitIDL的使用可以参考http://trac.webkit.org/wiki/WebKitIDL 。在Windows平台上idl文件末尾需要添加一个空行,不然在生成文件的时候会提示错误。


d. 修改DOMWindow.h,添加如下代码:

[html]  view plain copy
  1. class CooperWebObject;  
  2.   
  3. …  
  4.   
  5. public:  
  6.   
  7.     CooperWebObject* CooperWebObject() const;  
  8.   
  9. private:  
  10.   
  11.     mutable RefPtr<CooperWebObject> m_cooperWebObject;  
  12.       
e. 修改DOMWindow.cpp,添加如下代码:

    添加头文件:

[html]  view plain copy
  1. #include "CooperWebObject.h"  
    添加函数:

[html]  view plain copy
  1. CooperWebObject* DOMWindow::CooperWebObject()const  
  2. {  
  3.     if (!m_cooperWebObject)  
  4.        m_cooperWebObject = CooperWebObject::create();  
  5.     return m_cooperWebObject.get();  
  6. }  
  在resetDOMWindowProperties()函数中添加:

[html]  view plain copy
  1. m_cooperWebObject = 0;  
f. 修改DOMWindow.idl,添加如下一行 :
[html]  view plain copy
  1. attribute [Replaceable] CooperWebObject cooperWebObject;  
g. 修改DerivedSources.make, 参考Navigator.idl添加如下代码:
[html]  view plain copy
  1. $(WebCore)/page/CooperWebObject.idl \  

h. 修改CMakeLists.txt和WebCore.gypi,参考Navigator.idl, Navigator.h, Navigator.cpp添加相应的文件.

I.  对于VS Project,可以将相关的文件添加项目中,然后我的是重新编译整个solution才测试成功。

测试代码:

[html]  view plain copy
  1. <html>   
  2. <body>   
  3. <script>  
  4. document.write("<p> This is from cooperWebObject: ");   
  5. document.write(window.cooperWebObject.description + "</p>");   
  6. document.write("<br />");  
  7. document.write(cooperWebObject.description);  
  8. </script>   
  9. </body>   
  10. </html>  

二.和windowdocument一样成为全局变量,单实例。

这个实现最为复杂,关键要将对象在合适的位置创建,并更新到JSC的堆中才能达到功能。可以参考document的实现,并且需要考虑执行流程,必须对代码做过一些了解,才可能知道相关的改动量。

因为是DOM有一个新的全局对象,将新增文件放到WebCore/dom下。

A. CooperGlobalWebObject.h

[html]  view plain copy
  1. #ifndef CooperGlobalWebObject_h  
  2. #define CooperGlobalWebObject_h  
  3.   
  4. #include <wtf/Forward.h>  
  5. #include <wtf/HashMap.h>  
  6. #include <wtf/PassRefPtr.h>  
  7. #include <wtf/RefCounted.h>  
  8. #include <wtf/RefPtr.h>  
  9. #include <wtf/text/WTFString.h>  
  10.   
  11. namespace WebCore{  
  12.   
  13.     class CooperGlobalWebObject : public RefCounted<CooperGlobalWebObject>  
  14.     {  
  15.     public:  
  16.         static PassRefPtr<CooperGlobalWebObject> create( )  
  17.         {  
  18.             return adoptRef( new CooperGlobalWebObject() );  
  19.         }  
  20.   
  21.         String description() const;  
  22.   
  23.     private:  
  24.         CooperGlobalWebObject();  
  25.     };  
  26.   
  27. }//namespace WebCore  
  28.   
  29. #endif  

B. CooperGlobalWebObject.cpp

[html]  view plain copy
  1. #include "config.h"  
  2. #include "CooperGlobalWebObject.h"  
  3.   
  4. namespace WebCore{  
  5.   
  6.     CooperGlobalWebObject::CooperGlobalWebObject()  
  7.     {  
  8.         //construction  
  9.     }  
  10.   
  11.     String CooperGlobalWebObject::description() const  
  12.     {  
  13.         return "Hello World From Global Object!";  
  14.     }  
  15.   
  16. }//namespace WebCore  

C. CooperGlobalWebObject.idl

[html]  view plain copy
  1. [  
  2.     OmitConstructor  
  3. ] interface CooperGlobalWebObject {  
  4.     readonly attribute DOMString description;  
  5. };  

D. 修改DOMWindow.h,添加如下代码

[html]  view plain copy
  1. class CooperGlobalWebObject;  
  2.   
  3. public:  
  4.     CooperGlobalWebObject * cooperGlobalWebObject() const;  
  5.       
  6. private:  
  7.     mutable RefPtr<CooperGlobalWebObject> m_cooperGlobalWebObject;  

E. 修改DOMWindow.cpp,添加如下代码

[html]  view plain copy
  1. #include "CooperGlobalWebObject.h"  
在void DOMWindow::resetDOMWindowProperties()函数中添加一行

[html]  view plain copy
  1. m_cooperGlobalWebObject = 0;  
添加函数

[html]  view plain copy
  1. CooperGlobalWebObject* DOMWindows::cooperGlobalWebObject() const  
  2. {  
  3.     if (!m_cooperGlobalWebObject)  
  4.         m_cooperGlobalWebObject = CooperGlobalWebObject::create();  
  5.     return m_cooperGlobalWebObject.get();  
  6. }  

F.  修改 JSDOMWindowBase.h  updateDocument  下添加一行

[html]  view plain copy
  1. void updateCooperGlobalWebObject();  

G. 修改JSDOMWindowBase.cpp 

[html]  view plain copy
  1. #include "JSCooperGlobalWebObject.h"  
添加函数

[html]  view plain copy
  1. void void JSDOMWindowBase::updateCooperGlobalWebObject()  
  2. {  
  3.     ASSERT(m_impl->cooperGlobalWebObject);  
  4.     ExecState* exec = globalExec();  
  5.     symbolTablePutWithAttributes(this, exec->globalData(), Identifier(exec, "cooperglobalwebobject"),   
  6.         toJS(exec, this, m_impl->cooperGlobalWebObject()), DontDelete | ReadOnly);  
  7. }  

在finishCreation函数中的staticGlobals中添加一行

[html]  view plain copy
  1. GlobalPropertyInfo(Identifier(globalExec(), "cooperglobalwebobject"), jsNull(), DontDelete | ReadOnly),  

H.  修改 ScriptController.h  updateDocument  下添加一行

[html]  view plain copy
  1. void updateCooperGlobalWebObject();  

I. 修改ScriptController.cpp 

[html]  view plain copy
  1. #include "CooperGlobalWebObject.h"  
在updateDocument下面添加一行

[html]  view plain copy
  1. windowShell->window()->updateCooperGlobalWebObject();  
添加函数

[html]  view plain copy
  1. void ScriptController::updateCooperGlobalWebObject()  
  2. {  
  3.     for (ShellMap::iterator iter = m_windowShells.begin(); iter != m_windowShells.end(); ++iter) {  
  4.         JSLockHolder lock(iter->key->globalData());  
  5.         iter->value->window()->updateCooperGlobalWebObject();  
  6.     }  
  7. }  

J.  修改 Frame.cpp  setDocument 函数中 m_script.updateDocument下加入一行

[html]  view plain copy
  1. m_script.updateCooperGlobalWebObject();  

K. 修改DerivedSources.make,参考Document.idl添加一行

[html]  view plain copy
  1. $(WebCore)/dom/CooperGlobalWebObject.idl \  

L. 修改CMakeLists.txtWebCore.gypi参考Document.idl Document.h Document.cpp  JSDocument.cpp 添加相应内容。

三.多实例对象。可以在脚本中使用new创建。

myObj = new Object();

较第一类需要指定自定义建构函数,主要参考DOM中的ImageFloat32Array实现。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值