-
Notifications
You must be signed in to change notification settings - Fork 10
/
BrowserIntegration.cpp
52 lines (45 loc) · 1.67 KB
/
BrowserIntegration.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
namespace tomduncalf
{
namespace BrowserIntegration
{
BrowserIntegration::BrowserIntegration (BrowserComponent& b): browser (b)
{
browser.setOnMessageCallback ([this] (juce::var message) {
handleMessage (message);
});
}
void BrowserIntegration::registerBrowserCallback (juce::String name, BrowserCallback callback)
{
auto hasExistingCallbacks = callbacksByEventName.find (name) != callbacksByEventName.end();
if (hasExistingCallbacks)
callbacksByEventName[name].push_back (callback);
else
callbacksByEventName[name] = { callback };
}
void BrowserIntegration::sendEventToBrowser (juce::String eventType, juce::var data, bool suppressLog)
{
auto* obj = new juce::DynamicObject();
obj->setProperty ("eventType", eventType);
obj->setProperty ("data", data);
browser.sendMessage (juce::var (obj), suppressLog);
}
void BrowserIntegration::handleMessage (juce::var message)
{
if (! message.hasProperty ("eventType"))
{
jassertfalse; // malformed message
return;
}
auto eventType = message.getProperty ("eventType", "");
auto callbacks = callbacksByEventName.find (eventType);
if (callbacks == callbacksByEventName.end())
{
DBG ("No callbacks defined for " << eventType.toString());
jassertfalse;
return;
}
for (auto& callback: callbacks->second)
callback (message.getProperty ("data", juce::var()));
}
}// namespace BrowserIntegration
}// namespace tomduncalf