Chromium Code Reviews| Index: examples/android/App.java |
| diff --git a/examples/android/App.java b/examples/android/App.java |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8188c4347c28065bdc268b5a37895ebc980f8fe5 |
| --- /dev/null |
| +++ b/examples/android/App.java |
| @@ -0,0 +1,153 @@ |
| +// 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.chromium.examples.android; |
| + |
| +import android.content.Context; |
| +import android.util.Log; |
| + |
| +import org.chromium.mojo.bindings.Interface; |
| +import org.chromium.mojo.bindings.InterfaceRequest; |
| +import org.chromium.mojo.system.Core; |
| +import org.chromium.mojo.system.MessagePipeHandle; |
| +import org.chromium.mojo.system.MojoException; |
| +import org.chromium.mojo.system.RunLoop; |
| +import org.chromium.mojom.mojo.Application; |
| +import org.chromium.mojom.mojo.ExampleService; |
| +import org.chromium.mojom.mojo.ServiceProvider; |
| +import org.chromium.mojom.mojo.Shell; |
| + |
| +import java.util.HashMap; |
| +import java.util.Map; |
| + |
| +/** |
| + * App holds all the code for this example application. |
| + */ |
| +class App { |
| + private static String TAG = "ExampleServiceApp"; |
| + |
| + public static class ApplicationImpl implements Application { |
| + private final Core mCore; |
| + private Shell mShell; |
| + |
| + public ApplicationImpl(MessagePipeHandle applicationRequest, Core core) { |
|
qsr
2015/02/10 11:58:18
Maybe invert the 2 parameters. Core is some kind o
etiennej
2015/02/10 14:32:04
Done.
|
| + ApplicationImpl.MANAGER.bind(this, applicationRequest); |
| + mCore = core; |
| + } |
| + |
| + @Override |
| + public void initialize(Shell shell, String[] args) { |
| + mShell = shell; |
| + } |
| + |
| + @Override |
| + public void acceptConnection(String requestorUrl, |
| + InterfaceRequest<ServiceProvider> services, ServiceProvider exposedServices) { |
| + ServiceProviderImpl serviceProvider = new ServiceProviderImpl(); |
| + serviceProvider.addService( |
| + new ServiceFactoryBinder<ExampleService>() { |
| + @Override |
| + public void bindNewInstanceToMessagePipe( |
| + MessagePipeHandle pipe) { |
| + ExampleService.MANAGER.bind(new ExampleServiceImpl(), pipe); |
| + } |
| + |
| + @Override |
| + public String getInterfaceName() { |
| + return ExampleService.MANAGER.getName(); |
| + } |
| + } |
| + ); |
| + ServiceProvider.MANAGER.bind(serviceProvider, services); |
| + exposedServices.close(); |
| + }; |
| + |
| + @Override |
| + public void requestQuit() { |
| + mCore.getCurrentRunLoop().quit(); |
| + } |
| + |
| + @Override |
| + public void close() { |
| + if (mShell != null) { |
| + mShell.close(); |
| + } |
| + } |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) {} |
| + } |
| + |
| + /** |
| + * ServiceFactoryBinder holds the necessary information to bind a service interface to a |
| + * message pipe. |
| + */ |
| + private static interface ServiceFactoryBinder<T extends Interface> { |
| + public void bindNewInstanceToMessagePipe(MessagePipeHandle pipe); |
| + public String getInterfaceName(); |
| + } |
| + |
| + public static class ServiceProviderImpl implements ServiceProvider { |
| + private Map<String, ServiceFactoryBinder<? extends Interface>> mNameToServiceMap = |
| + new HashMap<String, ServiceFactoryBinder<? extends Interface>>(); |
| + |
| + public ServiceProviderImpl() {} |
| + |
| + /** |
| + * AddService adds a new service to the list of services provided by this application. |
| + */ |
| + public void addService(ServiceFactoryBinder<? extends Interface> binder) { |
| + mNameToServiceMap.put(binder.getInterfaceName(), binder); |
| + } |
| + |
| + @Override |
| + public void connectToService(String interfaceName, MessagePipeHandle pipe) { |
| + if (mNameToServiceMap.containsKey(interfaceName)) { |
| + try { |
| + mNameToServiceMap.get(interfaceName).bindNewInstanceToMessagePipe(pipe); |
| + } catch (Exception e) { |
|
qsr
2015/02/10 11:58:18
Why do you need to catch something here? Should no
etiennej
2015/02/10 14:32:04
Done.
|
| + pipe.close(); |
| + throw new RuntimeException(e); |
| + } |
| + } else { |
| + pipe.close(); |
| + } |
| + } |
| + |
| + @Override |
| + public void close() {} |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) {} |
| + } |
| + |
| + public static class ExampleServiceImpl implements ExampleService { |
| + public ExampleServiceImpl() {} |
| + |
| + @Override |
| + public void ping(short pingValue, ExampleService.PingResponse callback) { |
| + callback.call(pingValue); |
| + } |
| + |
| + @Override |
| + public void close() {} |
| + |
| + @Override |
| + public void onConnectionError(MojoException e) {} |
| + } |
| + |
| + public static void mojoMain(Context context, Core core, |
| + MessagePipeHandle applicationRequestHandle) { |
| + Log.i(TAG, "App.MojoMain called"); |
| + try (RunLoop runLoop = core.createDefaultRunLoop()) { |
| + Log.i(TAG, "Run loop created"); |
| + |
| + Application application = new ApplicationImpl(applicationRequestHandle, core); |
| + |
| + Log.i(TAG, "Setup done, starting run loop"); |
| + runLoop.run(); |
| + Log.i(TAG, "Run loop exited"); |
| + } |
| + } |
| +} |