This repository was archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 601
Expand file tree
/
Copy pathclient_handler.cpp
More file actions
433 lines (363 loc) · 13.4 KB
/
client_handler.cpp
File metadata and controls
433 lines (363 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
// 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 <stdio.h>
#include <sstream>
#include <string>
#include "include/cef_browser.h"
#include "include/cef_frame.h"
#include "include/wrapper/cef_stream_resource_handler.h"
#include "cefclient.h"
#include "resource_util.h"
#include "string_util.h"
#include "appshell/appshell_extensions.h"
#include "appshell/command_callbacks.h"
// Custom menu command Ids.
enum client_menu_ids {
CLIENT_ID_SHOW_DEVTOOLS = MENU_ID_USER_FIRST,
};
ClientHandler::BrowserWindowMap ClientHandler::browser_window_map_;
ClientHandler::ClientHandler()
: m_MainHwnd(NULL),
m_BrowserId(0),
m_EditHwnd(NULL),
m_BackHwnd(NULL),
m_ForwardHwnd(NULL),
m_StopHwnd(NULL),
m_ReloadHwnd(NULL),
m_bFormElementHasFocus(false),
m_quitting(false) {
callbackId = 0;
CreateProcessMessageDelegates(process_message_delegates_);
CreateRequestDelegates(request_delegates_);
// Default schemes that support cookies.
cookieable_schemes_.push_back("http");
cookieable_schemes_.push_back("https");
cookieable_schemes_.push_back("file");
// Register cookieable schemes with the global cookie manager.
CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager();
ASSERT(manager.get());
manager->SetSupportedSchemes(cookieable_schemes_);
}
ClientHandler::~ClientHandler() {
}
bool ClientHandler::OnProcessMessageReceived(
CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
bool handled = false;
// Check for callbacks first
if (message->GetName() == "executeCommandCallback") {
int32 commandId = message->GetArgumentList()->GetInt(0);
bool result = message->GetArgumentList()->GetBool(1);
CefRefPtr<CommandCallback> callback = command_callback_map_[commandId];
callback->CommandComplete(result);
command_callback_map_.erase(commandId);
handled = true;
}
// 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::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();
} else {
// Call the platform-specific code to hook up popup windows
PopupCreated(browser);
}
browser_window_map_[browser->GetHost()->GetWindowHandle()] = browser;
}
bool ClientHandler::DoClose(CefRefPtr<CefBrowser> browser) {
REQUIRE_UI_THREAD();
if (m_BrowserId == browser->GetIdentifier()) {
// Notify the parent window that it will be closed.
browser->GetHost()->ParentWindowWillClose();
}
// 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 (CanCloseBrowser(browser)) {
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);
}
browser_window_map_.erase(browser->GetHost()->GetWindowHandle());
}
if (m_quitting) {
DispatchCloseToNextBrowser();
}
}
std::vector<CefString> gDroppedFiles;
bool ClientHandler::OnDragEnter(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefDragData> dragData,
DragOperationsMask mask) {
REQUIRE_UI_THREAD();
if (dragData->IsFile()) {
gDroppedFiles.clear();
// Store the dragged files in a vector for later use
dragData->GetFileNames(gDroppedFiles);
}
return false;
}
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);
}
}
void ClientHandler::OnLoadError(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
ErrorCode errorCode,
const CefString& errorText,
const CefString& failedUrl) {
REQUIRE_UI_THREAD();
// 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);
}
CefRefPtr<CefResourceHandler> ClientHandler::GetResourceHandler(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request) {
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::OnLoadingStateChange(CefRefPtr<CefBrowser> browser,
bool isLoading,
bool canGoBack,
bool canGoForward) {
REQUIRE_UI_THREAD();
SetLoading(isLoading);
SetNavState(canGoBack, canGoForward);
}
bool ClientHandler::OnConsoleMessage(CefRefPtr<CefBrowser> browser,
const CefString& message,
const CefString& source,
int line) {
// Don't write the message to a console.log file. Instead, we'll just
// return false here so the message gets written to the console (output window
// in xcode, or console window in dev tools)
/*
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::OnRequestGeolocationPermission(
CefRefPtr<CefBrowser> browser,
const CefString& requesting_url,
int request_id,
CefRefPtr<CefGeolocationCallback> callback) {
// Allow geolocation access from all websites.
callback->Continue(true);
}
void ClientHandler::OnBeforeContextMenu(
CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefContextMenuParams> params,
CefRefPtr<CefMenuModel> model) {
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, false);
}
}
}
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;
}
}
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;
}
void ClientHandler::ShowDevTools(CefRefPtr<CefBrowser> browser) {
std::string devtools_url = browser->GetHost()->GetDevToolsURL(true);
if (!devtools_url.empty() &&
m_OpenDevToolsURLs.find(devtools_url) == m_OpenDevToolsURLs.end()) {
m_OpenDevToolsURLs.insert(devtools_url);
browser->GetMainFrame()->ExecuteJavaScript(
"window.open('" + devtools_url + "');", "about:blank", 0);
}
}
bool ClientHandler::SendJSCommand(CefRefPtr<CefBrowser> browser, const CefString& commandName, CefRefPtr<CommandCallback> callback)
{
CefRefPtr<CefProcessMessage> message = CefProcessMessage::Create("executeCommand");
message->GetArgumentList()->SetString(0, commandName);
if (callback) {
callbackId++;
command_callback_map_[callbackId] = callback;
message->GetArgumentList()->SetInt(1, callbackId);
}
return browser->SendProcessMessage(PID_RENDERER, message);
}
void ClientHandler::SendOpenFileCommand(CefRefPtr<CefBrowser> browser, const CefString &fileArray) {
std::string fileArrayStr(fileArray);
// FIXME: Use SendJSCommand once it supports parameters
std::string cmd = "require('command/CommandManager').execute('file.openDroppedFiles'," + fileArrayStr + ")";
// if files are droppend and the Open Dialog is visible, then browser is NULL
// This fixes https://github.com/adobe/brackets/issues/7752
if (browser) {
browser->GetMainFrame()->ExecuteJavaScript(CefString(cmd.c_str()),
browser->GetMainFrame()->GetURL(), 0);
}
}
void ClientHandler::DispatchCloseToNextBrowser()
{
// If the inner loop iterates thru all browsers and there's still at least one
// left (i.e. the first browser that was skipped), then re-start loop
while (browser_window_map_.size() > 0)
{
// Close the main window last. On Windows, closing the main window exits the
// application, so make sure all other windows get a crack at saving changes first.
bool skipMainWindow = (browser_window_map_.size() > 1);
BrowserWindowMap::const_iterator i;
for (i = browser_window_map_.begin(); i != browser_window_map_.end(); i++)
{
CefRefPtr<CefBrowser> browser = i->second;
if (skipMainWindow && browser && browser->GetIdentifier() == m_BrowserId) {
continue;
}
// Bring the window to the front before sending the command
BringBrowserWindowToFront(browser);
// This call initiates a quit sequence. We will continue to close browser windows
// unless AbortQuit() is called.
m_quitting = true;
CefRefPtr<CommandCallback> callback = new CloseWindowCommandCallback(browser);
if (SendJSCommand(browser, FILE_CLOSE_WINDOW, callback)) {
// JS Command successfully sent, so we're done
return;
}
// Sending JS command to browser failed, so remove it from queue, continue to next browser
browser_window_map_.erase(i->first);
}
}
}
void ClientHandler::AbortQuit()
{
m_quitting = false;
// Notify all browsers that quit was aborted
BrowserWindowMap::const_iterator i, last = browser_window_map_.end();
for (i = browser_window_map_.begin(); i != last; i++)
{
CefRefPtr<CefBrowser> browser = i->second;
SendJSCommand(browser, APP_ABORT_QUIT);
}
}
// static
void ClientHandler::CreateProcessMessageDelegates(
ProcessMessageDelegateSet& delegates) {
appshell_extensions::CreateProcessMessageDelegates(delegates);
}
// static
void ClientHandler::CreateRequestDelegates(RequestDelegateSet& delegates) {
}