Phantomjs中QtWebKit通过JS Binding添加

2018-07-18  本文已影响0人  kingbhy

这一篇主要介绍在window下添加新对象,网上也有一些文章介绍如何添加,文章有些老了,我试了好多次才成功,这里记录下来用于备忘。


一、Source/WebCore/page下添加新文件
  1. 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
  1. 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
  1. HorkyWebObject.idl

interface HorkyWebObject {
    readonly attribute DOMString description;
};

这里主要参考了网上的文章,但是这里的坑点是需要继承DOMWindowProperty,具体为什么可以参考通过idl文件生成的JSC binding,里面有用到vtable。

二、修改DOMWindow

  1. DOMWindow.h

public:
    ...
    HorkyWebObject* horkyWebObject() const;
    ...

private:
    ...
    mutable RefPtr<HorkyWebObject> m_horkyWebObject;
    ...

  1. 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();
}
  1. DOMWindow.idl
 [Replaceable] attribute HorkyWebObject horkyWebObject;

三、修改编译文件

  1. 修改Source/WebCore/DerivedSources.cpp
 #include "JSNavigator.cpp"
+#include "JSHorkyWebObject.cpp"
 #include "JSNode.cpp"

注意前面的+号。

  1. Source/WebCore/DerivedSources.pri
  $$PWD/page/Navigator.idl \
+ $$PWD/page/HorkyWebObject.idl \
  $$PWD/page/Performance.idl \
  1. /Source/WebCore/Target.pri
  page/Navigator.cpp \
+ page/HorkyWebObject.cpp \
  page/NavigatorBase.cpp \

注意这里(Phantomjs)编译使用的是qmake和make,所以修改的qt工程文件;如果使用cmake编译的话需要修改相应的cmake文件。

上一篇下一篇

猜你喜欢

热点阅读