-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathfunctions_android.cc
More file actions
322 lines (287 loc) · 12.5 KB
/
functions_android.cc
File metadata and controls
322 lines (287 loc) · 12.5 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright 2017 Google LLC
//
// 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
//
// 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 "functions/src/android/functions_android.h"
#include <jni.h>
#include "app/src/include/firebase/app.h"
#include "app/src/util.h"
#include "app/src/util_android.h"
#include "functions/src/android/callable_reference_android.h"
namespace firebase {
namespace functions {
namespace internal {
// clang-format off
#define FIREBASE_FUNCTIONS_METHODS(X) \
X(GetInstance, "getInstance", \
"(Lcom/google/firebase/FirebaseApp;Ljava/lang/String;)" \
"Lcom/google/firebase/functions/FirebaseFunctions;", \
util::kMethodTypeStatic), \
X(GetHttpsCallable, "getHttpsCallable", \
"(Ljava/lang/String;)" \
"Lcom/google/firebase/functions/HttpsCallableReference;", \
util::kMethodTypeInstance), \
X(GetHttpsCallableWithOptions, "getHttpsCallable", \
"(Ljava/lang/String;Lcom/google/firebase/functions/HttpsCallableOptions;)" \
"Lcom/google/firebase/functions/HttpsCallableReference;", \
util::kMethodTypeInstance), \
X(GetHttpsCallableFromURL, "getHttpsCallableFromUrl", \
"(Ljava/net/URL;)" \
"Lcom/google/firebase/functions/HttpsCallableReference;", \
util::kMethodTypeInstance), \
X(GetHttpsCallableFromURLWithOptions, "getHttpsCallableFromUrl", \
"(Ljava/net/URL;Lcom/google/firebase/functions/HttpsCallableOptions;)" \
"Lcom/google/firebase/functions/HttpsCallableReference;", \
util::kMethodTypeInstance), \
X(UseFunctionsEmulator, "useFunctionsEmulator", \
"(Ljava/lang/String;)V", \
util::kMethodTypeInstance)
// clang-format on
METHOD_LOOKUP_DECLARATION(firebase_functions, FIREBASE_FUNCTIONS_METHODS)
METHOD_LOOKUP_DEFINITION(firebase_functions,
PROGUARD_KEEP_CLASS
"com/google/firebase/functions/FirebaseFunctions",
FIREBASE_FUNCTIONS_METHODS)
// clang-format off
#define CALLABLE_OPTIONS_METHODS(X) \
X(BuilderConstructor, "<init>", "()V"), \
X(SetLimitedUseAppCheckTokens, "setLimitedUseAppCheckTokens", \
"(Z)Lcom/google/firebase/functions/HttpsCallableOptions$Builder;"), \
X(Build, "build", "()Lcom/google/firebase/functions/HttpsCallableOptions;")
// clang-format on
METHOD_LOOKUP_DECLARATION(callable_options_builder, CALLABLE_OPTIONS_METHODS)
METHOD_LOOKUP_DEFINITION(
callable_options_builder,
PROGUARD_KEEP_CLASS
"com/google/firebase/functions/HttpsCallableOptions$Builder",
CALLABLE_OPTIONS_METHODS)
// clang-format off
#define FUNCTIONS_EXCEPTION_METHODS(X) \
X(GetMessage, "getMessage", "()Ljava/lang/String;"), \
X(GetCode, "getCode", \
"()Lcom/google/firebase/functions/FirebaseFunctionsException$Code;")
// clang-format on
METHOD_LOOKUP_DECLARATION(functions_exception, FUNCTIONS_EXCEPTION_METHODS)
METHOD_LOOKUP_DEFINITION(
functions_exception,
PROGUARD_KEEP_CLASS
"com/google/firebase/functions/FirebaseFunctionsException",
FUNCTIONS_EXCEPTION_METHODS)
// clang-format off
#define FUNCTIONS_EXCEPTION_CODE_METHODS(X) \
X(Ordinal, "ordinal", "()I")
// clang-format on
METHOD_LOOKUP_DECLARATION(functions_exception_code,
FUNCTIONS_EXCEPTION_CODE_METHODS)
METHOD_LOOKUP_DEFINITION(
functions_exception_code,
PROGUARD_KEEP_CLASS
"com/google/firebase/functions/FirebaseFunctionsException$Code",
FUNCTIONS_EXCEPTION_CODE_METHODS)
Mutex FunctionsInternal::init_mutex_; // NOLINT
int FunctionsInternal::initialize_count_ = 0;
FunctionsInternal::FunctionsInternal(App* app, const char* region)
: region_(region) {
app_ = nullptr;
if (!Initialize(app)) return;
app_ = app;
const char kApiIdentifier[] = "Functions";
jni_task_id_ = CreateApiIdentifier(kApiIdentifier, this);
JNIEnv* env = app_->GetJNIEnv();
jstring region_str = env->NewStringUTF(region);
jobject platform_app = app_->GetPlatformApp();
jobject functions_obj = env->CallStaticObjectMethod(
firebase_functions::GetClass(),
firebase_functions::GetMethodId(firebase_functions::kGetInstance),
platform_app, region_str);
util::CheckAndClearJniExceptions(env);
env->DeleteLocalRef(platform_app);
env->DeleteLocalRef(region_str);
assert(functions_obj != nullptr);
obj_ = env->NewGlobalRef(functions_obj);
env->DeleteLocalRef(functions_obj);
}
FunctionsInternal::~FunctionsInternal() {
// If initialization failed, there is nothing to clean up.
if (app_ == nullptr) return;
JNIEnv* env = app_->GetJNIEnv();
util::CancelCallbacks(env, jni_task_id_.c_str());
env->DeleteGlobalRef(obj_);
obj_ = nullptr;
Terminate(app_);
app_ = nullptr;
util::CheckAndClearJniExceptions(env);
}
bool FunctionsInternal::Initialize(App* app) {
MutexLock init_lock(init_mutex_);
if (initialize_count_ == 0) {
JNIEnv* env = app->GetJNIEnv();
jobject activity = app->activity();
if (!(firebase_functions::CacheMethodIds(env, activity) &&
functions_exception::CacheMethodIds(env, activity) &&
functions_exception_code::CacheMethodIds(env, activity) &&
functions_exception_code::CacheFieldIds(env, activity) &&
callable_options_builder::CacheMethodIds(env, activity) &&
// Call Initialize on all other Functions internal classes.
HttpsCallableReferenceInternal::Initialize(app))) {
return false;
}
util::CheckAndClearJniExceptions(env);
}
initialize_count_++;
return true;
}
void FunctionsInternal::Terminate(App* app) {
MutexLock init_lock(init_mutex_);
assert(initialize_count_ > 0);
initialize_count_--;
if (initialize_count_ == 0) {
JNIEnv* env = app->GetJNIEnv();
firebase_functions::ReleaseClass(env);
functions_exception::ReleaseClass(env);
functions_exception_code::ReleaseClass(env);
callable_options_builder::ReleaseClass(env);
// Call Terminate on all other Functions internal classes.
HttpsCallableReferenceInternal::Terminate(app);
util::CheckAndClearJniExceptions(env);
}
}
App* FunctionsInternal::app() const { return app_; }
const char* FunctionsInternal::region() const { return region_.c_str(); }
Error FunctionsInternal::ErrorFromJavaFunctionsException(
jobject java_exception, std::string* error_message) const {
JNIEnv* env = app_->GetJNIEnv();
Error error = kErrorNone;
if (java_exception != nullptr) {
// Guarantee that it is a functions exception before getting the code.
if (env->IsInstanceOf(java_exception, functions_exception::GetClass())) {
jobject java_code = env->CallObjectMethod(
java_exception,
functions_exception::GetMethodId(functions_exception::kGetCode));
if (java_code) {
jint code = env->CallIntMethod(java_code,
functions_exception_code::GetMethodId(
functions_exception_code::kOrdinal));
env->DeleteLocalRef(java_code);
error = static_cast<Error>(code);
}
} else {
// The exception wasn't a Functions exception, so tag it as unknown.
error = kErrorUnknown;
}
if (error_message != nullptr) {
*error_message = util::GetMessageFromException(env, java_exception);
}
util::CheckAndClearJniExceptions(env);
}
return error;
}
HttpsCallableReferenceInternal* FunctionsInternal::GetHttpsCallable(
const char* name) const {
return GetHttpsCallable(name, HttpsCallableOptions());
}
HttpsCallableReferenceInternal* FunctionsInternal::GetHttpsCallable(
const char* name, const HttpsCallableOptions& options) const {
FIREBASE_ASSERT_RETURN(nullptr, name != nullptr);
JNIEnv* env = app_->GetJNIEnv();
// Create HttpsCallableOptions
jobject builder =
env->NewObject(callable_options_builder::GetClass(),
callable_options_builder::GetMethodId(
callable_options_builder::kBuilderConstructor));
jobject builder2 = env->CallObjectMethod(
builder,
callable_options_builder::GetMethodId(
callable_options_builder::kSetLimitedUseAppCheckTokens),
options.limited_use_app_check_token);
env->DeleteLocalRef(builder);
builder = builder2;
jobject java_options = env->CallObjectMethod(
builder,
callable_options_builder::GetMethodId(callable_options_builder::kBuild));
env->DeleteLocalRef(builder);
jobject name_string = env->NewStringUTF(name);
jobject callable_reference_obj = env->CallObjectMethod(
obj_,
firebase_functions::GetMethodId(
firebase_functions::kGetHttpsCallableWithOptions),
name_string, java_options);
env->DeleteLocalRef(name_string);
env->DeleteLocalRef(java_options);
if (util::LogException(env, kLogLevelError,
"Functions::GetHttpsCallable() (name = %s) failed",
name)) {
return nullptr;
}
HttpsCallableReferenceInternal* internal = new HttpsCallableReferenceInternal(
const_cast<FunctionsInternal*>(this), callable_reference_obj);
env->DeleteLocalRef(callable_reference_obj);
util::CheckAndClearJniExceptions(env);
return internal;
}
HttpsCallableReferenceInternal* FunctionsInternal::GetHttpsCallableFromURL(
const char* url) const {
return GetHttpsCallableFromURL(url, HttpsCallableOptions());
}
HttpsCallableReferenceInternal* FunctionsInternal::GetHttpsCallableFromURL(
const char* url, const HttpsCallableOptions& options) const {
FIREBASE_ASSERT_RETURN(nullptr, url != nullptr);
JNIEnv* env = app_->GetJNIEnv();
// Create HttpsCallableOptions
jobject builder =
env->NewObject(callable_options_builder::GetClass(),
callable_options_builder::GetMethodId(
callable_options_builder::kBuilderConstructor));
jobject builder2 = env->CallObjectMethod(
builder,
callable_options_builder::GetMethodId(
callable_options_builder::kSetLimitedUseAppCheckTokens),
options.limited_use_app_check_token);
env->DeleteLocalRef(builder);
builder = builder2;
jobject java_options = env->CallObjectMethod(
builder,
callable_options_builder::GetMethodId(callable_options_builder::kBuild));
env->DeleteLocalRef(builder);
jobject url_object = util::CharsToURL(env, url);
jobject callable_reference_obj = env->CallObjectMethod(
obj_,
firebase_functions::GetMethodId(
firebase_functions::kGetHttpsCallableFromURLWithOptions),
url_object, java_options);
env->DeleteLocalRef(url_object);
env->DeleteLocalRef(java_options);
if (util::LogException(
env, kLogLevelError,
"Functions::GetHttpsCallableFromURL() (url = %s) failed", url)) {
return nullptr;
}
HttpsCallableReferenceInternal* internal = new HttpsCallableReferenceInternal(
const_cast<FunctionsInternal*>(this), callable_reference_obj);
env->DeleteLocalRef(callable_reference_obj);
util::CheckAndClearJniExceptions(env);
return internal;
}
void FunctionsInternal::UseFunctionsEmulator(const char* origin) {
FIREBASE_ASSERT(origin != nullptr);
JNIEnv* env = app_->GetJNIEnv();
jobject origin_string = env->NewStringUTF(origin);
env->CallVoidMethod(obj_,
firebase_functions::GetMethodId(
firebase_functions::kUseFunctionsEmulator),
origin_string);
util::CheckAndClearJniExceptions(env);
env->DeleteLocalRef(origin_string);
}
} // namespace internal
} // namespace functions
} // namespace firebase