// Copyright 2021 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "weblayer/browser/google_account_access_token_fetcher_proxy.h" #include "base/android/jni_array.h" #include "base/android/jni_string.h" #include "weblayer/browser/java/jni/GoogleAccountAccessTokenFetcherProxy_jni.h" #include "weblayer/browser/profile_impl.h" namespace weblayer { GoogleAccountAccessTokenFetcherProxy::GoogleAccountAccessTokenFetcherProxy( JNIEnv* env, jobject obj, Profile* profile) : java_delegate_(env, obj), profile_(profile) { profile_->SetGoogleAccountAccessTokenFetchDelegate(this); } GoogleAccountAccessTokenFetcherProxy::~GoogleAccountAccessTokenFetcherProxy() { profile_->SetGoogleAccountAccessTokenFetchDelegate(nullptr); } void GoogleAccountAccessTokenFetcherProxy::FetchAccessToken( const std::set& scopes, OnTokenFetchedCallback callback) { JNIEnv* env = base::android::AttachCurrentThread(); std::vector scopes_as_vector(scopes.begin(), scopes.end()); // Copy |callback| on the heap to pass the pointer through JNI. This callback // will be deleted when it's run. jlong callback_id = reinterpret_cast(new OnTokenFetchedCallback(std::move(callback))); Java_GoogleAccountAccessTokenFetcherProxy_fetchAccessToken( env, java_delegate_, base::android::ToJavaArrayOfStrings(env, scopes_as_vector), reinterpret_cast(callback_id)); } void GoogleAccountAccessTokenFetcherProxy::OnAccessTokenIdentifiedAsInvalid( const std::set& scopes, const std::string& token) { JNIEnv* env = base::android::AttachCurrentThread(); std::vector scopes_as_vector(scopes.begin(), scopes.end()); Java_GoogleAccountAccessTokenFetcherProxy_onAccessTokenIdentifiedAsInvalid( env, java_delegate_, base::android::ToJavaArrayOfStrings(env, scopes_as_vector), base::android::ConvertUTF8ToJavaString(env, token)); } static jlong JNI_GoogleAccountAccessTokenFetcherProxy_CreateGoogleAccountAccessTokenFetcherProxy( JNIEnv* env, const base::android::JavaParamRef& proxy, jlong profile) { return reinterpret_cast(new GoogleAccountAccessTokenFetcherProxy( env, proxy, reinterpret_cast(profile))); } static void JNI_GoogleAccountAccessTokenFetcherProxy_DeleteGoogleAccountAccessTokenFetcherProxy( JNIEnv* env, jlong proxy) { delete reinterpret_cast(proxy); } static void JNI_GoogleAccountAccessTokenFetcherProxy_RunOnTokenFetchedCallback( JNIEnv* env, jlong callback_id, const base::android::JavaParamRef& token) { std::unique_ptr cb( reinterpret_cast(callback_id)); std::move(*cb).Run(base::android::ConvertJavaStringToUTF8(env, token)); } } // namespace weblayer