Skip to content

Commit 7a11ff0

Browse files
committedMar 22, 2021
initial commit
0 parents  commit 7a11ff0

39 files changed

+3232
-0
lines changed
 

‎.clang-format

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
BasedOnStyle: Mozilla
3+
ColumnLimit: 120
4+
AlignAfterOpenBracket: Align
5+
BinPackArguments: false
6+
SortIncludes: false
7+
FixNamespaceComments: true # add commend at end:
8+
NamespaceIndentation: None #intend content of namespace

‎.editorconfig

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# To learn more about .editorconfig see https://aka.ms/editorconfigdocs
2+
3+
# All files
4+
[*.{c,cpp,cxx,h,hpp,hxx}]
5+
indent_style = space
6+
charset = utf-8-bom
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.build.*
2+
dep/cef
3+
output

‎.gitmodules

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[submodule "CefViewCore"]
2+
path = CefViewCore
3+
url = https://github.com/CefView/CefViewCore

‎CMakeLists.txt

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#
2+
# The main config file for QCefView
3+
#
4+
cmake_minimum_required(VERSION 3.4.1)
5+
project(QCefView)
6+
7+
# Only generate Debug and Release configuration types.
8+
set(CMAKE_CONFIGURATION_TYPES Debug Release)
9+
# Set proejct architecture
10+
set(PROJECT_ARCH x86_64)
11+
12+
# Use folders in the resulting project files.
13+
set_property(GLOBAL PROPERTY OS_FOLDERS ON)
14+
15+
# C standard
16+
set(CMAKE_C_STANDARD_REQUIRED ON)
17+
set(CMAKE_C_STANDARD 11)
18+
19+
# C++ standard
20+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
21+
set(CMAKE_CXX_STANDARD 11)
22+
23+
if("${CMAKE_SYSTEM_NAME}" STREQUAL "Darwin")
24+
set(OS_MACOS 1)
25+
set(OS_POSIX 1)
26+
add_definitions(-DOS_MACOS=1 -DOS_POSIX=1)
27+
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Linux")
28+
set(OS_LINUX 1)
29+
set(OS_POSIX 1)
30+
add_definitions(-DOS_LINUX=1 -DOS_POSIX=1)
31+
elseif("${CMAKE_SYSTEM_NAME}" STREQUAL "Windows")
32+
set(OS_WINDOWS 1)
33+
add_definitions(-DOS_WINDOWS=1)
34+
endif()
35+
36+
# Config the QT package
37+
###############################################################
38+
set(QT_SDK_DIR "$ENV{QTDIR}")
39+
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} ${QT_SDK_DIR})
40+
###############################################################
41+
42+
# Config the CEF
43+
###############################################################
44+
# Disable the sandbox
45+
if ((NOT DEFINED USE_SANDBOX) OR (USE_SANDBOX STREQUAL "")
46+
OR (${USE_SANDBOX} MATCHES "(FALSE|false|0|OFF)"))
47+
option(USE_SANDBOX "Enable CEF Sandbox" OFF)
48+
elseif(${USE_SANDBOX} MATCHES "(TRUE|true|1|null|ON)")
49+
option(USE_SANDBOX "Enable CEF Sandbox" ON)
50+
else()
51+
message(FATAL_ERROR "++++++++++ INVALID FLAG USE_SANDBOX=" ${USE_SANDBOX})
52+
endif()
53+
###############################################################
54+
55+
if (OS_WINDOWS)
56+
add_link_options(/DEBUG)
57+
endif()
58+
59+
if (OS_MACOS)
60+
set(CMAKE_OSX_DEPLOYMENT_TARGET 10.9)
61+
endif()
62+
63+
set(CMAKE_SUPPRESS_REGENERATION TRUE)
64+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/output/$<CONFIG>/bin)
65+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/output/$<CONFIG>/bin)
66+
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/output/$<CONFIG>/lib)
67+
68+
# Include CefViewCore
69+
add_subdirectory(CefViewCore)
70+
71+
# Config QCefView target
72+
###############################################################
73+
add_subdirectory(src)
74+
75+
# Config the Demo project
76+
###############################################################
77+
if ((NOT DEFINED BUILD_DEMO) OR (BUILD_DEMO STREQUAL "")
78+
OR (${BUILD_DEMO} MATCHES "(FALSE|false|0|OFF)"))
79+
option(BUILD_DEMO "Build the demo" OFF)
80+
elseif(${BUILD_DEMO} MATCHES "(TRUE|true|1|null|ON)")
81+
option(BUILD_DEMO "Build the demo" ON)
82+
else()
83+
message(FATAL_ERROR "++++++++++ INVALID FLAG BUILD_DEMO=" ${BUILD_DEMO})
84+
endif()
85+
if (BUILD_DEMO)
86+
add_subdirectory(test/QCefViewTest)
87+
endif()
88+
###############################################################

‎CefViewCore

Submodule CefViewCore added at ece306b

‎generate-mac-proj.sh

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cmake -G "Xcode" -S . -B .build.mac -DBUILD_DEMO=ON -DUSE_SANDBOX=ON

‎generate-win-proj.bat

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cmake -G "Visual Studio 16 2019" -A Win32 -S . -B .build.win -DBUILD_DEMO=ON

‎include/QCefEvent.h

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#ifndef QCEFEVENT_H
2+
#define QCEFEVENT_H
3+
#pragma once
4+
#include <QCefView_global.h>
5+
6+
#pragma region qt_headers
7+
#include <QObject>
8+
#include <QString>
9+
#pragma endregion qt_headers
10+
11+
/// <summary>
12+
///
13+
/// </summary>
14+
class QCEFVIEW_EXPORT QCefEvent : public QObject
15+
{
16+
Q_OBJECT
17+
18+
public:
19+
/// <summary>
20+
///
21+
/// </summary>
22+
QCefEvent();
23+
24+
/// <summary>
25+
///
26+
/// </summary>
27+
/// <param name="name"></param>
28+
QCefEvent(const QString& name);
29+
30+
/// <summary>
31+
///
32+
/// </summary>
33+
/// <param name="name"></param>
34+
void setEventName(const QString& name);
35+
36+
/// <summary>
37+
///
38+
/// </summary>
39+
/// <param name="key"></param>
40+
/// <param name="value"></param>
41+
void setIntProperty(const QString& key, int value);
42+
43+
/// <summary>
44+
///
45+
/// </summary>
46+
/// <param name="key"></param>
47+
/// <param name="value"></param>
48+
void setDoubleProperty(const QString& key, double value);
49+
50+
/// <summary>
51+
///
52+
/// </summary>
53+
/// <param name="key"></param>
54+
/// <param name="value"></param>
55+
void setStringProperty(const QString& key, const QString& value);
56+
57+
/// <summary>
58+
///
59+
/// </summary>
60+
/// <param name="key"></param>
61+
/// <param name="value"></param>
62+
void setBoolProperty(const QString& key, bool value);
63+
};
64+
65+
#endif // QCEFEVENT_H

‎include/QCefQuery.h

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#ifndef QCEFQUERY_H
2+
#define QCEFQUERY_H
3+
#pragma once
4+
#include <QCefView_global.h>
5+
6+
#pragma region std_headers
7+
#include <cstdint>
8+
#pragma endregion std_headers
9+
10+
#pragma region qt_headers
11+
#include <QString>
12+
#include <QPointer>
13+
#include <QMetaType>
14+
#pragma endregion qt_headers
15+
16+
/// <summary>
17+
///
18+
/// </summary>
19+
class QCEFVIEW_EXPORT QCefQuery : public QObject
20+
{
21+
Q_OBJECT
22+
23+
public:
24+
/// <summary>
25+
///
26+
/// </summary>
27+
QCefQuery();
28+
29+
/// <summary>
30+
///
31+
/// </summary>
32+
/// <param name="req"></param>
33+
/// <param name="query"></param>
34+
QCefQuery(const QString& req, const int64_t query);
35+
36+
/// <summary>
37+
///
38+
/// </summary>
39+
/// <param name="other"></param>
40+
QCefQuery(const QCefQuery& other);
41+
42+
/// <summary>
43+
///
44+
/// </summary>
45+
/// <param name="other"></param>
46+
/// <returns></returns>
47+
QCefQuery& operator=(const QCefQuery& other);
48+
49+
/// <summary>
50+
///
51+
/// </summary>
52+
~QCefQuery();
53+
54+
/// <summary>
55+
///
56+
/// </summary>
57+
/// <returns></returns>
58+
const QString reqeust() const;
59+
60+
/// <summary>
61+
///
62+
/// </summary>
63+
/// <returns></returns>
64+
const int64_t id() const;
65+
66+
/// <summary>
67+
///
68+
/// </summary>
69+
/// <returns></returns>
70+
const QString response() const;
71+
72+
/// <summary>
73+
///
74+
/// </summary>
75+
/// <returns></returns>
76+
const bool result() const;
77+
78+
/// <summary>
79+
///
80+
/// </summary>
81+
/// <returns></returns>
82+
const int error() const;
83+
84+
/// <summary>
85+
///
86+
/// </summary>
87+
/// <param name="success"></param>
88+
/// <param name="response"></param>
89+
/// <param name="error"></param>
90+
void setResponseResult(bool success, const QString& response, int error = 0) const;
91+
92+
private:
93+
/// <summary>
94+
///
95+
/// </summary>
96+
int64_t id_;
97+
98+
/// <summary>
99+
///
100+
/// </summary>
101+
QString reqeust_;
102+
103+
/// <summary>
104+
///
105+
/// </summary>
106+
mutable QString response_;
107+
108+
/// <summary>
109+
///
110+
/// </summary>
111+
mutable bool restult_;
112+
113+
/// <summary>
114+
///
115+
/// </summary>
116+
mutable int error_;
117+
118+
///// <summary>
119+
/////
120+
///// </summary>
121+
// static int TYPEID;
122+
};
123+
124+
// Q_DECLARE_METATYPE(QCefQuery);
125+
126+
#endif // QCEFQUERY_H

