Skip to content

Commit 47faadc

Browse files
committed
refactor(runtime/{bridge,ipc,serviceworker,window}): clean up, clearer ownership
1 parent c962bb4 commit 47faadc

File tree

19 files changed

+270
-150
lines changed

19 files changed

+270
-150
lines changed

src/cli/main.cc

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2813,6 +2813,15 @@ int main (int argc, char* argv[]) {
28132813
}
28142814
settings.insert(std::make_pair("port", devPort));
28152815

2816+
if (!devHost.empty() && !devPort.empty() && devPort != "0") {
2817+
const auto url = URL(devHost + ":" + devPort);
2818+
if (settings["webview_insecure_domains"].empty()) {
2819+
settings["webview_insecure_domains"] = url.hostname;
2820+
} else {
2821+
settings["webview_insecure_domains"] += " " + url.hostname;
2822+
}
2823+
}
2824+
28162825
auto cnt = 0;
28172826

28182827
AndroidCliState androidState;
@@ -3195,6 +3204,29 @@ int main (int argc, char* argv[]) {
31953204
}
31963205
}
31973206

3207+
if (settings["webview_insecure_domains"].size() > 0) {
3208+
const auto links = parseStringList(trim(settings["webview_insecure_domains"]), ' ');
3209+
3210+
for (const auto link : links) {
3211+
auto domain = split(link, '?')[0];
3212+
settings["macos_app_transport_security_domain_exceptions"] += (
3213+
" <key>" + domain + "</key>\n"
3214+
" <dict>\n"
3215+
" <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>\n"
3216+
" <true/>\n"
3217+
" <key>NSTemporaryExceptionRequiresForwardSecrecy</key>\n"
3218+
" <false/>\n"
3219+
" <key>NSIncludesSubdomains</key>\n"
3220+
" <true/>\n"
3221+
" <key>NSTemporaryExceptionMinimumTLSVersion</key>\n"
3222+
" <string>1.0</string>\n"
3223+
" <key>NSTemporaryExceptionAllowsInsecureHTTPSLoads</key>\n"
3224+
" <false/>\n"
3225+
" </dict>\n"
3226+
);
3227+
}
3228+
}
3229+
31983230
auto plistInfo = tmpl(gMacOSInfoPList, settings);
31993231

