Commit 57b68a52 authored by Hixie's avatar Hixie

Clean up shell.dart

parent 91c0af60
...@@ -7,51 +7,55 @@ import 'dart:ui_internals' as internals; ...@@ -7,51 +7,55 @@ import 'dart:ui_internals' as internals;
import 'package:mojo/application.dart'; import 'package:mojo/application.dart';
import 'package:mojo/bindings.dart' as bindings; import 'package:mojo/bindings.dart' as bindings;
import 'package:mojo/core.dart' as core; import 'package:mojo/core.dart' as core;
import 'package:mojo/mojo/service_provider.mojom.dart'; import 'package:mojo/mojo/service_provider.mojom.dart' as mojom;
import 'package:mojo/mojo/shell.mojom.dart'; import 'package:mojo/mojo/shell.mojom.dart' as mojom;
// A replacement for shell.connectToService. Implementations should return true // A replacement for shell.connectToService. Implementations should return true
// if they handled the request, or false if the request should fall through // if they handled the request, or false if the request should fall through
// to the default requestService. // to the default requestService.
typedef bool OverrideConnectToService(String url, Object proxy); typedef bool OverrideConnectToService(String url, Object proxy);
// Set this to intercept calls to shell.connectToService and supply an class MojoShell {
// alternative implementation of a service (for example, a mock for testing). MojoShell._();
OverrideConnectToService overrideConnectToService;
ShellProxy _initShellProxy() { static mojom.ShellProxy _initShellProxy() {
core.MojoHandle shellHandle = new core.MojoHandle(internals.takeShellProxyHandle()); core.MojoHandle shellHandle = new core.MojoHandle(internals.takeShellProxyHandle());
if (!shellHandle.isValid) if (!shellHandle.isValid)
return null; return null;
return new ShellProxy.fromHandle(shellHandle); 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 { static ApplicationConnection _initEmbedderConnection() {
_Shell._(); 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) { ApplicationConnection connectToApplication(String url) {
if (_shell == null) if (_shell == null)
return null; return null;
ServiceProviderProxy services = new ServiceProviderProxy.unbound(); mojom.ServiceProviderProxy services = new mojom.ServiceProviderProxy.unbound();
ServiceProviderStub exposedServices = new ServiceProviderStub.unbound(); mojom.ServiceProviderStub exposedServices = new mojom.ServiceProviderStub.unbound();
_shell.connectToApplication(url, services, exposedServices); _shell.connectToApplication(url, services, exposedServices);
return new ApplicationConnection(exposedServices, services); 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) { void _connectToService(String url, bindings.ProxyBase proxy) {
if (_shell == null || url == null) { if (_shell == null || url == null) {
// If we don't have a shell or a url, we try to get the services from the // If we don't have a shell or a url, we try to get the services from the
...@@ -59,20 +63,12 @@ class _Shell { ...@@ -59,20 +63,12 @@ class _Shell {
_embedderConnection?.requestService(proxy); _embedderConnection?.requestService(proxy);
return; return;
} }
mojom.ServiceProviderProxy services = new mojom.ServiceProviderProxy.unbound();
ServiceProviderProxy services = new ServiceProviderProxy.unbound();
_shell.connectToApplication(url, services, null); _shell.connectToApplication(url, services, null);
core.MojoMessagePipe pipe = new core.MojoMessagePipe(); core.MojoMessagePipe pipe = new core.MojoMessagePipe();
proxy.impl.bind(pipe.endpoints[0]); proxy.impl.bind(pipe.endpoints[0]);
services.ptr.connectToService(proxy.serviceName, pipe.endpoints[1]); services.ptr.connectToService(proxy.serviceName, pipe.endpoints[1]);
services.close(); services.close();
} }
void connectToService(String url, bindings.ProxyBase proxy) {
if (overrideConnectToService != null && overrideConnectToService(url, proxy))
return;
_connectToService(url, proxy);
}
} }
final MojoShell shell = new MojoShell._();
final _Shell shell = new _Shell._();
import 'package:flutter/src/services/shell.dart' as shell; import 'package:flutter/src/services/shell.dart';
import 'package:mojo/bindings.dart' as bindings; import 'package:mojo/bindings.dart' as bindings;
// Tests can use ServiceMocker to register replacement implementations // 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