‎include/QCefSetting.h

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
#ifndef QCEFSETTINGS_H
2+
#define QCEFSETTINGS_H
3+
#pragma once
4+
#include <QCefView_global.h>
5+
6+
#pragma region qt_headers
7+
#include <QString>
8+
#include <QMetaType>
9+
#pragma endregion qt_headers
10+
11+
namespace QCefSetting {
12+
/// <summary>
13+
///
14+
/// </summary>
15+
void QCEFVIEW_EXPORT
16+
setBrowserSubProcessPath(const QString& path);
17+
18+
/// <summary>
19+
///
20+
/// </summary>
21+
const QCEFVIEW_EXPORT QString
22+
browserSubProcessPath();
23+
24+
/// <summary>
25+
///
26+
/// </summary>
27+
void QCEFVIEW_EXPORT
28+
setResourceDirectoryPath(const QString& path);
29+
30+
/// <summary>
31+
///
32+
/// </summary>
33+
const QCEFVIEW_EXPORT QString
34+
resourceDirectoryPath();
35+
36+
/// <summary>
37+
///
38+
/// </summary>
39+
void QCEFVIEW_EXPORT
40+
setLocalesDirectoryPath(const QString& path);
41+
42+
/// <summary>
43+
///
44+
/// </summary>
45+
const QCEFVIEW_EXPORT QString
46+
localesDirectoryPath();
47+
48+
/// <summary>
49+
///
50+
/// </summary>
51+
void QCEFVIEW_EXPORT
52+
setUserAgent(const QString& agent);
53+
54+
/// <summary>
55+
///
56+
/// </summary>
57+
const QCEFVIEW_EXPORT QString
58+
userAgent();
59+
60+
/// <summary>
61+
///
62+
/// </summary>
63+
void QCEFVIEW_EXPORT
64+
setCachePath(const QString& path);
65+
66+
/// <summary>
67+
///
68+
/// </summary>
69+
const QCEFVIEW_EXPORT QString
70+
cachePath();
71+
72+
/// <summary>
73+
///
74+
/// </summary>
75+
void QCEFVIEW_EXPORT
76+
setUserDataPath(const QString& path);
77+
78+
/// <summary>
79+
///
80+
/// </summary>
81+
const QCEFVIEW_EXPORT QString
82+
userDataPath();
83+
84+
/// <summary>
85+
///
86+
/// </summary>
87+
void QCEFVIEW_EXPORT
88+
setBridgeObjectName(const QString& name);
89+
90+
/// <summary>
91+
///
92+
/// </summary>
93+
const QCEFVIEW_EXPORT QString
94+
bridgeObjectName();
95+
96+
/// <summary>
97+
///
98+
/// </summary>
99+
void QCEFVIEW_EXPORT
100+
setPersistSessionCookies(bool enabled);
101+
102+
/// <summary>
103+
///
104+
/// </summary>
105+
const QCEFVIEW_EXPORT bool
106+
persistSessionCookies();
107+
108+
/// <summary>
109+
///
110+
/// </summary>
111+
void QCEFVIEW_EXPORT
112+
setPersistUserPreferences(bool enabled);
113+
114+
/// <summary>
115+
///
116+
/// </summary>
117+
const QCEFVIEW_EXPORT bool
118+
persistUserPreferences();
119+
120+
/// <summary>
121+
///
122+
/// </summary>
123+
void QCEFVIEW_EXPORT
124+
setLocale(const QString& locale);
125+
126+
/// <summary>
127+
///
128+
/// </summary>
129+
const QCEFVIEW_EXPORT QString
130+
locale();
131+
132+
/// <summary>
133+
///
134+
/// </summary>
135+
void QCEFVIEW_EXPORT
136+
setRemoteDebuggingPort(int port);
137+
138+
/// <summary>
139+
///
140+
/// </summary>
141+
const QCEFVIEW_EXPORT int
142+
remoteDebuggingPort();
143+
144+
/// <summary>
145+
///
146+
/// </summary>
147+
void QCEFVIEW_EXPORT
148+
setBackgroundColor(const QColor& color);
149+
150+
/// <summary>
151+
///
152+
/// </summary>
153+
const QCEFVIEW_EXPORT QColor
154+
backgroundColor();
155+
156+
/// <summary>
157+
///
158+
/// </summary>
159+
void QCEFVIEW_EXPORT
160+
setAcceptLanguageList(const QString& languages);
161+
162+
/// <summary>
163+
///
164+
/// </summary>
165+
const QCEFVIEW_EXPORT QString
166+
acceptLanguageList();
167+
168+
/// <summary>
169+
///
170+
/// </summary>
171+
void QCEFVIEW_EXPORT
172+
setGlobalCookie(const QString& name, const QString& value, const QString& domain, const QString& url);
173+
174+
}; // namespace QCefSetting
175+
176+
#endif

‎include/QCefView.h

+309
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,309 @@
1+
#ifndef QCEFVIEW_H
2+
#define QCEFVIEW_H
3+
#pragma once
4+
#include <QCefView_global.h>
5+
6+
#pragma region std_headers
7+
#include <memory>
8+
#pragma endregion std_headers
9+
10+
#pragma region qt_headers
11+
#include <QWidget>
12+
#include <QVariantList>
13+
#pragma endregion qt_headers
14+
15+
#include <QCefQuery.h>
16+
#include <QCefEvent.h>
17+
18+
/** Outline of QCefView:
19+
**
20+
** +--------------------------------------------------------------+
21+
** | QCefView(QWidget) |
22+
** | |
23+
** | +----------------------------------------------------+ |
24+
** | | WindowWrapper(QWidget) | |
25+
** | | | |
26+
** | | +-------------------------------------------+ | |
27+
** | | | CefWindow(QWindow) | | |
28+
** | | | | | |
29+
** | | | | | |
30+
** | | | | | |
31+
** | | +-------------------------------------------+ | |
32+
** | | | |
33+
** | +----------------------------------------------------+ |
34+
** | |
35+
** +--------------------------------------------------------------+
36+
**
37+
** Remarks:
38+
** The WindowWrapper and CefWindow are transparent to upper layer user.
39+
**
40+
**/
41+
42+
class CefContextMenuHandler;
43+
class CefDialogHandler;
44+
class CefDisplayHandler;
45+
class CefDownloadHandler;
46+
class CefJSDialogHandler;
47+
class CefKeyboardHandler;
48+
49+
/// <summary>
50+
///
51+
/// </summary>
52+
class QCEFVIEW_EXPORT QCefView : public QWidget
53+
{
54+
/// <summary>
55+
///
56+
/// </summary>
57+
Q_OBJECT
58+
59+
public:
60+
/// <summary>
61+
///
62+
/// </summary>
63+
QCefView(const QString url, QWidget* parent = 0);
64+
65+
/// <summary>
66+
///
67+
/// </summary>
68+
QCefView(QWidget* parent = 0);
69+
70+
/// <summary>
71+
///
72+
/// </summary>
73+
~QCefView();
74+
75+
/// <summary>
76+
///
77+
/// </summary>
78+
/// <param name="path"></param>
79+
/// <param name="url"></param>
80+
static void addLocalFolderResource(const QString& path, const QString& url, int priority = 0);
81+
82+
/// <summary>
83+
///
84+
/// </summary>
85+
/// <param name="path"></param>
86+
/// <param name="url"></param>
87+
/// <param name="password"></param>
88+
static void addArchiveResource(const QString& path, const QString& url, const QString& password = "");
89+
90+
/// <summary>
91+
///
92+
/// </summary>
93+
/// <param name="name"></param>
94+
/// <param name="value"></param>
95+
/// <param name="domain"></param>
96+
/// <param name="url"></param>
97+
void addCookie(const QString& name, const QString& value, const QString& domain, const QString& url);
98+
99+
/// <summary>
100+
/// Navigates to the content.
101+
/// </summary>
102+
/// <param name="content"></param>
103+
void navigateToString(const QString& content);
104+
105+
/// <summary>
106+
///
107+
/// </summary>
108+
/// <param name="url"></param>
109+
void navigateToUrl(const QString& url);
110+
111+
/// <summary>
112+
///
113+
/// </summary>
114+
/// <returns></returns>
115+
bool browserCanGoBack();
116+
117+
/// <summary>
118+
///
119+
/// </summary>
120+
/// <returns></returns>
121+
bool browserCanGoForward();
122+
123+
/// <summary>
124+
///
125+
/// </summary>
126+
void browserGoBack();
127+
128+
/// <summary>
129+
///
130+
/// </summary>
131+
void browserGoForward();
132+
133+
/// <summary>
134+
///
135+
/// </summary>
136+
/// <returns></returns>
137+
bool browserIsLoading();
138+
139+
/// <summary>
140+
///
141+
/// </summary>
142+
void browserReload();
143+
144+
/// <summary>
145+
///
146+
/// </summary>
147+
void browserStopLoad();
148+
149+
/// <summary>
150+
///
151+
/// </summary>
152+
/// <param name="name"></param>
153+
/// <param name="event"></param>
154+
/// <param name="frameId"></param>
155+
/// <returns></returns>
156+
bool triggerEvent(const QCefEvent& event);
157+
158+
/// <summary>
159+
///
160+
/// </summary>
161+
/// <param name="name"></param>
162+
/// <param name="event"></param>
163+
/// <param name="frameId"></param>
164+
/// <returns></returns>
165+
bool triggerEvent(const QCefEvent& event, int frameId);
166+
167+
/// <summary>
168+
///
169+
/// </summary>
170+
/// <param name="name"></param>
171+
/// <param name="event"></param>
172+
/// <returns></returns>
173+
bool broadcastEvent(const QCefEvent& event);
174+
175+
/// <summary>
176+
///
177+
/// </summary>
178+
/// <param name="query"></param>
179+
/// <returns></returns>
180+
bool responseQCefQuery(const QCefQuery& query);
181+
182+
/// <summary>
183+
///
184+
/// </summary>
185+
/// <param name="handler"></param>
186+
void setContextMenuHandler(CefContextMenuHandler* handler);
187+
188+
/// <summary>
189+
///
190+
/// </summary>
191+
/// <param name="handler"></param>
192+
void setDialogHandler(CefDialogHandler* handler);
193+
194+
/// <summary>
195+
///
196+
/// </summary>
197+
/// <param name="handler"></param>
198+
void setDisplayHandler(CefDisplayHandler* handler);
199+
200+
/// <summary>
201+
///
202+
/// </summary>
203+
/// <param name="handler"></param>
204+
void setDownloadHandler(CefDownloadHandler* handler);
205+
206+
/// <summary>
207+
///
208+
/// </summary>
209+
/// <param name="handler"></param>
210+
void setJSDialogHandler(CefJSDialogHandler* handler);
211+
212+
/// <summary>
213+
///
214+
/// </summary>
215+
/// <param name="handler"></param>
216+
void setKeyboardHandler(CefKeyboardHandler* handler);
217+
218+
/// <summary>
219+
///
220+
/// </summary>
221+
/// <param name="isLoading"></param>
222+
/// <param name="canGoBack"></param>
223+
/// <param name="canGoForward"></param>
224+
virtual void onLoadingStateChanged(bool isLoading, bool canGoBack, bool canGoForward);
225+
226+
/// <summary>
227+
///
228+
/// </summary>
229+
virtual void onLoadStart();
230+
231+
/// <summary>
232+
///
233+
/// </summary>
234+
/// <param name="httpStatusCode"></param>
235+
virtual void onLoadEnd(int httpStatusCode);
236+
237+
/// <summary>
238+
///
239+
/// </summary>
240+
/// <param name="errorCode"></param>
241+
/// <param name="errorMsg"></param>
242+
/// <param name="failedUrl"></param>
243+
/// <param name="handled"></param>
244+
virtual void onLoadError(int errorCode, const QString& errorMsg, const QString& failedUrl, bool& handled);
245+
246+
/// <summary>
247+
///
248+
/// </summary>
249+
/// <param name="region"></param>
250+
virtual void onDraggableRegionChanged(const QRegion& region);
251+
252+
/// <summary>
253+
///
254+
/// </summary>
255+
/// <param name="message"></param>
256+
/// <param name="level"></param>
257+
virtual void onConsoleMessage(const QString& message, int level);
258+
259+
/// <summary>
260+
///
261+
/// </summary>
262+
/// <param name="next"></param>
263+
virtual void onTakeFocus(bool next);
264+
265+
/// <summary>
266+
///
267+
/// </summary>
268+
/// <param name="url"></param>
269+
virtual void onQCefUrlRequest(const QString& url);
270+
271+
/// <summary>
272+
///
273+
/// </summary>
274+
/// <param name="query"></param>
275+
virtual void onQCefQueryRequest(const QCefQuery& query);
276+
277+
/// <summary>
278+
///
279+
/// </summary>
280+
/// <param name="browserId"></param>
281+
/// <param name="frameId"></param>
282+
/// <param name="method"></param>
283+
/// <param name="arguments"></param>
284+
virtual void onInvokeMethodNotify(int browserId, int frameId, const QString& method, const QVariantList& arguments);
285+
286+
protected:
287+
/// <summary>
288+
///
289+
/// </summary>
290+
/// <param name="event"></param>
291+
virtual void changeEvent(QEvent* event) override;
292+
293+
/// <summary>
294+
///
295+
/// </summary>
296+
/// <param name="watched"></param>
297+
/// <param name="event"></param>
298+
/// <returns></returns>
299+
virtual bool eventFilter(QObject* watched, QEvent* event) override;
300+
301+
private:
302+
/// <summary>
303+
///
304+
/// </summary>
305+
class Implementation;
306+
std::unique_ptr<Implementation> pImpl_;
307+
};
308+
309+
#endif // QCEFVIEW_H

