`
jiagou
  • 浏览: 2525590 次
文章分类
社区版块
存档分类
最新评论

webkit的js对象扩展(一)——binding方式创建自定义对象(单实例)

 
阅读更多

通过binding方式

要扩展一个全局JS对象除了要为webkit添加这个对象的头文件和cpp文件外,还需要为这个对象写一个idl文件以便webkit自动生成相应的代码;另外,还需要修改DOMWindow.*以便把新对象注册上去。下面以MyObject对象为例介绍具体步骤。

WebCore/page/

1.添加MyObject.h文件

  1. #ifndefMyObject_h
  2. #defineMyObject_h
  3. #include<wtf/PassRefPtr.h>
  4. #include<wtf/RefCounted.h>
  5. #include<wtf/RefPtr.h>
  6. namespaceWebCore{
  7. classFrame;
  8. classString;
  9. classMyObject:publicRefCounted<MyObject>{
  10. public:
  11. staticPassRefPtr<MyObject>create(Frame*frame)
  12. {
  13. returnadoptRef(newMyObject(frame));
  14. }
  15. ~MyObject();
  16. voiddisconnectFrame();
  17. Frame*frame()const{returnm_frame;}
  18. Stringdescription()const;
  19. private:
  20. MyObject(Frame*);
  21. Frame*m_frame;
  22. };
  23. }
  24. #endif
2.添加MyObject.cpp文件

  1. #include"MyObject.h"
  2. #include"PlatformString.h"
  3. namespaceWebCore{
  4. MyObject::MyObject(Frame*frame)
  5. :m_frame(frame)
  6. {
  7. }
  8. MyObject::~MyObject()
  9. {
  10. disconnectFrame();
  11. }
  12. voidMyObject::disconnectFrame()
  13. {
  14. m_frame=0;
  15. }
  16. StringMyObject::description()const//对象的属性
  17. {
  18. return"HelloWorld!";
  19. }
  20. }
3.添加MyObject.idl文件

  1. modulewindow{
  2. interfaceMyObject{
  3. readonlyattributeDOMStringdescription;
  4. };
  5. }
4.修改DOMWindow.h文件

添加如下声明:

  1. public:
  2. MyObject*myObject()const;
  3. MyObject*optionalMyObject()const{returnm_myObject.get();}
  4. private:
  5. mutableRefPtr<MyObject>m_myObject;
5.修改DOMWindow.cpp文件

添加接口实现

  1. MyObject*DOMWindow::myObject()const
  2. {
  3. if(!m_myObject)
  4. m_myObject=MyObject::create(m_frame);
  5. returnm_myObject.get();
  6. }
修改部分函数实现

void DOMWindow::clear()函数中添加:

  1. if(m_myObject)
  2. m_myObject->disconnectFrame();
  3. m_myObject=0;
6.修改DOMWindow.idl文件

添加:

  1. attribute[Replaceable]MyObjectMyObject;

7.修改CMakeLists.txt

将MyObject.cpp、MyObject.idl加入编译。

OK。以上步骤就添加了一个自定义的全局对象。这是单实例的,有时间了再把多实例的过程写下,也就是可以new了。

小弟新手,有问题请大家多多指教。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics