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 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
|
/******************************************************************************
Copyright (C) 2014 by John R. Bradley <jrb@turrettech.com>
Copyright (C) 2023 by Lain Bailey <lain@obsproject.com>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
#include "obs-browser-source.hpp"
#include "browser-client.hpp"
#include "browser-scheme.hpp"
#include "wide-string.hpp"
#include <nlohmann/json.hpp>
#include <util/threading.h>
#include <QApplication>
#include <util/dstr.h>
#include <functional>
#include <thread>
#include <mutex>
#ifdef __linux__
#include "linux-keyboard-helpers.hpp"
#endif
#ifdef ENABLE_BROWSER_QT_LOOP
#include <QEventLoop>
#include <QThread>
#endif
using namespace std;
extern bool QueueCEFTask(std::function<void()> task);
static mutex browser_list_mutex;
static BrowserSource *first_browser = nullptr;
static void SendBrowserVisibility(CefRefPtr<CefBrowser> browser, bool isVisible)
{
if (!browser)
return;
#if ENABLE_WASHIDDEN
if (isVisible) {
browser->GetHost()->WasResized();
browser->GetHost()->WasHidden(false);
browser->GetHost()->Invalidate(PET_VIEW);
} else {
browser->GetHost()->WasHidden(true);
}
#endif
CefRefPtr<CefProcessMessage> msg =
CefProcessMessage::Create("Visibility");
CefRefPtr<CefListValue> args = msg->GetArgumentList();
args->SetBool(0, isVisible);
SendBrowserProcessMessage(browser, PID_RENDERER, msg);
}
void DispatchJSEvent(std::string eventName, std::string jsonString,
BrowserSource *browser = nullptr);
BrowserSource::BrowserSource(obs_data_t *, obs_source_t *source_)
: source(source_)
{
/* Register Refresh hotkey */
auto refreshFunction = [](void *data, obs_hotkey_id, obs_hotkey_t *,
bool pressed) {
if (pressed) {
BrowserSource *bs = (BrowserSource *)data;
bs->Refresh();
}
};
obs_hotkey_register_source(source, "ObsBrowser.Refresh",
obs_module_text("RefreshNoCache"),
refreshFunction, (void *)this);
auto jsEventFunction = [](void *p, calldata_t *calldata) {
const auto eventName = calldata_string(calldata, "eventName");
if (!eventName)
return;
auto jsonString = calldata_string(calldata, "jsonString");
if (!jsonString)
jsonString = "null";
DispatchJSEvent(eventName, jsonString, (BrowserSource *)p);
};
proc_handler_t *ph = obs_source_get_proc_handler(source);
proc_handler_add(
ph,
"void javascript_event(string eventName, string jsonString)",
jsEventFunction, (void *)this);
/* defer update */
obs_source_update(source, nullptr);
lock_guard<mutex> lock(browser_list_mutex);
p_prev_next = &first_browser;
next = first_browser;
if (first_browser)
first_browser->p_prev_next = &next;
first_browser = this;
}
static void ActuallyCloseBrowser(CefRefPtr<CefBrowser> cefBrowser)
{
CefRefPtr<CefClient> client = cefBrowser->GetHost()->GetClient();
BrowserClient *bc = reinterpret_cast<BrowserClient *>(client.get());
if (bc) {
bc->bs = nullptr;
}
/*
* This stops rendering
* http://magpcss.org/ceforum/viewtopic.php?f=6&t=12079
* https://bitbucket.org/chromiumembedded/cef/issues/1363/washidden-api-got-broken-on-branch-2062)
*/
cefBrowser->GetHost()->WasHidden(true);
cefBrowser->GetHost()->CloseBrowser(true);
}
BrowserSource::~BrowserSource()
{
if (cefBrowser)
ActuallyCloseBrowser(cefBrowser);
}
void BrowserSource::Destroy()
{
destroying = true;
DestroyTextures();
lock_guard<mutex> lock(browser_list_mutex);
if (next)
next->p_prev_next = p_prev_next;
*p_prev_next = next;
QueueCEFTask([this]() { delete this; });
}
void BrowserSource::ExecuteOnBrowser(BrowserFunc func, bool async)
{
if (!async) {
#ifdef ENABLE_BROWSER_QT_LOOP
if (QThread::currentThread() == qApp->thread()) {
if (!!cefBrowser)
func(cefBrowser);
return;
}
#endif
os_event_t *finishedEvent;
os_event_init(&finishedEvent, OS_EVENT_TYPE_AUTO);
bool success = QueueCEFTask([&]() {
if (!!cefBrowser)
func(cefBrowser);
os_event_signal(finishedEvent);
});
if (success) {
os_event_wait(finishedEvent);
}
os_event_destroy(finishedEvent);
} else {
CefRefPtr<CefBrowser> browser = GetBrowser();
if (!!browser) {
#ifdef ENABLE_BROWSER_QT_LOOP
QueueBrowserTask(cefBrowser, func);
#else
QueueCEFTask([=]() { func(browser); });
#endif
}
}
}
bool BrowserSource::CreateBrowser()
{
return QueueCEFTask([this]() {
#ifdef ENABLE_BROWSER_SHARED_TEXTURE
if (hwaccel) {
obs_enter_graphics();
tex_sharing_avail = gs_shared_texture_available();
obs_leave_graphics();
}
#else
bool hwaccel = false;
#endif
CefRefPtr<BrowserClient> browserClient =
new BrowserClient(this, hwaccel && tex_sharing_avail,
reroute_audio, webpage_control_level);
CefWindowInfo windowInfo;
#if CHROME_VERSION_BUILD < 4430
windowInfo.width = width;
windowInfo.height = height;
#else
windowInfo.bounds.width = width;
windowInfo.bounds.height = height;
#endif
windowInfo.windowless_rendering_enabled = true;
#ifdef ENABLE_BROWSER_SHARED_TEXTURE
windowInfo.shared_texture_enabled = hwaccel;
#endif
CefBrowserSettings cefBrowserSettings;
#ifdef ENABLE_BROWSER_SHARED_TEXTURE
#ifdef BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED
if (!fps_custom) {
windowInfo.external_begin_frame_enabled = true;
cefBrowserSettings.windowless_frame_rate = 0;
} else {
cefBrowserSettings.windowless_frame_rate = fps;
}
#else
struct obs_video_info ovi;
obs_get_video_info(&ovi);
canvas_fps = (double)ovi.fps_num / (double)ovi.fps_den;
cefBrowserSettings.windowless_frame_rate =
(fps_custom) ? fps : canvas_fps;
#endif
#else
cefBrowserSettings.windowless_frame_rate = fps;
#endif
cefBrowserSettings.default_font_size = 16;
cefBrowserSettings.default_fixed_font_size = 16;
#if ENABLE_LOCAL_FILE_URL_SCHEME && CHROME_VERSION_BUILD < 4430
if (is_local) {
/* Disable web security for file:// URLs to allow
* local content access to remote APIs */
cefBrowserSettings.web_security = STATE_DISABLED;
}
#endif
auto browser = CefBrowserHost::CreateBrowserSync(
windowInfo, browserClient, url, cefBrowserSettings,
CefRefPtr<CefDictionaryValue>(), nullptr);
SetBrowser(browser);
if (reroute_audio)
cefBrowser->GetHost()->SetAudioMuted(true);
if (obs_source_showing(source))
is_showing = true;
SendBrowserVisibility(cefBrowser, is_showing);
});
}
void BrowserSource::DestroyBrowser()
{
ExecuteOnBrowser(ActuallyCloseBrowser, true);
SetBrowser(nullptr);
}
#if CHROME_VERSION_BUILD < 4103
void BrowserSource::ClearAudioStreams()
{
QueueCEFTask([this]() {
audio_streams.clear();
std::lock_guard<std::mutex> lock(audio_sources_mutex);
audio_sources.clear();
});
}
#endif
void BrowserSource::SendMouseClick(const struct obs_mouse_event *event,
int32_t type, bool mouse_up,
uint32_t click_count)
{
uint32_t modifiers = event->modifiers;
int32_t x = event->x;
int32_t y = event->y;
ExecuteOnBrowser(
[=](CefRefPtr<CefBrowser> cefBrowser) {
CefMouseEvent e;
e.modifiers = modifiers;
e.x = x;
e.y = y;
CefBrowserHost::MouseButtonType buttonType =
(CefBrowserHost::MouseButtonType)type;
cefBrowser->GetHost()->SendMouseClickEvent(
e, buttonType, mouse_up, click_count);
},
true);
}
void BrowserSource::SendMouseMove(const struct obs_mouse_event *event,
bool mouse_leave)
{
uint32_t modifiers = event->modifiers;
int32_t x = event->x;
int32_t y = event->y;
ExecuteOnBrowser(
[=](CefRefPtr<CefBrowser> cefBrowser) {
CefMouseEvent e;
e.modifiers = modifiers;
e.x = x;
e.y = y;
cefBrowser->GetHost()->SendMouseMoveEvent(e,
mouse_leave);
},
true);
}
void BrowserSource::SendMouseWheel(const struct obs_mouse_event *event,
int x_delta, int y_delta)
{
uint32_t modifiers = event->modifiers;
int32_t x = event->x;
int32_t y = event->y;
ExecuteOnBrowser(
[=](CefRefPtr<CefBrowser> cefBrowser) {
CefMouseEvent e;
e.modifiers = modifiers;
e.x = x;
e.y = y;
cefBrowser->GetHost()->SendMouseWheelEvent(e, x_delta,
y_delta);
},
true);
}
void BrowserSource::SendFocus(bool focus)
{
ExecuteOnBrowser(
[=](CefRefPtr<CefBrowser> cefBrowser) {
#if CHROME_VERSION_BUILD < 4430
cefBrowser->GetHost()->SendFocusEvent(focus);
#else
cefBrowser->GetHost()->SetFocus(focus);
#endif
},
true);
}
void BrowserSource::SendKeyClick(const struct obs_key_event *event, bool key_up)
{
if (destroying)
return;
std::string text = event->text;
#ifdef __linux__
uint32_t native_vkey = KeyboardCodeFromXKeysym(event->native_vkey);
uint32_t modifiers = event->native_modifiers;
#elif defined(_WIN32) || defined(__APPLE__)
uint32_t native_vkey = event->native_vkey;
uint32_t modifiers = event->modifiers;
#else
uint32_t native_vkey = event->native_vkey;
uint32_t native_scancode = event->native_scancode;
uint32_t modifiers = event->native_modifiers;
#endif
ExecuteOnBrowser(
[=](CefRefPtr<CefBrowser> cefBrowser) {
CefKeyEvent e;
e.windows_key_code = native_vkey;
#ifdef __APPLE__
e.native_key_code = native_vkey;
#endif
e.type = key_up ? KEYEVENT_KEYUP : KEYEVENT_RAWKEYDOWN;
if (!text.empty()) {
wstring wide = to_wide(text);
if (wide.size())
e.character = wide[0];
}
//e.native_key_code = native_vkey;
e.modifiers = modifiers;
cefBrowser->GetHost()->SendKeyEvent(e);
if (!text.empty() && !key_up) {
e.type = KEYEVENT_CHAR;
#ifdef __linux__
e.windows_key_code =
KeyboardCodeFromXKeysym(e.character);
#elif defined(_WIN32)
e.windows_key_code = e.character;
#elif !defined(__APPLE__)
e.native_key_code = native_scancode;
#endif
cefBrowser->GetHost()->SendKeyEvent(e);
}
},
true);
}
void BrowserSource::SetShowing(bool showing)
{
if (destroying)
return;
is_showing = showing;
if (shutdown_on_invisible) {
if (showing) {
Update();
} else {
DestroyBrowser();
}
} else {
ExecuteOnBrowser(
[=](CefRefPtr<CefBrowser> cefBrowser) {
CefRefPtr<CefProcessMessage> msg =
CefProcessMessage::Create("Visibility");
CefRefPtr<CefListValue> args =
msg->GetArgumentList();
args->SetBool(0, showing);
SendBrowserProcessMessage(cefBrowser,
PID_RENDERER, msg);
},
true);
nlohmann::json json;
json["visible"] = showing;
DispatchJSEvent("obsSourceVisibleChanged", json.dump(), this);
#if defined(BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED) && \
defined(ENABLE_BROWSER_SHARED_TEXTURE)
if (showing && !fps_custom) {
reset_frame = false;
}
#endif
SendBrowserVisibility(cefBrowser, showing);
if (showing)
return;
obs_enter_graphics();
if (!hwaccel && texture) {
DestroyTextures();
}
obs_leave_graphics();
}
}
void BrowserSource::SetActive(bool active)
{
ExecuteOnBrowser(
[=](CefRefPtr<CefBrowser> cefBrowser) {
CefRefPtr<CefProcessMessage> msg =
CefProcessMessage::Create("Active");
CefRefPtr<CefListValue> args = msg->GetArgumentList();
args->SetBool(0, active);
SendBrowserProcessMessage(cefBrowser, PID_RENDERER,
msg);
},
true);
nlohmann::json json;
json["active"] = active;
DispatchJSEvent("obsSourceActiveChanged", json.dump(), this);
}
void BrowserSource::Refresh()
{
ExecuteOnBrowser(
[](CefRefPtr<CefBrowser> cefBrowser) {
cefBrowser->ReloadIgnoreCache();
},
true);
}
void BrowserSource::SetBrowser(CefRefPtr<CefBrowser> b)
{
std::lock_guard<std::recursive_mutex> auto_lock(lockBrowser);
cefBrowser = b;
}
CefRefPtr<CefBrowser> BrowserSource::GetBrowser()
{
std::lock_guard<std::recursive_mutex> auto_lock(lockBrowser);
return cefBrowser;
}
#ifdef ENABLE_BROWSER_SHARED_TEXTURE
#ifdef BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED
inline void BrowserSource::SignalBeginFrame()
{
if (reset_frame) {
ExecuteOnBrowser(
[](CefRefPtr<CefBrowser> cefBrowser) {
cefBrowser->GetHost()->SendExternalBeginFrame();
},
true);
reset_frame = false;
}
}
#endif
#endif
void BrowserSource::Update(obs_data_t *settings)
{
if (settings) {
bool n_is_local;
int n_width;
int n_height;
bool n_fps_custom;
int n_fps;
bool n_shutdown;
bool n_restart;
bool n_reroute;
ControlLevel n_webpage_control_level;
std::string n_url;
std::string n_css;
n_is_local = obs_data_get_bool(settings, "is_local_file");
n_width = (int)obs_data_get_int(settings, "width");
n_height = (int)obs_data_get_int(settings, "height");
n_fps_custom = obs_data_get_bool(settings, "fps_custom");
n_fps = (int)obs_data_get_int(settings, "fps");
n_shutdown = obs_data_get_bool(settings, "shutdown");
n_restart = obs_data_get_bool(settings, "restart_when_active");
n_css = obs_data_get_string(settings, "css");
n_url = obs_data_get_string(settings,
n_is_local ? "local_file" : "url");
n_reroute = obs_data_get_bool(settings, "reroute_audio");
n_webpage_control_level = static_cast<ControlLevel>(
obs_data_get_int(settings, "webpage_control_level"));
if (n_is_local && !n_url.empty()) {
n_url = CefURIEncode(n_url, false);
#ifdef _WIN32
size_t slash = n_url.find("%2F");
size_t colon = n_url.find("%3A");
if (slash != std::string::npos &&
colon != std::string::npos && colon < slash)
n_url.replace(colon, 3, ":");
#endif
while (n_url.find("%5C") != std::string::npos)
n_url.replace(n_url.find("%5C"), 3, "/");
while (n_url.find("%2F") != std::string::npos)
n_url.replace(n_url.find("%2F"), 3, "/");
#if !ENABLE_LOCAL_FILE_URL_SCHEME
/* http://absolute/ based mapping for older CEF */
n_url = "http://absolute/" + n_url;
#elif defined(_WIN32)
/* Widows-style local file URL:
* file:///C:/file/path.webm */
n_url = "file:///" + n_url;
#else
/* UNIX-style local file URL:
* file:///home/user/file.webm */
n_url = "file://" + n_url;
#endif
}
#if ENABLE_LOCAL_FILE_URL_SCHEME
if (astrcmpi_n(n_url.c_str(), "http://absolute/", 16) == 0) {
/* Replace http://absolute/ URLs with file://
* URLs if file:// URLs are enabled */
n_url = "file:///" + n_url.substr(16);
n_is_local = true;
}
#endif
if (n_is_local == is_local && n_fps_custom == fps_custom &&
n_fps == fps && n_shutdown == shutdown_on_invisible &&
n_restart == restart && n_css == css && n_url == url &&
n_reroute == reroute_audio &&
n_webpage_control_level == webpage_control_level) {
if (n_width == width && n_height == height)
return;
width = n_width;
height = n_height;
ExecuteOnBrowser(
[=](CefRefPtr<CefBrowser> cefBrowser) {
const CefSize cefSize(width, height);
cefBrowser->GetHost()
->GetClient()
->GetDisplayHandler()
->OnAutoResize(cefBrowser,
cefSize);
cefBrowser->GetHost()->WasResized();
cefBrowser->GetHost()->Invalidate(
PET_VIEW);
},
true);
return;
}
is_local = n_is_local;
width = n_width;
height = n_height;
fps = n_fps;
fps_custom = n_fps_custom;
shutdown_on_invisible = n_shutdown;
reroute_audio = n_reroute;
webpage_control_level = n_webpage_control_level;
restart = n_restart;
css = n_css;
url = n_url;
obs_source_set_audio_active(source, reroute_audio);
}
DestroyBrowser();
DestroyTextures();
#if CHROME_VERSION_BUILD < 4103
ClearAudioStreams();
#endif
if (!shutdown_on_invisible || obs_source_showing(source))
create_browser = true;
first_update = false;
}
void BrowserSource::Tick()
{
if (create_browser && CreateBrowser())
create_browser = false;
#if defined(ENABLE_BROWSER_SHARED_TEXTURE)
#if defined(BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED)
if (!fps_custom)
reset_frame = true;
#else
struct obs_video_info ovi;
obs_get_video_info(&ovi);
double video_fps = (double)ovi.fps_num / (double)ovi.fps_den;
if (!fps_custom) {
if (!!cefBrowser && canvas_fps != video_fps) {
cefBrowser->GetHost()->SetWindowlessFrameRate(
video_fps);
canvas_fps = video_fps;
}
}
#endif
#endif
}
extern void ProcessCef();
void BrowserSource::Render()
{
bool flip = false;
#ifdef ENABLE_BROWSER_SHARED_TEXTURE
flip = hwaccel;
#endif
if (texture) {
#ifdef __APPLE__
gs_effect_t *effect =
obs_get_base_effect((hwaccel) ? OBS_EFFECT_DEFAULT_RECT
: OBS_EFFECT_DEFAULT);
#else
gs_effect_t *effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
#endif
bool linear_sample = extra_texture == NULL;
gs_texture_t *draw_texture = texture;
if (!linear_sample &&
!obs_source_get_texcoords_centered(source)) {
gs_copy_texture(extra_texture, texture);
draw_texture = extra_texture;
linear_sample = true;
}
const bool previous = gs_framebuffer_srgb_enabled();
gs_enable_framebuffer_srgb(true);
gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
gs_eparam_t *const image =
gs_effect_get_param_by_name(effect, "image");
const char *tech;
if (linear_sample) {
gs_effect_set_texture_srgb(image, draw_texture);
tech = "Draw";
} else {
gs_effect_set_texture(image, draw_texture);
tech = "DrawSrgbDecompress";
}
const uint32_t flip_flag = flip ? GS_FLIP_V : 0;
while (gs_effect_loop(effect, tech))
gs_draw_sprite(draw_texture, flip_flag, 0, 0);
gs_blend_state_pop();
gs_enable_framebuffer_srgb(previous);
}
#if defined(BROWSER_EXTERNAL_BEGIN_FRAME_ENABLED) && \
defined(ENABLE_BROWSER_SHARED_TEXTURE)
SignalBeginFrame();
#elif defined(ENABLE_BROWSER_QT_LOOP)
ProcessCef();
#endif
}
static void ExecuteOnBrowser(BrowserFunc func, BrowserSource *bs)
{
lock_guard<mutex> lock(browser_list_mutex);
if (bs) {
BrowserSource *bsw = reinterpret_cast<BrowserSource *>(bs);
bsw->ExecuteOnBrowser(func, true);
}
}
static void ExecuteOnAllBrowsers(BrowserFunc func)
{
lock_guard<mutex> lock(browser_list_mutex);
BrowserSource *bs = first_browser;
while (bs) {
BrowserSource *bsw = reinterpret_cast<BrowserSource *>(bs);
bsw->ExecuteOnBrowser(func, true);
bs = bs->next;
}
}
void DispatchJSEvent(std::string eventName, std::string jsonString,
BrowserSource *browser)
{
const auto jsEvent = [=](CefRefPtr<CefBrowser> cefBrowser) {
CefRefPtr<CefProcessMessage> msg =
CefProcessMessage::Create("DispatchJSEvent");
CefRefPtr<CefListValue> args = msg->GetArgumentList();
args->SetString(0, eventName);
args->SetString(1, jsonString);
SendBrowserProcessMessage(cefBrowser, PID_RENDERER, msg);
};
if (!browser)
ExecuteOnAllBrowsers(jsEvent);
else
ExecuteOnBrowser(jsEvent, browser);
}
|