OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.blimp.session; | 5 package org.chromium.blimp.session; |
6 | 6 |
7 import org.chromium.base.annotations.CalledByNative; | 7 import org.chromium.base.annotations.CalledByNative; |
8 import org.chromium.base.annotations.JNINamespace; | 8 import org.chromium.base.annotations.JNINamespace; |
9 import org.chromium.blimp.R; | |
10 import org.chromium.blimp.assignment.Result; | |
9 | 11 |
10 /** | 12 /** |
11 * The Java representation of a native BlimpClientSession. This is primarily us ed to provide access | 13 * The Java representation of a native BlimpClientSession. This is primarily us ed to provide access |
12 * to the native session methods and to facilitate passing a BlimpClientSession object between Java | 14 * to the native session methods and to facilitate passing a BlimpClientSession object between Java |
13 * classes with native counterparts. | 15 * classes with native counterparts. |
14 */ | 16 */ |
15 @JNINamespace("blimp::client") | 17 @JNINamespace("blimp::client") |
16 public class BlimpClientSession { | 18 public class BlimpClientSession { |
19 /** | |
20 * A callback for when the session needs to notify the UI about the state of the Blimp session. | |
21 */ | |
22 public interface Callback { | |
23 /** | |
24 * Called when an engine assignment has been successful or failed. | |
25 * @param result The result code of the assignment. See | |
26 * assignment_source.h for details. M aps to a value in | |
27 * org.chromium.blimp.AssignmentSource Result.java. | |
nyquist
2016/02/18 01:58:40
Optional nit: Even though it's generated, if peopl
David Trainor- moved to gerrit
2016/02/18 16:01:54
Done.
| |
28 * @param suggestedMessageResourceId A suggested resource id for a strin g to display to the | |
29 * user if necessary. | |
30 */ | |
31 void onAssignmentReceived(int result, int suggestedMessageResourceId); | |
32 } | |
33 | |
34 private final Callback mCallback; | |
17 private long mNativeBlimpClientSessionAndroidPtr; | 35 private long mNativeBlimpClientSessionAndroidPtr; |
18 | 36 |
19 public BlimpClientSession() { | 37 public BlimpClientSession(Callback callback) { |
38 mCallback = callback; | |
20 mNativeBlimpClientSessionAndroidPtr = nativeInit(); | 39 mNativeBlimpClientSessionAndroidPtr = nativeInit(); |
21 } | 40 } |
22 | 41 |
23 /** | 42 /** |
24 * Retrieves an assignment and uses it to connect to the engine. | 43 * Retrieves an assignment and uses it to connect to the engine. |
44 * @param token A login token for the account requesting access. | |
nyquist
2016/02/18 01:58:40
s/login/OAuth2 access/
David Trainor- moved to gerrit
2016/02/18 16:01:54
Done.
| |
25 */ | 45 */ |
26 public void connect() { | 46 public void connect(String token) { |
27 nativeConnect(mNativeBlimpClientSessionAndroidPtr); | 47 nativeConnect(mNativeBlimpClientSessionAndroidPtr, token); |
28 } | 48 } |
29 | 49 |
30 /** | 50 /** |
31 * Destroys the native BlimpClientSession. This class should not be used af ter this is called. | 51 * Destroys the native BlimpClientSession. This class should not be used af ter this is called. |
32 */ | 52 */ |
33 public void destroy() { | 53 public void destroy() { |
34 if (mNativeBlimpClientSessionAndroidPtr == 0) return; | 54 if (mNativeBlimpClientSessionAndroidPtr == 0) return; |
35 | 55 |
36 nativeDestroy(mNativeBlimpClientSessionAndroidPtr); | 56 nativeDestroy(mNativeBlimpClientSessionAndroidPtr); |
37 mNativeBlimpClientSessionAndroidPtr = 0; | 57 mNativeBlimpClientSessionAndroidPtr = 0; |
38 } | 58 } |
39 | 59 |
40 // Methods that are called by native via JNI. | 60 // Methods that are called by native via JNI. |
41 @CalledByNative | 61 @CalledByNative |
62 private void onAssignmentReceived(int result) { | |
63 if (mCallback != null) { | |
nyquist
2016/02/18 01:58:40
Nit: if (mCallback == null) return;
David Trainor- moved to gerrit
2016/02/18 16:01:54
Done.
| |
64 int resultMessageResourceId = R.string.assignment_failure_unknown; | |
65 switch (result) { | |
66 case Result.OK: | |
67 resultMessageResourceId = R.string.assignment_success; | |
68 break; | |
69 case Result.BAD_REQUEST: | |
70 resultMessageResourceId = R.string.assignment_failure_bad_re quest; | |
71 break; | |
72 case Result.BAD_RESPONSE: | |
73 resultMessageResourceId = R.string.assignment_failure_bad_re sponse; | |
74 break; | |
75 case Result.INVALID_PROTOCOL_VERSION: | |
76 resultMessageResourceId = R.string.assignment_failure_bad_ve rsion; | |
77 break; | |
78 case Result.EXPIRED_ACCESS_TOKEN: | |
79 resultMessageResourceId = R.string.assignment_failure_expire d_token; | |
80 break; | |
81 case Result.USER_INVALID: | |
82 resultMessageResourceId = R.string.assignment_failure_user_i nvalid; | |
83 break; | |
84 case Result.OUT_OF_VMS: | |
85 resultMessageResourceId = R.string.assignment_failure_out_of _vms; | |
86 break; | |
87 case Result.SERVER_ERROR: | |
88 resultMessageResourceId = R.string.assignment_failure_server _error; | |
89 break; | |
90 case Result.SERVER_INTERRUPTED: | |
91 resultMessageResourceId = R.string.assignment_failure_server _interrupted; | |
92 break; | |
93 case Result.NETWORK_FAILURE: | |
94 resultMessageResourceId = R.string.assignment_failure_networ k; | |
95 break; | |
96 case Result.UNKNOWN: | |
97 default: | |
98 resultMessageResourceId = R.string.assignment_failure_unknow n; | |
99 break; | |
100 } | |
101 mCallback.onAssignmentReceived(result, resultMessageResourceId); | |
102 } | |
103 } | |
104 | |
105 @CalledByNative | |
42 private long getNativePtr() { | 106 private long getNativePtr() { |
43 assert mNativeBlimpClientSessionAndroidPtr != 0; | 107 assert mNativeBlimpClientSessionAndroidPtr != 0; |
44 return mNativeBlimpClientSessionAndroidPtr; | 108 return mNativeBlimpClientSessionAndroidPtr; |
45 } | 109 } |
46 | 110 |
47 private native long nativeInit(); | 111 private native long nativeInit(); |
48 private native void nativeConnect(long nativeBlimpClientSessionAndroid); | 112 private native void nativeConnect(long nativeBlimpClientSessionAndroid, Stri ng token); |
49 private native void nativeDestroy(long nativeBlimpClientSessionAndroid); | 113 private native void nativeDestroy(long nativeBlimpClientSessionAndroid); |
50 } | 114 } |
OLD | NEW |