Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1176)

Side by Side Diff: remoting/android/java/src/org/chromium/chromoting/SessionAuthenticator.java

Issue 1537183002: Refactor Chromoting JNI code to use jni/Client (Java changes only). (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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.chromoting; 5 package org.chromium.chromoting;
6 6
7 import android.app.Activity; 7 import android.app.Activity;
8 import android.app.AlertDialog; 8 import android.app.AlertDialog;
9 import android.content.DialogInterface; 9 import android.content.DialogInterface;
10 import android.content.Intent; 10 import android.content.Intent;
11 import android.content.SharedPreferences; 11 import android.content.SharedPreferences;
12 import android.os.Build; 12 import android.os.Build;
13 import android.view.KeyEvent; 13 import android.view.KeyEvent;
14 import android.view.View; 14 import android.view.View;
15 import android.widget.CheckBox; 15 import android.widget.CheckBox;
16 import android.widget.TextView; 16 import android.widget.TextView;
17 import android.widget.Toast; 17 import android.widget.Toast;
18 18
19 import org.chromium.chromoting.jni.Client;
19 import org.chromium.chromoting.jni.JniInterface; 20 import org.chromium.chromoting.jni.JniInterface;
20 21
21 /** 22 /**
22 * This class performs the user-interaction needed to authenticate the session c onnection. This 23 * This class performs the user-interaction needed to authenticate the session c onnection. This
23 * includes showing the PIN prompt and requesting tokens for third-party authent ication. 24 * includes showing the PIN prompt and requesting tokens for third-party authent ication.
24 */ 25 */
25 public class SessionAuthenticator { 26 public class SessionAuthenticator {
26 /** 27 /**
27 * Application context used for getting user preferences, displaying UI, and fetching 28 * Application context used for getting user preferences, displaying UI, and fetching
28 * third-party tokens. 29 * third-party tokens.
29 */ 30 */
30 private Chromoting mApplicationContext; 31 private Chromoting mApplicationContext;
31 32
33 /** Client connection being authenticated. */
34 private final Client mClient;
35
32 /** Provides the tokenUrlPatterns for this host during fetchThirdPartyTokens (). */ 36 /** Provides the tokenUrlPatterns for this host during fetchThirdPartyTokens (). */
33 private HostInfo mHost; 37 private HostInfo mHost;
34 38
35 /** Object for fetching OAuth2 access tokens from third party authorization servers. */ 39 /** Object for fetching OAuth2 access tokens from third party authorization servers. */
36 private ThirdPartyTokenFetcher mTokenFetcher; 40 private ThirdPartyTokenFetcher mTokenFetcher;
37 41
38 public SessionAuthenticator(Chromoting context, HostInfo host) { 42 public SessionAuthenticator(Chromoting context, Client client, HostInfo host ) {
39 mApplicationContext = context; 43 mApplicationContext = context;
44 mClient = client;
40 mHost = host; 45 mHost = host;
41 } 46 }
42 47
43 public void displayAuthenticationPrompt(boolean pairingSupported) { 48 public void displayAuthenticationPrompt(boolean pairingSupported) {
44 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(mApplicationCont ext); 49 AlertDialog.Builder pinPrompt = new AlertDialog.Builder(mApplicationCont ext);
45 pinPrompt.setTitle(mApplicationContext.getString(R.string.title_authenti cate)); 50 pinPrompt.setTitle(mApplicationContext.getString(R.string.title_authenti cate));
46 pinPrompt.setMessage(mApplicationContext.getString(R.string.pin_message_ android)); 51 pinPrompt.setMessage(mApplicationContext.getString(R.string.pin_message_ android));
47 pinPrompt.setIcon(android.R.drawable.ic_lock_lock); 52 pinPrompt.setIcon(android.R.drawable.ic_lock_lock);
48 53
49 final View pinEntry = 54 final View pinEntry =
50 mApplicationContext.getLayoutInflater().inflate(R.layout.pin_dia log, null); 55 mApplicationContext.getLayoutInflater().inflate(R.layout.pin_dia log, null);
51 pinPrompt.setView(pinEntry); 56 pinPrompt.setView(pinEntry);
52 57
53 final TextView pinTextView = (TextView) pinEntry.findViewById(R.id.pin_d ialog_text); 58 final TextView pinTextView = (TextView) pinEntry.findViewById(R.id.pin_d ialog_text);
54 final CheckBox pinCheckBox = (CheckBox) pinEntry.findViewById(R.id.pin_d ialog_check); 59 final CheckBox pinCheckBox = (CheckBox) pinEntry.findViewById(R.id.pin_d ialog_check);
55 60
56 if (!pairingSupported) { 61 if (!pairingSupported) {
57 pinCheckBox.setChecked(false); 62 pinCheckBox.setChecked(false);
58 pinCheckBox.setVisibility(View.GONE); 63 pinCheckBox.setVisibility(View.GONE);
59 } 64 }
60 65
61 pinPrompt.setPositiveButton( 66 pinPrompt.setPositiveButton(
62 R.string.connect_button, new DialogInterface.OnClickListener() { 67 R.string.connect_button, new DialogInterface.OnClickListener() {
63 @Override 68 @Override
64 public void onClick(DialogInterface dialog, int which) { 69 public void onClick(DialogInterface dialog, int which) {
65 if (JniInterface.isConnected()) { 70 if (mClient.isConnected()) {
66 JniInterface.handleAuthenticationResponse( 71 mClient.handleAuthenticationResponse(
67 String.valueOf(pinTextView.getText()), 72 String.valueOf(pinTextView.getText()),
68 pinCheckBox.isChecked(), Build.MODEL); 73 pinCheckBox.isChecked(), Build.MODEL);
69 } else { 74 } else {
70 String message = 75 String message =
71 mApplicationContext.getString(R.string.error _network_error); 76 mApplicationContext.getString(R.string.error _network_error);
72 Toast.makeText(mApplicationContext, message, Toast.L ENGTH_LONG).show(); 77 Toast.makeText(mApplicationContext, message, Toast.L ENGTH_LONG).show();
73 } 78 }
74 } 79 }
75 }); 80 });
76 81
77 pinPrompt.setNegativeButton( 82 pinPrompt.setNegativeButton(
78 R.string.cancel, new DialogInterface.OnClickListener() { 83 R.string.cancel, new DialogInterface.OnClickListener() {
79 @Override 84 @Override
80 public void onClick(DialogInterface dialog, int which) { 85 public void onClick(DialogInterface dialog, int which) {
81 JniInterface.disconnectFromHost(); 86 JniInterface.destroyClient();
Sergey Ulanov 2015/12/21 17:58:15 can this be replaced with a call to mClient? i.e.
Lambros 2016/01/29 23:58:25 Done.
82 } 87 }
83 }); 88 });
84 89
85 final AlertDialog pinDialog = pinPrompt.create(); 90 final AlertDialog pinDialog = pinPrompt.create();
86 91
87 pinTextView.setOnEditorActionListener( 92 pinTextView.setOnEditorActionListener(
88 new TextView.OnEditorActionListener() { 93 new TextView.OnEditorActionListener() {
89 @Override 94 @Override
90 public boolean onEditorAction(TextView v, int actionId, KeyE vent event) { 95 public boolean onEditorAction(TextView v, int actionId, KeyE vent event) {
91 // The user pressed enter on the keypad (equivalent to t he connect button). 96 // The user pressed enter on the keypad (equivalent to t he connect button).
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
131 public void onTokenFetched(String code, String accessToken) { 136 public void onTokenFetched(String code, String accessToken) {
132 // The native client sends the OAuth authorization code to the h ost as the token so 137 // The native client sends the OAuth authorization code to the h ost as the token so
133 // that the host can obtain the shared secret from the third par ty authorization 138 // that the host can obtain the shared secret from the third par ty authorization
134 // server. 139 // server.
135 String token = code; 140 String token = code;
136 141
137 // The native client uses the OAuth access token as the shared s ecret to 142 // The native client uses the OAuth access token as the shared s ecret to
138 // authenticate itself with the host using spake. 143 // authenticate itself with the host using spake.
139 String sharedSecret = accessToken; 144 String sharedSecret = accessToken;
140 145
141 JniInterface.onThirdPartyTokenFetched(token, sharedSecret); 146 mClient.onThirdPartyTokenFetched(token, sharedSecret);
142 } 147 }
143 }; 148 };
144 mTokenFetcher = new ThirdPartyTokenFetcher(mApplicationContext, mHost.ge tTokenUrlPatterns(), 149 mTokenFetcher = new ThirdPartyTokenFetcher(mApplicationContext, mHost.ge tTokenUrlPatterns(),
145 callback); 150 callback);
146 mTokenFetcher.fetchToken(tokenUrl, clientId, scope); 151 mTokenFetcher.fetchToken(tokenUrl, clientId, scope);
147 } 152 }
148 153
149 public void onNewIntent(Intent intent) { 154 public void onNewIntent(Intent intent) {
150 if (mTokenFetcher != null) { 155 if (mTokenFetcher != null) {
151 if (mTokenFetcher.handleTokenFetched(intent)) { 156 if (mTokenFetcher.handleTokenFetched(intent)) {
152 mTokenFetcher = null; 157 mTokenFetcher = null;
153 } 158 }
154 } 159 }
155 } 160 }
156 161
157 /** Returns the pairing ID for the given host, or an empty string if not set . */ 162 /** Returns the pairing ID for the given host, or an empty string if not set . */
158 public String getPairingId(String hostId) { 163 public String getPairingId(String hostId) {
159 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO DE_PRIVATE); 164 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO DE_PRIVATE);
160 return prefs.getString(hostId + "_id", ""); 165 return prefs.getString(hostId + "_id", "");
161 } 166 }
162 167
163 /** Returns the pairing secret for the given host, or an empty string if not set. */ 168 /** Returns the pairing secret for the given host, or an empty string if not set. */
164 public String getPairingSecret(String hostId) { 169 public String getPairingSecret(String hostId) {
165 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO DE_PRIVATE); 170 SharedPreferences prefs = mApplicationContext.getPreferences(Activity.MO DE_PRIVATE);
166 return prefs.getString(hostId + "_secret", ""); 171 return prefs.getString(hostId + "_secret", "");
167 } 172 }
168 } 173 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698