getTabs method

Future<List<ChromeTab>> getTabs({
  1. Duration? retryFor,
})

Return all the available tabs.

This method can potentially throw a ConnectionException on some protocol issues.

An optional retryFor duration can be used to automatically re-try connections for some period of time. Anecdotally, Chrome can return errors when trying to list the available tabs very early in its startup sequence.

Implementation

Future<List<ChromeTab>> getTabs({
  Duration? retryFor,
}) async {
  final start = DateTime.now();
  DateTime? end = retryFor == null ? null : start.add(retryFor);

  var response = await getUrl('/json');
  var responseBody = await utf8.decodeStream(response.cast<List<int>>());

  late List decoded;
  while (true) {
    try {
      decoded = jsonDecode(responseBody);
      return List<ChromeTab>.from(decoded.map((m) => ChromeTab(m as Map)));
    } on FormatException catch (formatException) {
      if (end != null && end.isBefore(DateTime.now())) {
        // Delay for retryFor / 4 milliseconds.
        await Future.delayed(
          Duration(milliseconds: retryFor!.inMilliseconds ~/ 4),
        );
      } else {
        throw ConnectionException(
          formatException: formatException,
          responseStatus: '${response.statusCode} ${response.reasonPhrase}',
          responseBody: responseBody,
        );
      }
    }
  }
}