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_app.cpp
More file actions
201 lines (158 loc) · 7.87 KB
/
client_app.cpp
File metadata and controls
201 lines (158 loc) · 7.87 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
// Copyright (c) 2012 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.
// This file is shared by cefclient and cef_unittests so don't include using
// a qualified path.
#include "client_app.h" // NOLINT(build/include)
#include <string>
#include "include/cef_process_message.h"
#include "include/cef_task.h"
#include "include/cef_v8.h"
#include "include/base/cef_logging.h"
#include "config.h"
#include "appshell/appshell_extension_handler.h"
#include "appshell/appshell_helpers.h"
extern bool g_force_enable_acc;
ClientApp::ClientApp() {
CreateRenderDelegates(render_delegates_);
}
void ClientApp::OnWebKitInitialized() {
// Register the appshell extension.
std::string extension_code = appshell::GetExtensionJSSource();
CefRegisterExtension("appshell", extension_code,
new appshell::AppShellExtensionHandler(this));
// Execute delegate callbacks.
RenderDelegateSet::iterator it = render_delegates_.begin();
for (; it != render_delegates_.end(); ++it)
(*it)->OnWebKitInitialized(this);
}
void ClientApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) {
// Execute delegate callbacks.
RenderDelegateSet::iterator it = render_delegates_.begin();
for (; it != render_delegates_.end(); ++it)
(*it)->OnContextCreated(this, browser, frame, context);
}
void ClientApp::OnBeforeCommandLineProcessing(
const CefString& process_type,
CefRefPtr<CefCommandLine> command_line)
{
// Check if the user wants to enable renderer accessibility
// and if not, then disable renderer accessibility.
if (!g_force_enable_acc)
command_line->AppendSwitch("disable-renderer-accessibility");
}
void ClientApp::OnBeforeChildProcessLaunch(
CefRefPtr<CefCommandLine> command_line)
{
if (!g_force_enable_acc)
command_line->AppendSwitch("disable-renderer-accessibility");
}
void ClientApp::OnContextReleased(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context) {
// Execute delegate callbacks.
RenderDelegateSet::iterator it = render_delegates_.begin();
for (; it != render_delegates_.end(); ++it)
(*it)->OnContextReleased(this, browser, frame, context);
// This is to fix the crash on quit(https://github.com/adobe/brackets/issues/7683)
// after integrating CEF 2171.
// On Destruction, callback_map_ was getting destroyed
// in the ClientApp::~ClientApp(). However while removing
// all the elements, it was trying to destroy some stale
// objects which were already deleted. So to fix this, we
// are now explicitly clearing the map here.
CallbackMap::iterator iCallBack = callback_map_.begin();
for (; iCallBack != callback_map_.end();) {
if (iCallBack->second.first->IsSame(context))
callback_map_.erase(iCallBack++);
else
++iCallBack;
}
}
void ClientApp::OnUncaughtException(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefV8Context> context,
CefRefPtr<CefV8Exception> exception,
CefRefPtr<CefV8StackTrace> stackTrace) {
RenderDelegateSet::iterator it = render_delegates_.begin();
for (; it != render_delegates_.end(); ++it) {
(*it)->OnUncaughtException(this, browser, frame, context, exception,
stackTrace);
}
}
bool ClientApp::OnProcessMessageReceived(
CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
DCHECK(source_process == PID_BROWSER);
bool handled = false;
// Execute delegate callbacks.
RenderDelegateSet::iterator it = render_delegates_.begin();
for (; it != render_delegates_.end() && !handled; ++it) {
handled = (*it)->OnProcessMessageReceived(this, browser, source_process, message);
}
if (!handled) {
if (message->GetName() == "invokeCallback") {
// This is called by the appshell extension handler to invoke the asynchronous
// callback function
CefRefPtr<CefListValue> messageArgs = message->GetArgumentList();
int32 callbackId = messageArgs->GetInt(0);
CefRefPtr<CefV8Context> context = callback_map_[callbackId].first;
CefRefPtr<CefV8Value> callbackFunction = callback_map_[callbackId].second;
CefV8ValueList arguments;
context->Enter();
// Sanity check to make sure the context is still attched to a browser.
// Async callbacks could be initiated after a browser instance has been deleted,
// which can lead to bad things. If the browser instance has been deleted, don't
// invoke this callback.
if (context->GetBrowser()) {
for (size_t i = 1; i < messageArgs->GetSize(); i++) {
arguments.push_back(appshell::ListValueToV8Value(messageArgs, i));
}
callbackFunction->ExecuteFunction(NULL, arguments);
}
context->Exit();
callback_map_.erase(callbackId);
} else if (message->GetName() == "executeCommand") {
// This is called by the browser process to execute a command via JavaScript
//
// The first argument is the command name. This is required.
// The second argument is a message id. This is optional. If set, a response
// message will be sent back to the browser process.
CefRefPtr<CefListValue> messageArgs = message->GetArgumentList();
CefString commandName = messageArgs->GetString(0);
int messageId = messageArgs->GetSize() > 1 ? messageArgs->GetInt(1) : -1;
handled = false;
appshell::StContextScope ctx(browser->GetMainFrame()->GetV8Context());
CefRefPtr<CefV8Value> global = ctx.GetContext()->GetGlobal();
if (global->HasValue("brackets")) {
CefRefPtr<CefV8Value> brackets = global->GetValue("brackets");
if (brackets->HasValue("shellAPI")) {
CefRefPtr<CefV8Value> shellAPI = brackets->GetValue("shellAPI");
if (shellAPI->HasValue("executeCommand")) {
CefRefPtr<CefV8Value> executeCommand = shellAPI->GetValue("executeCommand");
if (executeCommand->IsFunction()) {
CefRefPtr<CefV8Value> retval;
CefV8ValueList args;
args.push_back(CefV8Value::CreateString(commandName));
retval = executeCommand->ExecuteFunction(global, args);
if (retval) {
handled = retval->GetBoolValue();
}
}
}
}
}
// Return a message saying whether or not the command was handled
if (messageId != -1) {
CefRefPtr<CefProcessMessage> result = CefProcessMessage::Create("executeCommandCallback");
result->GetArgumentList()->SetInt(0, messageId);
result->GetArgumentList()->SetBool(1, handled);
browser->SendProcessMessage(PID_BROWSER, result);
}
}
}
return handled;
}