-
Notifications
You must be signed in to change notification settings - Fork 10
/
BrowserComponent.cpp
89 lines (76 loc) · 2.85 KB
/
BrowserComponent.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
namespace tomduncalf
{
namespace BrowserIntegration
{
BrowserComponent::BrowserComponent(): juce::WindowsWebView2WebBrowserComponent (false)
{
loadUI();
}
BrowserComponent::BrowserComponent (juce::String initialUrl): juce::WindowsWebView2WebBrowserComponent (false)
{
goToURL (initialUrl);
}
void BrowserComponent::loadUI()
{
#if JUCE_MAC || JUCE_IOS
NSString* devServerIpNsString = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"DevServerIP"];
// TODO [NSBundle mainBundle] refers to the plugin host when running as AudioUnit
// so can't access plist - hardcoding for now
auto devServerIp = devServerIpNsString == nil ? "127.0.0.1" : [devServerIpNsString UTF8String];
#else
auto devServerIp = "127.0.0.1";
#endif
#if BROWSER_INTEGRATION_USE_DEV_SERVER_IN_DEBUG && JUCE_DEBUG
auto url = juce::String ("http://") + devServerIp + ":3000";
#else
auto url = "file://" + juce::File::getSpecialLocation (juce::File::SpecialLocationType::currentApplicationFile)
#if JUCE_MAC
.getChildFile ("Contents")
.getChildFile ("Resources")
#endif
.getChildFile ("build")
.getChildFile ("index.html")
.getFullPathName();
#endif
DBG ("Loading URL: " + url);
goToURL (url);
}
void BrowserComponent::sendMessage (const juce::var message, bool suppressLog)
{
const auto jsonMessage = juce::JSON::toString (message, true);
const auto url = "javascript:" + jsCallbackName + "(" + jsonMessage + ")";
if (! suppressLog)
DBG ("sendMessage: " << jsonMessage);
goToURL (url);
}
bool BrowserComponent::pageAboutToLoad (const juce::String& newURL)
{
if (newURL.startsWith (urlSchema))
{
if (onMessageCallback)
{
auto messageString = juce::URL::removeEscapeChars (newURL.substring (urlSchema.length()));
auto message = juce::JSON::parse (messageString);
DBG ("message: " << messageString);
onMessageCallback (message);
}
else
{
DBG ("No onMessageCallback defined in BrowserComponent");
jassertfalse;
}
return false;
}
else
return true;
}
void BrowserComponent::setOnMessageCallback (std::function<void (juce::var)> cb)
{
onMessageCallback = cb;
}
void BrowserComponent::scriptMessageReceived (const juce::var messageBody)
{
onMessageCallback (messageBody);
}
}// namespace BrowserIntegration
}// namespace tomduncalf