From 57b68a523defb097cfe9fcc9d55b0f3c85cb75b9 Mon Sep 17 00:00:00 2001 From: Hixie Date: Thu, 28 Jan 2016 12:26:27 -0800 Subject: [PATCH] Clean up shell.dart --- packages/flutter/lib/src/services/shell.dart | 74 +++++++++---------- .../flutter/test/services/mock_services.dart | 2 +- 2 files changed, 36 insertions(+), 40 deletions(-) diff --git a/packages/flutter/lib/src/services/shell.dart b/packages/flutter/lib/src/services/shell.dart index 93cb75393c..c0e373366b 100644 --- a/packages/flutter/lib/src/services/shell.dart +++ b/packages/flutter/lib/src/services/shell.dart @@ -7,51 +7,55 @@ import 'dart:ui_internals' as internals; import 'package:mojo/application.dart'; import 'package:mojo/bindings.dart' as bindings; import 'package:mojo/core.dart' as core; -import 'package:mojo/mojo/service_provider.mojom.dart'; -import 'package:mojo/mojo/shell.mojom.dart'; +import 'package:mojo/mojo/service_provider.mojom.dart' as mojom; +import 'package:mojo/mojo/shell.mojom.dart' as mojom; // A replacement for shell.connectToService. Implementations should return true // if they handled the request, or false if the request should fall through // to the default requestService. typedef bool OverrideConnectToService(String url, Object proxy); -// Set this to intercept calls to shell.connectToService and supply an -// alternative implementation of a service (for example, a mock for testing). -OverrideConnectToService overrideConnectToService; +class MojoShell { + MojoShell._(); -ShellProxy _initShellProxy() { - core.MojoHandle shellHandle = new core.MojoHandle(internals.takeShellProxyHandle()); - if (!shellHandle.isValid) - return null; - return new ShellProxy.fromHandle(shellHandle); -} + static mojom.ShellProxy _initShellProxy() { + core.MojoHandle shellHandle = new core.MojoHandle(internals.takeShellProxyHandle()); + if (!shellHandle.isValid) + return null; + return new mojom.ShellProxy.fromHandle(shellHandle); + } + static final mojom.Shell _shell = _initShellProxy()?.ptr; -ApplicationConnection _initEmbedderConnection() { - core.MojoHandle servicesHandle = new core.MojoHandle(internals.takeServicesProvidedByEmbedder()); - core.MojoHandle exposedServicesHandle = new core.MojoHandle(internals.takeServicesProvidedToEmbedder()); - if (!servicesHandle.isValid || !exposedServicesHandle.isValid) - return null; - ServiceProviderProxy services = new ServiceProviderProxy.fromHandle(servicesHandle); - ServiceProviderStub exposedServices = new ServiceProviderStub.fromHandle(exposedServicesHandle); - return new ApplicationConnection(exposedServices, services); -} - -final ShellProxy _shellProxy = _initShellProxy(); -final Shell _shell = _shellProxy?.ptr; -final ApplicationConnection _embedderConnection = _initEmbedderConnection(); - -class _Shell { - _Shell._(); + static ApplicationConnection _initEmbedderConnection() { + core.MojoHandle servicesHandle = new core.MojoHandle(internals.takeServicesProvidedByEmbedder()); + core.MojoHandle exposedServicesHandle = new core.MojoHandle(internals.takeServicesProvidedToEmbedder()); + if (!servicesHandle.isValid || !exposedServicesHandle.isValid) + return null; + mojom.ServiceProviderProxy services = new mojom.ServiceProviderProxy.fromHandle(servicesHandle); + mojom.ServiceProviderStub exposedServices = new mojom.ServiceProviderStub.fromHandle(exposedServicesHandle); + return new ApplicationConnection(exposedServices, services); + } + static final ApplicationConnection _embedderConnection = _initEmbedderConnection(); ApplicationConnection connectToApplication(String url) { if (_shell == null) return null; - ServiceProviderProxy services = new ServiceProviderProxy.unbound(); - ServiceProviderStub exposedServices = new ServiceProviderStub.unbound(); + mojom.ServiceProviderProxy services = new mojom.ServiceProviderProxy.unbound(); + mojom.ServiceProviderStub exposedServices = new mojom.ServiceProviderStub.unbound(); _shell.connectToApplication(url, services, exposedServices); return new ApplicationConnection(exposedServices, services); } + // Set this to intercept calls to shell.connectToService and supply an + // alternative implementation of a service (for example, a mock for testing). + OverrideConnectToService overrideConnectToService; + + void connectToService(String url, bindings.ProxyBase proxy) { + if (overrideConnectToService != null && overrideConnectToService(url, proxy)) + return; + _connectToService(url, proxy); + } + void _connectToService(String url, bindings.ProxyBase proxy) { if (_shell == null || url == null) { // If we don't have a shell or a url, we try to get the services from the @@ -59,20 +63,12 @@ class _Shell { _embedderConnection?.requestService(proxy); return; } - - ServiceProviderProxy services = new ServiceProviderProxy.unbound(); + mojom.ServiceProviderProxy services = new mojom.ServiceProviderProxy.unbound(); _shell.connectToApplication(url, services, null); core.MojoMessagePipe pipe = new core.MojoMessagePipe(); proxy.impl.bind(pipe.endpoints[0]); services.ptr.connectToService(proxy.serviceName, pipe.endpoints[1]); services.close(); } - - void connectToService(String url, bindings.ProxyBase proxy) { - if (overrideConnectToService != null && overrideConnectToService(url, proxy)) - return; - _connectToService(url, proxy); - } } - -final _Shell shell = new _Shell._(); +final MojoShell shell = new MojoShell._(); diff --git a/packages/flutter/test/services/mock_services.dart b/packages/flutter/test/services/mock_services.dart index 955cbe5cae..381426c031 100644 --- a/packages/flutter/test/services/mock_services.dart +++ b/packages/flutter/test/services/mock_services.dart @@ -1,4 +1,4 @@ -import 'package:flutter/src/services/shell.dart' as shell; +import 'package:flutter/src/services/shell.dart'; import 'package:mojo/bindings.dart' as bindings; // Tests can use ServiceMocker to register replacement implementations