forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgoogle_account_access_token_fetcher_proxy.cc
80 lines (66 loc) · 2.91 KB
/
google_account_access_token_fetcher_proxy.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// 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<std::string>& scopes,
OnTokenFetchedCallback callback) {
JNIEnv* env = base::android::AttachCurrentThread();
std::vector<std::string> 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<jlong>(new OnTokenFetchedCallback(std::move(callback)));
Java_GoogleAccountAccessTokenFetcherProxy_fetchAccessToken(
env, java_delegate_,
base::android::ToJavaArrayOfStrings(env, scopes_as_vector),
reinterpret_cast<jlong>(callback_id));
}
void GoogleAccountAccessTokenFetcherProxy::OnAccessTokenIdentifiedAsInvalid(
const std::set<std::string>& scopes,
const std::string& token) {
JNIEnv* env = base::android::AttachCurrentThread();
std::vector<std::string> 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<jobject>& proxy,
jlong profile) {
return reinterpret_cast<jlong>(new GoogleAccountAccessTokenFetcherProxy(
env, proxy, reinterpret_cast<ProfileImpl*>(profile)));
}
static void
JNI_GoogleAccountAccessTokenFetcherProxy_DeleteGoogleAccountAccessTokenFetcherProxy(
JNIEnv* env,
jlong proxy) {
delete reinterpret_cast<GoogleAccountAccessTokenFetcherProxy*>(proxy);
}
static void JNI_GoogleAccountAccessTokenFetcherProxy_RunOnTokenFetchedCallback(
JNIEnv* env,
jlong callback_id,
const base::android::JavaParamRef<jstring>& token) {
std::unique_ptr<OnTokenFetchedCallback> cb(
reinterpret_cast<OnTokenFetchedCallback*>(callback_id));
std::move(*cb).Run(base::android::ConvertJavaStringToUTF8(env, token));
}
} // namespace weblayer