这一篇主要介绍在window下添加新对象,网上也有一些文章介绍如何添加,文章有些老了,我试了好多次才成功,这里记录下来用于备忘。
一、Source/WebCore/page下添加新文件
- HorkyWebObject.h
#ifndef Horky_WebObject_h
#define Horky_WebObject_h
#include "DOMWindowProperty.h"
#include "ScriptWrappable.h"
#include <wtf/Forward.h>
#include <wtf/PassRefPtr.h>
#include <wtf/RefCounted.h>
#include <wtf/RefPtr.h>
#include <wtf/text/WTFString.h>
namespace WebCore {
class HorkyWebObject : public ScriptWrappable,
public RefCounted<HorkyWebObject>,
public DOMWindowProperty {
public:
static PassRefPtr<HorkyWebObject> create(Frame* frame) {
return adoptRef(new HorkyWebObject(frame));
}
String description() const;
//~HorkyWebObject();
private:
HorkyWebObject(Frame* frame);
};
} // namespace WebCore
#endif // Horky_WebObject_h
- HorkyWebObject.cpp
#include "config.h"
#include "HorkyWebObject.h"
namespace WebCore {
HorkyWebObject::HorkyWebObject(Frame* frame): DOMWindowProperty(frame) {
// construction
}
// HorkyWebObject::~HorkyWebObject() {
// // destruction
// }
String HorkyWebObject::description() const {
return "Hello World!";
}
} // namespace WebCore
- HorkyWebObject.idl
interface HorkyWebObject {
readonly attribute DOMString description;
};
这里主要参考了网上的文章,但是这里的坑点是需要继承DOMWindowProperty,具体为什么可以参考通过idl文件生成的JSC binding,里面有用到vtable。
二、修改DOMWindow
- DOMWindow.h
public:
...
HorkyWebObject* horkyWebObject() const;
...
private:
...
mutable RefPtr<HorkyWebObject> m_horkyWebObject;
...
- DOMWindow.cpp
void DOMWindow::resetDOMWindowProperties() {
...
m_sessionStorage = 0;
m_localStorage = 0;
m_applicationCache = 0;
+ m_horkyWebObject = 0;
}
HorkyWebObject* DOMWindow::horkyWebObject()const
{
if (!m_horkyWebObject)
m_horkyWebObject = HorkyWebObject::create(m_frame);
return m_horkyWebObject.get();
}
- DOMWindow.idl
[Replaceable] attribute HorkyWebObject horkyWebObject;
三、修改编译文件
- 修改Source/WebCore/DerivedSources.cpp
#include "JSNavigator.cpp"
+#include "JSHorkyWebObject.cpp"
#include "JSNode.cpp"
注意前面的+号。
- Source/WebCore/DerivedSources.pri
$$PWD/page/Navigator.idl \
+ $$PWD/page/HorkyWebObject.idl \
$$PWD/page/Performance.idl \
- /Source/WebCore/Target.pri
page/Navigator.cpp \
+ page/HorkyWebObject.cpp \
page/NavigatorBase.cpp \
注意这里(Phantomjs)编译使用的是qmake和make,所以修改的qt工程文件;如果使用cmake编译的话需要修改相应的cmake文件。
网友评论