OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 package org.chromium.examples.android; |
| 6 |
| 7 import android.content.Context; |
| 8 import android.util.Log; |
| 9 |
| 10 import org.chromium.mojo.bindings.Interface; |
| 11 import org.chromium.mojo.bindings.InterfaceRequest; |
| 12 import org.chromium.mojo.system.Core; |
| 13 import org.chromium.mojo.system.MessagePipeHandle; |
| 14 import org.chromium.mojo.system.MojoException; |
| 15 import org.chromium.mojo.system.RunLoop; |
| 16 import org.chromium.mojom.mojo.Application; |
| 17 import org.chromium.mojom.mojo.ExampleService; |
| 18 import org.chromium.mojom.mojo.ServiceProvider; |
| 19 import org.chromium.mojom.mojo.Shell; |
| 20 |
| 21 import java.util.HashMap; |
| 22 import java.util.Map; |
| 23 |
| 24 /** |
| 25 * App holds all the code for this example application. |
| 26 */ |
| 27 class App { |
| 28 private static String TAG = "ExampleServiceApp"; |
| 29 |
| 30 public static class ApplicationImpl implements Application { |
| 31 private final Core mCore; |
| 32 private Shell mShell; |
| 33 |
| 34 public ApplicationImpl(Core core, MessagePipeHandle applicationRequest)
{ |
| 35 ApplicationImpl.MANAGER.bind(this, applicationRequest); |
| 36 mCore = core; |
| 37 } |
| 38 |
| 39 @Override |
| 40 public void initialize(Shell shell, String[] args, String url) { |
| 41 mShell = shell; |
| 42 } |
| 43 |
| 44 @Override |
| 45 public void acceptConnection(String requestorUrl, |
| 46 InterfaceRequest<ServiceProvider> services, ServiceProvider expo
sedServices) { |
| 47 ServiceProviderImpl serviceProvider = new ServiceProviderImpl(); |
| 48 serviceProvider.addService( |
| 49 new ServiceFactoryBinder<ExampleService>() { |
| 50 @Override |
| 51 public void bindNewInstanceToMessagePipe( |
| 52 MessagePipeHandle pipe) { |
| 53 ExampleService.MANAGER.bind(new ExampleServiceImpl()
, pipe); |
| 54 } |
| 55 |
| 56 @Override |
| 57 public String getInterfaceName() { |
| 58 return ExampleService.MANAGER.getName(); |
| 59 } |
| 60 } |
| 61 ); |
| 62 ServiceProvider.MANAGER.bind(serviceProvider, services); |
| 63 exposedServices.close(); |
| 64 }; |
| 65 |
| 66 @Override |
| 67 public void requestQuit() { |
| 68 mCore.getCurrentRunLoop().quit(); |
| 69 } |
| 70 |
| 71 @Override |
| 72 public void close() { |
| 73 if (mShell != null) { |
| 74 mShell.close(); |
| 75 } |
| 76 } |
| 77 |
| 78 @Override |
| 79 public void onConnectionError(MojoException e) {} |
| 80 } |
| 81 |
| 82 /** |
| 83 * ServiceFactoryBinder holds the necessary information to bind a service in
terface to a |
| 84 * message pipe. |
| 85 */ |
| 86 private static interface ServiceFactoryBinder<T extends Interface> { |
| 87 public void bindNewInstanceToMessagePipe(MessagePipeHandle pipe); |
| 88 public String getInterfaceName(); |
| 89 } |
| 90 |
| 91 public static class ServiceProviderImpl implements ServiceProvider { |
| 92 private Map<String, ServiceFactoryBinder<? extends Interface>> mNameToSe
rviceMap = |
| 93 new HashMap<String, ServiceFactoryBinder<? extends Interface>>()
; |
| 94 |
| 95 public ServiceProviderImpl() {} |
| 96 |
| 97 /** |
| 98 * AddService adds a new service to the list of services provided by thi
s application. |
| 99 */ |
| 100 public void addService(ServiceFactoryBinder<? extends Interface> binder)
{ |
| 101 mNameToServiceMap.put(binder.getInterfaceName(), binder); |
| 102 } |
| 103 |
| 104 @Override |
| 105 public void connectToService(String interfaceName, MessagePipeHandle pip
e) { |
| 106 if (mNameToServiceMap.containsKey(interfaceName)) { |
| 107 mNameToServiceMap.get(interfaceName).bindNewInstanceToMessagePip
e(pipe); |
| 108 } else { |
| 109 pipe.close(); |
| 110 } |
| 111 } |
| 112 |
| 113 @Override |
| 114 public void close() {} |
| 115 |
| 116 @Override |
| 117 public void onConnectionError(MojoException e) {} |
| 118 } |
| 119 |
| 120 public static class ExampleServiceImpl implements ExampleService { |
| 121 public ExampleServiceImpl() {} |
| 122 |
| 123 @Override |
| 124 public void ping(short pingValue, ExampleService.PingResponse callback)
{ |
| 125 callback.call(pingValue); |
| 126 } |
| 127 |
| 128 @Override |
| 129 public void close() {} |
| 130 |
| 131 @Override |
| 132 public void onConnectionError(MojoException e) {} |
| 133 } |
| 134 |
| 135 public static void mojoMain(Context context, Core core, |
| 136 MessagePipeHandle applicationRequestHandle) { |
| 137 Log.i(TAG, "App.MojoMain called"); |
| 138 try (RunLoop runLoop = core.createDefaultRunLoop()) { |
| 139 Log.i(TAG, "Run loop created"); |
| 140 |
| 141 Application application = new ApplicationImpl(core, applicationReque
stHandle); |
| 142 |
| 143 Log.i(TAG, "Setup done, starting run loop"); |
| 144 runLoop.run(); |
| 145 Log.i(TAG, "Run loop exited"); |
| 146 } |
| 147 } |
| 148 } |
OLD | NEW |