日期:2014-05-16  浏览次数:20422 次

gecko中的js扩展
1 创建js对象对应的idl文件
假设要扩展一个hiGecko对象,那么先创建一个idl文件,命名为nsIDOMHiGecko.idl,它的内容如下
#include "nsISupports.idl" 
[scriptable, uuid(9285aaa6-19fd-4fdb-8621-916f856c024f)]
interface nsIDOMHiGecko : nsISupports
{
    attribute long myval ; /*可读可写的属性*/
    boolean getIsOK();     /*一个接口*/
};

2 修改引用的Makefile
这里暂时先用gecko/dom/interfaces/base/Makefile.in
在XPIDLSRCS 变量上加上 nsIDOMHiGecko.idl

3 编译(这步可以先不做,只是可以先编译下,提前看下生成的接口是不是想要的,也可以放到 第6步 编译gecko 一起做)

编译成功后可以查看一下objdir-gecko-cosb2g/dist/include/nsIDOMHiGecko.h文件,里面定义了相应的C++类接口,在它最后的注释里还给出了如何写对应的c++实现



4 编写实现类
实现类有两种实现方式都可以,一种继承安全接口,可以控制哪些接口和属性可以在js里访问,一种不继承安全接口,不具有上面的功能

a)先介绍第二种,不继承安全接口
定义实现类文件nsDOMHiGecko.h, nsDOMHiGecko.cpp
*********nsDOMHiGecko.h的内容************
#ifndef nsDOMHiGecko_h_
#define nsDOMHiGecko_h_
#include "nsIDOMHiGecko.h" 

class nsDOMHiGecko : public nsIDOMHiGecko
{
public:
    NS_DECL_ISUPPORTS //声明com引用计数接口
    NS_DECL_NSIDOMHIGECKO // 声明hiGecko的接口
    nsDOMHiGecko();

protected:
    ~nsDOMHiGecko();

private:
    int32_t mValue;  // hiGecko的属性,用来保存idl里的myval
};
#endif

********nsDOMHiGecko.cpp的内容************
#include "nsDOMHiGecko.h" 
#include "nsDOMClassInfoID.h" 

nsDOMHiGecko::nsDOMHiGecko()
: mValue(0)
{
}

nsDOMHiGecko::~nsDOMHiGecko()
{
}

DOMCI_DATA(DOMHiGecko, nsDOMHiGecko)
NS_INTERFACE_MAP_BEGIN(nsDOMHiGecko)
  NS_INTERFACE_MAP_ENTRY(nsISupports) // 用于com接口查询
  NS_INTERFACE_MAP_ENTRY(nsIDOMHiGecko) // 用于nsIDOMHiGecko接口查询
  NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(DOMHiGecko) // 用于classinfo接口查询
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(nsDOMHiGecko)
NS_IMPL_RELEASE(nsDOMHiGecko)

NS_IMETHODIMP nsDOMHiGecko::GetIsOK(bool *_retval)
{
    *_retval = true;

    return NS_OK;
}

NS_IMETHODIMP nsDOMHiGecko::GetMyval(int32_t *aMyval)
{
    *aMyval = mValue;
    return NS_OK;
}

NS_IMETHODIMP nsDOMHiGecko::SetMyval(int32_t aMyval)
{
    mValue = aMyval;
    return NS_OK;
}

b)继承安全接口的实现方式
头文件
#ifndef nsDOMHiGecko_h_
#define nsDOMHiGecko_h_
#include "nsIDOMHiGecko.h" 
#include "nsISecurityCheckedComponent.h" 

class nsDOMHiGecko : public nsIDOMHiGecko
                      ,public nsISecurityCheckedComponent
{
public:
    NS_DECL_ISUPPORTS
    NS_DECL_NSIDOMHIGECKO
    NS_DECL_NSISECURITYCHECKEDCOMPONENT
    nsDOMHiGecko();

protected:
    ~nsDOMHiGecko();
private:
    int32_t mValue;  // hiGecko的属性,用来保存idl里的myval
};

#endif


cpp文件
#include "nsDOMHiGecko.h" 
#include "nsDOMClassInfoID.h" 

nsDOMHiGecko::nsDOMHiGecko()
{
}

nsDOMHiGecko::~nsDOMHiGecko()
{
}

NS_INTERFACE_MAP_BEGIN(nsDOMHiGecko)
  NS_INTERFACE_MAP_ENTRY(nsIDOMHiGecko)
  NS_INTERFACE_MAP_ENTRY(nsISecurityCheckedComponent)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMHiGecko)
NS_INTERFACE_MAP_END

NS_IMPL_ADDREF(nsDOMHiGecko)
NS_IMPL_RELEASE(nsDOMHiGecko)

NS_IMETHODIMP nsDOMHiGecko::GetIsOK(bool *_retval)
{
    *_retval = true;
    return NS_OK;
}

static char* cloneAllAccess()
{
  static const char allAccess[] = "AllAccess";
  return (char*)nsMemory::Clone(allAccess, sizeof(allAccess));
}

static char* cloneUniversalXPConnect()
{
  static const char universalXPConnect[] = "UniversalXPConnect";
  return (char*)nsMemory::Clone(universalXPConnect, sizeof(universalXPConnect));
}

NS_IMETHODIMP nsDOMHiGecko::GetMyval(int32_t *aMyval)
{
    *aMyval = mValue;
    return NS_OK;
}

NS_IMETHODIMP nsDOMHiGecko::SetMyval(int32_t aMyval)
{
    mValue = aMyval;
    return NS_OK;
}

NS_IMETHODIMP
nsDOMHiGecko::CanCreateWrapper(const nsIID * iid, char **_retval)
{
  *_retval = cloneAllAccess();
  return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}

NS_IMETHODIMP //这个接口可以控制哪些方法可以在js里访问,这里用的是"allAccess"权限
nsDOMHiGecko::Ca