google_account_access_token_fetcher_proxy.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "weblayer/browser/google_account_access_token_fetcher_proxy.h"
  5. #include "base/android/jni_array.h"
  6. #include "base/android/jni_string.h"
  7. #include "weblayer/browser/java/jni/GoogleAccountAccessTokenFetcherProxy_jni.h"
  8. #include "weblayer/browser/profile_impl.h"
  9. namespace weblayer {
  10. GoogleAccountAccessTokenFetcherProxy::GoogleAccountAccessTokenFetcherProxy(
  11. JNIEnv* env,
  12. jobject obj,
  13. Profile* profile)
  14. : java_delegate_(env, obj), profile_(profile) {
  15. profile_->SetGoogleAccountAccessTokenFetchDelegate(this);
  16. }
  17. GoogleAccountAccessTokenFetcherProxy::~GoogleAccountAccessTokenFetcherProxy() {
  18. profile_->SetGoogleAccountAccessTokenFetchDelegate(nullptr);
  19. }
  20. void GoogleAccountAccessTokenFetcherProxy::FetchAccessToken(
  21. const std::set<std::string>& scopes,
  22. OnTokenFetchedCallback callback) {
  23. JNIEnv* env = base::android::AttachCurrentThread();
  24. std::vector<std::string> scopes_as_vector(scopes.begin(), scopes.end());
  25. // Copy |callback| on the heap to pass the pointer through JNI. This callback
  26. // will be deleted when it's run.
  27. jlong callback_id =
  28. reinterpret_cast<jlong>(new OnTokenFetchedCallback(std::move(callback)));
  29. Java_GoogleAccountAccessTokenFetcherProxy_fetchAccessToken(
  30. env, java_delegate_,
  31. base::android::ToJavaArrayOfStrings(env, scopes_as_vector),
  32. reinterpret_cast<jlong>(callback_id));
  33. }
  34. void GoogleAccountAccessTokenFetcherProxy::OnAccessTokenIdentifiedAsInvalid(
  35. const std::set<std::string>& scopes,
  36. const std::string& token) {
  37. JNIEnv* env = base::android::AttachCurrentThread();
  38. std::vector<std::string> scopes_as_vector(scopes.begin(), scopes.end());
  39. Java_GoogleAccountAccessTokenFetcherProxy_onAccessTokenIdentifiedAsInvalid(
  40. env, java_delegate_,
  41. base::android::ToJavaArrayOfStrings(env, scopes_as_vector),
  42. base::android::ConvertUTF8ToJavaString(env, token));
  43. }
  44. static jlong
  45. JNI_GoogleAccountAccessTokenFetcherProxy_CreateGoogleAccountAccessTokenFetcherProxy(
  46. JNIEnv* env,
  47. const base::android::JavaParamRef<jobject>& proxy,
  48. jlong profile) {
  49. return reinterpret_cast<jlong>(new GoogleAccountAccessTokenFetcherProxy(
  50. env, proxy, reinterpret_cast<ProfileImpl*>(profile)));
  51. }
  52. static void
  53. JNI_GoogleAccountAccessTokenFetcherProxy_DeleteGoogleAccountAccessTokenFetcherProxy(
  54. JNIEnv* env,
  55. jlong proxy) {
  56. delete reinterpret_cast<GoogleAccountAccessTokenFetcherProxy*>(proxy);
  57. }
  58. static void JNI_GoogleAccountAccessTokenFetcherProxy_RunOnTokenFetchedCallback(
  59. JNIEnv* env,
  60. jlong callback_id,
  61. const base::android::JavaParamRef<jstring>& token) {
  62. std::unique_ptr<OnTokenFetchedCallback> cb(
  63. reinterpret_cast<OnTokenFetchedCallback*>(callback_id));
  64. std::move(*cb).Run(base::android::ConvertJavaStringToUTF8(env, token));
  65. }
  66. } // namespace weblayer