diff --git a/packages/flutter/lib/services.dart b/packages/flutter/lib/services.dart index 1d21784b6e..f9a0b861ab 100644 --- a/packages/flutter/lib/services.dart +++ b/packages/flutter/lib/services.dart @@ -21,4 +21,4 @@ export 'src/services/image_resource.dart'; export 'src/services/keyboard.dart'; export 'src/services/print.dart'; export 'src/services/service_registry.dart'; -export 'src/services/shell.dart'; + diff --git a/packages/flutter/lib/src/http/mojo_client.dart b/packages/flutter/lib/src/http/mojo_client.dart index 14cea26a20..ce579da9ac 100644 --- a/packages/flutter/lib/src/http/mojo_client.dart +++ b/packages/flutter/lib/src/http/mojo_client.dart @@ -6,7 +6,7 @@ import 'dart:async'; import 'dart:convert'; import 'dart:typed_data'; -import 'package:flutter/src/services/shell.dart'; +import 'package:flutter/services.dart'; import 'package:mojo_services/mojo/network_service.mojom.dart' as mojo; import 'package:mojo_services/mojo/url_loader.mojom.dart' as mojo; import 'package:mojo/core.dart' as mojo; diff --git a/packages/flutter/lib/src/rendering/binding.dart b/packages/flutter/lib/src/rendering/binding.dart index 78046f918c..8059138273 100644 --- a/packages/flutter/lib/src/rendering/binding.dart +++ b/packages/flutter/lib/src/rendering/binding.dart @@ -18,7 +18,7 @@ import 'semantics.dart'; export 'package:flutter/gestures.dart' show HitTestResult; /// The glue between the render tree and the Flutter engine. -abstract class Renderer extends Scheduler +abstract class Renderer extends Object with Scheduler, MojoShell implements HitTestable { void initInstances() { @@ -116,7 +116,7 @@ void debugDumpSemanticsTree() { /// A concrete binding for applications that use the Rendering framework /// directly. This is the glue that binds the framework to the Flutter engine. -class RenderingFlutterBinding extends BindingBase with Scheduler, Gesturer, Renderer { +class RenderingFlutterBinding extends BindingBase with Scheduler, Gesturer, MojoShell, Renderer { RenderingFlutterBinding({ RenderBox root }) { assert(renderView != null); renderView.child = root; diff --git a/packages/flutter/lib/src/services/activity.dart b/packages/flutter/lib/src/services/activity.dart index 4c3feaee2e..6bba0fbe93 100644 --- a/packages/flutter/lib/src/services/activity.dart +++ b/packages/flutter/lib/src/services/activity.dart @@ -7,7 +7,7 @@ import 'dart:async'; import 'package:sky_services/activity/activity.mojom.dart'; -import 'shell.dart'; +import 'binding.dart'; export 'package:sky_services/activity/activity.mojom.dart'; diff --git a/packages/flutter/lib/src/services/asset_bundle.dart b/packages/flutter/lib/src/services/asset_bundle.dart index d7e79a0f6b..48dbe076cf 100644 --- a/packages/flutter/lib/src/services/asset_bundle.dart +++ b/packages/flutter/lib/src/services/asset_bundle.dart @@ -16,7 +16,7 @@ import 'fetch.dart'; import 'image_cache.dart'; import 'image_decoder.dart'; import 'image_resource.dart'; -import 'shell.dart'; +import 'binding.dart'; abstract class AssetBundle { ImageResource loadImage(String key); diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart index c9cf8a3132..51c0dcff7b 100644 --- a/packages/flutter/lib/src/services/binding.dart +++ b/packages/flutter/lib/src/services/binding.dart @@ -2,6 +2,14 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +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' as mojom; +import 'package:mojo/mojo/shell.mojom.dart' as mojom; + /// Base class for mixins that provide singleton services (also known as /// "bindings"). /// @@ -30,3 +38,85 @@ abstract class BindingBase { assert(() { _debugInitialized = true; return true; }); } } + +// 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); + +abstract class MojoShell extends BindingBase { + + void initInstances() { + super.initInstances(); + _instance = this; + } + + static MojoShell _instance; + static MojoShell get instance => _instance; + + static mojom.ShellProxy _initShellProxy() { + core.MojoHandle shellHandle = new core.MojoHandle(internals.takeShellProxyHandle()); + if (!shellHandle.isValid) + return null; + return new mojom.ShellProxy.fromHandle(shellHandle); + } + final mojom.Shell _shell = _initShellProxy()?.ptr; + + 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); + } + final ApplicationConnection _embedderConnection = _initEmbedderConnection(); + + /// Attempts to connect to an application via the Mojo shell. + ApplicationConnection connectToApplication(String url) { + if (_shell == null) + return null; + 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 [connectToService()] and supply an + /// alternative implementation of a service (for example, a mock for testing). + OverrideConnectToService overrideConnectToService; + + /// Attempts to connect to a service implementing the interface for the given proxy. + /// If an application URL is specified, the service will be requested from that application. + /// Otherwise, it will be requested from the embedder (the Flutter engine). + void connectToService(String url, bindings.ProxyBase proxy) { + if (overrideConnectToService != null && overrideConnectToService(url, proxy)) + return; + if (url == null || _shell == null) { + // If the application URL is null, it means the service to connect + // to is one provided by the embedder. + // If the applircation URL isn't null but there's no shell, then + // ask the embedder in case it provides it. (For example, if you're + // running on Android without the Mojo shell, then you can obtain + // the media service from the embedder directly, instead of having + // to ask the media application for it.) + // This makes it easier to write an application that works both + // with and without a Mojo environment. + _embedderConnection?.requestService(proxy); + return; + } + 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(); + } + + /// Registers a service to expose to the embedder. + void provideService(String interfaceName, ServiceFactory factory) { + _embedderConnection?.provideService(interfaceName, factory); + } +} +MojoShell get shell => MojoShell.instance; diff --git a/packages/flutter/lib/src/services/keyboard.dart b/packages/flutter/lib/src/services/keyboard.dart index 8e58165ef4..8f64cad151 100644 --- a/packages/flutter/lib/src/services/keyboard.dart +++ b/packages/flutter/lib/src/services/keyboard.dart @@ -6,7 +6,7 @@ import 'dart:async'; import 'package:mojo_services/keyboard/keyboard.mojom.dart'; -import 'shell.dart'; +import 'binding.dart'; export 'package:mojo_services/keyboard/keyboard.mojom.dart'; diff --git a/packages/flutter/lib/src/services/shell.dart b/packages/flutter/lib/src/services/shell.dart deleted file mode 100644 index c0e373366b..0000000000 --- a/packages/flutter/lib/src/services/shell.dart +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2015 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -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' 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); - -class MojoShell { - MojoShell._(); - - 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; - - 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; - 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 - // embedder directly instead of using the shell to connect. - _embedderConnection?.requestService(proxy); - return; - } - 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(); - } -} -final MojoShell shell = new MojoShell._(); diff --git a/packages/flutter/lib/src/widgets/binding.dart b/packages/flutter/lib/src/widgets/binding.dart index a73cffb85a..96ec7f4177 100644 --- a/packages/flutter/lib/src/widgets/binding.dart +++ b/packages/flutter/lib/src/widgets/binding.dart @@ -21,7 +21,7 @@ class BindingObserver { /// A concrete binding for applications based on the Widgets framework. /// This is the glue that binds the framework to the Flutter engine. -class WidgetFlutterBinding extends BindingBase with Scheduler, Gesturer, Renderer { +class WidgetFlutterBinding extends BindingBase with Scheduler, Gesturer, MojoShell, Renderer { WidgetFlutterBinding._(); diff --git a/packages/flutter/test/services/mock_services.dart b/packages/flutter/test/services/mock_services.dart index 381426c031..dd2adb240b 100644 --- a/packages/flutter/test/services/mock_services.dart +++ b/packages/flutter/test/services/mock_services.dart @@ -1,18 +1,18 @@ -import 'package:flutter/src/services/shell.dart'; +import 'package:flutter/services.dart'; import 'package:mojo/bindings.dart' as bindings; // Tests can use ServiceMocker to register replacement implementations // of Mojo services. -class _ServiceMocker { - _ServiceMocker() { +class ServiceMocker { + ServiceMocker._() { shell.overrideConnectToService = _connectToService; } // Map of interface names to mock implementations. - Map _interfaceMock = new Map(); + Map _interfaceMocks = {}; bool _connectToService(String url, bindings.ProxyBase proxy) { - Object mock = _interfaceMock[proxy.serviceName]; + Object mock = _interfaceMocks[proxy.serviceName]; if (mock != null) { // Replace the proxy's implementation of the service interface with the // mock. The mojom bindings put the "ptr" field on all proxies. @@ -24,9 +24,11 @@ class _ServiceMocker { } // Provide a mock implementation for a Mojo interface. + // Make sure you initialise the binding before calling this. + // For example, by calling `WidgetFlutterBinding.ensureInitialized();` void registerMockService(String interfaceName, Object mock) { - _interfaceMock[interfaceName] = mock; + _interfaceMocks[interfaceName] = mock; } } -final _ServiceMocker serviceMocker = new _ServiceMocker(); +final ServiceMocker serviceMocker = new ServiceMocker._(); diff --git a/packages/flutter/test/widget/input_test.dart b/packages/flutter/test/widget/input_test.dart index 904025dde9..dcac27a7dc 100644 --- a/packages/flutter/test/widget/input_test.dart +++ b/packages/flutter/test/widget/input_test.dart @@ -28,6 +28,7 @@ class MockKeyboard implements KeyboardService { } void main() { + WidgetFlutterBinding.ensureInitialized(); // for serviceMocker MockKeyboard mockKeyboard = new MockKeyboard(); serviceMocker.registerMockService(KeyboardService.serviceName, mockKeyboard);