‎include/QCefView_global.h

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef QCEFEVIEW_GLOBAL_H
2+
#define QCEFEVIEW_GLOBAL_H
3+
#pragma once
4+
5+
#pragma region qt_headers
6+
#include <QtCore/qglobal.h>
7+
#pragma endregion qt_headers
8+
9+
#ifdef QCEFVIEW_LIB
10+
#define QCEFVIEW_EXPORT Q_DECL_EXPORT
11+
#else
12+
#define QCEFVIEW_EXPORT Q_DECL_IMPORT
13+
#if _WIN32
14+
#pragma comment(lib, "QCefView.lib")
15+
#endif
16+
#endif
17+
18+
#endif // QCEFEVIEW_GLOBAL_H

‎src/CMakeLists.txt

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
### QCefView
2+
################################################################################################
3+
set(CMAKE_GLOBAL_AUTOGEN_TARGET OFF)
4+
set(CMAKE_AUTOMOC ON)
5+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
6+
find_package(Qt5 COMPONENTS Core GUI Widgets REQUIRED)
7+
8+
file(GLOB_RECURSE QCefView_INCLUDE_FILES
9+
"${CMAKE_CURRENT_SOURCE_DIR}/../include/*.h"
10+
)
11+
source_group(
12+
TREE "${CMAKE_CURRENT_SOURCE_DIR}/../include"
13+
PREFIX Include
14+
FILES ${QCefView_INCLUDE_FILES}
15+
)
16+
17+
file(GLOB_RECURSE QCefView_SRC_FILES
18+
"${CMAKE_CURRENT_SOURCE_DIR}/*.h"
19+
"${CMAKE_CURRENT_SOURCE_DIR}/*.cpp"
20+
)
21+
source_group(
22+
TREE ${CMAKE_CURRENT_SOURCE_DIR}
23+
PREFIX Source
24+
FILES ${QCefView_SRC_FILES}
25+
)
26+
27+
if (OS_WINDOWS)
28+
file(GLOB_RECURSE QCefView_windows_SRC_FILES
29+
"${CMAKE_CURRENT_SOURCE_DIR}/win/*.h"
30+
"${CMAKE_CURRENT_SOURCE_DIR}/win/*.cpp"
31+
)
32+
source_group(
33+
TREE "${CMAKE_CURRENT_SOURCE_DIR}/win"
34+
PREFIX Source
35+
FILES ${QCefView_windows_SRC_FILES}
36+
)
37+
38+
add_library(QCefView SHARED
39+
${QCefView_INCLUDE_FILES}
40+
${QCefView_SRC_FILES}
41+
${QCefView_windows_SRC_FILES}
42+
)
43+
44+
target_compile_definitions(QCefView PRIVATE
45+
UNICODE
46+
_UNICODE
47+
QCEFVIEW_LIB
48+
)
49+
endif() # OS_WINDOWS
50+
51+
if (OS_MACOS)
52+
file(GLOB_RECURSE QCefView_macOS_SRC_FILES
53+
"${CMAKE_CURRENT_SOURCE_DIR}/mac/*.h"
54+
"${CMAKE_CURRENT_SOURCE_DIR}/mac/*.cpp"
55+
"${CMAKE_CURRENT_SOURCE_DIR}/mac/*.mm"
56+
)
57+
source_group(
58+
TREE "${CMAKE_CURRENT_SOURCE_DIR}/mac"
59+
PREFIX Source
60+
FILES ${QCefView_macOS_SRC_FILES}
61+
)
62+
set(QCefView_INFO_PLIST_FILE "${CMAKE_CURRENT_LIST_DIR}/mac/Info.plist")
63+
64+
add_library(QCefView SHARED
65+
${QCefView_INCLUDE_FILES}
66+
${QCefView_SRC_FILES}
67+
${QCefView_macOS_SRC_FILES}
68+
${QCefView_INFO_PLIST_FILE}
69+
)
70+
71+
set_target_properties(QCefView
72+
PROPERTIES
73+
FRAMEWORK TRUE
74+
COMPILE_FLAGS "-fobjc-arc"
75+
PUBLIC_HEADER "${PerduCef_PUBLIC_HEADERS}"
76+
MACOSX_FRAMEWORK_INFO_PLIST "${PerduCef_INFO_PLIST_FILE}"
77+
MACOSX_FRAMEWORK_IDENTIFIER "com.perducef.perducefview"
78+
CLANG_ENABLE_OBJC_ARC "YES"
79+
XCODE_ATTRIBUTE_CLANG_CXX_LANGUAGE_STANDARD "gnu++11" # -std=gnu++11
80+
XCODE_ATTRIBUTE_CLANG_LINK_OBJC_RUNTIME "NO" # -fno-objc-link-runtime
81+
XCODE_ATTRIBUTE_COPY_PHASE_STRIP "NO"
82+
XCODE_ATTRIBUTE_DEAD_CODE_STRIPPING[variant=Release] "YES" # -Wl,-dead_strip
83+
XCODE_ATTRIBUTE_GCC_C_LANGUAGE_STANDARD "c99" # -std=c99
84+
)
85+
86+
add_custom_command(
87+
TARGET QCefView
88+
PRE_BUILD
89+
90+
#copy the cef framework to resource directory
91+
COMMAND ${CMAKE_COMMAND} -E copy_directory
92+
"$<TARGET_BUNDLE_DIR:QCefView>/../Chromium Embedded Framework.framework"
93+
"$<TARGET_FILE_DIR:QCefView>/Resources/Chromium Embedded Framework.framework"
94+
95+
#copy the cef framework to resource directory
96+
COMMAND ${CMAKE_COMMAND} -E copy_directory
97+
"$<TARGET_BUNDLE_DIR:QCefView>/../CefViewWing.app"
98+
"$<TARGET_FILE_DIR:QCefView>/Resources/CefViewWing.app"
99+
VERBATIM
100+
)
101+
endif() # OS_MACOS
102+
103+
target_include_directories(QCefView PRIVATE
104+
"${CMAKE_CURRENT_SOURCE_DIR}/../include"
105+
${CefViewCore_EXPORT_INCLUDE_PATH}
106+
)
107+
108+
add_dependencies(QCefView
109+
CefViewCore
110+
)
111+
112+
target_link_libraries(QCefView PRIVATE
113+
Qt5::Core
114+
Qt5::Gui
115+
Qt5::Widgets
116+
CefViewCore
117+
)
118+

‎src/QCefEvent.cpp

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <QCefEvent.h>
2+
3+
#pragma region qt_headers
4+
#include <QVariant>
5+
#pragma endregion qt_headers
6+
7+
#pragma region cef_headers
8+
#include <include/cef_app.h>
9+
#include <include/wrapper/cef_message_router.h>
10+
#pragma endregion cef_headers
11+
12+
QCefEvent::QCefEvent()
13+
: QObject(nullptr)
14+
{}
15+
16+
QCefEvent::QCefEvent(const QString& name)
17+
: QObject()
18+
{
19+
setObjectName(name);
20+
}
21+
22+
void
23+
QCefEvent::setEventName(const QString& name)
24+
{
25+
setObjectName(name);
26+
}
27+
28+
void
29+
QCefEvent::setIntProperty(const QString& key, int value)
30+
{
31+
Q_ASSERT(0 != QString::compare(key, "name", Qt::CaseInsensitive));
32+
setProperty(key.toUtf8().constData(), QVariant::fromValue(value));
33+
}
34+
35+
void
36+
QCefEvent::setDoubleProperty(const QString& key, double value)
37+
{
38+
Q_ASSERT(0 != QString::compare(key, "name", Qt::CaseInsensitive));
39+
setProperty(key.toUtf8().constData(), QVariant::fromValue(value));
40+
}
41+
42+
void
43+
QCefEvent::setStringProperty(const QString& key, const QString& value)
44+
{
45+
Q_ASSERT(0 != QString::compare(key, "name", Qt::CaseInsensitive));
46+
setProperty(key.toUtf8().constData(), QVariant::fromValue(value));
47+
}
48+
49+
void
50+
QCefEvent::setBoolProperty(const QString& key, bool value)
51+
{
52+
Q_ASSERT(0 != QString::compare(key, "name", Qt::CaseInsensitive));
53+
setProperty(key.toUtf8().constData(), QVariant::fromValue(value));
54+
}

