-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient_handler.cpp
516 lines (447 loc) · 16.8 KB
/
client_handler.cpp
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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright (c) 2011 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 "client_handler.h"
#include <algorithm>
#include <stdio.h>
#include <sstream>
#include <string>
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/cef_path_util.h"
#include "include/cef_process_util.h"
#include "include/cef_runnable.h"
#include "include/wrapper/cef_stream_resource_handler.h"
#include "usbtoken.h"
#include "client_renderer.h"
#include "client_switches.h"
#include "resource_util.h"
#include "string_util.h"
#include "inc/token.h"
#include "ep_pkcs11_scheme.h"
#include "inc/util_pkcs15.h"
ClientHandler::ClientHandler()
: m_MainHwnd(NULL),
app_running_(false),
m_BrowserId(0),
m_StartupURL("pkcs11://epsilon.ma"),
m_bFocusOnEditableField(false), m_bExternalDevTools(false) {
CreateProcessMessageDelegates(process_message_delegates_);
CreateRequestDelegates(request_delegates_);
}
ClientHandler::~ClientHandler() {
#if defined(WIN32)
if (m_watcherHnd) {
TerminateThread(m_watcherHnd, 0);
//CloseHandle(m_watcherHnd);
m_watcherHnd = NULL;
}
#endif
}
bool ClientHandler::OnProcessMessageReceived(
CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
bool handled = false;
// Execute delegate callbacks.
ProcessMessageDelegateSet::iterator it = process_message_delegates_.begin();
for (; it != process_message_delegates_.end() && !handled; ++it) {
handled = (*it)->OnProcessMessageReceived(this, browser, source_process,
message);
}
return handled;
}
void ClientHandler::OnBeforeContextMenu(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model) {
model->Clear();
//if ((params->GetTypeFlags() & (CM_TYPEFLAG_PAGE | CM_TYPEFLAG_FRAME)) != 0) {
// Add a separator if the menu already has items.
//if (model->GetCount() > 0)
//model->AddSeparator();
// Add a "Show DevTools" item to all context menus.
//model->AddItem(CLIENT_ID_SHOW_DEVTOOLS, "&Show DevTools");
//CefString devtools_url = browser->GetHost()->GetDevToolsURL(true);
//if (devtools_url.empty() ||
// m_OpenDevToolsURLs.find(devtools_url) != m_OpenDevToolsURLs.end()) {
// Disable the menu option if DevTools isn't enabled or if a window is
// already open for the current URL.
//model->SetEnabled(CLIENT_ID_SHOW_DEVTOOLS, true);
//}
// Test context menu features.
//BuildTestMenu(model);
//}
}
bool ClientHandler::OnContextMenuCommand(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
int command_id,
EventFlags event_flags) {
//switch (command_id) {
//case CLIENT_ID_SHOW_DEVTOOLS:
//ShowDevTools(browser);
//return true;
//default: // Allow default handling, if any.
return false; //ExecuteTestMenu(command_id);
//}
}
void ClientHandler::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
bool isLoading,
bool canGoBack,
bool canGoForward) {
REQUIRE_UI_THREAD();
SetLoading(isLoading);
}
//bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
// const CefString& message,
// const CefString& source,
// int line) {
// REQUIRE_UI_THREAD();
//
// bool first_message;
// std::string logFile;
//
// {
// AutoLock lock_scope(this);
//
// first_message = m_LogFile.empty();
// if (first_message) {
// std::stringstream ss;
// ss << AppGetWorkingDirectory();
//#if defined(OS_WIN)
// ss << "\\";
//#else
// ss << "/";
//#endif
// ss << "console.log";
// m_LogFile = ss.str();
// }
// logFile = m_LogFile;
// }
//
// FILE* file = fopen(logFile.c_str(), "a");
// if (file) {
// std::stringstream ss;
// ss << "Message: " << std::string(message) << "\r\nSource: " <<
// std::string(source) << "\r\nLine: " << line <<
// "\r\n-----------------------\r\n";
// fputs(ss.str().c_str(), file);
// fclose(file);
//
// //if (first_message)
// //SendNotification(NOTIFY_CONSOLE_MESSAGE);
// }
//
// return false;
//}
void ClientHandler::OnBeforeDownload(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
const CefString& suggested_name,
CefRefPtr<CefBeforeDownloadCallback> callback) {
REQUIRE_UI_THREAD();
// Continue the download and show the "Save As" dialog.
callback->Continue(GetDownloadPath(suggested_name), true);
}
void ClientHandler::OnDownloadUpdated(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDownloadItem> download_item,
CefRefPtr<CefDownloadItemCallback> callback) {
REQUIRE_UI_THREAD();
if (download_item->IsComplete()) {
SetLastDownloadFile(download_item->GetFullPath());
SendNotification(NOTIFY_DOWNLOAD_COMPLETE);
}
}
void ClientHandler::SetLastDownloadFile(const std::string& fileName) {
AutoLock lock_scope(this);
m_LastDownloadFile = fileName;
}
std::string ClientHandler::GetLastDownloadFile() {
AutoLock lock_scope(this);
return m_LastDownloadFile;
}
//void ClientHandler::OnRequestGeolocationPermission(
// CefRefPtr<CefBrowser> browser,
// const CefString& requesting_url,
// int request_id,
// CefRefPtr<CefGeolocationCallback> callback) {
// // Allow geolocation access from all websites.
// callback->Continue(true);
//}
bool ClientHandler::OnPreKeyEvent(CefRefPtr<CefBrowser> browser,
const CefKeyEvent& event,
CefEventHandle os_event,
bool* is_keyboard_shortcut) {
//ASSERT(m_bFocusOnEditableField == event.focus_on_editable_field);
//if (!event.focus_on_editable_field && event.windows_key_code == 0x20) {
// Special handling for the space character when an input element does not
// have focus. Handling the event in OnPreKeyEvent() keeps the event from
// being processed in the renderer. If we instead handled the event in the
// OnKeyEvent() method the space key would cause the window to scroll in
// addition to showing the alert box.
//if (event.type == KEYEVENT_RAWKEYDOWN) {
// browser->GetMainFrame()->ExecuteJavaScript(
// "alert('You pressed the space bar!');", "", 0);
//}
//return true;
//}
return false;
}
void ClientHandler::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
REQUIRE_UI_THREAD();
AutoLock lock_scope(this);
if (!m_Browser.get()) {
// We need to keep the main child window, but not popup windows
m_Browser = browser;
m_BrowserId = browser->GetIdentifier();
}
}
bool ClientHandler::DoClose(CefRefPtr<CefBrowser> browser) {
REQUIRE_UI_THREAD();
if (m_BrowserId == browser->GetIdentifier()) {
// Since the main window contains the browser window, we need to close
// the parent window instead of the browser window.
CloseMainWindow();
// Return true here so that we can skip closing the browser window
// in this pass. (It will be destroyed due to the call to close
// the parent above.)
return true;
}
// A popup browser window is not contained in another window, so we can let
// these windows close by themselves.
return false;
}
void ClientHandler::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
REQUIRE_UI_THREAD();
if (m_BrowserId == browser->GetIdentifier()) {
// Free the browser pointer so that the browser can be destroyed
m_Browser = NULL;
}
else if (browser->IsPopup()) {
// Remove the record for DevTools popup windows.
std::set<std::string>::iterator it =
m_OpenDevToolsURLs.find(browser->GetMainFrame()->GetURL());
if (it != m_OpenDevToolsURLs.end())
m_OpenDevToolsURLs.erase(it);
}
}
void ClientHandler::OnLoadStart(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame) {
REQUIRE_UI_THREAD();
if (m_BrowserId == browser->GetIdentifier() && frame->IsMain()) {
// We've just started loading a page
SetLoading(true);
}
}
void ClientHandler::OnLoadEnd(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
int httpStatusCode) {
REQUIRE_UI_THREAD();
if (m_BrowserId == browser->GetIdentifier() && frame->IsMain()) {
// We've just finished loading a page
SetLoading(false);
// Continue the DOM test.
// if (frame->GetURL() == dom_test::kTestUrl)
// dom_test::OnLoadEnd(browser);
}
}
void ClientHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl) {
REQUIRE_UI_THREAD();
// Don't display an error for downloaded files.
if (errorCode == ERR_ABORTED)
return;
// Don't display an error for external protocols that we allow the OS to
// handle. See OnProtocolExecution().
if (errorCode == ERR_UNKNOWN_URL_SCHEME) {
std::string urlStr = frame->GetURL();
if (urlStr.find("spotify:") == 0)
return;
}
// Display a load error message.
std::stringstream ss;
ss << "<html><body><h2>Failed to load URL " << std::string(failedUrl) <<
" with error " << std::string(errorText) << " (" << errorCode <<
").</h2></body></html>";
frame->LoadString(ss.str(), failedUrl);
}
void ClientHandler::OnRenderProcessTerminated(CefRefPtr<CefBrowser> browser,
TerminationStatus status) {
// Load the startup URL if that's not the website that we terminated on.
CefRefPtr<CefFrame> frame = browser->GetMainFrame();
std::string url = frame->GetURL();
std::transform(url.begin(), url.end(), url.begin(), tolower);
std::string startupURL = GetStartupURL();
if (url.find(startupURL) != 0)
frame->LoadURL(startupURL);
}
CefRefPtr<CefResourceHandler> ClientHandler::GetResourceHandler(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request) {
//std::string url = request->GetURL();
//if (url == "http://tests/request") {
// // Show the request contents
// std::string dump;
// DumpRequestContents(request, dump);
// CefRefPtr<CefStreamReader> stream =
// CefStreamReader::CreateForData(
// static_cast<void*>(const_cast<char*>(dump.c_str())),
// dump.size());
// ASSERT(stream.get());
// return new CefStreamResourceHandler("text/plain", stream);
//} else if (url == "http://tests/dialogs") {
// // Show the dialogs contents
// CefRefPtr<CefStreamReader> stream =
// GetBinaryResourceReader("dialogs.html");
// ASSERT(stream.get());
// return new CefStreamResourceHandler("text/html", stream);
////} else if (url == dom_test::kTestUrl) {
//// // Show the domaccess contents
// //CefRefPtr<CefStreamReader> stream =
// // GetBinaryResourceReader("domaccess.html");
// //ASSERT(stream.get());
// //return new CefStreamResourceHandler("text/html", stream);
//} else if (url == "http://tests/localstorage") {
// // Show the localstorage contents
// CefRefPtr<CefStreamReader> stream =
// GetBinaryResourceReader("localstorage.html");
// ASSERT(stream.get());
// return new CefStreamResourceHandler("text/html", stream);
//} else if (url == "http://tests/xmlhttprequest") {
// // Show the xmlhttprequest contents
// CefRefPtr<CefStreamReader> stream =
// GetBinaryResourceReader("xmlhttprequest.html");
// ASSERT(stream.get());
// return new CefStreamResourceHandler("text/html", stream);
//}
CefRefPtr<CefResourceHandler> handler;
// Execute delegate callbacks.
RequestDelegateSet::iterator it = request_delegates_.begin();
for (; it != request_delegates_.end() && !handler.get(); ++it)
handler = (*it)->GetResourceHandler(this, browser, frame, request);
return handler;
}
void ClientHandler::SetMainHwnd(CefWindowHandle hwnd) {
AutoLock lock_scope(this);
m_MainHwnd = hwnd;
}
//void ClientHandler::SetEditHwnd(CefWindowHandle hwnd) {
// AutoLock lock_scope(this);
// m_EditHwnd = hwnd;
//}
//void ClientHandler::SetButtonHwnds(CefWindowHandle backHwnd,
// CefWindowHandle forwardHwnd,
// CefWindowHandle reloadHwnd,
// CefWindowHandle stopHwnd) {
// AutoLock lock_scope(this);
// m_BackHwnd = backHwnd;
// m_ForwardHwnd = forwardHwnd;
// m_ReloadHwnd = reloadHwnd;
// m_StopHwnd = stopHwnd;
//}
std::string ClientHandler::GetLogFile() {
AutoLock lock_scope(this);
return m_LogFile;
}
//void ClientHandler::SetLastDownloadFile(const std::string& fileName) {
// AutoLock lock_scope(this);
// m_LastDownloadFile = fileName;
//}
//std::string ClientHandler::GetLastDownloadFile() {
// AutoLock lock_scope(this);
// return m_LastDownloadFile;
//}
bool ClientHandler::Save(const std::string& path, const std::string& data) {
FILE* f = fopen(path.c_str(), "w");
if (!f)
return false;
fwrite(data.c_str(), data.size(), 1, f);
fclose(f);
return true;
}
void ClientHandler::ShowDevTools(CefRefPtr<CefBrowser> browser) {
std::string devtools_url = browser->GetHost()->GetDevToolsURL(false);
if (!devtools_url.empty()) {
if (m_bExternalDevTools) {
// Open DevTools in an external browser window.
LaunchExternalBrowser(devtools_url);
} else if (m_OpenDevToolsURLs.find(devtools_url) ==
m_OpenDevToolsURLs.end()) {
// Open DevTools in a popup window.
m_OpenDevToolsURLs.insert(devtools_url);
browser->GetMainFrame()->ExecuteJavaScript(
"window.open('" + devtools_url + "');", "about:blank", 0);
}
}
}
// static
void ClientHandler::LaunchExternalBrowser(const std::string& url) {
if (CefCurrentlyOn(TID_PROCESS_LAUNCHER)) {
// Retrieve the current executable path.
CefString file_exe;
if (!CefGetPath(PK_FILE_EXE, file_exe))
return;
// Create the command line.
CefRefPtr<CefCommandLine> command_line =
CefCommandLine::CreateCommandLine();
command_line->SetProgram(file_exe);
command_line->AppendSwitchWithValue(usbtoken::kUrl, url);
// Launch the process.
CefLaunchProcess(command_line);
} else {
// Execute on the PROCESS_LAUNCHER thread.
CefPostTask(TID_PROCESS_LAUNCHER,
NewCefRunnableFunction(&ClientHandler::LaunchExternalBrowser, url));
}
}
// static
void ClientHandler::CreateProcessMessageDelegates(
ProcessMessageDelegateSet& delegates) {
epsilon::CreateProcessMessageDelegates(delegates);
}
// static
void ClientHandler::CreateRequestDelegates(RequestDelegateSet& delegates) {
}
//void ClientHandler::BuildTestMenu(CefRefPtr<CefMenuModel> model) {
// if (model->GetCount() > 0)
// model->AddSeparator();
//
// // Build the sub menu.
// CefRefPtr<CefMenuModel> submenu =
// model->AddSubMenu(CLIENT_ID_TESTMENU_SUBMENU, "Context Menu Test");
// submenu->AddCheckItem(CLIENT_ID_TESTMENU_CHECKITEM, "Check Item");
// submenu->AddRadioItem(CLIENT_ID_TESTMENU_RADIOITEM1, "Radio Item 1", 0);
// submenu->AddRadioItem(CLIENT_ID_TESTMENU_RADIOITEM2, "Radio Item 2", 0);
// submenu->AddRadioItem(CLIENT_ID_TESTMENU_RADIOITEM3, "Radio Item 3", 0);
//
// // Check the check item.
// if (m_TestMenuState.check_item)
// submenu->SetChecked(CLIENT_ID_TESTMENU_CHECKITEM, true);
//
// // Check the selected radio item.
// submenu->SetChecked(
// CLIENT_ID_TESTMENU_RADIOITEM1 + m_TestMenuState.radio_item, true);
//}
//bool ClientHandler::ExecuteTestMenu(int command_id) {
// if (command_id == CLIENT_ID_TESTMENU_CHECKITEM) {
// // Toggle the check item.
// m_TestMenuState.check_item ^= 1;
// return true;
// } else if (command_id >= CLIENT_ID_TESTMENU_RADIOITEM1 &&
// command_id <= CLIENT_ID_TESTMENU_RADIOITEM3) {
// // Store the selected radio item.
// m_TestMenuState.radio_item = (command_id - CLIENT_ID_TESTMENU_RADIOITEM1);
// return true;
// }
//
// // Allow default handling to proceed.
// return false;
//}