diff --git a/dwds/debug_extension_mv3/.gitignore b/dwds/debug_extension_mv3/.gitignore new file mode 100644 index 000000000..4878cbe8b --- /dev/null +++ b/dwds/debug_extension_mv3/.gitignore @@ -0,0 +1,3 @@ +build/ +compiled/ +extension_key.txt diff --git a/dwds/debug_extension_mv3/build.yaml b/dwds/debug_extension_mv3/build.yaml new file mode 100644 index 000000000..cdffb3aa9 --- /dev/null +++ b/dwds/debug_extension_mv3/build.yaml @@ -0,0 +1,26 @@ +targets: + $default: + builders: + build_web_compilers|entrypoint: + options: + dart2js_args: + - -O1 # Note: Change to -04 for minified JS. + - --csp + generate_for: + - web/**.dart + mv3_extension|client_js_copy_builder: + enabled: true + +builders: + client_js_copy_builder: + import: "tool/copy_builder.dart" + builder_factories: + - copyBuilder + build_extensions: + { + "web/{{}}.dart.js": ["compiled/{{}}.dart.js"], + "web/{{}}.png": ["compiled/{{}}.png"], + "web/manifest.json": ["compiled/manifest.json"], + } + auto_apply: none + build_to: source diff --git a/dwds/debug_extension_mv3/pubspec.yaml b/dwds/debug_extension_mv3/pubspec.yaml new file mode 100644 index 000000000..b94ad1564 --- /dev/null +++ b/dwds/debug_extension_mv3/pubspec.yaml @@ -0,0 +1,17 @@ +name: mv3_extension +publish_to: none +version: 1.30.0 +homepage: https://github.com/dart-lang/webdev +description: >- + A Chrome extension for Dart debugging. + +environment: + sdk: '>=2.12.0 <3.0.0' + +dependencies: + js: ^0.6.1+1 + +dev_dependencies: + build: ^2.0.0 + build_web_compilers: ^3.0.0 + build_runner: ^2.0.6 diff --git a/dwds/debug_extension_mv3/tool/build_extension.sh b/dwds/debug_extension_mv3/tool/build_extension.sh new file mode 100755 index 000000000..4600bb4b4 --- /dev/null +++ b/dwds/debug_extension_mv3/tool/build_extension.sh @@ -0,0 +1,20 @@ +#!/bin/bash + +# Copyright 2022 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +# INSTRUCTIONS: + +# Builds the unminifed dart2js app (see DDC issue: https://github.com/dart-lang/sdk/issues/49869): +# ./tool/build_extension.sh + +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +echo "Building dart2js-compiled extension to /compiled directory." +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +dart run build_runner build web --output build --release + +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +echo "Updating manifest.json in /compiled directory." +echo "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" +dart tool/update_dev_files.dart diff --git a/dwds/debug_extension_mv3/tool/copy_builder.dart b/dwds/debug_extension_mv3/tool/copy_builder.dart new file mode 100644 index 000000000..07a0c79ab --- /dev/null +++ b/dwds/debug_extension_mv3/tool/copy_builder.dart @@ -0,0 +1,40 @@ +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:build/build.dart'; + +/// Factory for the build script. +Builder copyBuilder(_) => _CopyBuilder(); + +class _CopyBuilder extends Builder { + @override + Map> get buildExtensions => { + "web/{{}}.dart.js": ["compiled/{{}}.dart.js"], + "web/{{}}.png": ["compiled/{{}}.png"], + "web/manifest.json": ["compiled/manifest.json"], + }; + + @override + void build(BuildStep buildStep) async { + final inputAsset = buildStep.inputId; + final allowedOutputs = buildStep.allowedOutputs; + + if (allowedOutputs.length != 1) { + return; + } + + final outputAsset = allowedOutputs.first; + await _copyBinaryFile(buildStep, + inputAsset: inputAsset, outputAsset: outputAsset); + } + + Future _copyBinaryFile( + BuildStep buildStep, { + required AssetId inputAsset, + required AssetId outputAsset, + }) { + return buildStep.writeAsBytes( + outputAsset, buildStep.readAsBytes(inputAsset)); + } +} diff --git a/dwds/debug_extension_mv3/tool/update_dev_files.dart b/dwds/debug_extension_mv3/tool/update_dev_files.dart new file mode 100644 index 000000000..22bb4c4cb --- /dev/null +++ b/dwds/debug_extension_mv3/tool/update_dev_files.dart @@ -0,0 +1,78 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'dart:io'; + +void main() async { + _updateManifestJson(); +} + +/// Adds the Googler extension key. +Future _updateManifestJson() async { + final manifestJson = File('compiled/manifest.json'); + final extensionKeyTxt = File('extension_key.txt'); + final extensionKey = await extensionKeyTxt.exists() + ? await extensionKeyTxt.readAsString() + : null; + _transformDevFile(manifestJson, (line) { + if (_matchesKey(line: line, key: 'name')) { + return [ + line, + if (extensionKey != null) + _newKeyValue( + oldLine: line, + newKey: 'key', + newValue: extensionKey, + ), + ]; + } else { + return [line]; + } + }); +} + +Future _transformDevFile( + File devFile, List Function(String) transformLine) async { + final lines = devFile.readAsLinesSync(); + final newLines = []; + for (final line in lines) { + newLines.addAll(transformLine(line)); + } + final content = newLines.joinWithNewLine(); + return devFile.writeAsStringSync(content); +} + +bool _matchesKey({required String line, required String key}) { + return line.trimLeft().startsWith('"$key":'); +} + +String _newKeyValue({ + required String oldLine, + String? newKey, + String? newValue, +}) { + final lineStart = oldLine.leftPadding(); + final key = newKey != null ? '"$newKey": ' : ''; + final value = newValue != null ? '"$newValue"' : ''; + final lineEnd = oldLine.trim().endsWith(',') ? ',' : ''; + return '$lineStart$key$value$lineEnd'; +} + +extension LeftPaddingExtension on String { + String leftPadding() { + String padding = ''; + int idx = 0; + while (idx < length && this[idx] == ' ') { + padding += ' '; + idx++; + } + return padding; + } +} + +extension JoinExtension on List { + String joinWithNewLine() { + return '${join('\n')}\n'; + } +} diff --git a/dwds/debug_extension_mv3/web/background.dart b/dwds/debug_extension_mv3/web/background.dart new file mode 100644 index 000000000..b1478cba4 --- /dev/null +++ b/dwds/debug_extension_mv3/web/background.dart @@ -0,0 +1,19 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +@JS() +library background; + +import 'package:js/js.dart'; + +import 'chrome_api.dart'; +import 'web_api.dart'; + +void main() { + console.log('Running Dart Debug Extension.'); + // Detect clicks on the Dart Debug Extension icon. + chrome.action.onClicked.addListener(allowInterop((_) { + console.log('Detected click on the Dart Debug Extension icon.'); + })); +} diff --git a/dwds/debug_extension_mv3/web/chrome_api.dart b/dwds/debug_extension_mv3/web/chrome_api.dart new file mode 100644 index 000000000..9bc20b02e --- /dev/null +++ b/dwds/debug_extension_mv3/web/chrome_api.dart @@ -0,0 +1,34 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:js/js.dart'; + +@JS() +external Chrome get chrome; + +@JS() +@anonymous +class Chrome { + external Action get action; +} + +@JS() +@anonymous +class Action { + // https://developer.chrome.com/docs/extensions/reference/action/#event-onClicked + external OnClickedHandler get onClicked; +} + +@JS() +@anonymous +class OnClickedHandler { + external void addListener(void Function(Tab tab) callback); +} + +@JS() +@anonymous +class Tab { + external int get id; + external String get url; +} diff --git a/dwds/debug_extension_mv3/web/dart_dev.png b/dwds/debug_extension_mv3/web/dart_dev.png new file mode 100644 index 000000000..8d0479209 Binary files /dev/null and b/dwds/debug_extension_mv3/web/dart_dev.png differ diff --git a/dwds/debug_extension_mv3/web/manifest.json b/dwds/debug_extension_mv3/web/manifest.json new file mode 100644 index 000000000..4434a260a --- /dev/null +++ b/dwds/debug_extension_mv3/web/manifest.json @@ -0,0 +1,11 @@ +{ + "name": "[MV3] Dart Debug Extension", + "version": "1.0", + "manifest_version": 3, + "action": { + "default_icon": "dart_dev.png" + }, + "background": { + "service_worker": "background.dart.js" + } +} diff --git a/dwds/debug_extension_mv3/web/web_api.dart b/dwds/debug_extension_mv3/web/web_api.dart new file mode 100644 index 000000000..8b927b405 --- /dev/null +++ b/dwds/debug_extension_mv3/web/web_api.dart @@ -0,0 +1,15 @@ +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +import 'package:js/js.dart'; + +@JS() +external Console get console; + +@JS() +@anonymous +class Console { + external void log(String header, + [String style1, String style2, String style3]); +}