@@ -246,6 +246,10 @@ Future<void> _maybeConnectToDwds(int tabId, Object? params) async {
246246 if (debugInfo == null ) return ;
247247 if (contextOrigin != debugInfo.appOrigin) return ;
248248 final contextId = context['id' ] as int ;
249+ // Find the correct frame to connect to (this is necessary if the Dart app is
250+ // embedded in an IFRAME):
251+ final isDartFrame = await _isDartFrame (tabId: tabId, contextId: contextId);
252+ if (! isDartFrame) return ;
249253 final connected = await _connectToDwds (
250254 dartAppContextId: contextId,
251255 dartAppTabId: tabId,
@@ -258,6 +262,33 @@ Future<void> _maybeConnectToDwds(int tabId, Object? params) async {
258262 }
259263}
260264
265+ Future <bool > _isDartFrame ({required int tabId, required int contextId}) {
266+ final completer = Completer <bool >();
267+ chrome.debugger.sendCommand (
268+ Debuggee (tabId: tabId),
269+ 'Runtime.evaluate' ,
270+ _InjectedParams (
271+ expression:
272+ '[window.\$ dartAppId, window.\$ dartAppInstanceId, window.\$ dwdsVersion]' ,
273+ returnByValue: true ,
274+ contextId: contextId), allowInterop ((dynamic response) {
275+ final evalResponse = response as _EvalResponse ;
276+ final value = evalResponse.result.value;
277+ final appId = value? [0 ];
278+ final instanceId = value? [1 ];
279+ final dwdsVersion = value? [2 ];
280+ final frameIdentifier = 'Frame at tab $tabId with context $contextId ' ;
281+ if (appId == null || instanceId == null ) {
282+ debugWarn ('$frameIdentifier is not a Dart frame.' );
283+ completer.complete (false );
284+ } else {
285+ debugLog ('Dart $frameIdentifier is using DWDS $dwdsVersion .' );
286+ completer.complete (true );
287+ }
288+ }));
289+ return completer.future;
290+ }
291+
261292Future <bool > _connectToDwds ({
262293 required int dartAppContextId,
263294 required int dartAppTabId,
@@ -689,3 +720,25 @@ class _DebugSession {
689720 }
690721 }
691722}
723+
724+ @JS ()
725+ @anonymous
726+ class _EvalResponse {
727+ external _EvalResult get result;
728+ }
729+
730+ @JS ()
731+ @anonymous
732+ class _EvalResult {
733+ external List <String ?>? get value;
734+ }
735+
736+ @JS ()
737+ @anonymous
738+ class _InjectedParams {
739+ external String get expresion;
740+ external bool get returnByValue;
741+ external int get contextId;
742+ external factory _InjectedParams (
743+ {String ? expression, bool ? returnByValue, int ? contextId});
744+ }
0 commit comments