|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +@JS('chrome') |
| 6 | +library background; |
| 7 | + |
| 8 | +import 'package:js/js.dart'; |
| 9 | +import 'package:sse/client/sse_client.dart'; |
| 10 | + |
| 11 | +// GENERATE: |
| 12 | +// pub run build_runner build web -o build -r |
| 13 | +void main() { |
| 14 | + addListener(allowInterop((e) { |
| 15 | + var query = QueryInfo(active: true, currentWindow: true); |
| 16 | + Tab currentTab; |
| 17 | + |
| 18 | + // Sends commands to debugger attached to the current tab. |
| 19 | + // |
| 20 | + // Extracts the extension backend port from the injected JS. |
| 21 | + var callback = allowInterop((List<Tab> tabs) { |
| 22 | + currentTab = tabs[0]; |
| 23 | + attach(Debuggee(tabId: currentTab.id), '1.3', allowInterop(() {})); |
| 24 | + sendCommand(Debuggee(tabId: currentTab.id), 'Debugger.enable', |
| 25 | + CommandParams(), allowInterop((e) {})); |
| 26 | + sendCommand(Debuggee(tabId: currentTab.id), 'Runtime.evaluate', |
| 27 | + CommandParams(expression: '\$extensionPort'), |
| 28 | + allowInterop((RemoteObject e) { |
| 29 | + var port = e.result.value; |
| 30 | + startSseClient(port); |
| 31 | + })); |
| 32 | + }); |
| 33 | + |
| 34 | + queryTabs(query, allowInterop((List tabs) { |
| 35 | + callback(List.from(tabs)); |
| 36 | + })); |
| 37 | + })); |
| 38 | +} |
| 39 | + |
| 40 | +// Starts an SSE client. |
| 41 | +// |
| 42 | +// Creates 2 channels which connect to the SSE handler at the extension |
| 43 | +// backend, send a simple message. |
| 44 | +Future<void> startSseClient(port) async { |
| 45 | + var channel = SseClient('http://localhost:$port/test'); |
| 46 | + channel.stream.listen((s) { |
| 47 | + channel.sink.add('This is channel 1.'); |
| 48 | + }, onError: (e) { |
| 49 | + channel.close(); |
| 50 | + }, cancelOnError: true); |
| 51 | + |
| 52 | + var channel2 = SseClient('http://localhost:$port/test'); |
| 53 | + await channel2.onOpen.first; |
| 54 | + channel2.sink.add('This is channel 2.'); |
| 55 | +} |
| 56 | + |
| 57 | +@JS('browserAction.onClicked.addListener') |
| 58 | +external void addListener(Function callback); |
| 59 | + |
| 60 | +@JS('debugger.sendCommand') |
| 61 | +external void sendCommand(Debuggee target, String method, |
| 62 | + CommandParams commandParams, Function callback); |
| 63 | + |
| 64 | +@JS('debugger.attach') |
| 65 | +external void attach( |
| 66 | + Debuggee target, String requiredVersion, Function callback); |
| 67 | + |
| 68 | +@JS('tabs.query') |
| 69 | +external void queryTabs(QueryInfo queryInfo, Function callback); |
| 70 | + |
| 71 | +@JS() |
| 72 | +@anonymous |
| 73 | +class QueryInfo { |
| 74 | + external bool get active; |
| 75 | + external bool get currentWindow; |
| 76 | + external factory QueryInfo({bool active, bool currentWindow}); |
| 77 | +} |
| 78 | + |
| 79 | +@JS() |
| 80 | +@anonymous |
| 81 | +class Debuggee { |
| 82 | + external dynamic get tabId; |
| 83 | + external String get extensionId; |
| 84 | + external String get targetId; |
| 85 | + external factory Debuggee( |
| 86 | + {dynamic tabId, String extensionId, String targetId}); |
| 87 | +} |
| 88 | + |
| 89 | +@JS() |
| 90 | +@anonymous |
| 91 | +class CommandParams { |
| 92 | + external String get expression; |
| 93 | + external factory CommandParams({String expression}); |
| 94 | +} |
| 95 | + |
| 96 | +@JS() |
| 97 | +@anonymous |
| 98 | +class Tab { |
| 99 | + external int get id; |
| 100 | +} |
| 101 | + |
| 102 | +@JS() |
| 103 | +@anonymous |
| 104 | +class RemoteObject { |
| 105 | + external EvaluationResult get result; |
| 106 | +} |
| 107 | + |
| 108 | +@JS() |
| 109 | +@anonymous |
| 110 | +class EvaluationResult { |
| 111 | + external dynamic get value; |
| 112 | +} |
0 commit comments