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.blimp.session; | |
6 | |
7 import org.chromium.base.annotations.CalledByNative; | |
8 import org.chromium.base.annotations.JNINamespace; | |
9 import org.chromium.blimp.R; | |
10 import org.chromium.blimp_public.session.AssignmentRequestResult; | |
11 import org.chromium.ui.base.WindowAndroid; | |
12 | |
13 import java.util.ArrayList; | |
14 import java.util.List; | |
15 | |
16 /** | |
17 * The Java representation of a native BlimpClientSession. This is primarily us
ed to provide access | |
18 * to the native session methods and to facilitate passing a BlimpClientSession
object between Java | |
19 * classes with native counterparts. | |
20 */ | |
21 @JNINamespace("blimp::client") | |
22 public class BlimpClientSession { | |
23 /** | |
24 * An observer for when the session needs to notify the UI about the state o
f the Blimp session. | |
25 */ | |
26 public interface ConnectionObserver { | |
27 /** | |
28 * Called when an engine assignment has been successful or failed. | |
29 * @param result The result code of the assignment.
See | |
30 * assignment_source.h for details. M
aps to a value in | |
31 * {@link Result}. | |
32 * @param suggestedMessageResourceId A suggested resource id for a strin
g to display to the | |
33 * user if necessary. | |
34 * @param engineInfo IP address and version of blimp eng
ine. | |
35 */ | |
36 void onAssignmentReceived( | |
37 int result, int suggestedMessageResourceId, EngineInfo engineInf
o); | |
38 | |
39 /** | |
40 * Called when a connection to the engine was made successfully. | |
41 */ | |
42 void onConnected(); | |
43 | |
44 /** | |
45 * Called when the engine connection was dropped. | |
46 * @param reason The string-based error code. | |
47 * See net/base/net_errors.h for a complete list of codes | |
48 * and their explanations. | |
49 */ | |
50 void onDisconnected(String reason); | |
51 | |
52 /** | |
53 * Called to update the debug UI about network statistics for the curren
t web page. | |
54 * @param received Number of bytes received. | |
55 * @param sent Number of bytes sent. | |
56 * @param commit Number of commits completed. | |
57 */ | |
58 void updateDebugStatsUI(int received, int sent, int commits); | |
59 } | |
60 | |
61 private final String mAssignerUrl; | |
62 private final List<ConnectionObserver> mObservers; | |
63 private long mNativeBlimpClientSessionAndroidPtr; | |
64 | |
65 public BlimpClientSession(String assignerUrl, WindowAndroid windowAndroid) { | |
66 mAssignerUrl = assignerUrl; | |
67 mObservers = new ArrayList<ConnectionObserver>(); | |
68 mNativeBlimpClientSessionAndroidPtr = | |
69 nativeInit(mAssignerUrl, windowAndroid.getNativePointer()); | |
70 } | |
71 | |
72 /** | |
73 * Add an observer to be notified about the connection status. | |
74 * @param observer The observer to add. | |
75 */ | |
76 public void addObserver(ConnectionObserver observer) { | |
77 mObservers.add(observer); | |
78 } | |
79 | |
80 /** | |
81 * Remove an observer from the observer list. | |
82 * @param observer The observer to remove. | |
83 */ | |
84 public void removeObserver(ConnectionObserver observer) { | |
85 mObservers.remove(observer); | |
86 } | |
87 | |
88 /** | |
89 * Retrieves an assignment and uses it to connect to the engine. | |
90 * @param token A OAuth2 access token for the account requesting access. | |
91 */ | |
92 public void connect(String token) { | |
93 nativeConnect(mNativeBlimpClientSessionAndroidPtr, token); | |
94 } | |
95 | |
96 /** | |
97 * Destroys the native BlimpClientSession. This class should not be used af
ter this is called. | |
98 */ | |
99 public void destroy() { | |
100 if (mNativeBlimpClientSessionAndroidPtr == 0) return; | |
101 | |
102 mObservers.clear(); | |
103 nativeDestroy(mNativeBlimpClientSessionAndroidPtr); | |
104 mNativeBlimpClientSessionAndroidPtr = 0; | |
105 } | |
106 | |
107 // Methods that are called by native via JNI. | |
108 @CalledByNative | |
109 private void onAssignmentReceived(int result, String engineIP, String engine
Version) { | |
110 if (mObservers.isEmpty()) return; | |
111 | |
112 int resultMessageResourceId = R.string.assignment_failure_unknown; | |
113 switch (result) { | |
114 case AssignmentRequestResult.OK: | |
115 resultMessageResourceId = R.string.assignment_success; | |
116 break; | |
117 case AssignmentRequestResult.BAD_REQUEST: | |
118 resultMessageResourceId = R.string.assignment_failure_bad_reques
t; | |
119 break; | |
120 case AssignmentRequestResult.BAD_RESPONSE: | |
121 resultMessageResourceId = R.string.assignment_failure_bad_respon
se; | |
122 break; | |
123 case AssignmentRequestResult.INVALID_PROTOCOL_VERSION: | |
124 resultMessageResourceId = R.string.assignment_failure_bad_versio
n; | |
125 break; | |
126 case AssignmentRequestResult.EXPIRED_ACCESS_TOKEN: | |
127 resultMessageResourceId = R.string.assignment_failure_expired_to
ken; | |
128 break; | |
129 case AssignmentRequestResult.USER_INVALID: | |
130 resultMessageResourceId = R.string.assignment_failure_user_inval
id; | |
131 break; | |
132 case AssignmentRequestResult.OUT_OF_VMS: | |
133 resultMessageResourceId = R.string.assignment_failure_out_of_vms
; | |
134 break; | |
135 case AssignmentRequestResult.SERVER_ERROR: | |
136 resultMessageResourceId = R.string.assignment_failure_server_err
or; | |
137 break; | |
138 case AssignmentRequestResult.SERVER_INTERRUPTED: | |
139 resultMessageResourceId = R.string.assignment_failure_server_int
errupted; | |
140 break; | |
141 case AssignmentRequestResult.NETWORK_FAILURE: | |
142 resultMessageResourceId = R.string.assignment_failure_network; | |
143 break; | |
144 case AssignmentRequestResult.UNKNOWN: | |
145 default: | |
146 resultMessageResourceId = R.string.assignment_failure_unknown; | |
147 break; | |
148 } | |
149 for (ConnectionObserver observer : mObservers) { | |
150 observer.onAssignmentReceived(result, resultMessageResourceId, | |
151 new EngineInfo(engineIP, engineVersion, mAssignerUrl)); | |
152 } | |
153 } | |
154 | |
155 @CalledByNative | |
156 void onConnected() { | |
157 for (ConnectionObserver observer : mObservers) { | |
158 observer.onConnected(); | |
159 } | |
160 } | |
161 | |
162 @CalledByNative | |
163 void onDisconnected(String reason) { | |
164 for (ConnectionObserver observer : mObservers) { | |
165 observer.onDisconnected(reason); | |
166 } | |
167 } | |
168 | |
169 @CalledByNative | |
170 private long getNativePtr() { | |
171 assert mNativeBlimpClientSessionAndroidPtr != 0; | |
172 return mNativeBlimpClientSessionAndroidPtr; | |
173 } | |
174 | |
175 /** | |
176 * Makes a JNI call to pull the debug statistics. | |
177 */ | |
178 public int[] getDebugStats() { | |
179 if (mNativeBlimpClientSessionAndroidPtr == 0) return null; | |
180 return nativeGetDebugInfo(mNativeBlimpClientSessionAndroidPtr); | |
181 } | |
182 | |
183 private native long nativeInit(String assignerUrl, long windowAndroidPtr); | |
184 private native void nativeConnect(long nativeBlimpClientSessionAndroid, Stri
ng token); | |
185 private native void nativeDestroy(long nativeBlimpClientSessionAndroid); | |
186 private native int[] nativeGetDebugInfo(long nativeBlimpClientSessionAndroid
); | |
187 } | |
OLD | NEW |