本文的内容主要参考网上收集的资料,不过在Android 4.0 webkit上做扩展时,碰到一些问题,觉得有必要记录下来。
所谓扩展JavaScript对象,就是增加一个JS对象,但它并没有定义在标准的JS对象集合中。如果网页中包含了扩展的JS对象,使用普通的浏览器就会报JS错误。
下面以添加HelloObject对象为例说明具体步骤,该对象具有description属性:
1. 添加HelloObject.h, HelloObject.cpp, HelloObject.idl文件,简单起见,将这三个文件放到Source/WebCore/page目录下。
#ifndef HelloObject_h
#define HelloObject_h#include <wtf/PassRefPtr.h>
#include <wtf/RefCounted.h>#include "PlatformString.h"
namespace WebCore {
class HelloObject : public RefCounted<HelloObject> {
public:
static PassRefPtr<HelloObject> create() { return adoptRef(new HelloObject()); }String description() const;
private:
HelloObject();
};} // namespace WebCore
#endif // HelloObject_h
HelloObject.h
#include "config.h"
#include "HelloObject.h"namespace WebCore {
HelloObject::HelloObject()
{
}String HelloObject::description() const
{
return "Hello Object";
}} // namespace WebCore
HelloObject.cpp
module window {
interface [OmitConstructor] HelloObject {
readonly attribute DOMString description;
};}
HelloObject.idl
2. 修改Source/WebCore/page/下的DOMWindow.h文件,添加如下声明:
class HelloObject;
…
public:
HelloObject* helloObject() const;
HelloObject* optionalHelloObject() const { return m_helloObject.get(); }
private:
mutable RefPtr<HelloObject> m_helloObject;
3. 修改Source/WebCore/page/下的DOMWindow.cpp文件,添加接口实现:
HelloObject* DOMWindow::helloObject() const
{
if (!m_helloObject)
m_helloObject = HelloObject::create();
return m_helloObject.get();
}
在DOMWindow::clear()函数中添加一行语句:
m_helloObject = 0;
4. 修改DOMWindow.idl文件,添加:
attribute [Replaceable] HelloObject helloObject;
5. 接下来需要修改编译系统,让android编译系统编译新增的文件:
首先修改Source/WebCore/Android.mk,增加page/HelloObject.cpp到LOCAL_SRC_FILES变量,其次需要修改Source/WebCore/Android.derived.v8bindings.mk,增加$(intermediates)/bindings/V8HelloObject.h到GEN变量。(注:这个是必须的,否则就不会根据HelloObject.idl生成V8HelloObject.h文件,在编译时会出错,这也是折腾了半天得出的成果)
至此,工作基本上完成,待webkit重新编译后,可以用如下的网页进行验证:
<html>
<body>
<script type="text/javascript">
document.write("<p> This is from HelloObject: ");
document.write(helloObject.description + "</p>");
</script>
</body>
</html>
[参考资料]
1. webkit的js对象扩展(一)——binding方式创建自定义对象(单实例)
2. android 上 webkit js 本地扩展之全局本地对象实现步骤
。。。
JSDOMWindow.cpp:210:22: error: HelloObject.h: No such file or directory怎么解决
HelloObject.h文件有没有添加到Source/WebCore/page/ ?
public:
HelloObject* helloObject() const;
HelloObject* optionalHelloObject() const { return m_helloObject.get(); }
private:
mutable RefPtr m_helloObject;
请问这个加在Domwindows.h的哪里呢,前后要不要加上
#if ENABLE(HelloOjbect)
#endif
呢?
只要是放在类定义里面,都可以,不需要加上ENABLE(HelloOjbect)
4>C:WebKitWebKit-r114801WebKitBuildDebugobjWebCoreDerivedSourcesJSDOMWindow.cpp(179) : fatal error C1083: Cannot open include file: ‘JSHelloObject.h’: No such file or directory
怎么解决呢?
5>C:WebKit-r114801WebKitBuildDebugobjWebCoreDerivedSourcesJSDOMWindow.cpp(1300) : error C2665: ‘WebCore::toJS’ : none of the 137 overloads could convert all the argument types
froyo/out/target/product/obj/STATIC_LIBRARIES/libwebcore_intermediates/WebCore/bindings/V8DOMWindow.cpp:148: undefined reference to `WebCore::toV8(WebCore::HelloObject*)’
collect2: ld returned 1 exit status
请问这个是什么问题呢, 头文件我看都是有的;
module window {
interface [OmitConstructor] HelloObject {
readonly attribute DOMString description;
};
}
报错MIDL2025
DOMWindow.h:446: error: ‘m_iSTB’ was not declared in this scope
DOMWindow.h: At global scope:
DOMWindow.h:531: error: non-member ‘m_iSTB’ cannot be declared ‘mutable’
DOMWindow.h: At global scope:
DOMWindow.h:531: error: non-member ‘m_iSTB’ cannot be declared ‘mutable’
make: *** [out/libwebcore_intermediates/Source/WebCore/dom/CompositionEvent.o] Error 1
make: *** Waiting for unfinished jobs….
make: *** [out/Source/WebCore/dom/DeviceMotionController.o] Error 1
In file included from external/webkit/Source/WebCore/bindings/generic/BindingSecurityBase.cpp:34:
DOMWindow.h: In member function ‘WebCore::ISTB* WebCore::DOMWindow::optionalISTB() const’:
DOMWindow.h:446: error: ‘m_iSTB’ was not declared in this scope
DOMWindow.h: At global scope:
DOMWindow.h:531: error: non-member ‘m_iSTB’ cannot be declared ‘mutable’
make: *** [out/libwebcore_intermediates/Source/WebCore/bindings/generic/BindingSecurityBase.o] Error 1
In file included from external/webkit/Source/WebCore/dom/Document.cpp:50:
DOMWindow.h: In member function ‘WebCore::ISTB* WebCore::DOMWindow::optionalISTB() const’:
page/DOMWindow.h:446: error: ‘m_iSTB’ was not declared in this scope
page/DOMWindow.h: At global scope:
WebCore/page/DOMWindow.h:531: error: non-member ‘m_iSTB’ cannot be declared ‘mutable’
make: *** [out/libwebcore_intermediates/Source/WebCore/dom/Document.o] Error 1
我现在是按照你文章做的。只是换了个名字。可编译不过~~~
真牛,最近进浏览器组,总能搜到你的文章,而且都是收藏+反复看的!!!
请教下高人,webkit里面可以扩展hml标签么? 看到有人扩展通过修改渲染引擎扩展了html标签,是怎么实现的?