Commit fab160cf authored by Adam Barth's avatar Adam Barth

Rationalize embedder.dart and shell.dart

This patch combines embedder.dart and shell.dart into one thing. We should now
handle a bunch of error cases better.

  * embedder.connectToApplication has moved to shell.connectToApplication,
    matching the rest of the mojo universe.
  * embedder.connecttoService has moved to shell.connnectToService (and merged
    with shell.requestService).
  * shell.requestService is now shell.connectToService, matching the rest of
    the mojo universe.
  * serviceRegistry has moved from embedder.serviceRegistry to a top-level
    getter.

Fixes #1803
parent 856ee978
...@@ -68,7 +68,7 @@ class PianoApp extends StatelessComponent { ...@@ -68,7 +68,7 @@ class PianoApp extends StatelessComponent {
Future _loadSounds() async { Future _loadSounds() async {
MediaServiceProxy mediaService = new MediaServiceProxy.unbound(); MediaServiceProxy mediaService = new MediaServiceProxy.unbound();
try { try {
shell.requestService(null, mediaService); shell.connectToService(null, mediaService);
List<Future<MediaPlayerPrepareResponseParams>> pending = <Future<MediaPlayerPrepareResponseParams>>[]; List<Future<MediaPlayerPrepareResponseParams>> pending = <Future<MediaPlayerPrepareResponseParams>>[];
for (PianoKey key in keys) for (PianoKey key in keys)
pending.add(key.load(mediaService)); pending.add(key.load(mediaService));
......
...@@ -13,7 +13,6 @@ library services; ...@@ -13,7 +13,6 @@ library services;
export 'src/services/activity.dart'; export 'src/services/activity.dart';
export 'src/services/asset_bundle.dart'; export 'src/services/asset_bundle.dart';
export 'src/services/embedder.dart';
export 'src/services/fetch.dart'; export 'src/services/fetch.dart';
export 'src/services/image_cache.dart'; export 'src/services/image_cache.dart';
export 'src/services/image_decoder.dart'; export 'src/services/image_decoder.dart';
......
...@@ -22,7 +22,7 @@ const int MULTIPLE_TASK = 0x08000000; ...@@ -22,7 +22,7 @@ const int MULTIPLE_TASK = 0x08000000;
ActivityProxy _initActivityProxy() { ActivityProxy _initActivityProxy() {
ActivityProxy activity = new ActivityProxy.unbound(); ActivityProxy activity = new ActivityProxy.unbound();
shell.requestService('mojo:sky_viewer', activity); shell.connectToService(null, activity);
return activity; return activity;
} }
...@@ -40,7 +40,7 @@ final UserFeedback userFeedback = _userFeedbackProxy.ptr; ...@@ -40,7 +40,7 @@ final UserFeedback userFeedback = _userFeedbackProxy.ptr;
PathServiceProxy _initPathServiceProxy() { PathServiceProxy _initPathServiceProxy() {
PathServiceProxy proxy = new PathServiceProxy.unbound(); PathServiceProxy proxy = new PathServiceProxy.unbound();
shell.requestService(null, proxy); shell.connectToService(null, proxy);
return proxy; return proxy;
} }
......
...@@ -44,7 +44,7 @@ class NetworkAssetBundle extends AssetBundle { ...@@ -44,7 +44,7 @@ class NetworkAssetBundle extends AssetBundle {
Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async { Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async {
core.MojoDataPipeConsumer bundleData = (await fetchUrl(relativeUrl)).body; core.MojoDataPipeConsumer bundleData = (await fetchUrl(relativeUrl)).body;
AssetUnpackerProxy unpacker = new AssetUnpackerProxy.unbound(); AssetUnpackerProxy unpacker = new AssetUnpackerProxy.unbound();
shell.requestService("mojo:asset_bundle", unpacker); shell.connectToService("mojo:asset_bundle", unpacker);
unpacker.ptr.unpackZipStream(bundleData, bundle); unpacker.ptr.unpackZipStream(bundleData, bundle);
unpacker.close(); unpacker.close();
} }
......
// 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';
import 'package:mojo/mojo/shell.mojom.dart';
import 'package:mojo_services/mojo/service_registry.mojom.dart';
final _EmbedderImpl embedder = new _EmbedderImpl();
class _EmbedderImpl {
ApplicationConnection _connection;
ServiceRegistryProxy _serviceRegistry;
ShellProxy _shell;
bool _internalsHasNoShell = false;
ShellProxy get shell {
if (_internalsHasNoShell || _shell != null) return _shell;
try {
_shell = new ShellProxy.fromHandle(
new core.MojoHandle(internals.takeShellProxyHandle()));
} catch (e) {
_internalsHasNoShell = true;
}
return _shell;
}
ApplicationConnection get connection {
if (_connection == null) {
var stubHandle =
new core.MojoHandle(internals.takeServicesProvidedToEmbedder());
var proxyHandle =
new core.MojoHandle(internals.takeServicesProvidedByEmbedder());
_connection = new ApplicationConnection(stubHandle.isValid
? new ServiceProviderStub.fromHandle(stubHandle)
: null, proxyHandle.isValid
? new ServiceProviderProxy.fromHandle(proxyHandle)
: null);
}
return _connection;
}
ApplicationConnection connectToApplication(String url) {
var proxy = new ServiceProviderProxy.unbound();
var stub = new ServiceProviderStub.unbound();
shell.ptr.connectToApplication(url, proxy, stub);
return new ApplicationConnection(stub, proxy);
}
void connectToService(String url, bindings.ProxyBase proxy) {
var appSp = new ServiceProviderProxy.unbound();
shell.ptr.connectToApplication(url, appSp, null);
var pipe = new core.MojoMessagePipe();
proxy.impl.bind(pipe.endpoints[0]);
appSp.ptr.connectToService(proxy.name, pipe.endpoints[1]);
appSp.close();
}
ServiceRegistryProxy get serviceRegistry {
if (_serviceRegistry == null) {
_serviceRegistry = new ServiceRegistryProxy.fromHandle(
new core.MojoHandle(internals.takeServiceRegistry()));
}
return _serviceRegistry;
}
}
...@@ -17,7 +17,7 @@ export 'package:mojo/mojo/url_response.mojom.dart' show UrlResponse; ...@@ -17,7 +17,7 @@ export 'package:mojo/mojo/url_response.mojom.dart' show UrlResponse;
NetworkServiceProxy _initNetworkService() { NetworkServiceProxy _initNetworkService() {
NetworkServiceProxy networkService = new NetworkServiceProxy.unbound(); NetworkServiceProxy networkService = new NetworkServiceProxy.unbound();
shell.requestService("mojo:authenticated_network_service", networkService); shell.connectToService("mojo:authenticated_network_service", networkService);
return networkService; return networkService;
} }
......
...@@ -14,7 +14,7 @@ class _KeyboardConnection { ...@@ -14,7 +14,7 @@ class _KeyboardConnection {
_KeyboardConnection() { _KeyboardConnection() {
proxy = new KeyboardServiceProxy.unbound(); proxy = new KeyboardServiceProxy.unbound();
shell.requestService("mojo:keyboard", proxy); shell.connectToService("mojo:keyboard", proxy);
} }
KeyboardServiceProxy proxy; KeyboardServiceProxy proxy;
......
// 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_services/mojo/service_registry.mojom.dart';
import 'package:mojo/core.dart' as core;
ServiceRegistryProxy _initServiceRegistryProxy() {
core.MojoHandle serviceRegistryHandle = new core.MojoHandle(internals.takeShellProxyHandle());
if (!serviceRegistryHandle.isValid)
return null;
return new ServiceRegistryProxy.fromHandle(serviceRegistryHandle);
}
final ServiceRegistryProxy _serviceRegistryProxy = _initServiceRegistryProxy();
final ServiceRegistry serviceRegistry = _serviceRegistryProxy?.ptr;
...@@ -5,47 +5,74 @@ ...@@ -5,47 +5,74 @@
import 'dart:ui_internals' as internals; 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/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';
import 'package:mojo/mojo/shell.mojom.dart';
import 'embedder.dart'; // A replacement for shell.connectToService. Implementations should return true
ApplicationConnection _initConnection() {
int rawHandle = internals.takeServicesProvidedByEmbedder();
core.MojoHandle proxyHandle = new core.MojoHandle(rawHandle);
ServiceProviderProxy serviceProvider = null;
if (proxyHandle.isValid) serviceProvider =
new ServiceProviderProxy.fromHandle(proxyHandle);
return new ApplicationConnection(null, serviceProvider);
}
// A replacement for requestService. 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 OverrideRequestService(String url, Object proxy); typedef bool OverrideConnectToService(String url, Object proxy);
// Set this to intercept calls to requestService and supply an alternative // Set this to intercept calls to shell.connectToService and supply an
// implementation of a service (for example, a mock for testing). // alternative implementation of a service (for example, a mock for testing).
OverrideRequestService overrideRequestService; OverrideConnectToService overrideConnectToService;
class _ShellImpl { ShellProxy _initShellProxy() {
_ShellImpl._(); core.MojoHandle shellHandle = new core.MojoHandle(internals.takeShellProxyHandle());
if (!shellHandle.isValid)
return null;
return new ShellProxy.fromHandle(shellHandle);
}
final ApplicationConnection _connection = _initConnection(); ApplicationConnection _initEmbedderConnection() {
core.MojoHandle servicesHandle = new core.MojoHandle(internals.takeServicesProvidedByEmbedder());
core.MojoHandle exposedServicesHandle = new core.MojoHandle(internals.takeServicesProvidedToEmbedder());
ServiceProviderProxy services = servicesHandle.isValid ?
new ServiceProviderProxy.fromHandle(servicesHandle) : null;
ServiceProviderStub exposedServices = exposedServicesHandle.isValid ?
new ServiceProviderStub.fromHandle(exposedServicesHandle) : null;
return new ApplicationConnection(exposedServices, services);
}
void _requestService(String url, Object proxy) { final ShellProxy _shellProxy = _initShellProxy();
if (embedder.shell == null) _connection.requestService(proxy); final Shell _shell = _shellProxy?.ptr;
else embedder.connectToService(url, proxy); final ApplicationConnection _embedderConnection = _initEmbedderConnection();
class _Shell {
_Shell._();
ApplicationConnection connectToApplication(String url) {
if (_shell == null)
return null;
ServiceProviderProxy services = new ServiceProviderProxy.unbound();
ServiceProviderStub exposedServices = new ServiceProviderStub.unbound();
_shell.connectToApplication(url, services, exposedServices);
return new ApplicationConnection(exposedServices, services);
} }
void requestService(String url, Object proxy) { void _connectToService(String url, bindings.ProxyBase proxy) {
if (overrideRequestService != null) { if (_shell == null || url == null) {
if (overrideRequestService(url, proxy)) // If we don't have a shell or a url, we try to get the services from the
return; // embedder directly instead of using the shell to connect.
_embedderConnection.requestService(proxy);
return;
} }
_requestService(url, proxy); ServiceProviderProxy services = new ServiceProviderProxy.unbound();
_shell.connectToApplication(url, services, null);
var pipe = new core.MojoMessagePipe();
proxy.impl.bind(pipe.endpoints[0]);
services.ptr.connectToService(proxy.name, pipe.endpoints[1]);
services.close();
}
void connectToService(String url, Object proxy) {
if (overrideConnectToService != null && overrideConnectToService(url, proxy))
return;
_connectToService(url, proxy);
} }
} }
final _ShellImpl shell = new _ShellImpl._(); final _Shell shell = new _Shell._();
...@@ -55,7 +55,7 @@ class SoundEffectPlayer { ...@@ -55,7 +55,7 @@ class SoundEffectPlayer {
SoundEffectPlayer() { SoundEffectPlayer() {
_mediaService = new MediaServiceProxy.unbound(); _mediaService = new MediaServiceProxy.unbound();
shell.requestService(null, _mediaService); shell.connectToService(null, _mediaService);
} }
MediaServiceProxy _mediaService; MediaServiceProxy _mediaService;
...@@ -165,7 +165,7 @@ class SoundTrackPlayer { ...@@ -165,7 +165,7 @@ class SoundTrackPlayer {
SoundTrackPlayer() { SoundTrackPlayer() {
_mediaService = new MediaServiceProxy.unbound(); _mediaService = new MediaServiceProxy.unbound();
shell.requestService(null, _mediaService); shell.connectToService(null, _mediaService);
} }
MediaServiceProxy _mediaService; MediaServiceProxy _mediaService;
......
...@@ -4,13 +4,13 @@ import 'package:flutter/src/services/shell.dart' as shell; ...@@ -4,13 +4,13 @@ import 'package:flutter/src/services/shell.dart' as shell;
// of Mojo services. // of Mojo services.
class _ServiceMocker { class _ServiceMocker {
_ServiceMocker() { _ServiceMocker() {
shell.overrideRequestService = _requestService; shell.overrideConnectToService = _connectToService;
} }
// Map of interface names to mock implementations. // Map of interface names to mock implementations.
Map<String, Object> _interfaceMock = new Map<String, Object>(); Map<String, Object> _interfaceMock = new Map<String, Object>();
bool _requestService(String url, dynamic proxy) { bool _connectToService(String url, dynamic proxy) {
Object mock = _interfaceMock[proxy.impl.name]; Object mock = _interfaceMock[proxy.impl.name];
if (mock != null) { if (mock != null) {
// Replace the proxy's implementation of the service interface with the // Replace the proxy's implementation of the service interface with the
......
...@@ -23,7 +23,7 @@ const String kBundleFile = 'app.flx'; ...@@ -23,7 +23,7 @@ const String kBundleFile = 'app.flx';
UpdateServiceProxy _initUpdateService() { UpdateServiceProxy _initUpdateService() {
UpdateServiceProxy updateService = new UpdateServiceProxy.unbound(); UpdateServiceProxy updateService = new UpdateServiceProxy.unbound();
shell.requestService(null, updateService); shell.connectToService(null, updateService);
return updateService; return updateService;
} }
......
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