‎src/QCefQuery.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include <QCefQuery.h>
2+
3+
#pragma region cef_headers
4+
#include <include/cef_app.h>
5+
#include <include/wrapper/cef_message_router.h>
6+
#pragma endregion cef_headers
7+
8+
#include <QCefView.h>
9+
10+
// int QCefQuery::TYPEID = qRegisterMetaType<QCefQuery>("QCefQuery");
11+
12+
//////////////////////////////////////////////////////////////////////////
13+
14+
QCefQuery::QCefQuery()
15+
: id_(-1)
16+
, restult_(false)
17+
, error_(0)
18+
{}
19+
20+
QCefQuery::QCefQuery(const QString& req, const int64_t query)
21+
: reqeust_(req)
22+
, id_(query)
23+
, restult_(false)
24+
, error_(0)
25+
{}
26+
27+
QCefQuery::QCefQuery(const QCefQuery& other)
28+
{
29+
reqeust_ = other.reqeust_;
30+
id_ = other.id_;
31+
restult_ = other.restult_;
32+
response_ = other.response_;
33+
}
34+
35+
QCefQuery&
36+
QCefQuery::operator=(const QCefQuery& other)
37+
{
38+
reqeust_ = other.reqeust_;
39+
id_ = other.id_;
40+
restult_ = other.restult_;
41+
response_ = other.response_;
42+
return *this;
43+
}
44+
45+
QCefQuery::~QCefQuery() {}
46+
47+
const QString
48+
QCefQuery::reqeust() const
49+
{
50+
return reqeust_;
51+
}
52+
53+
const int64_t
54+
QCefQuery::id() const
55+
{
56+
return id_;
57+
}
58+
59+
const QString
60+
QCefQuery::response() const
61+
{
62+
return response_;
63+
}
64+
65+
const bool
66+
QCefQuery::result() const
67+
{
68+
return restult_;
69+
}
70+
71+
const int
72+
QCefQuery::error() const
73+
{
74+
return error_;
75+
}
76+
77+
void
78+
QCefQuery::setResponseResult(bool success, const QString& response, int error /*= 0*/) const
79+
{
80+
restult_ = success;
81+
response_ = response;
82+
error_ = error;
83+
}

‎src/QCefSetting.cpp

+197
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
#include "QCefSetting.h"
2+
3+
#pragma region qt_headers
4+
#include <QColor.h>
5+
#pragma endregion qt_headers
6+
7+
#include "details/CCefSetting.h"
8+
9+
void
10+
QCefSetting::setBrowserSubProcessPath(const QString& path)
11+
{
12+
CCefSetting::initializeInstance();
13+
CCefSetting::browser_sub_process_path.FromString(path.toStdString());
14+
}
15+
16+
const QString
17+
QCefSetting::browserSubProcessPath()
18+
{
19+
CCefSetting::initializeInstance();
20+
return QString::fromStdString(CCefSetting::browser_sub_process_path.ToString());
21+
}
22+
23+
void
24+
QCefSetting::setResourceDirectoryPath(const QString& path)
25+
{
26+
CCefSetting::initializeInstance();
27+
CCefSetting::resource_directory_path.FromString(path.toStdString());
28+
}
29+
30+
const QString
31+
QCefSetting::resourceDirectoryPath()
32+
{
33+
CCefSetting::initializeInstance();
34+
return QString::fromStdString(CCefSetting::resource_directory_path.ToString());
35+
}
36+
37+
void
38+
QCefSetting::setLocalesDirectoryPath(const QString& path)
39+
{
40+
CCefSetting::initializeInstance();
41+
CCefSetting::locales_directory_path.FromString(path.toStdString());
42+
}
43+
44+
const QString
45+
QCefSetting::localesDirectoryPath()
46+
{
47+
CCefSetting::initializeInstance();
48+
return QString::fromStdString(CCefSetting::locales_directory_path.ToString());
49+
}
50+
51+
void
52+
QCefSetting::setUserAgent(const QString& agent)
53+
{
54+
CCefSetting::initializeInstance();
55+
CCefSetting::user_agent.FromString(agent.toStdString());
56+
}
57+
58+
const QString
59+
QCefSetting::userAgent()
60+
{
61+
CCefSetting::initializeInstance();
62+
return QString::fromStdString(CCefSetting::user_agent.ToString());
63+
}
64+
65+
void
66+
QCefSetting::setCachePath(const QString& path)
67+
{
68+
CCefSetting::initializeInstance();
69+
CCefSetting::cache_path.FromString(path.toStdString());
70+
}
71+
72+
const QString
73+
QCefSetting::cachePath()
74+
{
75+
CCefSetting::initializeInstance();
76+
return QString::fromStdString(CCefSetting::cache_path.ToString());
77+
}
78+
79+
void
80+
QCefSetting::setUserDataPath(const QString& path)
81+
{
82+
CCefSetting::initializeInstance();
83+
CCefSetting::user_data_path.FromString(path.toStdString());
84+
}
85+
86+
const QString
87+
QCefSetting::userDataPath()
88+
{
89+
CCefSetting::initializeInstance();
90+
return QString::fromStdString(CCefSetting::user_data_path.ToString());
91+
}
92+
93+
void
94+
QCefSetting::setBridgeObjectName(const QString& name)
95+
{
96+
CCefSetting::initializeInstance();
97+
CCefSetting::bridge_object_name.FromString(name.toStdString());
98+
}
99+
100+
const QString
101+
QCefSetting::bridgeObjectName()
102+
{
103+
CCefSetting::initializeInstance();
104+
return QString::fromStdString(CCefSetting::bridge_object_name);
105+
}
106+
107+
void
108+
QCefSetting::setPersistSessionCookies(bool enabled)
109+
{
110+
CCefSetting::initializeInstance();
111+
CCefSetting::persist_session_cookies = enabled ? true : false;
112+
}
113+
114+
const bool
115+
QCefSetting::persistSessionCookies()
116+
{
117+
CCefSetting::initializeInstance();
118+
return CCefSetting::persist_session_cookies ? true : false;
119+
}
120+
121+
void
122+
QCefSetting::setPersistUserPreferences(bool enabled)
123+
{
124+
CCefSetting::initializeInstance();
125+
CCefSetting::persist_user_preferences = enabled ? true : false;
126+
}
127+
128+
const bool
129+
QCefSetting::persistUserPreferences()
130+
{
131+
CCefSetting::initializeInstance();
132+
return CCefSetting::persist_user_preferences ? true : false;
133+
}
134+
135+
void
136+
QCefSetting::setLocale(const QString& locale)
137+
{
138+
CCefSetting::initializeInstance();
139+
CCefSetting::locale.FromString(locale.toStdString());
140+
}
141+
142+
const QString
143+
QCefSetting::locale()
144+
{
145+
CCefSetting::initializeInstance();
146+
return QString::fromStdString(CCefSetting::locale.ToString());
147+
}
148+
149+
void
150+
QCefSetting::setRemoteDebuggingPort(int port)
151+
{
152+
CCefSetting::initializeInstance();
153+
CCefSetting::remote_debugging_port = port;
154+
}
155+
156+
const int
157+
QCefSetting::remoteDebuggingPort()
158+
{
159+
CCefSetting::initializeInstance();
160+
return CCefSetting::remote_debugging_port;
161+
}
162+
163+
void
164+
QCefSetting::setBackgroundColor(const QColor& color)
165+
{
166+
CCefSetting::initializeInstance();
167+
CCefSetting::background_color = static_cast<cef_color_t>(color.rgba());
168+
}
169+
170+
const QColor
171+
QCefSetting::backgroundColor()
172+
{
173+
CCefSetting::initializeInstance();
174+
return QColor::fromRgba(static_cast<QRgb>(CCefSetting::background_color));
175+
}
176+
177+
void
178+
QCefSetting::setAcceptLanguageList(const QString& languages)
179+
{
180+
CCefSetting::initializeInstance();
181+
CCefSetting::accept_language_list.FromString(languages.toStdString());
182+
}
183+
184+
const QString
185+
QCefSetting::acceptLanguageList()
186+
{
187+
CCefSetting::initializeInstance();
188+
return QString::fromStdString(CCefSetting::accept_language_list.ToString());
189+
}
190+
191+
void QCEFVIEW_EXPORT
192+
QCefSetting::setGlobalCookie(const QString& name, const QString& value, const QString& domain, const QString& url)
193+
{
194+
CCefSetting::initializeInstance();
195+
CCefSetting::global_cookie_list.push_back(
196+
{ name.toStdString(), value.toStdString(), domain.toStdString(), url.toStdString() });
197+
}

‎src/QCefView.cpp

+593
Large diffs are not rendered by default.

‎src/details/CCefDelegate.cpp

