Skip to content

Commit e75c45e

Browse files
authored
Injected client adds isFlutterApp to global window object (#1806)
1 parent ba5e3ec commit e75c45e

File tree

9 files changed

+93
-32
lines changed

9 files changed

+93
-32
lines changed

dwds/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- Include the entire exception description up to the stacktrace in
1010
`mapExceptionStackTrace`.
1111
- Allow enabling experiments in the expression compiler service.
12+
- Include an optional param to `Dwds.start` to indicate whether it a Flutter app
13+
or not.
1214

1315
## 16.0.1
1416

dwds/lib/dart_web_debug_service.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ class Dwds {
8787
SdkConfigurationProvider? sdkConfigurationProvider,
8888
bool emitDebugEvents = true,
8989
bool isInternalBuild = false,
90+
bool isFlutterApp = false,
9091
}) async {
9192
globalLoadStrategy = loadStrategy;
9293
sdkConfigurationProvider ??= DefaultSdkConfigurationProvider();
@@ -129,6 +130,7 @@ class Dwds {
129130
enableDevtoolsLaunch: enableDevtoolsLaunch,
130131
emitDebugEvents: emitDebugEvents,
131132
isInternalBuild: isInternalBuild,
133+
isFlutterApp: isFlutterApp,
132134
);
133135

134136
final devHandler = DevHandler(

dwds/lib/data/debug_info.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ abstract class DebugInfo implements Built<DebugInfo, DebugInfoBuilder> {
2222
String? get dwdsVersion;
2323
String? get extensionUrl;
2424
bool? get isInternalBuild;
25+
bool? get isFlutterApp;
2526
}

dwds/lib/data/debug_info.g.dart

Lines changed: 36 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dwds/lib/src/handlers/injector.dart

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class DwdsInjector {
3737
final bool _useSseForInjectedClient;
3838
final bool _emitDebugEvents;
3939
final bool _isInternalBuild;
40+
final bool _isFlutterApp;
4041

4142
DwdsInjector(
4243
this._loadStrategy, {
@@ -45,11 +46,13 @@ class DwdsInjector {
4546
bool useSseForInjectedClient = true,
4647
bool emitDebugEvents = true,
4748
bool isInternalBuild = false,
49+
bool isFlutterApp = false,
4850
}) : _extensionUri = extensionUri,
4951
_enableDevtoolsLaunch = enableDevtoolsLaunch,
5052
_useSseForInjectedClient = useSseForInjectedClient,
5153
_emitDebugEvents = emitDebugEvents,
52-
_isInternalBuild = isInternalBuild;
54+
_isInternalBuild = isInternalBuild,
55+
_isFlutterApp = isFlutterApp;
5356

5457
/// Returns the embedded dev handler paths.
5558
///
@@ -115,6 +118,7 @@ class DwdsInjector {
115118
_enableDevtoolsLaunch,
116119
_emitDebugEvents,
117120
_isInternalBuild,
121+
_isFlutterApp,
118122
);
119123
body += await _loadStrategy.bootstrapFor(entrypoint);
120124
_logger.info('Injected debugging metadata for '
@@ -140,16 +144,16 @@ class DwdsInjector {
140144
/// Returns the provided body with the main function hoisted into a global
141145
/// variable and a snippet of JS that loads the injected client.
142146
String _injectClientAndHoistMain(
143-
String body,
144-
String appId,
145-
String devHandlerPath,
146-
String entrypointPath,
147-
String? extensionUri,
148-
LoadStrategy loadStrategy,
149-
bool enableDevtoolsLaunch,
150-
bool emitDebugEvents,
151-
bool isInternalBuild,
152-
) {
147+
String body,
148+
String appId,
149+
String devHandlerPath,
150+
String entrypointPath,
151+
String? extensionUri,
152+
LoadStrategy loadStrategy,
153+
bool enableDevtoolsLaunch,
154+
bool emitDebugEvents,
155+
bool isInternalBuild,
156+
bool isFlutterApp) {
153157
final bodyLines = body.split('\n');
154158
final extensionIndex =
155159
bodyLines.indexWhere((line) => line.contains(mainExtensionMarker));
@@ -169,7 +173,8 @@ String _injectClientAndHoistMain(
169173
loadStrategy,
170174
enableDevtoolsLaunch,
171175
emitDebugEvents,
172-
isInternalBuild);
176+
isInternalBuild,
177+
isFlutterApp);
173178
result += '''
174179
// Injected by dwds for debugging support.
175180
if(!window.\$dwdsInitialized) {
@@ -204,6 +209,7 @@ String _injectedClientSnippet(
204209
bool enableDevtoolsLaunch,
205210
bool emitDebugEvents,
206211
bool isInternalBuild,
212+
bool isFlutterApp,
207213
) {
208214
var injectedBody = 'window.\$dartAppId = "$appId";\n'
209215
'window.\$dartReloadConfiguration = "${loadStrategy.reloadConfiguration}";\n'
@@ -215,6 +221,7 @@ String _injectedClientSnippet(
215221
'window.\$dartEntrypointPath = "$entrypointPath";\n'
216222
'window.\$dartEmitDebugEvents = $emitDebugEvents;\n'
217223
'window.\$isInternalBuild = $isInternalBuild;\n'
224+
'window.\$isFlutterApp = $isFlutterApp;\n'
218225
'${loadStrategy.loadClientSnippet(_clientScript)}';
219226
if (extensionUri != null) {
220227
injectedBody += 'window.\$dartExtensionUri = "$extensionUri";\n';

dwds/lib/src/injected/client.js

Lines changed: 21 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dwds/test/handlers/injector_test.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,12 @@ void main() {
205205
expect(result.body, contains('\$isInternalBuild'));
206206
});
207207

208+
test('the injected client contains a global \$isFlutterApp', () async {
209+
final result = await http.get(Uri.parse(
210+
'http://localhost:${server.port}/dwds/src/injected/client.js'));
211+
expect(result.body, contains('\$isFlutterApp'));
212+
});
213+
208214
test('serves the injected client', () async {
209215
final result = await http.get(Uri.parse(
210216
'http://localhost:${server.port}/dwds/src/injected/client.js'));

dwds/test/puppeteer/extension_test.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ void main() async {
8888
expect(debugInfo.appOrigin, isNotNull);
8989
expect(debugInfo.appUrl, isNotNull);
9090
expect(debugInfo.isInternalBuild, isNotNull);
91+
expect(debugInfo.isFlutterApp, isNotNull);
9192
await appTab.close();
9293
});
9394

dwds/web/client.dart

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,8 @@ Future<void>? main() {
181181
..appOrigin = window.location.origin
182182
..appUrl = window.location.href
183183
..extensionUrl = windowContext['\$dartExtensionUri']
184-
..isInternalBuild = windowContext['\$isInternalBuild'])));
184+
..isInternalBuild = windowContext['\$isInternalBuild']
185+
..isFlutterApp = windowContext['\$isFlutterApp'])));
185186

186187
dispatchEvent(CustomEvent('dart-app-ready', detail: debugInfoJson));
187188
}, (error, stackTrace) {
@@ -278,4 +279,7 @@ external set emitRegisterEvent(void Function(String) func);
278279
@JS(r'$isInternalBuild')
279280
external bool get isInternalBuild;
280281

282+
@JS(r'$isFlutterApp')
283+
external bool get isFlutterApp;
284+
281285
bool get _isChromium => window.navigator.vendor.contains('Google');

0 commit comments

Comments
 (0)