32003232
writeFile(paths.pathPackage / pathBase / "Info.plist", plistInfo);
@@ -4846,6 +4878,29 @@ int main (int argc, char* argv[]) {
48464878
xCodeProjectVariables["ios_category"] = settings["ios_category"];
48474879
xCodeProjectVariables["ios_protocol"] = settings["ios_protocol"];
48484880

4881+
if (settings["webview_insecure_domains"].size() > 0) {
4882+
const auto links = parseStringList(trim(settings["webview_insecure_domains"]), ' ');
4883+
4884+
for (const auto link : links) {
4885+
auto domain = split(link, '?')[0];
4886+
settings["ios_app_transport_security_domain_exceptions"] += (
4887+
" <key>" + domain + "</key>\n"
4888+
" <dict>\n"
4889+
" <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>\n"
4890+
" <true/>\n"
4891+
" <key>NSTemporaryExceptionRequiresForwardSecrecy</key>\n"
4892+
" <false/>\n"
4893+
" <key>NSIncludesSubdomains</key>\n"
4894+
" <true/>\n"
4895+
" <key>NSTemporaryExceptionMinimumTLSVersion</key>\n"
4896+
" <string>1.0</string>\n"
4897+
" <key>NSTemporaryExceptionAllowsInsecureHTTPSLoads</key>\n"
4898+
" <false/>\n"
4899+
" </dict>\n"
4900+
);
4901+
}
4902+
}
4903+
48494904
writeFile(paths.platformSpecificOutputPath / "exportOptions.plist", tmpl(gXCodeExportOptions, settings));
48504905
writeFile(paths.platformSpecificOutputPath / "Info.plist", tmpl(gIOSInfoPList, settings));
48514906
writeFile(pathToProject / "project.pbxproj", tmpl(gXCodeProject, xCodeProjectVariables));

src/cli/templates.hh

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1314,6 +1314,49 @@ constexpr auto gIOSInfoPList = R"XML(<?xml version="1.0" encoding="UTF-8"?>
13141314
<dict>
13151315
<key>NSAllowsArbitraryLoads</key>
13161316
<true/>
1317+
1318+
<key>NSAllowsLocalNetworking</key>
1319+
<true/>
1320+
1321+
<key>NSExceptionDomains</key>
1322+
<dict>
1323+
{{ios_app_transport_security_domain_exceptions}}
1324+
<key>127.0.0.1</key>
1325+
<dict>
1326+
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
1327+
<true/>
1328+
1329+
<key>NSTemporaryExceptionRequiresForwardSecrecy</key>
1330+
<false/>
1331+
1332+
<key>NSIncludesSubdomains</key>
1333+
<false/>
1334+
1335+
<key>NSTemporaryExceptionMinimumTLSVersion</key>
1336+
<string>1.0</string>
1337+
1338+
<key>NSTemporaryExceptionAllowsInsecureHTTPSLoads</key>
1339+
<false/>
1340+
</dict>
1341+
1342+
<key>localhost</key>
1343+
<dict>
1344+
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
1345+
<true/>
1346+
1347+
<key>NSTemporaryExceptionRequiresForwardSecrecy</key>
1348+
<false/>
1349+
1350+
<key>NSIncludesSubdomains</key>
1351+
<false/>
1352+
1353+
<key>NSTemporaryExceptionMinimumTLSVersion</key>
1354+
<string>1.0</string>
1355+
1356+
<key>NSTemporaryExceptionAllowsInsecureHTTPSLoads</key>
1357+
<false/>
1358+
</dict>
1359+
</dict>
13171360
</dict>
13181361
13191362

src/runtime/bridge.hh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,13 @@ namespace ssc::runtime::bridge {
3838
const core::services::Notifications::NotificationPresentedObserver notificationPresentedObserver;
3939

4040
DispatchHandler dispatchHandler = nullptr;
41+
webview::SchemeHandlers schemeHandlers;
42+
webview::Navigator navigator;
43+
ipc::Router router;
4144

4245
using window::IBridge::IBridge;
4346
Bridge (const Options&);
44-
~Bridge ();
47+
~Bridge () override;
4548

4649
void init () override;
4750

src/runtime/bridge/bridge.cc

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,10 @@ export * from '{{url}}'
104104

105105
Bridge::Bridge (
106106
const Options& options
107-
) : window::IBridge(options.dispatcher, options.context, options.client, options.userConfig)
107+
) : window::IBridge(options.dispatcher, options.context, options.client, options.userConfig),
108+
schemeHandlers(*this),
109+
navigator(*this),
110+
router(*this)
108111
{
109112
// '-1' may mean the bridge is running as a WebKit web process extension
110113
if (options.client.index >= 0) {
@@ -137,13 +140,15 @@ export * from '{{url}}'
137140
}
138141

139142
Bridge::~Bridge () {
140-
auto& runtime = static_cast<runtime::Runtime&>(this->context);
141-
// remove observers
142-
runtime.services.geolocation.removePermissionChangeObserver(this->geolocationPermissionChangeObserver);
143-
runtime.services.networkStatus.removeObserver(this->networkStatusObserver);
144-
runtime.services.notifications.removePermissionChangeObserver(this->notificationsPermissionChangeObserver);
145-
runtime.services.notifications.removeNotificationResponseObserver(this->notificationResponseObserver);
146-
runtime.services.notifications.removeNotificationPresentedObserver(this->notificationPresentedObserver);
143+
auto app = App::sharedApplication();
144+
if (app && !app->shouldExit) {
145+
// remove observers
146+
app->runtime.services.geolocation.removePermissionChangeObserver(this->geolocationPermissionChangeObserver);
147+
app->runtime.services.networkStatus.removeObserver(this->networkStatusObserver);
148+
app->runtime.services.notifications.removePermissionChangeObserver(this->notificationsPermissionChangeObserver);
149+
app->runtime.services.notifications.removeNotificationResponseObserver(this->notificationResponseObserver);
150+
app->runtime.services.notifications.removeNotificationPresentedObserver(this->notificationPresentedObserver);
151+
}
147152
}
148153

149154
void Bridge::init () {
@@ -292,7 +297,7 @@ export * from '{{url}}'
292297
this->schemeHandlers.configure(configuration);
293298
this->schemeHandlers.registerSchemeHandler("ipc", [this](
294299
const auto request,
295-
const auto bridge,
300+
const auto& bridge,
296301
auto callbacks,
297302
auto callback
298303
) {
@@ -407,7 +412,7 @@ export * from '{{url}}'
407412

408413
this->schemeHandlers.registerSchemeHandler("socket", [this](
409414
const auto request,
410-
const auto bridge,
415+
const auto& bridge,
411416
auto callbacks,
412417
auto callback
413418
) {
@@ -416,9 +421,7 @@ export * from '{{url}}'
416421
auto userConfig = this->userConfig;
417422
auto bundleIdentifier = userConfig["meta_bundle_identifier"];
418423
auto globalBundleIdentifier = globalConfig["meta_bundle_identifier"];
419-
auto window = app->runtime.windowManager.getWindowForBridge(
420-
reinterpret_cast<const window::IBridge*>(bridge)
421-
);
424+
auto window = app->runtime.windowManager.getWindowForBridge(&bridge);
422425

423426
// if there was no window, then this is a bad request as scheme
424427
// handlers should only be handled directly in a window with
@@ -832,7 +835,7 @@ export * from '{{url}}'
832835

833836
this->schemeHandlers.registerSchemeHandler("node", [this](
834837
const auto request,
835-
const auto router,
838+
const auto& bridge,
836839
auto callbacks,
837840
auto callback
838841
) {
@@ -1001,8 +1004,7 @@ export * from '{{url}}'
10011004
if (
10021005
globalUserConfig["meta_bundle_identifier"] == this->userConfig["meta_bundle_identifier"] ||
10031006
!globalProtocolHandlers.contains(scheme)
1004-
) {
1005-
1007+
) {
10061008
auto scriptURL = trim(entry.second);
10071009

10081010
if (scriptURL.size() == 0) {
@@ -1084,14 +1086,12 @@ export * from '{{url}}'
10841086

10851087
this->schemeHandlers.registerSchemeHandler(scheme, [this](
10861088
auto request,
1087-
auto bridge,
1089+
const auto& bridge,
10881090
auto callbacks,
10891091
auto callback
10901092
) {
10911093
auto app = App::sharedApplication();
1092-
auto window = app->runtime.windowManager.getWindowForBridge(
1093-
reinterpret_cast<const window::IBridge*>(bridge)
1094-
);
1094+
auto window = app->runtime.windowManager.getWindowForBridge(&bridge);
10951095

10961096
auto fetch = serviceworker::Request();
10971097
SharedPointer<serviceworker::Server> serviceWorkerServer = nullptr;

src/runtime/ipc.hh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,12 @@
1111
#include "http.hh"
1212
#include "url.hh"
1313

14-
namespace ssc::runtime::ipc {
14+
namespace ssc::runtime::bridge {
1515
// forward
16-
class IBridge;
16+
class Bridge;
17+
}
1718

19+
namespace ssc::runtime::ipc {
1820
/**
1921
* A `Client` represents a unique caller of the IPC channel in a webview
2022
* or the runtime.
@@ -151,13 +153,14 @@ namespace ssc::runtime::ipc {
151153

152154
public:
153155
context::Dispatcher& dispatcher;
154-
IBridge& bridge;
156+
bridge::Bridge& bridge;
155157

156158
Listeners listeners;
157159
Mutex mutex;
158160
Table table;
159161

160-
Router (IBridge&);
162+
// owned by bridge
163+
Router (bridge::Bridge&);
161164

162165
// the `Router` instance is strictly owned and cannot be copied or moved
163166
Router () = delete;
@@ -190,7 +193,6 @@ namespace ssc::runtime::ipc {
190193
context::Dispatcher& dispatcher;
191194
Map<String, String> userConfig;
192195
Client client;
193-
Router router;
194196
int index = 0;
195197

196198
IBridge (
@@ -201,10 +203,10 @@ namespace ssc::runtime::ipc {
201203
) : dispatcher(dispatcher),
202204
userConfig(userConfig),
203205
context(context),
204-
client(client),
205-
router(*this)
206+
client(client)
206207
{}
207208

209+
virtual ~IBridge () {};
208210
virtual bool active () const = 0;
209211
virtual bool emit (const String&, const String& = "") = 0;
210212
virtual bool emit (const String&, const JSON::Any& = {}) = 0;

src/runtime/ipc/router.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
#include "../string.hh"
1+
#include "../bridge.hh"
22
#include "../crypto.hh"
3+
#include "../string.hh"
34
#include "../ipc.hh"
45

56
using ssc::runtime::string::toLowerCase;
67
using ssc::runtime::crypto::rand64;
78

89
namespace ssc::runtime::ipc {
9-
Router::Router (IBridge& bridge)
10+
Router::Router (bridge::Bridge& bridge)
1011
: dispatcher(bridge.dispatcher),
1112
bridge(bridge)
1213
{}

0 commit comments

Comments
 (0)