From 4b2e936b19d1407061114029d6ee4167c438a1dc Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Mon, 2 Mar 2015 13:33:56 -0800 Subject: [PATCH] Make it possible for SkyShell embedders to register services We now register the sensor service from SkyDemo, which shows that embedders of SkyShell can register their own Java services. R=eseidel@chromium.org Review URL: https://codereview.chromium.org/969753003 --- engine/src/flutter/shell/BUILD.gn | 4 +-- .../sky/shell/JavaServiceProvider.java | 16 +++------- .../org/domokit/sky/shell/ServiceFactory.java | 19 ++++++++++++ .../domokit/sky/shell/ServiceRegistry.java | 31 +++++++++++++++++++ .../org/domokit/sky/shell/SkyApplication.java | 24 +++++++++----- 5 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 engine/src/flutter/shell/org/domokit/sky/shell/ServiceFactory.java create mode 100644 engine/src/flutter/shell/org/domokit/sky/shell/ServiceRegistry.java diff --git a/engine/src/flutter/shell/BUILD.gn b/engine/src/flutter/shell/BUILD.gn index d583784cf0..86f6e043a8 100644 --- a/engine/src/flutter/shell/BUILD.gn +++ b/engine/src/flutter/shell/BUILD.gn @@ -75,6 +75,8 @@ android_library("java") { "org/domokit/sky/shell/GestureProvider.java", "org/domokit/sky/shell/JavaServiceProvider.java", "org/domokit/sky/shell/PlatformView.java", + "org/domokit/sky/shell/ServiceFactory.java", + "org/domokit/sky/shell/ServiceRegistry.java", "org/domokit/sky/shell/SkyActivity.java", "org/domokit/sky/shell/SkyApplication.java", "org/domokit/sky/shell/SkyMain.java", @@ -88,8 +90,6 @@ android_library("java") { "//mojo/public/java:system", "//mojo/services/network/public/interfaces:interfaces_java", "//sky/services/oknet", - "//sky/services/sensors", - "//sky/services/sensors:interfaces_java", "//sky/services/viewport:viewport_java", ] } diff --git a/engine/src/flutter/shell/org/domokit/sky/shell/JavaServiceProvider.java b/engine/src/flutter/shell/org/domokit/sky/shell/JavaServiceProvider.java index bda18c83fb..47996f4665 100644 --- a/engine/src/flutter/shell/org/domokit/sky/shell/JavaServiceProvider.java +++ b/engine/src/flutter/shell/org/domokit/sky/shell/JavaServiceProvider.java @@ -12,14 +12,10 @@ import org.chromium.mojo.system.Core; import org.chromium.mojo.system.MessagePipeHandle; import org.chromium.mojo.system.MojoException; import org.chromium.mojo.system.impl.CoreImpl; -import org.chromium.mojom.mojo.NetworkService; import org.chromium.mojom.mojo.ServiceProvider; -import org.chromium.mojom.sensors.SensorService; -import org.domokit.oknet.NetworkServiceImpl; -import org.domokit.sensors.SensorServiceImpl; /** - * A class to intialize the network. + * A collection of services implemented in Java. **/ @JNINamespace("sky::shell") public class JavaServiceProvider implements ServiceProvider { @@ -49,13 +45,11 @@ public class JavaServiceProvider implements ServiceProvider { @Override public void connectToService(String interfaceName, MessagePipeHandle pipe) { - if (interfaceName.equals(NetworkService.MANAGER.getName())) { - new NetworkServiceImpl(mContext, mCore, pipe); - return; - } else if (interfaceName.equals(SensorService.MANAGER.getName())) { - new SensorServiceImpl(mContext, mCore, pipe); + ServiceFactory factory = ServiceRegistry.SHARED.get(interfaceName); + if (factory == null) { + pipe.close(); return; } - pipe.close(); + factory.connectToService(mContext, mCore, pipe); } } diff --git a/engine/src/flutter/shell/org/domokit/sky/shell/ServiceFactory.java b/engine/src/flutter/shell/org/domokit/sky/shell/ServiceFactory.java new file mode 100644 index 0000000000..a2d6cb1e66 --- /dev/null +++ b/engine/src/flutter/shell/org/domokit/sky/shell/ServiceFactory.java @@ -0,0 +1,19 @@ +// 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. + +package org.domokit.sky.shell; + +import android.content.Context; + +import org.chromium.mojo.system.Core; +import org.chromium.mojo.system.MessagePipeHandle; + +/** + * An interface for creating services. Instances of this interface can be + * registered with ServiceRegistry and thereby made available to non-Java + * clients. + **/ +public interface ServiceFactory { + public void connectToService(Context context, Core core, MessagePipeHandle pipe); +} diff --git a/engine/src/flutter/shell/org/domokit/sky/shell/ServiceRegistry.java b/engine/src/flutter/shell/org/domokit/sky/shell/ServiceRegistry.java new file mode 100644 index 0000000000..49affd1435 --- /dev/null +++ b/engine/src/flutter/shell/org/domokit/sky/shell/ServiceRegistry.java @@ -0,0 +1,31 @@ +// 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. + +package org.domokit.sky.shell; + +import java.util.Map; +import java.util.TreeMap; + +/** + * An registry for services. + **/ +public class ServiceRegistry { + private Map mRegistrations; + + public static final ServiceRegistry SHARED = new ServiceRegistry(); + + private ServiceRegistry() { + mRegistrations = new TreeMap(); + } + + public void register(String interfaceName, ServiceFactory connector) { + assert !mRegistrations.containsKey(interfaceName); + assert connector != null; + mRegistrations.put(interfaceName, connector); + } + + public ServiceFactory get(String interfaceName) { + return mRegistrations.get(interfaceName); + } +} diff --git a/engine/src/flutter/shell/org/domokit/sky/shell/SkyApplication.java b/engine/src/flutter/shell/org/domokit/sky/shell/SkyApplication.java index 0faf0c5d78..54798de680 100644 --- a/engine/src/flutter/shell/org/domokit/sky/shell/SkyApplication.java +++ b/engine/src/flutter/shell/org/domokit/sky/shell/SkyApplication.java @@ -4,6 +4,7 @@ package org.domokit.sky.shell; +import android.content.Context; import android.util.Log; import org.chromium.base.BaseChromiumApplication; @@ -12,16 +13,20 @@ import org.chromium.base.ResourceExtractor; import org.chromium.base.library_loader.LibraryLoader; import org.chromium.base.library_loader.LibraryProcessType; import org.chromium.base.library_loader.ProcessInitException; +import org.chromium.mojo.system.Core; +import org.chromium.mojo.system.MessagePipeHandle; +import org.chromium.mojom.mojo.NetworkService; +import org.domokit.oknet.NetworkServiceImpl; /** - * MojoShell implementation of {@link android.app.Application}, managing application-level global + * Sky implementation of {@link android.app.Application}, managing application-level global * state and initializations. */ public class SkyApplication extends BaseChromiumApplication { private static final String TAG = "SkyApplication"; private static final String PRIVATE_DATA_DIRECTORY_SUFFIX = "sky_shell"; private static final String[] SKY_MANDATORY_PAKS = { - "icudtl.dat", + "icudtl.dat", }; @Override @@ -29,19 +34,14 @@ public class SkyApplication extends BaseChromiumApplication { super.onCreate(); initializeJavaUtils(); initializeNative(); + initializeServiceRegistry(); ResourceExtractor.setMandatoryPaksToExtract(SKY_MANDATORY_PAKS); } - /** - * Initializes Java-side utils. - */ private void initializeJavaUtils() { PathUtils.setPrivateDataDirectorySuffix(PRIVATE_DATA_DIRECTORY_SUFFIX); } - /** - * Loads the native library. - */ private void initializeNative() { try { LibraryLoader.get(LibraryProcessType.PROCESS_BROWSER).ensureInitialized(); @@ -50,4 +50,12 @@ public class SkyApplication extends BaseChromiumApplication { throw new RuntimeException(e); } } + + private void initializeServiceRegistry() { + ServiceRegistry.SHARED.register(NetworkService.MANAGER.getName(), new ServiceFactory() { + public void connectToService(Context context, Core core, MessagePipeHandle pipe) { + new NetworkServiceImpl(context, core, pipe); + } + }); + } }