Chromium Code Reviews| 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(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.
| |
| 35 ApplicationImpl.MANAGER.bind(this, applicationRequest); | |
| 36 mCore = core; | |
| 37 } | |
| 38 | |
| 39 @Override | |
| 40 public void initialize(Shell shell, String[] args) { | |
| 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 try { | |
| 108 mNameToServiceMap.get(interfaceName).bindNewInstanceToMessag ePipe(pipe); | |
| 109 } 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.
| |
| 110 pipe.close(); | |
| 111 throw new RuntimeException(e); | |
| 112 } | |
| 113 } else { | |
| 114 pipe.close(); | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 @Override | |
| 119 public void close() {} | |
| 120 | |
| 121 @Override | |
| 122 public void onConnectionError(MojoException e) {} | |
| 123 } | |
| 124 | |
| 125 public static class ExampleServiceImpl implements ExampleService { | |
| 126 public ExampleServiceImpl() {} | |
| 127 | |
| 128 @Override | |
| 129 public void ping(short pingValue, ExampleService.PingResponse callback) { | |
| 130 callback.call(pingValue); | |
| 131 } | |
| 132 | |
| 133 @Override | |
| 134 public void close() {} | |
| 135 | |
| 136 @Override | |
| 137 public void onConnectionError(MojoException e) {} | |
| 138 } | |
| 139 | |
| 140 public static void mojoMain(Context context, Core core, | |
| 141 MessagePipeHandle applicationRequestHandle) { | |
| 142 Log.i(TAG, "App.MojoMain called"); | |
| 143 try (RunLoop runLoop = core.createDefaultRunLoop()) { | |
| 144 Log.i(TAG, "Run loop created"); | |
| 145 | |
| 146 Application application = new ApplicationImpl(applicationRequestHand le, core); | |
| 147 | |
| 148 Log.i(TAG, "Setup done, starting run loop"); | |
| 149 runLoop.run(); | |
| 150 Log.i(TAG, "Run loop exited"); | |
| 151 } | |
| 152 } | |
| 153 } | |
| OLD | NEW |