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.Interface.Manager; | |
12 import org.chromium.mojo.bindings.InterfaceRequest; | |
13 import org.chromium.mojo.system.Core; | |
14 import org.chromium.mojo.system.MessagePipeHandle; | |
15 import org.chromium.mojo.system.MojoException; | |
16 import org.chromium.mojo.system.RunLoop; | |
17 import org.chromium.mojom.mojo.Application; | |
18 import org.chromium.mojom.mojo.ExampleService; | |
19 import org.chromium.mojom.mojo.ServiceProvider; | |
20 import org.chromium.mojom.mojo.Shell; | |
21 | |
22 import java.util.ArrayList; | |
23 import java.util.HashMap; | |
24 import java.util.List; | |
25 import java.util.Map; | |
26 import java.util.concurrent.Callable; | |
27 | |
28 /** | |
29 * App holds all the code for this example application. | |
30 */ | |
31 class App { | |
32 private static String TAG = "AndroidTestApp"; | |
qsr
2015/02/06 17:16:14
Android doesn't really tell us anything when looki
etiennej
2015/02/09 14:09:51
Done.
| |
33 | |
34 public static class ApplicationImpl implements Application { | |
35 private final Core mCore; | |
36 private Shell mShell; | |
37 private List<ServiceProvider> mProviders = new ArrayList<ServiceProvider >(); | |
qsr
2015/02/06 17:16:14
final?
Also, I think you do not need those. Each
etiennej
2015/02/09 14:09:51
Done.
| |
38 | |
39 public ApplicationImpl(MessagePipeHandle applicationRequest, Core core) { | |
40 ApplicationImpl.MANAGER.bind(this, applicationRequest); | |
41 mCore = core; | |
42 } | |
43 | |
44 @Override | |
45 public void initialize(Shell shell, String[] args) { | |
46 mShell = shell; | |
47 } | |
48 | |
49 @Override | |
50 public void acceptConnection(String requestorUrl, | |
51 InterfaceRequest<ServiceProvider> services, ServiceProvider expo sedServices) { | |
52 ServiceProviderImpl serviceProvider = new ServiceProviderImpl(); | |
53 serviceProvider.addService( | |
54 new ServiceFactoryBinder<ExampleService>( | |
55 ExampleServiceImpl.MANAGER, | |
56 new Callable<ExampleService>() { | |
57 @Override | |
58 public ExampleService call() { | |
59 return new ExampleServiceImpl(); | |
60 } | |
61 } | |
62 )); | |
63 ServiceProvider.MANAGER.bind(serviceProvider, services); | |
64 mProviders.add(serviceProvider); | |
65 exposedServices.close(); | |
66 }; | |
67 | |
68 @Override | |
69 public void requestQuit() { | |
70 mCore.getCurrentRunLoop().quit(); | |
71 } | |
72 | |
73 @Override | |
74 public void close() { | |
75 if (mShell != null) { | |
76 mShell.close(); | |
77 } | |
78 } | |
79 | |
80 @Override | |
81 public void onConnectionError(MojoException e) {} | |
82 } | |
83 | |
84 /** | |
85 * ServiceFactoryBinder holds the necessary information to bind a service in terface to a | |
86 * message pipe. | |
87 */ | |
88 private static class ServiceFactoryBinder<T extends Interface> { | |
89 private final Callable<T> mFactory; | |
90 private final Manager<T, ?> mManager; | |
91 | |
92 public ServiceFactoryBinder(Manager<T, ?> manager, | |
93 Callable<T> factory) { | |
94 mFactory = factory; | |
95 mManager = manager; | |
96 } | |
97 | |
98 public void bindNewInstanceToMessagePipe(MessagePipeHandle pipe) throws Exception { | |
qsr
2015/02/06 17:16:14
Why do you need to throws Exception here? If that'
etiennej
2015/02/09 14:09:51
Done.
| |
99 mManager.bind(mFactory.call(), pipe); | |
100 } | |
101 | |
102 public String getInterfaceName() { | |
103 return mManager.getName(); | |
104 } | |
105 } | |
106 | |
107 public static class ServiceProviderImpl implements ServiceProvider { | |
108 private Map<String, ServiceFactoryBinder> mNameToServiceMap = | |
qsr
2015/02/06 17:16:14
I'm surprise you do not have a warning. Please use
etiennej
2015/02/09 14:09:51
Done.
| |
109 new HashMap<String, ServiceFactoryBinder>(); | |
110 | |
111 public ServiceProviderImpl() {} | |
112 | |
113 /** | |
114 * AddService adds a new service to the list of services provided by thi s application. | |
115 */ | |
116 public void addService(ServiceFactoryBinder binder) { | |
117 mNameToServiceMap.put(binder.getInterfaceName(), binder); | |
118 } | |
119 | |
120 @Override | |
121 public void connectToService(String interfaceName, MessagePipeHandle pip e) { | |
122 if (mNameToServiceMap.containsKey(interfaceName)) { | |
123 try { | |
124 mNameToServiceMap.get(interfaceName).bindNewInstanceToMessag ePipe(pipe); | |
125 } catch (Exception e) { | |
126 pipe.close(); | |
127 throw new RuntimeException(e); | |
128 } | |
129 } else { | |
130 pipe.close(); | |
131 } | |
132 } | |
133 | |
134 @Override | |
135 public void close() {} | |
136 | |
137 @Override | |
138 public void onConnectionError(MojoException e) {} | |
139 } | |
140 | |
141 public static class ExampleServiceImpl implements ExampleService { | |
142 public ExampleServiceImpl() {} | |
143 | |
144 @Override | |
145 public void ping(short pingValue, ExampleService.PingResponse callback) { | |
146 callback.call(pingValue); | |
147 } | |
148 | |
149 @Override | |
150 public void close() {} | |
151 | |
152 @Override | |
153 public void onConnectionError(MojoException e) {} | |
154 } | |
155 | |
156 public static void mojoMain(Context context, Core core, int applicationReque stHandle) { | |
157 Log.i(TAG, "App.MojoMain called"); | |
158 RunLoop runLoop = core.createDefaultRunLoop(); | |
159 Log.i(TAG, "Run loop created"); | |
160 | |
161 MessagePipeHandle handle = | |
162 core.acquireNativeHandle(applicationRequestHandle).toMessagePipe Handle(); | |
163 Application application = new ApplicationImpl(handle, core); | |
164 | |
165 Log.i(TAG, "Setup done, starting run loop"); | |
166 runLoop.run(); | |
167 Log.i(TAG, "Run loop exited"); | |
168 } | |
169 } | |
OLD | NEW |