#include "nsISupports.idl"
[scriptable, uuid(9285aaa6-19fd-4fdb-8621-916f856c024f)]
interface nsIDOMHiGecko : nsISupports
{
attribute long myval ; /*可读可写的属性*/
boolean getIsOK(); /*一个接口*/
};
3 编译(这步可以先不做,只是可以先编译下,提前看下生成的接口是不是想要的,也可以放到 第6步 编译gecko 一起做)
编译成功后可以查看一下objdir-gecko-cosb2g/dist/include/nsIDOMHiGecko.h文件,里面定义了相应的C++类接口,在它最后的注释里还给出了如何写对应的c++实现
*********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
#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;
}
#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
#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::CanCallMethod(const nsIID * iid, const PRUnichar *methodName,
char **_retval)
{
// OK if you're cool enough
*_retval = cloneAllAccess();
return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP //这个接口可以控制哪些属性可以在js里读取,这里用的是"allAccess"权限
nsDOMHiGecko::CanGetProperty(const nsIID * iid,
const PRUnichar *propertyName,
char **_retval)
{
// OK if you're cool enough
*_retval = cloneAllAccess();
return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
NS_IMETHODIMP //这个接口可以控制哪些属性可以在js里写入,这里用的是"allAccess"权限
nsDOMHiGecko::CanSetProperty(const nsIID * iid,
const PRUnichar *propertyName,
char **_retval)
{
// OK if you're cool enough
*_retval = cloneAllAccess();
return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
}
a. 在nsIDOMWindow.idl里定义hiGecko属性
readonly attribute nsIDOMHiGecko hiGecko;
nsRefPtr<nsDOMHiGecko> mHiGecko;
class nsDOMHiGecko;
#include "nsDOMHiGecko.h"
mHiGecko = nullptr;
NS_IMETHODIMP
nsGlobalWindow::GetHiGecko(nsIDOMHiGecko * *aHiGecko)
{
FORWARD_TO_OUTER(GetHiGecko, (aHiGecko), NS_ERROR_NOT_INITIALIZED);
if (!mHiGecko) {
mHiGecko = new nsDOMHiGecko();
if (!mHiGecko) {
return NS_ERROR_OUT_OF_MEMORY;
}
}
NS_ADDREF(*aHiGecko = mHiGecko);
return NS_OK;
}
nsCOMPtr<nsIDOMHiGecko> mHiGecko; // 注意这里用的是接口类,而不是实现类
class nsIDOMHiGecko;
#include "nsDOMHiGecko.h" // 这里也是实现类
在nsGlobalWindow类的方法CleanUp里加上
mHiGecko = nullptr;
NS_IMETHODIMP
nsGlobalWindow::GetHiGecko(nsIDOMHiGecko * *aHiGecko)
{
FORWARD_TO_OUTER(GetHiGecko, (aHiGecko), NS_ERROR_NOT_INITIALIZED);
if (!mHiGecko) {
nsresult rv;
mHiGecko = do_CreateInstance(NS_DOMHIGECKO_CONTRACTID, &rv);
NS_ENSURE_SUCCESS(rv, rv);
}
NS_ADDREF(*aHiGecko = mHiGecko);
return NS_OK;
}
#include "nsDOMHiGecko.h"
// {5851d0eb-becd-4857-af8e-a78578c2c36f}
#define NS_DOMHIGECKO_CID \
{0x5851d0eb, 0xbecd, 0x4857, {0xaf, 0x8e, 0xa7, 0x85, 0x78, 0xc2, 0xc3, 0x6f}}
#define NS_DOMHIGECKO_CONTRACTID \
"@mozilla.org/higecko;1"
DOMCI_CLASS(DOMHiGecko)
NS_DEFINE_CLASSINFO_DATA(DOMHiGecko, nsDOMGenericSH, DOM_DEFAULT_SCRIPTABLE_FLAGS)
NS_ENSURE_TRUE(cx, NS_ERROR_FAILURE);
DOM_CLASSINFO_MAP_BEGIN(DOMHiGecko, nsIDOMHiGecko)
DOM_CLASSINFO_MAP_ENTRY(nsIDOMHiGecko)
DOM_CLASSINFO_MAP_END
static jsid sDOMHiGecko_id;
f. 在Makefile里添加实现类
这里先加到gecko/dom/base/Makefile.in<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" http-equiv="pragma">
<title>Cut The Rope</title>
</head>
<body>
<script>
window.self.console.log("************ok************");
window.hiGecko.myval = 34;
window.console.log(window.hiGecko.myval);
window.console.log(window.hiGecko.getIsOK());
</script>
</body>
</html>