Commit 57b68a52 authored by Hixie's avatar Hixie

Clean up shell.dart

parent 91c0af60
......@@ -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);
}
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();
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;
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._();
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
......
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