+137
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
#include "CCefDelegate.h"
2+
3+
CCefDelegate::CCefDelegate(QCefView* view, QCefWindow* window)
4+
: view_(view)
5+
, window_(window)
6+
{}
7+
8+
void
9+
CCefDelegate::setCefBrowserWindowHandle(CefWindowHandle win)
10+
{
11+
if (window_)
12+
window_->setCefBrowserWindow(win);
13+
}
14+
15+
void
16+
CCefDelegate::loadingStateChanged(bool isLoading, bool canGoBack, bool canGoForward)
17+
{
18+
if (view_)
19+
view_->onLoadingStateChanged(isLoading, canGoBack, canGoForward);
20+
}
21+
22+
void
23+
CCefDelegate::loadStart()
24+
{
25+
if (view_)
26+
view_->onLoadStart();
27+
}
28+
29+
void
30+
CCefDelegate::loadEnd(int httpStatusCode)
31+
{
32+
if (view_)
33+
view_->onLoadEnd(httpStatusCode);
34+
}
35+
36+
void
37+
CCefDelegate::loadError(int errorCode, const std::string& errorMsg, const std::string& failedUrl, bool& handled)
38+
{
39+
if (view_) {
40+
auto msg = QString::fromStdString(errorMsg);
41+
auto url = QString::fromStdString(failedUrl);
42+
view_->onLoadError(errorCode, msg, url, handled);
43+
}
44+
}
45+
46+
void
47+
CCefDelegate::draggableRegionChanged(const std::vector<CefDraggableRegion>& regions)
48+
{
49+
if (view_) {
50+
// Determine new draggable region.
51+
QRegion region;
52+
std::vector<CefDraggableRegion>::const_iterator it = regions.begin();
53+
for (; it != regions.end(); ++it) {
54+
region += QRegion(it->bounds.x, it->bounds.y, it->bounds.width, it->bounds.height);
55+
}
56+
view_->onDraggableRegionChanged(region);
57+
}
58+
}
59+
60+
void
61+
CCefDelegate::consoleMessage(const std::string& message, int level)
62+
{
63+
if (view_) {
64+
auto msg = QString::fromStdString(message);
65+
view_->onConsoleMessage(msg, level);
66+
}
67+
}
68+
69+
void
70+
CCefDelegate::takeFocus(bool next)
71+
{
72+
if (view_)
73+
view_->onTakeFocus(next);
74+
}
75+
76+
void
77+
CCefDelegate::processUrlRequest(const std::string& url)
78+
{
79+
if (view_) {
80+
auto u = QString::fromStdString(url);
81+
view_->onQCefUrlRequest(u);
82+
}
83+
}
84+
85+
void
86+
CCefDelegate::processQueryRequest(const std::string& request, const int64_t query_id)
87+
{
88+
if (view_) {
89+
auto req = QString::fromStdString(request);
90+
view_->onQCefQueryRequest(QCefQuery(req, query_id));
91+
}
92+
}
93+
94+
void
95+
CCefDelegate::invokeMethodNotify(int browserId,
96+
int frameId,
97+
const std::string& method,
98+
const CefRefPtr<CefListValue>& arguments)
99+
{
100+
if (!view_)
101+
return;
102+
103+
auto m = QString::fromStdString(method);
104+
105+
QVariantList argumentList;
106+
QString qStr;
107+
for (int idx = 0; idx < arguments->GetSize(); idx++) {
108+
if (CefValueType::VTYPE_NULL == arguments->GetType(idx))
109+
argumentList.push_back(0);
110+
else if (CefValueType::VTYPE_BOOL == arguments->GetType(idx))
111+
argumentList.push_back(QVariant::fromValue(arguments->GetBool(idx)));
112+
else if (CefValueType::VTYPE_INT == arguments->GetType(idx))
113+
argumentList.push_back(QVariant::fromValue(arguments->GetInt(idx)));
114+
else if (CefValueType::VTYPE_DOUBLE == arguments->GetType(idx))
115+
argumentList.push_back(QVariant::fromValue(arguments->GetDouble(idx)));
116+
else if (CefValueType::VTYPE_STRING == arguments->GetType(idx)) {
117+
#if defined(CEF_STRING_TYPE_UTF16)
118+
qStr = QString::fromUtf16((char16_t*)arguments->GetString(idx).c_str());
119+
#elif defined(CEF_STRING_TYPE_UTF8)
120+
qStr = QString::fromUtf8(arguments->GetString(idx).c_str());
121+
#elif defined(CEF_STRING_TYPE_WIDE)
122+
qStr = QString::fromWCharArray(arguments->GetString(idx).c_str());
123+
#endif
124+
argumentList.push_back(qStr);
125+
} else {
126+
// invalid type
127+
}
128+
}
129+
130+
view_->onInvokeMethodNotify(browserId, frameId, m, argumentList);
131+
}
132+
133+
void
134+
CCefDelegate::browserIsDestroying()
135+
{
136+
return;
137+
}

‎src/details/CCefDelegate.h

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
#pragma once
2+
3+
#pragma region qt_headers
4+
#include <QWindow>
5+
#include <QPointer>
6+
#include <QVariant>
7+
#include <QHash>
8+
#include <QMutex>
9+
#include <QRegion>
10+
#pragma endregion qt_headers
11+
12+
#pragma region std_headers
13+
#include <memory>
14+
#pragma endregion std_headers
15+
16+
#pragma region cef_headers
17+
#include <include/cef_app.h>
18+
#pragma endregion cef_headers
19+
20+
#include <CefViewBrowserDelegate.h>
21+
22+
#include <QCefQuery.h>
23+
#include <QCefView.h>
24+
25+
#include "QCefWindow.h"
26+
27+
class CCefDelegate
28+
: public CefViewBrowserDelegateInterface
29+
, public std::enable_shared_from_this<CCefDelegate>
30+
{
31+
public:
32+
explicit CCefDelegate(QCefView* view, QCefWindow* window);
33+
34+
virtual void setCefBrowserWindowHandle(CefWindowHandle win) override;
35+
36+
virtual void loadingStateChanged(bool isLoading, bool canGoBack, bool canGoForward) override;
37+
38+
virtual void loadStart() override;
39+
40+
virtual void loadEnd(int httpStatusCode) override;
41+
42+
virtual void loadError(int errorCode,
43+
const std::string& errorMsg,
44+
const std::string& failedUrl,
45+
bool& handled) override;
46+
47+
virtual void draggableRegionChanged(const std::vector<CefDraggableRegion>& regions) override;
48+
49+
virtual void consoleMessage(const std::string& message, int level) override;
50+
51+
virtual void takeFocus(bool next) override;
52+
53+
virtual void processUrlRequest(const std::string& url) override;
54+
55+
virtual void processQueryRequest(const std::string& query, const int64_t query_id) override;
56+
57+
virtual void invokeMethodNotify(int browserId,
58+
int frameId,
59+
const std::string& method,
60+
const CefRefPtr<CefListValue>& arguments) override;
61+
62+
virtual void browserIsDestroying() override;
63+
64+
private:
65+
QCefView* view_;
66+
QCefWindow* window_;
67+
};

‎src/details/CCefManager.cpp

+102
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
#include "CCefManager.h"
2+
3+
#include "CCefSetting.h"
4+
5+
CCefManager::CCefManager()
6+
{
7+
nBrowserRefCount_ = 0;
8+
}
9+
10+
CCefManager&
11+
CCefManager::getInstance()
12+
{
13+
static CCefManager s_instance;
14+
return s_instance;
15+
}
16+
17+
void
18+
CCefManager::initializeCef()
19+
{
20+
// This is not the first time initialization
21+
if (++nBrowserRefCount_ > 1)
22+
return;
23+
24+
// Enable High-DPI support on Windows 7 or newer.
25+
CefEnableHighDPISupport();
26+
27+
// This is the first time initialization
28+
CCefSetting::initializeInstance();
29+
30+
CefString(&cef_settings_.browser_subprocess_path) = CCefSetting::browser_sub_process_path;
31+
CefString(&cef_settings_.resources_dir_path) = CCefSetting::resource_directory_path;
32+
CefString(&cef_settings_.locales_dir_path) = CCefSetting::locales_directory_path;
33+
CefString(&cef_settings_.user_agent) = CCefSetting::user_agent;
34+
CefString(&cef_settings_.cache_path) = CCefSetting::cache_path;
35+
CefString(&cef_settings_.user_data_path) = CCefSetting::user_data_path;
36+
CefString(&cef_settings_.locale) = CCefSetting::locale;
37+
CefString(&cef_settings_.accept_language_list) = CCefSetting::accept_language_list;
38+
39+
cef_settings_.persist_session_cookies = CCefSetting::persist_session_cookies;
40+
cef_settings_.persist_user_preferences = CCefSetting::persist_user_preferences;
41+
cef_settings_.remote_debugging_port = CCefSetting::remote_debugging_port;
42+
cef_settings_.background_color = CCefSetting::background_color;
43+
cef_settings_.no_sandbox = true;
44+
cef_settings_.pack_loading_disabled = false;
45+
cef_settings_.multi_threaded_message_loop = true;
46+
47+
#ifndef NDEBUG
48+
cef_settings_.log_severity = LOGSEVERITY_DEFAULT;
49+
cef_settings_.remote_debugging_port = CCefSetting::remote_debugging_port;
50+
#else
51+
cef_settings_.log_severity = LOGSEVERITY_DISABLE;
52+
#endif
53+
54+
app_ = new CefViewBrowserApp(CCefSetting::bridge_object_name);
55+
56+
#if defined(OS_WINDOWS)
57+
HINSTANCE hInstance = ::GetModuleHandle(nullptr);
58+
CefMainArgs main_args(hInstance);
59+
#else
60+
CefMainArgs main_args;
61+
#endif
62+
63+
// Initialize CEF.
64+
void* sandboxInfo = nullptr;
65+
if (!CefInitialize(main_args, cef_settings_, app_, sandboxInfo))
66+
assert(0);
67+
}
68+
69+
bool
70+
CCefManager::addCookie(const std::string& name,
71+
const std::string& value,
72+
const std::string& domain,
73+
const std::string& url)
74+
{
75+
CefCookie cookie;
76+
CefString(&cookie.name).FromString(name);
77+
CefString(&cookie.value).FromString(value);
78+
CefString(&cookie.domain).FromString(domain);
79+
return CefCookieManager::GetGlobalManager(nullptr)->SetCookie(CefString(url), cookie, nullptr);
80+
}
81+
82+
void
83+
CCefManager::uninitializeCef()
84+
{
85+
// This is not the last time release
86+
if (--nBrowserRefCount_ > 0)
87+
return;
88+
89+
// Destroy the application
90+
app_ = nullptr;
91+
92+
// The last time release
93+
// TO-DO (sheen) when we reach here, it is possible there are pending
94+
// IO requests, and they will fire the DCHECK when complete or aborted
95+
releaseCef();
96+
}
97+
98+
void
99+
CCefManager::releaseCef()
100+
{
101+
CefShutdown();
102+
}

‎src/details/CCefManager.h

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#pragma once
2+
3+
#pragma region std_headers
4+
#include <mutex>
5+
#pragma endregion std_headers
6+
7+
#include <CefViewBrowserApp.h>
8+
9+
/// <summary>
10+
///
11+
/// </summary>
12+
class CCefManager
13+
{
14+
15+
protected:
16+
/// <summary>
17+
///
18+
/// </summary>
19+
CCefManager();
20+
21+
/// <summary>
22+
///
23+
/// </summary>
24+
~CCefManager(){};
25+
26+
public:
27+
/// <summary>
28+
///
29+
/// </summary>
30+
/// <returns></returns>
31+
static CCefManager& getInstance();
32+
33+
/// <summary>
34+
///
35+
/// </summary>
36+
void initializeCef();
37+
38+
/// <summary>
39+
///
40+
/// </summary>
41+
/// <param name="name"></param>
42+
/// <param name="value"></param>
43+
/// <param name="domain"></param>
44+
/// <param name="url"></param>
45+
/// <returns></returns>
46+
bool addCookie(const std::string& name, const std::string& value, const std::string& domain, const std::string& url);
47+
48+
/// <summary>
49+
///
50+
/// </summary>
51+
void uninitializeCef();
52+
53+
protected:
54+
/// <summary>
55+
///
56+
/// </summary>
57+
void releaseCef();
58+
59+
private:
60+
/// <summary>
61+
///
62+
/// </summary>
63+
CefRefPtr<CefViewBrowserApp> app_;
64+
65+
/// <summary>
66+
///
67+
/// </summary>
68+
CefSettings cef_settings_;
69+
70+
/// <summary>
71+
///
72+
/// </summary>
73+
int64_t nBrowserRefCount_;
74+
};

