forked from JeffreyZksun/HTML5UI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQJsCppBridge.cpp
More file actions
59 lines (49 loc) · 1.49 KB
/
QJsCppBridge.cpp
File metadata and controls
59 lines (49 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include "stdafx.h"
#include <QtWebkit/QWebFrame>
#include "QJsCppBridge.h"
#include "QJsApiHandler.h"
#include "QJsApiProxy.h"
#include "debug/moc_QJsCppBridge.cpp"
QJsCppBridge::QJsCppBridge(QWebFrame *pWebFrame) : m_pWebFrame(pWebFrame)
{
if(m_pWebFrame)
{
// Listen to the javaScriptWindowObjectCleared signal.
// This step is important because WebKit is going to clear all the JavaScript objects when loading a new page,
// so you need to add back the bridge every time:
QObject::connect(m_pWebFrame, SIGNAL(javaScriptWindowObjectCleared()), this, SLOT(bridge_javaScriptWindowObjectCleared()));
QObject::connect(m_pWebFrame, SIGNAL(loadFinished(bool)), this, SLOT(bridge_loadFinished(bool)));
}
m_pJsApiHandler = new QJsApiHandler();
m_pJsApiProxy = new QJsApiProxy(pWebFrame);
}
QJsCppBridge::~QJsCppBridge()
{
m_pWebFrame = NULL;
if(m_pJsApiHandler)
{
delete m_pJsApiHandler;
m_pJsApiHandler = NULL;
}
if(m_pJsApiProxy)
{
delete m_pJsApiProxy;
m_pJsApiProxy = NULL;
}
}
// The jsHandler is exposed as a global object in the Java script.
// It can be used as var result = jsHandler.onEvent("parameter data");
void QJsCppBridge::bridge_javaScriptWindowObjectCleared()
{
if(m_pWebFrame)
{
m_pWebFrame->addToJavaScriptWindowObject("jsHandler", m_pJsApiHandler);
}
}
void QJsCppBridge::bridge_loadFinished(bool b)
{
if(m_pJsApiProxy)
{
QString result = m_pJsApiProxy->onEvent("Initialization completes!");
}
}