-
Notifications
You must be signed in to change notification settings - Fork 57
/
webview_app.cc
338 lines (288 loc) · 11.3 KB
/
webview_app.cc
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
// Copyright (c) 2013 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that
// can be found in the LICENSE file.
#include "webview_app.h"
#include <string>
#include "include/cef_browser.h"
#include "include/cef_command_line.h"
#include "include/views/cef_browser_view.h"
#include "include/views/cef_window.h"
#include "include/wrapper/cef_helpers.h"
namespace {
// When using the Views framework this object provides the delegate
// implementation for the CefWindow that hosts the Views-based browser.
class SimpleWindowDelegate : public CefWindowDelegate {
public:
explicit SimpleWindowDelegate(CefRefPtr<CefBrowserView> browser_view)
: browser_view_(browser_view) {}
void OnWindowCreated(CefRefPtr<CefWindow> window) override {
// Add the browser view and show the window.
window->AddChildView(browser_view_);
window->Show();
// Give keyboard focus to the browser view.
browser_view_->RequestFocus();
}
void OnWindowDestroyed(CefRefPtr<CefWindow> window) override {
browser_view_ = nullptr;
}
bool CanClose(CefRefPtr<CefWindow> window) override {
// Allow the window to close if the browser says it's OK.
CefRefPtr<CefBrowser> browser = browser_view_->GetBrowser();
if (browser)
return browser->GetHost()->TryCloseBrowser();
return true;
}
CefSize GetPreferredSize(CefRefPtr<CefView> view) override {
return CefSize(1280, 720);
}
private:
CefRefPtr<CefBrowserView> browser_view_;
IMPLEMENT_REFCOUNTING(SimpleWindowDelegate);
DISALLOW_COPY_AND_ASSIGN(SimpleWindowDelegate);
};
class SimpleBrowserViewDelegate : public CefBrowserViewDelegate {
public:
SimpleBrowserViewDelegate() {}
bool OnPopupBrowserViewCreated(CefRefPtr<CefBrowserView> browser_view,
CefRefPtr<CefBrowserView> popup_browser_view,
bool is_devtools) override {
// Create a new top-level Window for the popup. It will show itself after
// creation.
CefWindow::CreateTopLevelWindow(
new SimpleWindowDelegate(popup_browser_view));
// We created the Window.
return true;
}
private:
IMPLEMENT_REFCOUNTING(SimpleBrowserViewDelegate);
DISALLOW_COPY_AND_ASSIGN(SimpleBrowserViewDelegate);
};
} // namespace
WebviewApp::WebviewApp(CefRefPtr<WebviewHandler> handler) {
m_handler = handler;
}
WebviewApp::ProcessType WebviewApp::GetProcessType(CefRefPtr<CefCommandLine> command_line)
{
// The command-line flag won't be specified for the browser process.
if (!command_line->HasSwitch("type"))
{
return BrowserProcess;
}
const std::string& process_type = command_line->GetSwitchValue("type");
if (process_type == "renderer")
return RendererProcess;
#if defined(OS_LINUX)
else if (process_type == "zygote")
return ZygoteProcess;
#endif
return OtherProcess;
}
void WebviewApp::OnBeforeCommandLineProcessing(const CefString &process_type, CefRefPtr<CefCommandLine> command_line)
{
// Pass additional command-line flags to the browser process.
if (process_type.empty())
{
if (!m_bEnableGPU)
{
command_line->AppendSwitch("disable-gpu");
command_line->AppendSwitch("disable-gpu-compositing");
}
command_line->AppendSwitch("disable-web-security"); //disable web security
command_line->AppendSwitch("allow-running-insecure-content"); //allow running insecure content in secure pages
// Don't create a "GPUCache" directory when cache-path is unspecified.
command_line->AppendSwitch("disable-gpu-shader-disk-cache"); //disable gpu shader disk cache
command_line->AppendSwitch("no-sanbox");
//http://www.chromium.org/developers/design-documents/process-models
if (m_uMode == 1)
{
command_line->AppendSwitch("process-per-site"); //each site in its own process
command_line->AppendSwitchWithValue("renderer-process-limit ", "8"); //limit renderer process count to decrease memory usage
}
else if (m_uMode == 2)
{
command_line->AppendSwitch("process-per-tab"); //each tab in its own process
}
else if (m_uMode == 3)
{
command_line->AppendSwitch("single-process"); //all in one process
}
command_line->AppendSwitchWithValue("autoplay-policy", "no-user-gesture-required"); //autoplay policy for media
//Support cross domain requests
std::string values = command_line->GetSwitchValue("disable-features");
if (values == "")
{
values = "SameSiteByDefaultCookies,CookiesWithoutSameSiteMustBeSecure";
}
else
{
values += ",SameSiteByDefaultCookies,CookiesWithoutSameSiteMustBeSecure";
}
if (values.find("CalculateNativeWinOcclusion") == size_t(-1))
{
values += ",CalculateNativeWinOcclusion";
}
command_line->AppendSwitchWithValue("disable-features", values);
// for unsafe domain, add domain to whitelist
if (!m_strFilterDomain.empty())
{
command_line->AppendSwitch("ignore-certificate-errors"); //ignore certificate errors
command_line->AppendSwitchWithValue("unsafely-treat-insecure-origin-as-secure",
m_strFilterDomain);
}
}
#ifdef __APPLE__
command_line->AppendSwitch("use-mock-keychain");
command_line->AppendSwitch("single-process");
#endif
#ifdef __linux__
#endif
}
void WebviewApp::OnContextInitialized()
{
CEF_REQUIRE_UI_THREAD();
// CefBrowserSettings browser_settings;
// browser_settings.windowless_frame_rate = 60;
//
// CefWindowInfo window_info;
// window_info.SetAsWindowless(0);
//
// // create browser
// CefBrowserHost::CreateBrowser(window_info, m_handler, "", browser_settings, nullptr, nullptr);
}
// CefRefPtr<CefClient> WebviewApp::GetDefaultClient() {
// // Called when a new browser window is created via the Chrome runtime UI.
// return WebviewHandler::GetInstance();
// }
void WebviewApp::SetUnSafelyTreatInsecureOriginAsSecure(const CefString &strFilterDomain)
{
m_strFilterDomain = strFilterDomain;
}
void WebviewApp::OnWebKitInitialized()
{
//inject js function for jssdk
std::string extensionCode = R"(
var external = {};
var clientSdk = {};
(() => {
clientSdk.jsCmd = (functionName, arg1, arg2, arg3) => {
if (typeof arg1 === 'function') {
native function jsCmd(functionName, arg1);
return jsCmd(functionName, arg1);
}
else if (typeof arg2 === 'function') {
jsonString = arg1;
if (typeof arg1 !== 'string'){
jsonString = JSON.stringify(arg1);
}
native function jsCmd(functionName, jsonString, arg2);
return jsCmd(functionName, jsonString, arg2);
}
else if (typeof arg3 === 'function') {
jsonString = arg1;
if (typeof arg1 !== 'string'){
jsonString = JSON.stringify(arg1);
}
native function jsCmd(functionName, jsonString, arg2, arg3);
return jsCmd(functionName, jsonString, arg2, arg3);
}else {
}
};
external.JavaScriptChannel = (n,e,r) => {
var a;
null == r ? a = '' : (a = '_' + new Date + (1e3 + Math.floor(8999 * Math.random())), window[a] = function (n, e) {
return function () {
try {
e && e.call && e.call(null, arguments[1])
} finally {
delete window[n]
}
}
}(a, r));
try {
external.StartRequest(external.GetNextReqID(), n, a, JSON.stringify(e || {}), '')
} catch (l) {
console.log('messeage send')
}
}
external.EvaluateCallback = (nReqID, result) => {
native function EvaluateCallback();
EvaluateCallback(nReqID, result);
}
external.StartRequest = (nReqID, strCmd, strCallBack, strArgs, strLog) => {
native function StartRequest();
StartRequest(nReqID, strCmd, strCallBack, strArgs, strLog);
};
external.GetNextReqID = () => {
native function GetNextReqID();
return GetNextReqID();
};
})();
)";
CefRefPtr<CefJSHandler> handler = new CefJSHandler();
if (!m_render_js_bridge.get())
m_render_js_bridge.reset(new CefJSBridge);
handler->AttachJSBridge(m_render_js_bridge);
CefRegisterExtension("v8/extern", extensionCode, handler);
}
void WebviewApp::OnBrowserCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefDictionaryValue> extra_info)
{
if (!m_render_js_bridge.get()) {
m_render_js_bridge.reset(new CefJSBridge);
}
}
void WebviewApp::SetProcessMode(uint32_t uMode)
{
m_uMode = uMode;
}
void WebviewApp::SetEnableGPU(bool bEnable)
{
m_bEnableGPU = bEnable;
}
void WebviewApp::OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_line)
{
}
void WebviewApp::OnBrowserDestroyed(CefRefPtr<CefBrowser> browser)
{
}
void WebviewApp::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)
{
}
void WebviewApp::OnContextReleased(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context)
{
if (m_render_js_bridge.get())
{
m_render_js_bridge->RemoveCallbackFuncWithFrame(frame);
}
}
void WebviewApp::OnUncaughtException(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefV8Context> context, CefRefPtr<CefV8Exception> exception, CefRefPtr<CefV8StackTrace> stackTrace)
{
}
void WebviewApp::OnFocusedNodeChanged(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefRefPtr<CefDOMNode> node)
{
//Get node attribute
bool is_editable = (node.get() && node->IsEditable());
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create(kFocusedNodeChangedMessage);
message->GetArgumentList()->SetBool(0, is_editable);
if (is_editable)
{
CefRect rect = node->GetElementBounds();
message->GetArgumentList()->SetInt(1, rect.x);
message->GetArgumentList()->SetInt(2, rect.y + rect.height);
}
frame->SendProcessMessage(PID_BROWSER, message);
}
bool WebviewApp::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, CefProcessId source_process, CefRefPtr<CefProcessMessage> message)
{
const CefString& message_name = message->GetName();
if (message_name == kExecuteJsCallbackMessage)
{
int callbackId = message->GetArgumentList()->GetInt(0);
bool error = message->GetArgumentList()->GetBool(1);
CefString result = message->GetArgumentList()->GetString(2);
if (m_render_js_bridge.get())
{
m_render_js_bridge->ExecuteJSCallbackFunc(callbackId, error, result);
}
}
return false;
}