‎src/details/CCefSetting.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include "CCefSetting.h"
2+
3+
#pragma region qt_headers
4+
#include <QCoreApplication>
5+
#include <QDir>
6+
#pragma endregion qt_headers
7+
8+
#include <CefViewCoreProtocol.h>
9+
10+
CefString CCefSetting::bridge_object_name;
11+
12+
CefString CCefSetting::browser_sub_process_path;
13+
14+
CefString CCefSetting::resource_directory_path;
15+
16+
CefString CCefSetting::locales_directory_path;
17+
18+
CefString CCefSetting::user_agent;
19+
20+
CefString CCefSetting::cache_path;
21+
22+
CefString CCefSetting::user_data_path;
23+
24+
int CCefSetting::persist_session_cookies;
25+
26+
int CCefSetting::persist_user_preferences;
27+
28+
CefString CCefSetting::locale;
29+
30+
int CCefSetting::remote_debugging_port = 9999;
31+
32+
cef_color_t CCefSetting::background_color;
33+
34+
CefString CCefSetting::accept_language_list;
35+
36+
std::list<CCefSetting::CookieItem> CCefSetting::global_cookie_list;
37+
38+
void
39+
CCefSetting::initializeInstance()
40+
{
41+
static CCefSetting s_instance;
42+
}

‎src/details/CCefSetting.h

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#pragma once
2+
3+
#pragma region stl_headers
4+
#include <list>
5+
#pragma endregion stl_headers
6+
7+
#pragma region cef_headers
8+
#include <include/cef_base.h>
9+
#pragma endregion cef_headers
10+
11+
/// <summary>
12+
///
13+
/// </summary>
14+
class CCefSetting
15+
{
16+
protected:
17+
/// <summary>
18+
///
19+
/// </summary>
20+
CCefSetting();
21+
22+
/// <summary>
23+
///
24+
/// </summary>
25+
~CCefSetting(){};
26+
27+
public:
28+
/// <summary>
29+
///
30+
/// </summary>
31+
static void initializeInstance();
32+
33+
public:
34+
/// <summary>
35+
///
36+
/// </summary>
37+
static CefString bridge_object_name;
38+
39+
/// <summary>
40+
///
41+
/// </summary>
42+
static CefString browser_sub_process_path;
43+
44+
/// <summary>
45+
///
46+
/// </summary>
47+
static CefString resource_directory_path;
48+
49+
/// <summary>
50+
///
51+
/// </summary>
52+
static CefString locales_directory_path;
53+
54+
/// <summary>
55+
///
56+
/// </summary>
57+
static CefString user_agent;
58+
59+
/// <summary>
60+
///
61+
/// </summary>
62+
static CefString cache_path;
63+
64+
/// <summary>
65+
///
66+
/// </summary>
67+
static CefString user_data_path;
68+
69+
/// <summary>
70+
///
71+
/// </summary>
72+
static int persist_session_cookies;
73+
74+
/// <summary>
75+
///
76+
/// </summary>
77+
static int persist_user_preferences;
78+
79+
/// <summary>
80+
///
81+
/// </summary>
82+
static CefString locale;
83+
84+
/// <summary>
85+
///
86+
/// </summary>
87+
static int remote_debugging_port;
88+
89+
/// <summary>
90+
///
91+
/// </summary>
92+
static cef_color_t background_color;
93+
94+
/// <summary>
95+
///
96+
/// </summary>
97+
static CefString accept_language_list;
98+
99+
/// <summary>
100+
///
101+
/// </summary>
102+
typedef struct CookieItem
103+
{
104+
std::string name;
105+
std::string value;
106+
std::string domain;
107+
std::string url;
108+
} CookieItem;
109+
static std::list<CookieItem> global_cookie_list;
110+
};

‎src/details/QCefWindow.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "QCefWindow.h"
2+
3+
#pragma region qt_headers
4+
#include <QCoreApplication>
5+
#include <QResizeEvent>
6+
#include <QPaintDevice>
7+
#include <QPainter>
8+
#include <QDebug>
9+
#pragma endregion qt_headers
10+
11+
#include "CCefManager.h"
12+
13+
QCefWindow::QCefWindow(QWindow* parent, QCefView* hostView /*= 0*/)
14+
: QWindow(parent)
15+
, hwndCefBrowser_(nullptr)
16+
{
17+
setFlags(Qt::FramelessWindowHint);
18+
19+
CCefManager::getInstance().initializeCef();
20+
}
21+
22+
QCefWindow::~QCefWindow()
23+
{
24+
if (hwndCefBrowser_)
25+
hwndCefBrowser_ = nullptr;
26+
27+
CCefManager::getInstance().uninitializeCef();
28+
}
29+
30+
void
31+
QCefWindow::setCefBrowserWindow(CefWindowHandle win)
32+
{
33+
hwndCefBrowser_ = win;
34+
syncCefBrowserWindow();
35+
}
36+
37+
void
38+
QCefWindow::syncCefBrowserWindow()
39+
{
40+
#if defined(OS_WINDOWS)
41+
double w = width() * devicePixelRatio();
42+
double h = height() * devicePixelRatio();
43+
if (hwndCefBrowser_)
44+
::SetWindowPos(hwndCefBrowser_, NULL, 0, 0, ceil(w), ceil(h), SWP_NOZORDER | SWP_NOSENDCHANGING | SWP_DEFERERASE);
45+
#else
46+
return;
47+
#endif
48+
}
49+
50+
void
51+
QCefWindow::exposeEvent(QExposeEvent* e)
52+
{
53+
syncCefBrowserWindow();
54+
QWindow::exposeEvent(e);
55+
}
56+
57+
void
58+
QCefWindow::resizeEvent(QResizeEvent* e)
59+
{
60+
syncCefBrowserWindow();
61+
QWindow::resizeEvent(e);
62+
}

‎src/details/QCefWindow.h

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#pragma once
2+
3+
#pragma region qt_headers
4+
#include <QWindow>
5+
#include <QPointer>
6+
#include <QVariant>
7+
#include <QHash>
8+
#include <QMutex>
9+
#include <QRegion>
10+
#pragma endregion qt_headers
11+
12+
#pragma region cef_headers
13+
#include <include/cef_app.h>
14+
#pragma endregion cef_headers
15+
16+
#include <CefViewBrowserDelegate.h>
17+
18+
#include <QCefQuery.h>
19+
#include <QCefView.h>
20+
21+
/// <summary>
22+
///
23+
/// </summary>
24+
class QCefWindow : public QWindow
25+
{
26+
Q_OBJECT
27+
28+
public:
29+
/// <summary>
30+
///
31+
/// </summary>
32+
33+
public:
34+
/// <summary>
35+
///
36+
/// </summary>
37+
/// <param name="parent"></param>
38+
explicit QCefWindow(QWindow* parent, QCefView* hostView = 0);
39+
40+
/// <summary>
41+
///
42+
/// </summary>
43+
~QCefWindow();
44+
45+
/// <summary>
46+
///
47+
/// </summary>
48+
/// <param name="win"></param>
49+
void setCefBrowserWindow(CefWindowHandle win);
50+
51+
protected:
52+
/// <summary>
53+
///
54+
/// </summary>
55+
void syncCefBrowserWindow();
56+
57+
/// <summary>
58+
///
59+
/// </summary>
60+
/// <param name="e"></param>
61+
virtual void exposeEvent(QExposeEvent* e);
62+
63+
/// <summary>
64+
///
65+
/// </summary>
66+
/// <param name="e"></param>
67+
virtual void resizeEvent(QResizeEvent* e);
68+
69+
private:
70+
/// <summary>
71+
///
72+
/// </summary>
73+
CefWindowHandle hwndCefBrowser_;
74+
};

‎src/mac/Info.plist

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>CFBundleDevelopmentRegion</key>
6+
<string>$(DEVELOPMENT_LANGUAGE)</string>
7+
<key>CFBundleExecutable</key>
8+
<string>$(EXECUTABLE_NAME)</string>
9+
<key>CFBundleIconFile</key>
10+
<string></string>
11+
<key>CFBundleIdentifier</key>
12+
<string>com.cefview.qcefview</string>
13+
<key>CFBundleInfoDictionaryVersion</key>
14+
<string>6.0</string>
15+
<key>CFBundleName</key>
16+
<string>$(PRODUCT_NAME)</string>
17+
<key>CFBundlePackageType</key>
18+
<string>$(PRODUCT_BUNDLE_PACKAGE_TYPE)</string>
19+
<key>CFBundleShortVersionString</key>
20+
<string>1.0</string>
21+
<key>CFBundleVersion</key>
22+
<string>1</string>
23+
<key>LSMinimumSystemVersion</key>
24+
<string>$(MACOSX_DEPLOYMENT_TARGET)</string>
25+
<key>NSHumanReadableCopyright</key>
26+
<string>Copyright © 2020 CefView. All rights reserved.</string>
27+
<key>LSUIElement</key>
28+
<string>1</string>
29+
<key>LSFileQuarantineEnabled</key>
30+
<true/>
31+
<key>NSSupportsAutomaticGraphicsSwitching</key>
32+
<true/>
33+
</dict>
34+
</plist>

‎src/mac/details/CCefSetting_mac.mm

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "details/CCefSetting.h"
2+
3+
#pragma region qt_headers
4+
#include <QCoreApplication>
5+
#include <QDir>
6+
#pragma endregion qt_headers
7+
8+
#include <CefViewCoreProtocol.h>
9+
10+
CCefSetting::CCefSetting()
11+
{
12+
user_agent.FromString(CEFVIEW_USER_AGENT);
13+
14+
QDir currentDir = QDir::current();
15+
16+
QString renderExecutablePath = currentDir.filePath(RENDER_PROCESS_NAME ".exe");
17+
browser_sub_process_path.FromString(QDir::toNativeSeparators(renderExecutablePath).toStdString());
18+
19+
QString resourceFolderPath = currentDir.filePath(RESOURCE_DIRECTORY_NAME);
20+
resource_directory_path.FromString(QDir::toNativeSeparators(resourceFolderPath).toStdString());
21+
22+
QString localeFolderPath = resourceFolderPath.filePath(LOCALES_DIRECTORY_NAME);
23+
locales_directory_path.FromString(QDir::toNativeSeparators(localeFolderPath).toStdString());
24+
}

‎src/win/details/CCefSetting_win.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "details/CCefSetting.h"
2+
3+
#pragma region qt_headers
4+
#include <QCoreApplication>
5+
#include <QDir>
6+
#pragma endregion qt_headers
7+
8+
#include <CefViewCoreProtocol.h>
9+
10+
CCefSetting::CCefSetting()
11+
{
12+
user_agent.FromString(CEFVIEW_USER_AGENT);
13+
14+
QDir ExeDir = QCoreApplication::applicationDirPath();
15+
16+
QString strExePath = ExeDir.filePath(RENDER_PROCESS_NAME);
17+
browser_sub_process_path.FromString(QDir::toNativeSeparators(strExePath).toStdString());
18+
19+
QString strResPath = ExeDir.filePath(RESOURCE_DIRECTORY_NAME);
20+
resource_directory_path.FromString(QDir::toNativeSeparators(strResPath).toStdString());
21+
22+
QDir ResPath(strResPath);
23+
locales_directory_path.FromString(QDir::toNativeSeparators(ResPath.filePath(LOCALES_DIRECTORY_NAME)).toStdString());
24+
}

