desktop.dart 2.1 KB
Newer Older
1
// Copyright 2019 The Chromium Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'dart:async';

7
import 'base/io.dart';
8
import 'base/platform.dart';
9
import 'base/process_manager.dart';
10 11
import 'convert.dart';
import 'device.dart';
12
import 'version.dart';
13

14
// Only launch or display desktop embedding devices if
15
// `ENABLE_FLUTTER_DESKTOP` environment variable is set to true.
16
bool get flutterDesktopEnabled {
17
  _flutterDesktopEnabled ??= platform.environment['ENABLE_FLUTTER_DESKTOP']?.toLowerCase() == 'true';
18
  return _flutterDesktopEnabled && !FlutterVersion.instance.isStable;
19
}
20
bool _flutterDesktopEnabled;
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53

/// Kills a process on linux or macOS.
Future<bool> killProcess(String executable) async {
  final RegExp whitespace = RegExp(r'\s+');
  bool succeeded = true;
  try {
    final ProcessResult result = await processManager.run(<String>[
      'ps', 'aux',
    ]);
    if (result.exitCode != 0) {
      return false;
    }
    final List<String> lines = result.stdout.split('\n');
    for (String line in lines) {
      if (!line.contains(executable)) {
        continue;
      }
      final List<String> values = line.split(whitespace);
      if (values.length < 2) {
        continue;
      }
      final String pid = values[1];
      final ProcessResult killResult = await processManager.run(<String>[
        'kill', pid,
      ]);
      succeeded &= killResult.exitCode == 0;
    }
    return true;
  } on ArgumentError {
    succeeded = false;
  }
  return succeeded;
}
54 55

class DesktopLogReader extends DeviceLogReader {
56
  final StreamController<List<int>> _inputController = StreamController<List<int>>.broadcast();
57 58

  void initializeProcess(Process process) {
59 60 61 62 63
    process.stdout.listen(_inputController.add);
    process.stderr.listen(_inputController.add);
    process.exitCode.then((int result) {
      _inputController.close();
    });
64 65 66 67
  }

  @override
  Stream<String> get logLines {
68 69 70
    return _inputController.stream
      .transform(utf8.decoder)
      .transform(const LineSplitter());
71 72 73 74 75
  }

  @override
  String get name => 'desktop';
}