1
- // Copyright (C) 2017 Google Inc. All Rights Reserved .
1
+ // Copyright 2018 Google Inc. All rights reserved .
2
2
//
3
- // Licensed under the Apache License, Version 2.0 (the "License");
4
- // you may not use this file except in compliance with the License.
5
- // You may obtain a copy of the License at
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
6
//
7
- // http://www.apache.org/licenses/LICENSE-2.0
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
8
//
9
- // Unless required by applicable law or agreed to in writing, software
10
- // distributed under the License is distributed on an "AS IS" BASIS,
11
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- // See the License for the specific language governing permissions and
13
- // limitations under the License.
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
14
15
15
#include " google_signin.h"
16
16
#include < android/log.h>
17
17
#include < cassert>
18
18
#include " google_signin_user_impl.h"
19
- #include " jni_init .h"
19
+ #include " jni_context .h"
20
20
21
21
#define TAG " native-googlesignin"
22
22
#define HELPER_CLASSNAME " com/google/googlesignin/GoogleSignInHelper"
@@ -85,7 +85,8 @@ public static void nativeOnResult(long requestHandle, int result,
85
85
" Lcom/google/android/gms/auth/api/signin/GoogleSignInAccount;" \
86
86
" )V"
87
87
88
- namespace googlesignin {
88
+ namespace google {
89
+ namespace signin {
89
90
90
91
class GoogleSignInFuture ;
91
92
@@ -94,13 +95,13 @@ class GoogleSignInFuture;
94
95
// For the public methods see google_signin.h for details.
95
96
class GoogleSignIn ::GoogleSignInImpl {
96
97
public:
97
- jobject activity_ ;
98
+ JNIContext jni_ ;
98
99
GoogleSignInFuture *current_result_;
99
100
Configuration *current_configuration_;
100
101
101
102
// Constructs the implementation providing the Java activity to use when
102
103
// making calls.
103
- GoogleSignInImpl (jobject activity);
104
+ GoogleSignInImpl (jobject activity, JavaVM *vm );
104
105
~GoogleSignInImpl ();
105
106
106
107
void Configure (const Configuration &configuration);
@@ -121,8 +122,8 @@ class GoogleSignIn::GoogleSignInImpl {
121
122
void Disconnect ();
122
123
123
124
// Native method implementation for the Java class.
124
- static void NativeOnAuthResult (JNIEnv *env , jobject obj, jlong handle ,
125
- jint result, jobject user);
125
+ static void NativeOnAuthResult (JNIContext &jni_context , jobject obj,
126
+ jlong handle, jint result, jobject user);
126
127
127
128
private:
128
129
void CallConfigure ();
@@ -176,18 +177,16 @@ class GoogleSignInFuture : public Future<GoogleSignIn::SignInResult> {
176
177
};
177
178
178
179
// Constructs a new instance. The static members are initialized if need-be.
179
- GoogleSignIn::GoogleSignInImpl::GoogleSignInImpl (jobject activity)
180
- : current_result_(nullptr ), current_configuration_(nullptr ) {
181
- JNIEnv *env = GetJniEnv ();
182
-
183
- activity_ = env->NewGlobalRef (activity);
180
+ GoogleSignIn::GoogleSignInImpl::GoogleSignInImpl (jobject activity, JavaVM *vm)
181
+ : jni_(activity, vm), current_result_(nullptr ),
182
+ current_configuration_ (nullptr ) {
183
+ JNIEnv *env = jni_.GetJniEnv ();
184
184
185
185
if (!helper_clazz_) {
186
186
// Find the java helper class and initialize it.
187
- helper_clazz_ = FindClass (HELPER_CLASSNAME, activity );
187
+ helper_clazz_ = jni_. FindClass (HELPER_CLASSNAME);
188
188
189
189
assert (helper_clazz_);
190
-
191
190
if (helper_clazz_) {
192
191
helper_clazz_ = (jclass)env->NewGlobalRef (helper_clazz_);
193
192
env->RegisterNatives (helper_clazz_, methods,
@@ -209,71 +208,74 @@ GoogleSignIn::GoogleSignInImpl::GoogleSignInImpl(jobject activity)
209
208
}
210
209
211
210
GoogleSignIn::GoogleSignInImpl::~GoogleSignInImpl () {
212
- JNIEnv *env = GetJniEnv ();
213
-
214
- env->DeleteGlobalRef (activity_);
215
- activity_ = nullptr ;
216
211
delete current_result_;
217
212
current_result_ = nullptr ;
218
213
}
219
214
220
215
void GoogleSignIn::GoogleSignInImpl::EnableDebugLogging (bool flag) {
221
- JNIEnv *env = GetJniEnv ();
216
+ JNIEnv *env = jni_. GetJniEnv ();
222
217
223
218
env->CallStaticVoidMethod (helper_clazz_, enable_debug_method_, flag);
224
-
225
219
}
226
220
227
221
void GoogleSignIn::GoogleSignInImpl::Configure (
228
222
const Configuration &configuration) {
229
223
delete current_configuration_;
230
224
current_configuration_ = new Configuration (configuration);
231
225
226
+ if (configuration.web_client_id ) {
227
+ current_configuration_->web_client_id = strdup (configuration.web_client_id );
228
+ }
229
+
232
230
delete current_result_;
233
231
current_result_ = new GoogleSignInFuture ();
234
232
235
233
CallConfigure ();
236
234
}
237
235
238
236
void GoogleSignIn::GoogleSignInImpl::CallConfigure () {
239
- JNIEnv *env = GetJniEnv ();
237
+ JNIEnv *env = jni_. GetJniEnv ();
240
238
241
239
if (!current_configuration_) {
242
240
__android_log_print (ANDROID_LOG_ERROR, TAG, " configuration is null!?" );
243
241
return ;
244
242
}
245
243
jstring j_web_client_id =
246
- current_configuration_->web_client_id .empty () ? nullptr
247
- : env->NewStringUTF (current_configuration_->web_client_id .c_str ());
244
+ current_configuration_->web_client_id
245
+ ? env->NewStringUTF (current_configuration_->web_client_id )
246
+ : nullptr ;
248
247
249
248
jstring j_account_name =
250
- current_configuration_->account_name .empty () ? nullptr
251
- : env->NewStringUTF (current_configuration_->account_name .c_str ());
249
+ current_configuration_->account_name
250
+ ? env->NewStringUTF (current_configuration_->account_name )
251
+ : nullptr ;
252
252
253
253
jobjectArray j_auth_scopes = nullptr ;
254
254
255
- if (current_configuration_->additional_scopes . size () > 0 ) {
256
- jclass string_clazz = FindClass (" java/lang/String" , activity_ );
255
+ if (current_configuration_->additional_scope_count > 0 ) {
256
+ jclass string_clazz = jni_. FindClass (" java/lang/String" );
257
257
j_auth_scopes = env->NewObjectArray (
258
- current_configuration_->additional_scopes . size () , string_clazz, nullptr );
258
+ current_configuration_->additional_scope_count , string_clazz, nullptr );
259
259
260
- for (int i = 0 ; i < current_configuration_->additional_scopes . size () ; i++) {
260
+ for (int i = 0 ; i < current_configuration_->additional_scope_count ; i++) {
261
261
env->SetObjectArrayElement (
262
262
j_auth_scopes, i,
263
- env->NewStringUTF (current_configuration_->additional_scopes [i]. c_str () ));
263
+ env->NewStringUTF (current_configuration_->additional_scopes [i]));
264
264
}
265
265
env->DeleteLocalRef (string_clazz);
266
266
}
267
267
268
+ jobject j_activity = jni_.GetActivity ();
268
269
env->CallStaticVoidMethod (
269
- helper_clazz_, config_method_, activity_ ,
270
+ helper_clazz_, config_method_, j_activity ,
270
271
current_configuration_->use_game_signin , j_web_client_id,
271
272
current_configuration_->request_auth_code ,
272
273
current_configuration_->force_token_refresh ,
273
274
current_configuration_->request_email ,
274
275
current_configuration_->request_id_token ,
275
276
current_configuration_->hide_ui_popups , j_account_name, j_auth_scopes,
276
277
reinterpret_cast <jlong>(current_result_));
278
+ env->DeleteLocalRef (j_activity);
277
279
278
280
if (j_web_client_id) {
279
281
env->DeleteLocalRef (j_web_client_id);
@@ -289,32 +291,36 @@ void GoogleSignIn::GoogleSignInImpl::CallConfigure() {
289
291
}
290
292
291
293
Future<GoogleSignIn::SignInResult> &GoogleSignIn::GoogleSignInImpl::SignIn () {
292
- JNIEnv *env = GetJniEnv ();
294
+ JNIEnv *env = jni_. GetJniEnv ();
293
295
294
296
if (current_result_) {
295
297
current_result_->SetResult (nullptr );
296
298
}
297
299
298
300
CallConfigure ();
299
301
300
- env->CallStaticVoidMethod (helper_clazz_, signin_method_, activity_,
302
+ jobject j_activity = jni_.GetActivity ();
303
+ env->CallStaticVoidMethod (helper_clazz_, signin_method_, j_activity,
301
304
(jlong)current_result_);
305
+ env->DeleteLocalRef (j_activity);
302
306
303
307
return *current_result_;
304
308
}
305
309
306
310
Future<GoogleSignIn::SignInResult>
307
311
&GoogleSignIn::GoogleSignInImpl::SignInSilently () {
308
- JNIEnv *env = GetJniEnv ();
312
+ JNIEnv *env = jni_. GetJniEnv ();
309
313
310
314
if (current_result_) {
311
315
current_result_->SetResult (nullptr );
312
316
}
313
317
314
318
CallConfigure ();
315
319
316
- env->CallStaticVoidMethod (helper_clazz_, signinsilently_method_, activity_,
320
+ jobject j_activity = jni_.GetActivity ();
321
+ env->CallStaticVoidMethod (helper_clazz_, signinsilently_method_, j_activity,
317
322
(jlong)current_result_);
323
+ env->DeleteLocalRef (j_activity);
318
324
319
325
return *current_result_;
320
326
}
@@ -327,30 +333,34 @@ const Future<GoogleSignIn::SignInResult>
327
333
328
334
// Signs out.
329
335
void GoogleSignIn::GoogleSignInImpl::SignOut () {
330
- JNIEnv *env = GetJniEnv ();
336
+ JNIEnv *env = jni_. GetJniEnv ();
331
337
338
+ jobject j_activity = jni_.GetActivity ();
332
339
__android_log_print (ANDROID_LOG_INFO, TAG,
333
340
" helper: %x method: %x activity: %x" ,
334
341
(uintptr_t )helper_clazz_, (uintptr_t )signin_method_,
335
- (uintptr_t )activity_ );
342
+ (uintptr_t )j_activity );
336
343
337
- env->CallStaticVoidMethod (helper_clazz_, signout_method_, activity_);
344
+ env->CallStaticVoidMethod (helper_clazz_, signout_method_, j_activity);
345
+ env->DeleteLocalRef (j_activity);
338
346
}
339
347
340
348
// Signs out.
341
349
void GoogleSignIn::GoogleSignInImpl::Disconnect () {
342
- JNIEnv *env = GetJniEnv ();
350
+ JNIEnv *env = jni_. GetJniEnv ();
343
351
344
- env->CallStaticVoidMethod (helper_clazz_, disconnect_method_, activity_);
352
+ jobject j_activity = jni_.GetActivity ();
353
+ env->CallStaticVoidMethod (helper_clazz_, disconnect_method_, j_activity);
354
+ env->DeleteLocalRef (j_activity);
345
355
}
346
356
347
- void GoogleSignIn::GoogleSignInImpl::NativeOnAuthResult (
348
- JNIEnv *env, jobject obj, jlong handle, jint result, jobject user) {
357
+ void GoogleSignIn::GoogleSignInImpl::NativeOnAuthResult (JNIContext &jni_context,
358
+ jobject obj, jlong handle, jint result, jobject user) {
349
359
GoogleSignInFuture *future = reinterpret_cast <GoogleSignInFuture *>(handle);
350
360
if (future) {
351
361
SignInResult *rc = new GoogleSignIn::SignInResult ();
352
362
rc->StatusCode = result;
353
- rc->User = GoogleSignInUserImpl::UserFromAccount (user);
363
+ rc->User = GoogleSignInUserImpl::UserFromAccount (jni_context, user);
354
364
355
365
if (rc->User ) {
356
366
__android_log_print (ANDROID_LOG_INFO, TAG, " User Display Name is %s" ,
@@ -362,8 +372,9 @@ void GoogleSignIn::GoogleSignInImpl::NativeOnAuthResult(
362
372
363
373
// Public class implementation. These are called by external callers to use the
364
374
// Google Sign-in API.
365
- GoogleSignIn::GoogleSignIn (jobject activity)
366
- : impl_(new GoogleSignInImpl(activity)) {}
375
+
376
+ GoogleSignIn::GoogleSignIn (jobject activity, JavaVM *vm)
377
+ : impl_(new GoogleSignInImpl(activity, vm)) {}
367
378
368
379
void GoogleSignIn::EnableDebugLogging (bool flag) {
369
380
impl_->EnableDebugLogging (flag);
@@ -389,4 +400,5 @@ void GoogleSignIn::SignOut() { impl_->SignOut(); }
389
400
390
401
void GoogleSignIn::Disconnect () { impl_->Disconnect (); }
391
402
392
- } // namespace googlesignin
403
+ } // namespace signin
404
+ } // namespace google
0 commit comments