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
This commit is contained in:
Adam Barth
2015-03-02 13:33:56 -08:00
parent e69fc6a47d
commit 4b2e936b19
5 changed files with 73 additions and 21 deletions

View File

@@ -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",
]
}

View File

@@ -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);
}
}

View File

@@ -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);
}

View File

@@ -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<String, ServiceFactory> mRegistrations;
public static final ServiceRegistry SHARED = new ServiceRegistry();
private ServiceRegistry() {
mRegistrations = new TreeMap<String, ServiceFactory>();
}
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);
}
}

View File

@@ -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);
}
});
}
}