提交 4e1056c4 编写于 作者: A Adam Barth

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
上级 61042104
......@@ -10,7 +10,7 @@
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
<application android:name="org.domokit.sky.shell.SkyApplication" android:label="Sky">
<application android:name="SkyDemoApplication" android:label="Sky">
<activity android:name="SkyDemoActivity"
android:launchMode="singleTask"
android:theme="@android:style/Theme.Holo.Light.NoActionBar"
......
......@@ -9,9 +9,17 @@ import("//build/config/android/rules.gni")
import("//sky/apk/rules.gni")
android_library("java") {
java_files = [ "org/domokit/sky/demo/SkyDemoActivity.java" ]
java_files = [
"org/domokit/sky/demo/SkyDemoActivity.java",
"org/domokit/sky/demo/SkyDemoApplication.java",
]
deps = [
"//base:base_java",
"//mojo/public/java:bindings",
"//mojo/public/java:system",
"//sky/services/sensors",
"//sky/services/sensors:interfaces_java",
"//sky/shell:java",
]
}
......
// 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.demo;
import android.content.Context;
import org.chromium.mojo.system.Core;
import org.chromium.mojo.system.MessagePipeHandle;
import org.chromium.mojom.sensors.SensorService;
import org.domokit.sensors.SensorServiceImpl;
import org.domokit.sky.shell.ServiceFactory;
import org.domokit.sky.shell.ServiceRegistry;
import org.domokit.sky.shell.SkyApplication;
/**
* SkyDemo implementation of {@link android.app.Application}
*/
public class SkyDemoApplication extends SkyApplication {
@Override
public void onCreate() {
super.onCreate();
ServiceRegistry.SHARED.register(SensorService.MANAGER.getName(), new ServiceFactory() {
@Override
public void connectToService(Context context, Core core, MessagePipeHandle pipe) {
new SensorServiceImpl(context, core, pipe);
}
});
}
}
......@@ -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",
]
}
......
......@@ -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);
}
}
// 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);
}
// 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);
}
}
......@@ -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);
}
});
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册