‎test/QCefViewTest/CMakeLists.txt

+117
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
cmake_minimum_required(VERSION 3.4.1)
2+
project(QCefViewTest)
3+
4+
set(CMAKE_AUTOMOC ON)
5+
set(CMAKE_AUTOUIC ON)
6+
set(CMAKE_AUTORCC ON)
7+
8+
set(CMAKE_INCLUDE_CURRENT_DIR ON)
9+
find_package(Qt5 COMPONENTS Core GUI Widgets REQUIRED)
10+
11+
get_target_property(_qmake_executable Qt5::qmake IMPORTED_LOCATION)
12+
get_filename_component(_qt_bin_dir "${_qmake_executable}" DIRECTORY)
13+
14+
if (OS_WINDOWS)
15+
find_program(DEPLOYQT_EXECUTABLE windeployqt HINTS "${_qt_bin_dir}")
16+
elseif (OS_MACOS)
17+
find_program(DEPLOYQT_EXECUTABLE macdeployqt HINTS "${_qt_bin_dir}")
18+
elseif (OS_LINUX)
19+
else ()
20+
endif()
21+
22+
include_directories(
23+
${CMAKE_SOURCE_DIR}/include
24+
)
25+
26+
file(GLOB_RECURSE _SRC_FILES
27+
"*.h"
28+
"*.cpp"
29+
)
30+
31+
file(GLOB_RECURSE _UI_FILES
32+
"*.ui"
33+
)
34+
source_group("Form Files" ${_UI_FILES})
35+
36+
if (OS_WINDOWS)
37+
file(GLOB_RECURSE _RES_FILES
38+
"*.qrc"
39+
"*.rc"
40+
)
41+
source_group("Resource Files" ${_RES_FILES})
42+
43+
add_executable(${PROJECT_NAME} WIN32
44+
${_SRC_FILES}
45+
${_UI_FILES}
46+
${_RES_FILES}
47+
)
48+
49+
target_compile_definitions(${PROJECT_NAME} PRIVATE
50+
UNICODE
51+
_UNICODE
52+
)
53+
54+
set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
55+
PROPERTY
56+
VS_STARTUP_PROJECT ${PROJECT_NAME}
57+
)
58+
59+
set_property(TARGET ${PROJECT_NAME}
60+
PROPERTY
61+
FOLDER "Test"
62+
VS_DEBUGGER_WORKING_DIRECTORY "$<TARGET_FILE_DIR:${PROJECT_NAME}>"
63+
)
64+
65+
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
66+
# Embed the manifest file into the target
67+
COMMAND mt.exe -manifest \"${CMAKE_CURRENT_SOURCE_DIR}\\${PROJECT_NAME}.manifest\" -inputresource:\"$<TARGET_FILE:${PROJECT_NAME}>\" -outputresource:\"$<TARGET_FILE:${PROJECT_NAME}>\"
68+
# Copy the TEST HTML file
69+
COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_SOURCE_DIR}/QCefViewTestPage.html $<TARGET_FILE_DIR:${PROJECT_NAME}>
70+
# Deploy the Qt Application
71+
COMMAND ${DEPLOYQT_EXECUTABLE}
72+
--no-svg
73+
--no-translations
74+
--no-compiler-runtime
75+
$<TARGET_FILE:${PROJECT_NAME}>
76+
)
77+
endif()
78+
79+
if (OS_MACOS)
80+
file(GLOB_RECURSE _RES_FILES
81+
"*.qrc"
82+
)
83+
source_group("Resource Files" ${_RES_FILES})
84+
85+
add_executable(${PROJECT_NAME} MACOSX_BUNDLE
86+
${_SRC_FILES}
87+
${_UI_FILES}
88+
${_RES_FILES}
89+
)
90+
91+
set_property(TARGET ${PROJECT_NAME}
92+
PROPERTY
93+
FOLDER "Test"
94+
)
95+
96+
add_custom_command(TARGET ${PROJECT_NAME} PRE_BUILD
97+
# Copy the TEST HTML file
98+
COMMAND ${CMAKE_COMMAND} -E copy_if_different
99+
"${CMAKE_CURRENT_SOURCE_DIR}/QCefViewTestPage.html"
100+
"$<TARGET_BUNDLE_CONTENT_DIR:${PROJECT_NAME}>/Resources/QCefViewTestPage.html"
101+
)
102+
103+
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
104+
# Deploy the Qt Application
105+
COMMAND ${DEPLOYQT_EXECUTABLE}
106+
$<TARGET_BUNDLE_DIR:${PROJECT_NAME}>
107+
-codesign="-"
108+
109+
)
110+
endif()
111+
112+
target_link_libraries(${PROJECT_NAME} PUBLIC
113+
Qt5::Core
114+
Qt5::Gui
115+
Qt5::Widgets
116+
QCefView
117+
)
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
2+
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3">
3+
<description>QCefViewTest Applicatoin</description>
4+
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
5+
<security>
6+
<requestedPrivileges>
7+
<requestedExecutionLevel
8+
level="asInvoker"
9+
uiAccess="false"
10+
/>
11+
</requestedPrivileges>
12+
</security>
13+
</trustInfo>
14+
<compatibility xmlns="urn:schemas-microsoft-com:compatibility.v1">
15+
<application>
16+
<!-- Windows 10 -->
17+
<supportedOS Id="{8e0f7a12-bfb3-4fe8-b9a5-48fd50a15a9a}"/>
18+
<!-- Windows 8.1 -->
19+
<supportedOS Id="{1f676c76-80e1-4239-95bb-83d0f6d0da78}"/>
20+
<!-- Windows Vista -->
21+
<supportedOS Id="{e2011457-1546-43c5-a5fe-008deee3d3f0}"/>
22+
<!-- Windows 7 -->
23+
<supportedOS Id="{35138b9a-5d96-4fbd-8e2d-a2440225f93a}"/>
24+
<!-- Windows 8 -->
25+
<supportedOS Id="{4a2f28e3-53b9-4441-ba9c-d69d4a4a6e38}"/>
26+
</application>
27+
</compatibility>
28+
</assembly>
+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<html>
2+
3+
<head>
4+
<script>
5+
function onColorChanged(event) {
6+
var kvlist = [];
7+
for (var key in event) {
8+
kvlist.push(key + ":" + event[key])
9+
}
10+
alert(kvlist.join('\n'));
11+
12+
document.getElementById("main").style.backgroundColor = event["color"];
13+
}
14+
15+
function onLoad() {
16+
if (typeof (CallBridge) == 'undefined') {
17+
return;
18+
}
19+
20+
CallBridge.addEventListener("colorChange", function (event) {
21+
var kvlist = [];
22+
for (var key in event) {
23+
kvlist.push(key + ":" + event[key])
24+
}
25+
//alert(kvlist.join('\n'));
26+
27+
document.getElementById("main").style.backgroundColor = event["color"];
28+
})
29+
}
30+
31+
function onDragAreaMouseDown() {
32+
CallBridge.invokeMethod("onDragAreaMouseDown");
33+
}
34+
35+
function onInvokeMethodClicked() {
36+
CallBridge.invokeMethod("TestMethod", 1, false, "arg3");
37+
}
38+
39+
function onCallBridgeQueryClicked() {
40+
var query = {
41+
request: document.getElementById("message").value,
42+
onSuccess: function (response) {
43+
alert(response);
44+
},
45+
onFailure: function (error_code, error_message) {
46+
alert(error_message);
47+
}
48+
}
49+
window.CefViewQuery(query);
50+
}
51+
52+
53+
/**
54+
* getFuntionName()
55+
* @param {Function} func
56+
* @return {String}
57+
*/
58+
function getFunctionName(func) {
59+
if (typeof func == 'function' || typeof func == 'object') {
60+
var name = ('' + func).match(/function\s*([\w\$]*)\s*\(/);
61+
}
62+
return name && name[1];
63+
}
64+
/**
65+
* trace
66+
* @param [int] [count=10]
67+
* @return {String}
68+
*/
69+
trace = function () {
70+
var stack = [],
71+
caller = arguments.callee.caller;
72+
73+
while (caller) {
74+
stack.unshift(getFunctionName(caller));
75+
caller = caller && caller.caller;
76+
}
77+
return 'functions on stack:' + '\n' + stack.join('\n');
78+
}
79+
80+
function OpenPopup() {
81+
var popup = window.open("", "PopupWindow", "width=800, height=600");
82+
popup.document.write("<p>This is popup window.</p>");
83+
}
84+
</script>
85+
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>
86+
<style>
87+
.noselect {
88+
user-select: none;
89+
/* Non-prefixed version, currently supported by Chrome, Edge, Opera and Firefox */
90+
}
91+
</style>
92+
</head>
93+
94+
<body onload="onLoad()" id="main" class="noselect">
95+
<h1 align="center" style="font-size:12pt; font-family:MS Shell Dlg 2;">Web Area</h1>
96+
<div id="firstVue">
97+
<input type="number" v-model.number="my_step" />
98+
<button v-on:click="clickButton">Add</button>
99+
<p>{{my_total}}</p>
100+
</div>
101+
<div align="center">
102+
<div style="background-color:green; -webkit-app-region:drag;" onmousedown="onDragAreaMouseDown()">
103+
<h1>Dragging area</h1>
104+
</div>
105+
<br />
106+
107+
<label> Test Case for InvokeMethod </label>
108+
<br />
109+
<input type="button" value="Invoke Method" onclick="onInvokeMethodClicked()" />
110+
<br />
111+
<br />
112+
113+
<label> Test Case for QCefQuery </label>
114+
<br />
115+
<textarea id="message"
116+
style="width:320px; height:120px;">this message will be processed by native code.</textarea>
117+
<br />
118+
<input type="button" value="Query" onclick="onCallBridgeQueryClicked()" />
119+
<br />
120+
<br />
121+
</div>
122+
</body>
123+
<script type="text/javascript">
124+
var myVue = new Vue({
125+
el: '#firstVue',
126+
data: {
127+
my_step: 1,
128+
my_total: 0
129+
},
130+
methods: {
131+
clickButton: function () {
132+
this.my_total = this.my_total + this.my_step
133+
}
134+
}
135+
})
136+
</script>
137+
138+
</html>

‎test/QCefViewTest/customcefview.cpp

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#if defined(OS_WINDOWS)
2+
#include <windows.h>
3+
#else
4+
#endif
5+
6+
#include <QMessageBox>
7+
#include <QColor>
8+
#include <QRandomGenerator>
9+
10+
#include "customcefview.h"
11+
12+
CustomCefView::~CustomCefView() {}
13+
14+
void
15+
CustomCefView::changeColor()
16+
{
17+
QColor color(QRandomGenerator::global()->generate());
18+
19+
QCefEvent event("colorChange");
20+
event.setStringProperty("color", color.name());
21+
broadcastEvent(event);
22+
}
23+
24+
void
25+
CustomCefView::onDraggableRegionChanged(const QRegion& region)
26+
{}
27+
28+
void
29+
CustomCefView::onQCefUrlRequest(const QString& url)
30+
{
31+
QMetaObject::invokeMethod(
32+
this,
33+
[=]() {
34+
QString title("QCef URL Request");
35+
QString text = QString("Current Thread: QT_UI\r\n"
36+
"URL: %1")
37+
.arg(url);
38+
39+
QMessageBox::information(this->window(), title, text);
40+
},
41+
Qt::QueuedConnection);
42+
}
43+
44+
void
45+
CustomCefView::onQCefQueryRequest(const QCefQuery& query)
46+
{
47+
QMetaObject::invokeMethod(
48+
this,
49+
[=]() {
50+
QString title("QCef Query Request");
51+
QString text = QString("Current Thread: QT_UI\r\n"
52+
"Query: %1")
53+
.arg(query.reqeust());
54+
55+
QMessageBox::information(this->window(), title, text);
56+
57+
QString response = query.reqeust().toUpper();
58+
query.setResponseResult(true, response);
59+
responseQCefQuery(query);
60+
},
61+
Qt::QueuedConnection);
62+
}
63+
64+
void
65+
CustomCefView::onInvokeMethodNotify(int browserId, int frameId, const QString& method, const QVariantList& arguments)
66+
{
67+
if (0 == method.compare("onDragAreaMouseDown")) {
68+
#if defined(OS_WINDOWS)
69+
HWND hWnd = ::GetAncestor((HWND)(this->window()->winId()), GA_ROOT);
70+
// get current mouse cursor position
71+
POINT pt;
72+
::GetCursorPos(&pt);
73+
// in case the mouse is being captured, try to release it
74+
::ReleaseCapture();
75+
// simulate that the mouse left button is down on the title area
76+
::SendMessage(hWnd, WM_NCLBUTTONDOWN, HTCAPTION, POINTTOPOINTS(pt));
77+
#endif
78+
return;
79+
} else if (0 == method.compare("TestMethod")) {
80+
QMetaObject::invokeMethod(
81+
this,
82+
[=]() {
83+
QString title("QCef InvokeMethod Notify");
84+
QString text = QString("Current Thread: QT_UI\r\n"
85+
"Method: %1\r\n"
86+
"Arguments: ...")
87+
.arg(method);
88+
89+
QMessageBox::information(this->window(), title, text);
90+
},
91+
Qt::QueuedConnection);
92+
} else {
93+
}
94+
}

‎test/QCefViewTest/customcefview.h

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#ifndef CUSTOMCEFVIEW_H
2+
#define CUSTOMCEFVIEW_H
3+
4+
#include <QCefView.h>
5+
6+
class CustomCefView : public QCefView
7+
{
8+
Q_OBJECT
9+
10+
public:
11+
using QCefView::QCefView;
12+
~CustomCefView();
13+
14+
void changeColor();
15+
16+
protected:
17+
virtual void onDraggableRegionChanged(const QRegion& region) override;
18+
19+
virtual void onQCefUrlRequest(const QString& url) override;
20+
21+
virtual void onQCefQueryRequest(const QCefQuery& query) override;
22+
23+
virtual void onInvokeMethodNotify(int browserId,
24+
int frameId,
25+
const QString& method,
26+
const QVariantList& arguments) override;
27+
28+
private:
29+
};
30+
31+
#endif // CUSTOMCEFVIEW_H

‎test/QCefViewTest/main.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include "qcefviewtest.h"
2+
#include <QtWidgets/QApplication>
3+
4+
int
5+
main(int argc, char* argv[])
6+
{
7+
QApplication a(argc, argv);
8+
QCefViewTest w;
9+
w.show();
10+
return a.exec();
11+
}

‎test/QCefViewTest/qcefviewtest.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include "qcefviewtest.h"
2+
3+
#include <QDir>
4+
#include <QCoreApplication>
5+
#include <QHBoxLayout>
6+
7+
#include <QCefSetting.h>
8+
9+
QCefViewTest::QCefViewTest(QWidget* parent)
10+
: QMainWindow(parent)
11+
{
12+
ui.setupUi(this);
13+
QHBoxLayout* layout = new QHBoxLayout();
14+
layout->setContentsMargins(2, 2, 2, 2);
15+
layout->setSpacing(3);
16+
17+
connect(ui.btn_changeColor, SIGNAL(clicked()), this, SLOT(onBtnChangeColorClicked()));
18+
layout->addWidget(ui.nativeContainer);
19+
20+
QCefSetting::setBridgeObjectName("CallBridge");
21+
22+
QDir dir = QCoreApplication::applicationDirPath();
23+
QString uri = QDir::toNativeSeparators(dir.filePath("QCefViewTestPage.html"));
24+
cefview = new CustomCefView(uri, this);
25+
// cefview = new CustomCefView("http://www.google.com/", this);
26+
ui.cefContainer->layout()->addWidget(cefview);
27+
layout->addWidget(ui.cefContainer);
28+
29+
centralWidget()->setLayout(layout);
30+
}
31+
32+
QCefViewTest::~QCefViewTest() {}
33+
34+
void
35+
QCefViewTest::onBtnChangeColorClicked()
36+
{
37+
cefview->changeColor();
38+
}

‎test/QCefViewTest/qcefviewtest.h

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef QCEFVIEWTEST_H
2+
#define QCEFVIEWTEST_H
3+
4+
#include <QtWidgets/QMainWindow>
5+
#include "ui_qcefviewtest.h"
6+
#include "customcefview.h"
7+
8+
class QCefViewTest : public QMainWindow
9+
{
10+
Q_OBJECT
11+
12+
public:
13+
QCefViewTest(QWidget *parent = 0);
14+
~QCefViewTest();
15+
16+
protected slots:
17+
void onBtnChangeColorClicked();
18+
private:
19+
Ui::QCefViewTestClass ui;
20+
CustomCefView* cefview;
21+
};
22+
23+
#endif // QCEFVIEWTEST_H

‎test/QCefViewTest/qcefviewtest.qrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<RCC>
2+
<qresource prefix="QCefViewTest">
3+
</qresource>
4+
</RCC>

‎test/QCefViewTest/qcefviewtest.ui

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ui version="4.0">
3+
<class>QCefViewTestClass</class>
4+
<widget class="QMainWindow" name="QCefViewTestClass">
5+
<property name="geometry">
6+
<rect>
7+
<x>0</x>
8+
<y>0</y>
9+
<width>600</width>
10+
<height>400</height>
11+
</rect>
12+
</property>
13+
<property name="windowTitle">
14+
<string>QCefViewTest</string>
15+
</property>
16+
<widget class="QWidget" name="centralWidget">
17+
<property name="styleSheet">
18+
<string notr="true">#centralWidget {
19+
background-color: rgb(0, 85, 0);
20+
}</string>
21+
</property>
22+
<widget class="QWidget" name="nativeContainer" native="true">
23+
<property name="geometry">
24+
<rect>
25+
<x>20</x>
26+
<y>19</y>
27+
<width>120</width>
28+
<height>361</height>
29+
</rect>
30+
</property>
31+
<property name="sizePolicy">
32+
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
33+
<horstretch>0</horstretch>
34+
<verstretch>0</verstretch>
35+
</sizepolicy>
36+
</property>
37+
<property name="minimumSize">
38+
<size>
39+
<width>120</width>
40+
<height>0</height>
41+
</size>
42+
</property>
43+
<property name="maximumSize">
44+
<size>
45+
<width>120</width>
46+
<height>16777215</height>
47+
</size>
48+
</property>
49+
<property name="styleSheet">
50+
<string notr="true">#nativeContainer {
51+
background-color: rgb(170, 255, 255);
52+
}</string>
53+
</property>
54+
<layout class="QVBoxLayout" name="verticalLayout_nativeContainer">
55+
<property name="spacing">
56+
<number>6</number>
57+
</property>
58+
<property name="leftMargin">
59+
<number>0</number>
60+
</property>
61+
<property name="topMargin">
62+
<number>0</number>
63+
</property>
64+
<property name="rightMargin">
65+
<number>0</number>
66+
</property>
67+
<property name="bottomMargin">
68+
<number>0</number>
69+
</property>
70+
<item>
71+
<widget class="QLabel" name="label">
72+
<property name="styleSheet">
73+
<string notr="true">#label{
74+
font: 12pt &quot;MS Shell Dlg 2&quot;;
75+
}</string>
76+
</property>
77+
<property name="text">
78+
<string>Native Area</string>
79+
</property>
80+
<property name="alignment">
81+
<set>Qt::AlignCenter</set>
82+
</property>
83+
</widget>
84+
</item>
85+
<item>
86+
<widget class="QPushButton" name="btn_changeColor">
87+
<property name="text">
88+
<string>ChangeColor</string>
89+
</property>
90+
</widget>
91+
</item>
92+
<item>
93+
<spacer name="verticalSpacer">
94+
<property name="orientation">
95+
<enum>Qt::Vertical</enum>
96+
</property>
97+
<property name="sizeHint" stdset="0">
98+
<size>
99+
<width>20</width>
100+
<height>40</height>
101+
</size>
102+
</property>
103+
</spacer>
104+
</item>
105+
</layout>
106+
</widget>
107+
<widget class="QWidget" name="cefContainer" native="true">
108+
<property name="geometry">
109+
<rect>
110+
<x>230</x>
111+
<y>20</y>
112+
<width>331</width>
113+
<height>351</height>
114+
</rect>
115+
</property>
116+
<property name="styleSheet">
117+
<string notr="true">#cefContainer {
118+
background-color: rgb(85, 170, 255);
119+
}</string>
120+
</property>
121+
<layout class="QVBoxLayout" name="verticalLayout_cefContainer">
122+
<property name="spacing">
123+
<number>0</number>
124+
</property>
125+
<property name="leftMargin">
126+
<number>0</number>
127+
</property>
128+
<property name="topMargin">
129+
<number>0</number>
130+
</property>
131+
<property name="rightMargin">
132+
<number>0</number>
133+
</property>
134+
<property name="bottomMargin">
135+
<number>0</number>
136+
</property>
137+
</layout>
138+
</widget>
139+
</widget>
140+
</widget>
141+
<layoutdefault spacing="6" margin="11"/>
142+
<resources>
143+
<include location="qcefviewtest.qrc"/>
144+
</resources>
145+
<connections/>
146+
</ui>

0 commit comments

Comments
 (0)
Please sign in to comment.