Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a warning in the Dart Debug Extension when a user tries to debug a sharded Dart app #1462

Merged
merged 13 commits into from
Dec 10, 2021
27 changes: 26 additions & 1 deletion dwds/debug_extension/web/background.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ const _allowedEvents = {'Overlay.inspectNodeRequested'};
// Map of Chrome tab ID to encoded vm service protocol URI.
final _tabIdToEncodedUri = <int, String>{};

// Map of Chrome tab ID to warnings for that tab.
final _tabIdToWarning = <int, String>{};

final _debuggableTabs = <int>{};

final _tabsToAttach = <Tab>{};
Expand Down Expand Up @@ -67,6 +70,12 @@ void main() {
var callback = allowInterop((List<Tab> tabs) async {
currentTab = tabs[0];
if (!_debuggableTabs.contains(currentTab.id)) return;

if (_tabIdToWarning.containsKey(currentTab.id)) {
alert(_tabIdToWarning[currentTab.id]);
return;
}

attach(Debuggee(tabId: currentTab.id), '1.3', allowInterop(() async {
if (lastError != null) {
String alertMessage;
Expand Down Expand Up @@ -100,6 +109,10 @@ void main() {

onMessageAddListener(allowInterop(
(Request request, Sender sender, Function sendResponse) async {
// Register any warnings for the tab:
if (request.warning != '') {
_tabIdToWarning[sender.tab.id] = request.warning;
}
_debuggableTabs.add(sender.tab.id);
_updateIcon();
// TODO(grouma) - We can conditionally auto start debugging here.
Expand Down Expand Up @@ -362,9 +375,20 @@ void _updateIcon() {
var query = QueryInfo(active: true, currentWindow: true);
queryTabs(query, allowInterop((List tabs) {
var tabList = List<Tab>.from(tabs);
if (tabList.isEmpty || _debuggableTabs.contains(tabList.first.id)) {
// If tabList is empty, the user has likely navigated to a different window.
// Therefore, do not update the icon:
if (tabList.isEmpty || tabList.first == null || tabList.first.id == null) {
return;
}

if (_tabIdToWarning.containsKey(tabList.first.id)) {
// Set the warning icon (red):
setIcon(IconInfo(path: 'dart_warning.png'));
} else if (_debuggableTabs.contains(tabList.first.id)) {
// Set the debuggable icon (blue):
setIcon(IconInfo(path: 'dart.png'));
} else {
// Set the default icon (grey):
setIcon(IconInfo(path: 'dart_grey.png'));
}
}));
Expand Down Expand Up @@ -514,6 +538,7 @@ class Request {
external int get tabId;
external String get name;
external dynamic get options;
external String get warning;
external factory Request({int tabId, String name, dynamic options});
}

Expand Down
50 changes: 23 additions & 27 deletions dwds/debug_extension/web/content.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
var hasSent = false;
const MULTIPLE_APPS_ATTRIBUTE = 'data-multiple-dart-apps';

const MULTIPLE_APPS_WARNING = 'It appears that you are running multiple Dart apps ' +
'and/or sub-apps. Dart debugging is currently not supported in a multi-app ' +
'environment.';

function sendMessage(e) {
if (hasSent) return;
hasSent = true;
chrome.runtime.sendMessage(e, function (response) {
});
const hasMultipleApps = document
.documentElement
.getAttribute(MULTIPLE_APPS_ATTRIBUTE);
const warning = hasMultipleApps == 'true' ? MULTIPLE_APPS_WARNING : '';
chrome.runtime.sendMessage(Object.assign(e, {warning: warning}));
}

document.addEventListener('dart-app-ready', function (e) {
document.addEventListener('dart-app-ready', function(e) {
sendMessage(e);
});

var targetNode = document.head;
var observerOptions = {
childList: true,
attributes: true,
subtree: true
};

function callback(mutationList, observer) {
mutationList.forEach((mutation) => {
if (mutation.type == 'childList') {
var addedNodes = mutation.addedNodes
for (var i = 0; i < addedNodes.length; i++) {
if (addedNodes[i].tagName == 'SCRIPT' &&
addedNodes[i].src.includes('dart_sdk.js')) {
sendMessage({});
}
}
function multipleDartAppsCallback(mutationList) {
mutationList.forEach(function(mutation) {
if (mutation.type !== "attributes") return;
if (mutation.attributeName === MULTIPLE_APPS_ATTRIBUTE) {
sendMessage({});
}
});
}
};

// Watch for changes to the multiple apps data-attribute and update accordingly:
var multipleDartAppsObserver = new MutationObserver(multipleDartAppsCallback);
multipleDartAppsObserver.observe(document.documentElement, {
attributeFilter: [MULTIPLE_APPS_ATTRIBUTE]
});

// TODO(grouma) - This will become unnecessary when package:dwds version 11.0.0
// hits Flutter stable.
var observer = new MutationObserver(callback);
observer.observe(targetNode, observerOptions);

Binary file added dwds/debug_extension/web/dart_warning.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.