diff --git a/.gitignore b/.gitignore index 1e837bc9..20168e73 100644 --- a/.gitignore +++ b/.gitignore @@ -16,8 +16,6 @@ ProjectSettings/ Temp/ obj/ -.DS_Store - # Don't check in the resolver GoogleSignInPlugin/Assets/PlayServicesResolver/ diff --git a/staging/native/src/android/google_signin.cc b/staging/native/src/android/google_signin.cc index 59848e4f..bf54f4b3 100644 --- a/staging/native/src/android/google_signin.cc +++ b/staging/native/src/android/google_signin.cc @@ -1,22 +1,22 @@ -// Copyright (C) 2017 Google Inc. All Rights Reserved. +// Copyright 2018 Google Inc. All rights reserved. // -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. #include "google_signin.h" #include #include #include "google_signin_user_impl.h" -#include "jni_init.h" +#include "jni_context.h" #define TAG "native-googlesignin" #define HELPER_CLASSNAME "com/google/googlesignin/GoogleSignInHelper" @@ -85,7 +85,8 @@ public static void nativeOnResult(long requestHandle, int result, "Lcom/google/android/gms/auth/api/signin/GoogleSignInAccount;" \ ")V" -namespace googlesignin { +namespace google { +namespace signin { class GoogleSignInFuture; @@ -94,13 +95,13 @@ class GoogleSignInFuture; // For the public methods see google_signin.h for details. class GoogleSignIn::GoogleSignInImpl { public: - jobject activity_; + JNIContext jni_; GoogleSignInFuture *current_result_; Configuration *current_configuration_; // Constructs the implementation providing the Java activity to use when // making calls. - GoogleSignInImpl(jobject activity); + GoogleSignInImpl(jobject activity, JavaVM *vm); ~GoogleSignInImpl(); void Configure(const Configuration &configuration); @@ -121,8 +122,8 @@ class GoogleSignIn::GoogleSignInImpl { void Disconnect(); // Native method implementation for the Java class. - static void NativeOnAuthResult(JNIEnv *env, jobject obj, jlong handle, - jint result, jobject user); + static void NativeOnAuthResult(JNIContext &jni_context, jobject obj, + jlong handle, jint result, jobject user); private: void CallConfigure(); @@ -176,18 +177,16 @@ class GoogleSignInFuture : public Future { }; // Constructs a new instance. The static members are initialized if need-be. -GoogleSignIn::GoogleSignInImpl::GoogleSignInImpl(jobject activity) - : current_result_(nullptr), current_configuration_(nullptr) { - JNIEnv *env = GetJniEnv(); - - activity_ = env->NewGlobalRef(activity); +GoogleSignIn::GoogleSignInImpl::GoogleSignInImpl(jobject activity, JavaVM *vm) + : jni_(activity, vm), current_result_(nullptr), + current_configuration_(nullptr) { + JNIEnv *env = jni_.GetJniEnv(); if (!helper_clazz_) { // Find the java helper class and initialize it. - helper_clazz_ = FindClass(HELPER_CLASSNAME, activity); + helper_clazz_ = jni_.FindClass(HELPER_CLASSNAME); assert(helper_clazz_); - if (helper_clazz_) { helper_clazz_ = (jclass)env->NewGlobalRef(helper_clazz_); env->RegisterNatives(helper_clazz_, methods, @@ -209,19 +208,14 @@ GoogleSignIn::GoogleSignInImpl::GoogleSignInImpl(jobject activity) } GoogleSignIn::GoogleSignInImpl::~GoogleSignInImpl() { - JNIEnv *env = GetJniEnv(); - - env->DeleteGlobalRef(activity_); - activity_ = nullptr; delete current_result_; current_result_ = nullptr; } void GoogleSignIn::GoogleSignInImpl::EnableDebugLogging(bool flag) { - JNIEnv *env = GetJniEnv(); + JNIEnv *env = jni_.GetJniEnv(); env->CallStaticVoidMethod(helper_clazz_, enable_debug_method_, flag); - } void GoogleSignIn::GoogleSignInImpl::Configure( @@ -229,6 +223,10 @@ void GoogleSignIn::GoogleSignInImpl::Configure( delete current_configuration_; current_configuration_ = new Configuration(configuration); + if (configuration.web_client_id) { + current_configuration_->web_client_id = strdup(configuration.web_client_id); + } + delete current_result_; current_result_ = new GoogleSignInFuture(); @@ -236,37 +234,40 @@ void GoogleSignIn::GoogleSignInImpl::Configure( } void GoogleSignIn::GoogleSignInImpl::CallConfigure() { - JNIEnv *env = GetJniEnv(); + JNIEnv *env = jni_.GetJniEnv(); if (!current_configuration_) { __android_log_print(ANDROID_LOG_ERROR, TAG, "configuration is null!?"); return; } jstring j_web_client_id = - current_configuration_->web_client_id.empty() ? nullptr - : env->NewStringUTF(current_configuration_->web_client_id.c_str()); + current_configuration_->web_client_id + ? env->NewStringUTF(current_configuration_->web_client_id) + : nullptr; jstring j_account_name = - current_configuration_->account_name.empty() ? nullptr - : env->NewStringUTF(current_configuration_->account_name.c_str()); + current_configuration_->account_name + ? env->NewStringUTF(current_configuration_->account_name) + : nullptr; jobjectArray j_auth_scopes = nullptr; - if (current_configuration_->additional_scopes.size() > 0) { - jclass string_clazz = FindClass("java/lang/String", activity_); + if (current_configuration_->additional_scope_count > 0) { + jclass string_clazz = jni_.FindClass("java/lang/String"); j_auth_scopes = env->NewObjectArray( - current_configuration_->additional_scopes.size(), string_clazz, nullptr); + current_configuration_->additional_scope_count, string_clazz, nullptr); - for (int i = 0; i < current_configuration_->additional_scopes.size(); i++) { + for (int i = 0; i < current_configuration_->additional_scope_count; i++) { env->SetObjectArrayElement( j_auth_scopes, i, - env->NewStringUTF(current_configuration_->additional_scopes[i].c_str())); + env->NewStringUTF(current_configuration_->additional_scopes[i])); } env->DeleteLocalRef(string_clazz); } + jobject j_activity = jni_.GetActivity(); env->CallStaticVoidMethod( - helper_clazz_, config_method_, activity_, + helper_clazz_, config_method_, j_activity, current_configuration_->use_game_signin, j_web_client_id, current_configuration_->request_auth_code, current_configuration_->force_token_refresh, @@ -274,6 +275,7 @@ void GoogleSignIn::GoogleSignInImpl::CallConfigure() { current_configuration_->request_id_token, current_configuration_->hide_ui_popups, j_account_name, j_auth_scopes, reinterpret_cast(current_result_)); + env->DeleteLocalRef(j_activity); if (j_web_client_id) { env->DeleteLocalRef(j_web_client_id); @@ -289,7 +291,7 @@ void GoogleSignIn::GoogleSignInImpl::CallConfigure() { } Future &GoogleSignIn::GoogleSignInImpl::SignIn() { - JNIEnv *env = GetJniEnv(); + JNIEnv *env = jni_.GetJniEnv(); if (current_result_) { current_result_->SetResult(nullptr); @@ -297,15 +299,17 @@ Future &GoogleSignIn::GoogleSignInImpl::SignIn() { CallConfigure(); - env->CallStaticVoidMethod(helper_clazz_, signin_method_, activity_, + jobject j_activity = jni_.GetActivity(); + env->CallStaticVoidMethod(helper_clazz_, signin_method_, j_activity, (jlong)current_result_); + env->DeleteLocalRef(j_activity); return *current_result_; } Future &GoogleSignIn::GoogleSignInImpl::SignInSilently() { - JNIEnv *env = GetJniEnv(); + JNIEnv *env = jni_.GetJniEnv(); if (current_result_) { current_result_->SetResult(nullptr); @@ -313,8 +317,10 @@ Future CallConfigure(); - env->CallStaticVoidMethod(helper_clazz_, signinsilently_method_, activity_, + jobject j_activity = jni_.GetActivity(); + env->CallStaticVoidMethod(helper_clazz_, signinsilently_method_, j_activity, (jlong)current_result_); + env->DeleteLocalRef(j_activity); return *current_result_; } @@ -327,30 +333,34 @@ const Future // Signs out. void GoogleSignIn::GoogleSignInImpl::SignOut() { - JNIEnv *env = GetJniEnv(); + JNIEnv *env = jni_.GetJniEnv(); + jobject j_activity = jni_.GetActivity(); __android_log_print(ANDROID_LOG_INFO, TAG, "helper: %x method: %x activity: %x", (uintptr_t)helper_clazz_, (uintptr_t)signin_method_, - (uintptr_t)activity_); + (uintptr_t)j_activity); - env->CallStaticVoidMethod(helper_clazz_, signout_method_, activity_); + env->CallStaticVoidMethod(helper_clazz_, signout_method_, j_activity); + env->DeleteLocalRef(j_activity); } // Signs out. void GoogleSignIn::GoogleSignInImpl::Disconnect() { - JNIEnv *env = GetJniEnv(); + JNIEnv *env = jni_.GetJniEnv(); - env->CallStaticVoidMethod(helper_clazz_, disconnect_method_, activity_); + jobject j_activity = jni_.GetActivity(); + env->CallStaticVoidMethod(helper_clazz_, disconnect_method_, j_activity); + env->DeleteLocalRef(j_activity); } -void GoogleSignIn::GoogleSignInImpl::NativeOnAuthResult( - JNIEnv *env, jobject obj, jlong handle, jint result, jobject user) { +void GoogleSignIn::GoogleSignInImpl::NativeOnAuthResult(JNIContext &jni_context, + jobject obj, jlong handle, jint result, jobject user) { GoogleSignInFuture *future = reinterpret_cast(handle); if (future) { SignInResult *rc = new GoogleSignIn::SignInResult(); rc->StatusCode = result; - rc->User = GoogleSignInUserImpl::UserFromAccount(user); + rc->User = GoogleSignInUserImpl::UserFromAccount(jni_context, user); if (rc->User) { __android_log_print(ANDROID_LOG_INFO, TAG, "User Display Name is %s", @@ -362,8 +372,9 @@ void GoogleSignIn::GoogleSignInImpl::NativeOnAuthResult( // Public class implementation. These are called by external callers to use the // Google Sign-in API. -GoogleSignIn::GoogleSignIn(jobject activity) - : impl_(new GoogleSignInImpl(activity)) {} + +GoogleSignIn::GoogleSignIn(jobject activity, JavaVM *vm) + : impl_(new GoogleSignInImpl(activity, vm)) {} void GoogleSignIn::EnableDebugLogging(bool flag) { impl_->EnableDebugLogging(flag); @@ -389,4 +400,5 @@ void GoogleSignIn::SignOut() { impl_->SignOut(); } void GoogleSignIn::Disconnect() { impl_->Disconnect(); } -} // namespace googlesignin +} // namespace signin +} // namespace google diff --git a/staging/native/src/include/google_signin.h b/staging/native/src/include/google_signin.h index 8086c06b..9b97edf0 100644 --- a/staging/native/src/include/google_signin.h +++ b/staging/native/src/include/google_signin.h @@ -1,32 +1,29 @@ -// Copyright (C) 2017 Google Inc. All Rights Reserved. +// Copyright 2018 Google Inc. All rights reserved. // -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at // -// http://www.apache.org/licenses/LICENSE-2.0 +// http://www.apache.org/licenses/LICENSE-2.0 // -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. -#ifndef GOOGLESIGNIN_GOOGLESIGNIN_H // NOLINT -#define GOOGLESIGNIN_GOOGLESIGNIN_H - -#if !defined(__ANDROID__) -#error "This class is for Android only." -#endif - -#include -#include -#include +#ifndef GOOGLE_SIGNIN_GOOGLESIGNIN_H // NOLINT +#define GOOGLE_SIGNIN_GOOGLESIGNIN_H #include "future.h" // NOLINT #include "google_signin_user.h" // NOLINT -namespace googlesignin { +#if defined(__ANDROID__) +#include +#endif + +namespace google { +namespace signin { class GoogleSignIn { public: @@ -37,16 +34,28 @@ class GoogleSignIn { /// and Android. /// enum StatusCode { + /// The operation was successful, but used the device's cache. + /// + kStatusCodeSuccessCached = -1, + + /// The operation was successful. + kStatusCodeSuccess = 0, + /// The result is uninitialized. kStatusCodeUninitialized = 100, - /// The client attempted to call a method from an API that - /// failed to connect. - kStatusCodeApiNotConnected = 17, - /// The result was canceled either due to client disconnect - /// or cancel(). - kStatusCodeCanceled = 16, + /// The client attempted to connect to the service with an + /// invalid account name specified. + kStatusCodeInvalidAccount = 5, + + /// A network error occurred. Retrying should resolve the problem. + /// + kStatusCodeNetworkError = 7, + + /// An internal error occurred. Retrying should resolve the + /// problem. + kStatusCodeInternalError = 8, /// The application is misconfigured. /// This error is not recoverable. @@ -60,39 +69,29 @@ class GoogleSignIn { /// information. kStatusCodeError = 13, - /// An internal error occurred. Retrying should resolve the - /// problem. - kStatusCodeInternalError = 8, - /// A blocking call was interrupted while waiting and did not /// run to completion. kStatusCodeInterrupted = 14, - /// The client attempted to connect to the service with an - /// invalid account name specified. - kStatusCodeInvalidAccount = 5, - - /// A network error occurred. Retrying should resolve the problem. - /// - kStatusCodeNetworkError = 7, - - /// The operation was successful. - kStatusCodeSuccess = 0, - - /// The operation was successful, but was used the device's cache. - /// - kStatusCodeSuccessCached = -1, - /// Timed out while awaiting the result. kStatusCodeTimeout = 15, + + /// The result was canceled either due to client disconnect + /// or cancel(). + kStatusCodeCanceled = 16, + + /// The client attempted to call a method from an API that + /// failed to connect. + kStatusCodeApiNotConnected = 17, }; // Defines the configuration for the sign-in process. struct Configuration { /// true to use games signin, false for default signin. + /// games signing only works on Android. bool use_game_signin; - /// web client id associated with this app. - std::string web_client_id; + /// Web client id associated with this app. + const char *web_client_id; /// true for getting an auth code when authenticating. /// Note: This may trigger re-consent on iOS. Ideally, this /// is set to true once, and the result sent to the server where the @@ -109,14 +108,15 @@ class GoogleSignIn { /// recommended for VR applications. bool hide_ui_popups; /// account name to use when authenticating, null indicates use default. - std::string account_name; + const char *account_name; /// additional scopes to request, requires consent. - std::vector additional_scopes; + const char **additional_scopes; + int additional_scope_count; - Configuration() = default; - ~Configuration() = default; Configuration(Configuration const ©) = default; Configuration(Configuration &&move) = delete; + ~Configuration() = default; + Configuration &operator=(Configuration const ©) = delete; Configuration &operator=(Configuration &&move) = delete; }; @@ -125,17 +125,22 @@ class GoogleSignIn { struct SignInResult { GoogleSignInUser *User; int StatusCode; + SignInResult() = default; - ~SignInResult() = default; SignInResult(SignInResult const ©) = default; SignInResult(SignInResult &&move) = delete; + ~SignInResult() = default; + SignInResult &operator=(SignInResult const ©) = delete; SignInResult &operator=(SignInResult &&move) = delete; }; - // Constructs a new instance. The activity parameter is needed to - // add a fragment to the activity which performs the sign-in operation. - GoogleSignIn(jobject activity); + // Constructs a new instance. +#if defined(__ANDROID__) + GoogleSignIn(jobject activity, JavaVM *vm); +#else + GoogleSignIn(); +#endif // Enables verbose logging. void EnableDebugLogging(bool flag); @@ -164,6 +169,8 @@ class GoogleSignIn { class GoogleSignInImpl; GoogleSignInImpl *impl_; }; -} // namespace googlesignin + +} // namespace signin +} // namespace google #endif // GOOGLESIGNIN_GOOGLESIGNIN_H NOLINT