Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2092377
  • 博文数量: 229
  • 博客积分: 7217
  • 博客等级: 上校
  • 技术积分: 3224
  • 用 户 组: 普通用户
  • 注册时间: 2009-02-19 17:23
个人简介

个人主页https://xugaoxiang.com,微信公众号: Dev_Club 或者搜索 程序员Club

文章分类

全部博文(229)

文章存档

2017年(1)

2016年(20)

2015年(23)

2013年(1)

2012年(23)

2011年(68)

2010年(62)

2009年(31)

分类: LINUX

2015-05-15 13:58:32

本文介绍了一种在以webkit为核心的浏览器中添加本地javascript对象的方法。该方法为前端javascript提供了访问系统资源的渠道,可以实现快速开发。该方法是私有的,在普通浏览器上进行调用,会报js错误。

实例
本例将创建一个名为LJFrontPanel的类以及类的实例ljfrontpanel,这个类提供了一个方法writeLed(const String&),方法中只有简单的操作,作为示例而已。将下面创建的LJFrontPanel.h、LJFrontPanel.cpp和LJFrontPanel.idl放到WebCore/page下。

LJFrontPanel.h

点击(此处)折叠或打开

  1.     #ifndef LJFrontPanel_H
  2.     #define LJFrontPanel_H

  3.     #include <wtf/PassRefPtr.h>
  4.     #include <wtf/RefCounted.h>

  5.     #include <stdio.h>
  6.     #include <string.h>
  7.     #include "CString.h"
  8.     #include "PlatformString.h"

  9.     namespace WebCore {

  10.         class LJFrontPanel : public RefCounted<LJFrontPanel> {
  11.         public:
  12.             static PassRefPtr<LJFrontPanel> create() { return adoptRef(new LJFrontPanel()); }
  13.             bool writeLed(const String&);

  14.         private:
  15.             LJFrontPanel();
  16.         };

  17.     } // namespace WebCore

  18.     #endif // LJFrontPanel_H

LJFrontPanel.cpp

点击(此处)折叠或打开

  1. #include "LJFrontPanel.h"

  2.     namespace WebCore {

  3.     LJFrontPanel::LJFrontPanel()
  4.     {
  5.     }

  6.     bool LJFrontPanel::writeLed(const String& buff)
  7.     {
  8.         char tmpBuff[5] = {0};

  9.         if((buff.isEmpty()) == true){
  10.             return false;
  11.         }
  12.         else{
  13.             printf("tmpBuff=%s\n",buff.utf8().data());
  14.             strncpy(tmpBuff,buff.utf8().data(),5);
  15.             return true;
  16.         }
  17.     }

  18.     } // namespace WebCore

LJFrontPanel.idl

点击(此处)折叠或打开

  1. module window {

  2.         interface LJFrontPanel{
  3.         boolean writeLed(in DOMString buff);
  4.         };

  5.     }

修改WebCore/page/DOMWindow.h

点击(此处)折叠或打开

  1. //添加声明
  2.     class LJFrontPanel;
  3.         .
  4.         .
  5.         .
  6.     //以下两句放在public里
  7.     LJFrontPanel* ljfrontpanel() const;
  8.     LJFrontPanel* optionalLJFrontPanel() const { return m_ljfrontpanel.get();}
  9.         .
  10.         .
  11.         .
  12.     //这句放在private里
  13.     mutable RefPtr<LJFrontPanel> m_ljfrontpanel;

修改WebCore/page/DOMWindow.cpp

点击(此处)折叠或打开

  1. //引用头文件
  2.     #include "LJFrontPanel.h"
  3.     
  4.     //创建js对象
  5.     LJFrontPanel* DOMWindow::ljfrontpanel() const
  6.     {
  7.         if (!m_ljfrontpanel)
  8.             m_ljfrontpanel = LJFrontPanel::create();
  9.         return m_ljfrontpanel.get();
  10.     }
  11.     
  12.     //消毁js对象,在DOMWindow::clear()方法中实现
  13.     m_ljfrontpanel = 0;

修改WebCore/page/DOMWindow.idl

点击(此处)折叠或打开

  1. //添加
  2.     attribute [Replaceable] LJFrontPanel ljfrontpanel;

测试
重编webkit,打开下面的测试页面

点击(此处)折叠或打开

  1. <html>
  2.     <body>
  3.     <script type="text/javascript">
  4.     document.write("
    ============= This is from LJWebKit JSExt ljfrontpanel:"
    );
  5.     document.write(ljfrontpanel.writeLed("07:59"));

  6.     </script>
  7.     </body>
  8.     </html>

备注
1、用上述方法创建的javascript对象,是作为DOMWindow的子对象,它是在window创建时创建,在window消毁前释放,即它的生命周和跟window是一样的    
2、经测试,在javascript对象提供的方法中不能使用int型,而是使用long型,而js里的string型则对应于webkit里的String型。这点在给头端提供接口时要注意。    
3、创建的对象作为DOMWindow的子对象,这个js实例只能由window创建,而用户是无法通过类似new LJFrontPanel()的方式创建的。

参考资料
1、<    
2、<    
3、<    
4、<%E6%89%A9%E5%B1%95/>


阅读(2213) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~