-
Notifications
You must be signed in to change notification settings - Fork 1
/
client_app.cpp
443 lines (368 loc) · 14.8 KB
/
client_app.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
// 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_cookie.h"
#include "include/cef_process_message.h"
#include "include/cef_task.h"
#include "include/cef_v8.h"
#include "util.h" // NOLINT(build/include)
namespace {
// Forward declarations.
void SetList(CefRefPtr<CefV8Value> source, CefRefPtr<CefListValue> target);
void SetList(CefRefPtr<CefListValue> source, CefRefPtr<CefV8Value> target);
// Transfer a Dictionary object to a V8 value.
void SetObjectValue(CefRefPtr<CefV8Value> target,
CefRefPtr<CefDictionaryValue> source) {
CefDictionaryValue::KeyList keys;
if(source->GetKeys(keys))
for(CefDictionaryValue::KeyList::iterator key = keys.begin(); key != keys.end(); ++key) {
CefRefPtr<CefV8Value> new_value;
CefValueType type = source->GetType(*key);
switch(type) {
case VTYPE_DICTIONARY: {
CefRefPtr<CefDictionaryValue> o = source->GetDictionary(*key);
new_value = CefV8Value::CreateObject(NULL);
SetObjectValue(new_value, o);
}
break;
case VTYPE_LIST:{
CefRefPtr<CefListValue> list = source->GetList(*key);
new_value = CefV8Value::CreateArray(list->GetSize());
SetList(list, new_value);
}
break;
case VTYPE_BOOL:
new_value = CefV8Value::CreateBool(source->GetBool(*key));
break;
case VTYPE_DOUBLE:
new_value = CefV8Value::CreateDouble(source->GetDouble(*key));
break;
case VTYPE_INT:
new_value = CefV8Value::CreateInt(source->GetInt(*key));
break;
case VTYPE_STRING:
new_value = CefV8Value::CreateString(source->GetString(*key));
break;
default:
break;
}
if (new_value.get()) {
target->SetValue(*key, new_value, V8_PROPERTY_ATTRIBUTE_NONE);
} else {
target->SetValue(*key, CefV8Value::CreateNull(), V8_PROPERTY_ATTRIBUTE_NONE);
}
}
}
// Transfer a V8 value to a Dictionary object.
void SetObjectValue(CefRefPtr<CefDictionaryValue> object,
CefRefPtr<CefV8Value> value) {
std::vector<CefString> keys;
if(value->GetKeys(keys))
for(std::vector<CefString>::iterator key = keys.begin(); key != keys.end(); ++key) {
CefRefPtr<CefV8Value> val = value->GetValue(*key);
if(val->IsObject()) {
CefRefPtr<CefDictionaryValue> new_obj = CefDictionaryValue::Create();
SetObjectValue(new_obj, val);
object->SetDictionary(*key, new_obj);
} else if (val->IsArray()) {
CefRefPtr<CefListValue> new_list = CefListValue::Create();
SetList(val, new_list);
object->SetList(*key, new_list);
} else if (val->IsString()) {
object->SetString(*key, val->GetStringValue());
} else if (val->IsBool()) {
object->SetBool(*key, val->GetBoolValue());
} else if (val->IsInt()) {
object->SetInt(*key, val->GetIntValue());
} else if (val->IsDouble()) {
object->SetDouble(*key, val->GetDoubleValue());
}
}
}
// Transfer a V8 value to a List index.
void SetListValue(CefRefPtr<CefListValue> list, int index,
CefRefPtr<CefV8Value> value) {
if(value->IsObject()) {
CefRefPtr<CefDictionaryValue> new_obj = CefDictionaryValue::Create();
SetObjectValue(new_obj, value);
list->SetDictionary(index, new_obj);
} else 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) {
ASSERT(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));
}
// 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_DICTIONARY: {
CefRefPtr<CefDictionaryValue> obj = value->GetDictionary(index);
new_value = CefV8Value::CreateObject(NULL);
SetObjectValue(new_value, obj);
} break;
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:
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) {
ASSERT(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 client_app extension.
class ClientAppExtensionHandler : public CefV8Handler {
public:
explicit ClientAppExtensionHandler(CefRefPtr<ClientApp> client_app)
: client_app_(client_app) {
}
virtual bool Execute(const CefString& name,
CefRefPtr<CefV8Value> object,
const CefV8ValueList& arguments,
CefRefPtr<CefV8Value>& retval,
CefString& exception) {
bool handled = false;
if (name == "SendMessage") {
// Send a message to the browser process.
if ((arguments.size() == 1 || arguments.size() == 2) &&
arguments[0]->IsString()) {
CefRefPtr<CefBrowser> browser =
CefV8Context::GetCurrentContext()->GetBrowser();
ASSERT(browser.get());
CefString name = arguments[0]->GetStringValue();
if (!name.empty()) {
CefRefPtr<CefProcessMessage> message =
CefProcessMessage::Create(name);
// Translate the arguments, if any.
if (arguments.size() == 2 && arguments[1]->IsArray())
SetList(arguments[1], message->GetArgumentList());
browser->SendProcessMessage(PID_BROWSER, message);
handled = true;
}
}
} else if (name == "SetMessageCallback") {
// Set a message callback.
if (arguments.size() == 2 && arguments[0]->IsString() &&
arguments[1]->IsFunction()) {
std::string name = arguments[0]->GetStringValue();
CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
int browser_id = context->GetBrowser()->GetIdentifier();
client_app_->SetMessageCallback(name, browser_id, context,
arguments[1]);
handled = true;
}
} else if (name == "RemoveMessageCallback") {
// Remove a message callback.
if (arguments.size() == 1 && arguments[0]->IsString()) {
std::string name = arguments[0]->GetStringValue();
CefRefPtr<CefV8Context> context = CefV8Context::GetCurrentContext();
int browser_id = context->GetBrowser()->GetIdentifier();
bool removed = client_app_->RemoveMessageCallback(name, browser_id);
retval = CefV8Value::CreateBool(removed);
handled = true;
}
} else if (name == "GetCurrentLanguage") {
retval = CefV8Value::CreateString(client_app_->GetCurrentLanguage());
handled = true;
}
if (!handled)
exception = "Invalid method arguments";
return true;
}
private:
CefRefPtr<ClientApp> client_app_;
IMPLEMENT_REFCOUNTING(ClientAppExtensionHandler);
};
} // namespace
ClientApp::ClientApp()
/*: proxy_type_(CEF_PROXY_TYPE_DIRECT)*/ {
CreateBrowserDelegates(browser_delegates_);
CreateRenderDelegates(render_delegates_);
// Default schemes that support cookies.
//cookieable_schemes_.push_back("http");
//cookieable_schemes_.push_back("https");
//cookieable_schemes_.push_back("pkcs11");
}
void ClientApp::SetMessageCallback(const std::string& message_name,
int browser_id,
CefRefPtr<CefV8Context> context,
CefRefPtr<CefV8Value> function) {
ASSERT(CefCurrentlyOn(TID_RENDERER));
callback_map_.insert(
std::make_pair(std::make_pair(message_name, browser_id),
std::make_pair(context, function)));
}
bool ClientApp::RemoveMessageCallback(const std::string& message_name,
int browser_id) {
ASSERT(CefCurrentlyOn(TID_RENDERER));
CallbackMap::iterator it =
callback_map_.find(std::make_pair(message_name, browser_id));
if (it != callback_map_.end()) {
callback_map_.erase(it);
return true;
}
return false;
}
void ClientApp::OnContextInitialized() {
// Register cookieable schemes with the global cookie manager.
CefRefPtr<CefCookieManager> manager = CefCookieManager::GetGlobalManager();
ASSERT(manager.get());
manager->SetSupportedSchemes(cookieable_schemes_);
// Execute delegate callbacks.
BrowserDelegateSet::iterator it = browser_delegates_.begin();
for (; it != browser_delegates_.end(); ++it)
(*it)->OnContextInitialized(this);
}
void ClientApp::OnBeforeChildProcessLaunch(
CefRefPtr<CefCommandLine> command_line) {
// Execute delegate callbacks.
BrowserDelegateSet::iterator it = browser_delegates_.begin();
for (; it != browser_delegates_.end(); ++it)
(*it)->OnBeforeChildProcessLaunch(this, command_line);
}
///void ClientApp::GetProxyForUrl(const CefString& url,
// CefProxyInfo& proxy_info) {
//proxy_info.proxyType = proxy_type_;
//if (!proxy_config_.empty())
//CefString(&proxy_info.proxyList) = proxy_config_;
//}
void ClientApp::OnWebKitInitialized() {
// Register the client_app extension.
std::string extension_code = GetExtensionJSSource();
CefRegisterExtension("v8/app", extension_code,
new ClientAppExtensionHandler(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);
// Remove any JavaScript callbacks registered for the context that has been
// released.
if (!callback_map_.empty()) {
CallbackMap::iterator it = callback_map_.begin();
for (; it != callback_map_.end();) {
if (it->second.first->IsSame(context))
callback_map_.erase(it++);
else
++it;
}
}
}
void ClientApp::OnFocusedNodeChanged(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame,
CefRefPtr<CefDOMNode> node) {
// Execute delegate callbacks.
//RenderDelegateSet::iterator it = render_delegates_.begin();
//for (; it != render_delegates_.end(); ++it)
// (*it)->OnFocusedNodeChanged(this, browser, frame, node);
}
bool ClientApp::OnProcessMessageReceived(
CefRefPtr<CefBrowser> browser,
CefProcessId source_process,
CefRefPtr<CefProcessMessage> message) {
ASSERT(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)
return true;
// Execute the registered JavaScript callback if any.
if (!callback_map_.empty()) {
CefString message_name = message->GetName();
CallbackMap::const_iterator it = callback_map_.find(
std::make_pair(message_name.ToString(),
browser->GetIdentifier()));
if (it != callback_map_.end()) {
// Enter the context.
it->second.first->Enter();
CefV8ValueList arguments;
// First argument is the message name.
//arguments.push_back(CefV8Value::CreateString(message_name));
// Second argument is the list of message arguments.
CefRefPtr<CefListValue> list = message->GetArgumentList();
CefRefPtr<CefV8Value> args = CefV8Value::CreateArray(list->GetSize());
SetList(list, args);
arguments.push_back(args);
// Execute the callback.
CefRefPtr<CefV8Value> retval =
it->second.second->ExecuteFunction(NULL, arguments);
if (retval.get()) {
if (retval->IsBool())
handled = retval->GetBoolValue();
}
// Exit the context.
it->second.first->Exit();
}
}
return handled;
}