Commit e8a21f5d authored by Hixie's avatar Hixie

Turn MojoShell into an actual binding

parent 9e784f0c
......@@ -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';
......@@ -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;
......
......@@ -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;
......
......@@ -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';
......
......@@ -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);
......
......@@ -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;
......@@ -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';
......
// 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._();
......@@ -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._();
......
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<String, Object> _interfaceMock = new Map<String, Object>();
Map<String, Object> _interfaceMocks = <String, Object>{};
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._();
......@@ -28,6 +28,7 @@ class MockKeyboard implements KeyboardService {
}
void main() {
WidgetFlutterBinding.ensureInitialized(); // for serviceMocker
MockKeyboard mockKeyboard = new MockKeyboard();
serviceMocker.registerMockService(KeyboardService.serviceName, mockKeyboard);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment