forked from adobe/brackets-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_app.cpp
More file actions
396 lines (322 loc) · 14.1 KB
/
client_app.cpp
File metadata and controls
396 lines (322 loc) · 14.1 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
// 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"
namespace {
// Forward declarations.
void SetList(CefRefPtr<CefV8Value> source, CefRefPtr<CefListValue> target);
void SetList(CefRefPtr<CefListValue> source, CefRefPtr<CefV8Value> target);
// Transfer a V8 value to a List index.
void SetListValue(CefRefPtr<CefListValue> list, int index,
CefRefPtr<CefV8Value> value) {
if (value->IsArray()) {
CefRefPtr<CefListValue> new_list = CefListValue::Create();
SetList(value, new_list);
list->SetList(index, new_list);
} else if (value->IsString()) {
list->SetString(index, value->GetStringValue());
} else if (value->IsBool()) {
list->SetBool(index, value->GetBoolValue());
} else if (value->IsInt()) {
list->SetInt(index, value->GetIntValue());
} else if (value->IsDouble()) {
list->SetDouble(index, value->GetDoubleValue());
}
}
// Transfer a V8 array to a List.
void SetList(CefRefPtr<CefV8Value> source, CefRefPtr<CefListValue> target) {
DCHECK(source->IsArray());
int arg_length = source->GetArrayLength();
if (arg_length == 0)
return;
// Start with null types in all spaces.
target->SetSize(arg_length);
for (int i = 0; i < arg_length; ++i)
SetListValue(target, i, source->GetValue(i));
}
CefRefPtr<CefV8Value> ListValueToV8Value(CefRefPtr<CefListValue> value, int index)
{
CefRefPtr<CefV8Value> new_value;
CefValueType type = value->GetType(index);
switch (type) {
case VTYPE_LIST: {
CefRefPtr<CefListValue> list = value->GetList(index);
new_value = CefV8Value::CreateArray(list->GetSize());
SetList(list, new_value);
} break;
case VTYPE_BOOL:
new_value = CefV8Value::CreateBool(value->GetBool(index));
break;
case VTYPE_DOUBLE:
new_value = CefV8Value::CreateDouble(value->GetDouble(index));
break;
case VTYPE_INT:
new_value = CefV8Value::CreateInt(value->GetInt(index));
break;
case VTYPE_STRING:
new_value = CefV8Value::CreateString(value->GetString(index));
break;
default:
new_value = CefV8Value::CreateNull();
break;
}
return new_value;
}
// Transfer a List value to a V8 array index.
void SetListValue(CefRefPtr<CefV8Value> list, int index,
CefRefPtr<CefListValue> value) {
CefRefPtr<CefV8Value> new_value;
CefValueType type = value->GetType(index);
switch (type) {
case VTYPE_LIST: {
CefRefPtr<CefListValue> listValue = value->GetList(index);
new_value = CefV8Value::CreateArray(listValue->GetSize());
SetList(listValue, new_value);
} break;
case VTYPE_BOOL:
new_value = CefV8Value::CreateBool(value->GetBool(index));
break;
case VTYPE_DOUBLE:
new_value = CefV8Value::CreateDouble(value->GetDouble(index));
break;
case VTYPE_INT:
new_value = CefV8Value::CreateInt(value->GetInt(index));
break;
case VTYPE_STRING:
new_value = CefV8Value::CreateString(value->GetString(index));
break;
default:
break;
}
if (new_value.get()) {
list->SetValue(index, new_value);
} else {
list->SetValue(index, CefV8Value::CreateNull());
}
}
// Transfer a List to a V8 array.
void SetList(CefRefPtr<CefListValue> source, CefRefPtr<CefV8Value> target) {
DCHECK(target->IsArray());
int arg_length = source->GetSize();
if (arg_length == 0)
return;
for (int i = 0; i < arg_length; ++i)
SetListValue(target, i, source);
}
// Handles the native implementation for the appshell extension.
class AppShellExtensionHandler : public CefV8Handler {
public:
explicit AppShellExtensionHandler(CefRefPtr<ClientApp> client_app)
: client_app_(client_app)
, messageId(0) {
}
virtual bool Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) {
// The only messages that are handled here is getElapsedMilliseconds(),
// GetCurrentLanguage(), GetApplicationSupportDirectory(), and GetRemoteDebuggingPort().
// All other messages are passed to the browser process.
if (name == "GetElapsedMilliseconds") {
retval = CefV8Value::CreateDouble(client_app_->GetElapsedMilliseconds());
} else if (name == "GetCurrentLanguage") {
retval = CefV8Value::CreateString(client_app_->GetCurrentLanguage());
} else if (name == "GetApplicationSupportDirectory") {
retval = CefV8Value::CreateString(ClientApp::AppGetSupportDirectory());
} else if (name == "GetUserDocumentsDirectory") {
retval = CefV8Value::CreateString(ClientApp::AppGetDocumentsDirectory());
} else if (name == "GetRemoteDebuggingPort") {
retval = CefV8Value::CreateInt(REMOTE_DEBUGGING_PORT);
} else {
// Pass all messages to the browser process. Look in appshell_extensions.cpp for implementation.
CefRefPtr<CefBrowser> browser =
CefV8Context::GetCurrentContext()->GetBrowser();
if (!browser.get()) {
// If we don't have a browser, we can't handle the command.
return false;
}
CefRefPtr<CefProcessMessage> message =
CefProcessMessage::Create(name);
CefRefPtr<CefListValue> messageArgs = message->GetArgumentList();
// The first argument must be a callback function
if (arguments.size() > 0 && !arguments[0]->IsFunction()) {
std::string functionName = name;
fprintf(stderr, "Function called without callback param: %s\n", functionName.c_str());
return false;
}
if (arguments.size() > 0) {
// The first argument is the message id
client_app_->AddCallback(messageId, CefV8Context::GetCurrentContext(), arguments[0]);
SetListValue(messageArgs, 0, CefV8Value::CreateInt(messageId));
}
// Pass the rest of the arguments
for (unsigned int i = 1; i < arguments.size(); i++)
SetListValue(messageArgs, i, arguments[i]);
browser->SendProcessMessage(PID_BROWSER, message);
messageId++;
}
return true;
}
private:
CefRefPtr<ClientApp> client_app_;
int32 messageId;
IMPLEMENT_REFCOUNTING(AppShellExtensionHandler);
};
} // namespace
ClientApp::ClientApp() {
CreateRenderDelegates(render_delegates_);
}
void ClientApp::OnWebKitInitialized() {
// Register the appshell extension.
std::string extension_code = GetExtensionJSSource();
CefRegisterExtension("appshell", extension_code,
new 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::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;
}
}
//Simple stack class to ensure calls to Enter and Exit are balanced
class StContextScope {
public:
StContextScope( const CefRefPtr<CefV8Context>& ctx )
: m_ctx(NULL) {
if( ctx && ctx->Enter() ) {
m_ctx = ctx;
}
}
~StContextScope() {
if(m_ctx) {
m_ctx->Exit();
}
}
const CefRefPtr<CefV8Context>& GetContext() const {
return m_ctx;
}
private:
CefRefPtr<CefV8Context> m_ctx;
};
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(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;
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;
}