initServiceExtensions method Null safety

void initServiceExtensions(
  1. RegisterServiceExtensionCallback registerServiceExtensionCallback
)

Called to register service extensions.

See also:

Implementation

void initServiceExtensions(RegisterServiceExtensionCallback registerServiceExtensionCallback) {
  final FlutterExceptionHandler defaultExceptionHandler = FlutterError.presentError;

  if (isStructuredErrorsEnabled()) {
    FlutterError.presentError = _reportStructuredError;
  }
  _registerServiceExtensionCallback = registerServiceExtensionCallback;
  assert(!_debugServiceExtensionsRegistered);
  assert(() {
    _debugServiceExtensionsRegistered = true;
    return true;
  }());

  SchedulerBinding.instance.addPersistentFrameCallback(_onFrameStart);

  _registerBoolServiceExtension(
    name: WidgetInspectorServiceExtensions.structuredErrors.name,
    getter: () async => FlutterError.presentError == _reportStructuredError,
    setter: (bool value) {
      FlutterError.presentError = value ? _reportStructuredError : defaultExceptionHandler;
      return Future<void>.value();
    },
  );

  _registerBoolServiceExtension(
    name: WidgetInspectorServiceExtensions.show.name,
    getter: () async => WidgetsApp.debugShowWidgetInspectorOverride,
    setter: (bool value) {
      if (WidgetsApp.debugShowWidgetInspectorOverride == value) {
        return Future<void>.value();
      }
      WidgetsApp.debugShowWidgetInspectorOverride = value;
      return forceRebuild();
    },
  );

  if (isWidgetCreationTracked()) {
    // Service extensions that are only supported if widget creation locations
    // are tracked.
    _registerBoolServiceExtension(
      name: WidgetInspectorServiceExtensions.trackRebuildDirtyWidgets.name,
      getter: () async => _trackRebuildDirtyWidgets,
      setter: (bool value) async {
        if (value == _trackRebuildDirtyWidgets) {
          return;
        }
        _rebuildStats.resetCounts();
        _trackRebuildDirtyWidgets = value;
        if (value) {
          assert(debugOnRebuildDirtyWidget == null);
          debugOnRebuildDirtyWidget = _onRebuildWidget;
          // Trigger a rebuild so there are baseline stats for rebuilds
          // performed by the app.
          await forceRebuild();
          return;
        } else {
          debugOnRebuildDirtyWidget = null;
          return;
        }
      },
    );

    _registerBoolServiceExtension(
      name: WidgetInspectorServiceExtensions.trackRepaintWidgets.name,
      getter: () async => _trackRepaintWidgets,
      setter: (bool value) async {
        if (value == _trackRepaintWidgets) {
          return;
        }
        _repaintStats.resetCounts();
        _trackRepaintWidgets = value;
        if (value) {
          assert(debugOnProfilePaint == null);
          debugOnProfilePaint = _onPaint;
          // Trigger an immediate paint so the user has some baseline painting
          // stats to view.
          void markTreeNeedsPaint(RenderObject renderObject) {
            renderObject.markNeedsPaint();
            renderObject.visitChildren(markTreeNeedsPaint);
          }
          final RenderObject root = RendererBinding.instance.renderView;
          markTreeNeedsPaint(root);
        } else {
          debugOnProfilePaint = null;
        }
      },
    );
  }

  _registerSignalServiceExtension(
    name: WidgetInspectorServiceExtensions.disposeAllGroups.name,
    callback: () async {
      disposeAllGroups();
      return null;
    },
  );
  _registerObjectGroupServiceExtension(
    name: WidgetInspectorServiceExtensions.disposeGroup.name,
    callback: (String name) async {
      disposeGroup(name);
      return null;
    },
  );
  _registerSignalServiceExtension(
    name: WidgetInspectorServiceExtensions.isWidgetTreeReady.name,
    callback: isWidgetTreeReady,
  );
  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.disposeId.name,
    callback: (String? objectId, String objectGroup) async {
      disposeId(objectId, objectGroup);
      return null;
    },
  );
  _registerServiceExtensionVarArgs(
    name: WidgetInspectorServiceExtensions.setPubRootDirectories.name,
    callback: (List<String> args) async {
      setPubRootDirectories(args);
      return null;
    },
  );
  _registerServiceExtensionVarArgs(
    name: WidgetInspectorServiceExtensions.addPubRootDirectories.name,
    callback: (List<String> args) async {
      addPubRootDirectories(args);
      return null;
    },
  );
  _registerServiceExtensionVarArgs(
    name: WidgetInspectorServiceExtensions.removePubRootDirectories.name,
    callback: (List<String> args) async {
      removePubRootDirectories(args);
      return null;
    },
  );
  registerServiceExtension(
    name: WidgetInspectorServiceExtensions.getPubRootDirectories.name,
    callback: pubRootDirectories,
  );
  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.setSelectionById.name,
    callback: setSelectionById,
  );
  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.getParentChain.name,
    callback: _getParentChain,
  );
  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.getProperties.name,
    callback: _getProperties,
  );
  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.getChildren.name,
    callback: _getChildren,
  );

  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.getChildrenSummaryTree.name,
    callback: _getChildrenSummaryTree,
  );

  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.getChildrenDetailsSubtree.name,
    callback: _getChildrenDetailsSubtree,
  );

  _registerObjectGroupServiceExtension(
    name: WidgetInspectorServiceExtensions.getRootWidget.name,
    callback: _getRootWidget,
  );
  _registerObjectGroupServiceExtension(
    name: WidgetInspectorServiceExtensions.getRootRenderObject.name,
    callback: _getRootRenderObject,
  );
  _registerObjectGroupServiceExtension(
    name: WidgetInspectorServiceExtensions.getRootWidgetSummaryTree.name,
    callback: _getRootWidgetSummaryTree,
  );
  registerServiceExtension(
    name: WidgetInspectorServiceExtensions.getRootWidgetSummaryTreeWithPreviews.name,
    callback: _getRootWidgetSummaryTreeWithPreviews,
  );
  registerServiceExtension(
    name: WidgetInspectorServiceExtensions.getDetailsSubtree.name,
    callback: (Map<String, String> parameters) async {
      assert(parameters.containsKey('objectGroup'));
      final String? subtreeDepth = parameters['subtreeDepth'];
      return <String, Object?>{
        'result': _getDetailsSubtree(
          parameters['arg'],
          parameters['objectGroup'],
          subtreeDepth != null ? int.parse(subtreeDepth) : 2,
        ),
      };
    },
  );
  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.getSelectedRenderObject.name,
    callback: _getSelectedRenderObject,
  );
  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.getSelectedWidget.name,
    callback: _getSelectedWidget,
  );
  _registerServiceExtensionWithArg(
    name: WidgetInspectorServiceExtensions.getSelectedSummaryWidget.name,
    callback: _getSelectedSummaryWidget,
  );

  _registerSignalServiceExtension(
    name: WidgetInspectorServiceExtensions.isWidgetCreationTracked.name,
    callback: isWidgetCreationTracked,
  );
  registerServiceExtension(
    name: WidgetInspectorServiceExtensions.screenshot.name,
    callback: (Map<String, String> parameters) async {
      assert(parameters.containsKey('id'));
      assert(parameters.containsKey('width'));
      assert(parameters.containsKey('height'));

      final ui.Image? image = await screenshot(
        toObject(parameters['id']),
        width: double.parse(parameters['width']!),
        height: double.parse(parameters['height']!),
        margin: parameters.containsKey('margin') ?
            double.parse(parameters['margin']!) : 0.0,
        maxPixelRatio: parameters.containsKey('maxPixelRatio') ?
            double.parse(parameters['maxPixelRatio']!) : 1.0,
        debugPaint: parameters['debugPaint'] == 'true',
      );
      if (image == null) {
        return <String, Object?>{'result': null};
      }
      final ByteData? byteData = await image.toByteData(format:ui.ImageByteFormat.png);

      return <String, Object>{
        'result': base64.encoder.convert(Uint8List.view(byteData!.buffer)),
      };
    },
  );
  registerServiceExtension(
    name: WidgetInspectorServiceExtensions.getLayoutExplorerNode.name,
    callback: _getLayoutExplorerNode,
  );
  registerServiceExtension(
    name: WidgetInspectorServiceExtensions.setFlexFit.name,
    callback: _setFlexFit,
  );
  registerServiceExtension(
    name: WidgetInspectorServiceExtensions.setFlexFactor.name,
    callback: _setFlexFactor,
  );
  registerServiceExtension(
    name: WidgetInspectorServiceExtensions.setFlexProperties.name,
    callback: _setFlexProperties,
  );
}