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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
content / browser / android / java / gin_java_method_invocation_helper_unittest.cc [blame]
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/browser/android/java/gin_java_method_invocation_helper.h"
#include <stddef.h>
#include <memory>
#include <utility>
#include "base/android/jni_android.h"
#include "base/values.h"
#include "content/common/android/gin_java_bridge_value.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
class NullObjectDelegate
: public GinJavaMethodInvocationHelper::ObjectDelegate {
public:
NullObjectDelegate() = default;
NullObjectDelegate(const NullObjectDelegate&) = delete;
NullObjectDelegate& operator=(const NullObjectDelegate&) = delete;
~NullObjectDelegate() override = default;
base::android::ScopedJavaLocalRef<jobject> GetLocalRef(JNIEnv* env) override {
return base::android::ScopedJavaLocalRef<jobject>();
}
base::android::ScopedJavaLocalRef<jclass> GetLocalClassRef(
JNIEnv* env) override {
return base::android::ScopedJavaLocalRef<jclass>();
}
const JavaMethod* FindMethod(const std::string& method_name,
size_t num_parameters) override {
return nullptr;
}
bool IsObjectGetClassMethod(const JavaMethod* method) override {
return false;
}
const base::android::JavaRef<jclass>& GetSafeAnnotationClass() override {
return safe_annotation_class_;
}
private:
base::android::ScopedJavaGlobalRef<jclass> safe_annotation_class_;
};
class NullDispatcherDelegate
: public GinJavaMethodInvocationHelper::DispatcherDelegate {
public:
NullDispatcherDelegate() = default;
NullDispatcherDelegate(const NullDispatcherDelegate&) = delete;
NullDispatcherDelegate& operator=(const NullDispatcherDelegate&) = delete;
~NullDispatcherDelegate() override = default;
JavaObjectWeakGlobalRef GetObjectWeakRef(
GinJavaBoundObject::ObjectID object_id) override {
return JavaObjectWeakGlobalRef();
}
};
} // namespace
class GinJavaMethodInvocationHelperTest : public testing::Test {
};
namespace {
class CountingDispatcherDelegate
: public GinJavaMethodInvocationHelper::DispatcherDelegate {
public:
CountingDispatcherDelegate() = default;
CountingDispatcherDelegate(const CountingDispatcherDelegate&) = delete;
CountingDispatcherDelegate& operator=(const CountingDispatcherDelegate&) =
delete;
~CountingDispatcherDelegate() override = default;
JavaObjectWeakGlobalRef GetObjectWeakRef(
GinJavaBoundObject::ObjectID object_id) override {
counters_[object_id]++;
return JavaObjectWeakGlobalRef();
}
void AssertInvocationsCount(GinJavaBoundObject::ObjectID begin_object_id,
GinJavaBoundObject::ObjectID end_object_id) {
EXPECT_EQ(end_object_id - begin_object_id,
static_cast<int>(counters_.size()));
for (GinJavaBoundObject::ObjectID i = begin_object_id;
i < end_object_id; ++i) {
EXPECT_LT(0, counters_[i]) << "ObjectID: " << i;
}
}
private:
typedef std::map<GinJavaBoundObject::ObjectID, int> Counters;
Counters counters_;
};
} // namespace
TEST_F(GinJavaMethodInvocationHelperTest, RetrievalOfObjectsNoObjects) {
base::Value::List no_objects;
for (int i = 0; i < 10; ++i) {
no_objects.Append(i);
}
auto helper = base::MakeRefCounted<GinJavaMethodInvocationHelper>(
std::make_unique<NullObjectDelegate>(), "foo", no_objects);
CountingDispatcherDelegate counter;
helper->Init(&counter);
counter.AssertInvocationsCount(0, 0);
}
TEST_F(GinJavaMethodInvocationHelperTest, RetrievalOfObjectsHaveObjects) {
base::Value::List objects;
objects.Append(100);
objects.Append(base::Value::FromUniquePtrValue(
GinJavaBridgeValue::CreateObjectIDValue(1)));
base::Value::List sub_list;
sub_list.Append(200);
sub_list.Append(base::Value::FromUniquePtrValue(
GinJavaBridgeValue::CreateObjectIDValue(2)));
objects.Append(std::move(sub_list));
base::Value::Dict sub_dict;
sub_dict.Set("1", 300);
sub_dict.Set("2", base::Value::FromUniquePtrValue(
GinJavaBridgeValue::CreateObjectIDValue(3)));
objects.Append(std::move(sub_dict));
base::Value::List sub_list_with_dict;
base::Value::Dict sub_sub_dict;
sub_sub_dict.Set("1", base::Value::FromUniquePtrValue(
GinJavaBridgeValue::CreateObjectIDValue(4)));
sub_list_with_dict.Append(std::move(sub_sub_dict));
objects.Append(std::move(sub_list_with_dict));
base::Value::Dict sub_dict_with_list;
base::Value::List sub_sub_list;
sub_sub_list.Append(base::Value::FromUniquePtrValue(
GinJavaBridgeValue::CreateObjectIDValue(5)));
sub_dict_with_list.Set("1", std::move(sub_sub_list));
objects.Append(std::move(sub_dict_with_list));
auto helper = base::MakeRefCounted<GinJavaMethodInvocationHelper>(
std::make_unique<NullObjectDelegate>(), "foo", objects);
CountingDispatcherDelegate counter;
helper->Init(&counter);
counter.AssertInvocationsCount(1, 6);
}
namespace {
class ObjectIsGoneObjectDelegate : public NullObjectDelegate {
public:
ObjectIsGoneObjectDelegate() :
get_local_ref_called_(false) {
// We need a Java Method object to create a valid JavaMethod instance.
JNIEnv* env = base::android::AttachCurrentThread();
base::android::ScopedJavaLocalRef<jclass> clazz(
base::android::GetClass(env, "java/lang/Object"));
jmethodID method_id =
base::android::MethodID::Get<base::android::MethodID::TYPE_INSTANCE>(
env, clazz.obj(), "hashCode", "()I");
EXPECT_TRUE(method_id);
base::android::ScopedJavaLocalRef<jobject> method_obj(
env, env->ToReflectedMethod(clazz.obj(), method_id, false));
EXPECT_TRUE(method_obj.obj());
method_ = std::make_unique<JavaMethod>(method_obj);
}
ObjectIsGoneObjectDelegate(const ObjectIsGoneObjectDelegate&) = delete;
ObjectIsGoneObjectDelegate& operator=(const ObjectIsGoneObjectDelegate&) =
delete;
~ObjectIsGoneObjectDelegate() override = default;
base::android::ScopedJavaLocalRef<jobject> GetLocalRef(JNIEnv* env) override {
get_local_ref_called_ = true;
return NullObjectDelegate::GetLocalRef(env);
}
const JavaMethod* FindMethod(const std::string& method_name,
size_t num_parameters) override {
return method_.get();
}
bool get_local_ref_called() { return get_local_ref_called_; }
const std::string& get_method_name() { return method_->name(); }
protected:
std::unique_ptr<JavaMethod> method_;
bool get_local_ref_called_;
};
} // namespace
TEST_F(GinJavaMethodInvocationHelperTest, HandleObjectIsGone) {
base::Value::List no_objects;
auto object_delegate_unique = std::make_unique<ObjectIsGoneObjectDelegate>();
ObjectIsGoneObjectDelegate* object_delegate = object_delegate_unique.get();
auto helper = base::MakeRefCounted<GinJavaMethodInvocationHelper>(
std::move(object_delegate_unique), object_delegate->get_method_name(),
no_objects);
NullDispatcherDelegate dispatcher;
helper->Init(&dispatcher);
EXPECT_FALSE(object_delegate->get_local_ref_called());
EXPECT_EQ(mojom::GinJavaBridgeError::kGinJavaBridgeNoError,
helper->GetInvocationError());
helper->Invoke();
EXPECT_TRUE(object_delegate->get_local_ref_called());
EXPECT_TRUE(helper->HoldsPrimitiveResult());
EXPECT_TRUE(helper->GetPrimitiveResult().empty());
EXPECT_EQ(mojom::GinJavaBridgeError::kGinJavaBridgeObjectIsGone,
helper->GetInvocationError());
}
namespace {
class MethodNotFoundObjectDelegate : public NullObjectDelegate {
public:
MethodNotFoundObjectDelegate() : find_method_called_(false) {}
MethodNotFoundObjectDelegate(const MethodNotFoundObjectDelegate&) = delete;
MethodNotFoundObjectDelegate& operator=(const MethodNotFoundObjectDelegate&) =
delete;
~MethodNotFoundObjectDelegate() override = default;
base::android::ScopedJavaLocalRef<jobject> GetLocalRef(JNIEnv* env) override {
return base::android::ScopedJavaLocalRef<jobject>(
env, static_cast<jobject>(env->FindClass("java/lang/String")));
}
const JavaMethod* FindMethod(const std::string& method_name,
size_t num_parameters) override {
find_method_called_ = true;
return nullptr;
}
bool find_method_called() const { return find_method_called_; }
protected:
bool find_method_called_;
};
} // namespace
TEST_F(GinJavaMethodInvocationHelperTest, HandleMethodNotFound) {
base::Value::List no_objects;
auto object_delegate_unique =
std::make_unique<MethodNotFoundObjectDelegate>();
MethodNotFoundObjectDelegate* object_delegate = object_delegate_unique.get();
auto helper = base::MakeRefCounted<GinJavaMethodInvocationHelper>(
std::move(object_delegate_unique), "foo", no_objects);
NullDispatcherDelegate dispatcher;
helper->Init(&dispatcher);
EXPECT_FALSE(object_delegate->find_method_called());
EXPECT_EQ(mojom::GinJavaBridgeError::kGinJavaBridgeNoError,
helper->GetInvocationError());
helper->Invoke();
EXPECT_TRUE(object_delegate->find_method_called());
EXPECT_TRUE(helper->HoldsPrimitiveResult());
EXPECT_TRUE(helper->GetPrimitiveResult().empty());
EXPECT_EQ(mojom::GinJavaBridgeError::kGinJavaBridgeMethodNotFound,
helper->GetInvocationError());
}
namespace {
class GetClassObjectDelegate : public MethodNotFoundObjectDelegate {
public:
GetClassObjectDelegate() : get_class_called_(false) {}
GetClassObjectDelegate(const GetClassObjectDelegate&) = delete;
GetClassObjectDelegate& operator=(const GetClassObjectDelegate&) = delete;
~GetClassObjectDelegate() override = default;
const JavaMethod* FindMethod(const std::string& method_name,
size_t num_parameters) override {
find_method_called_ = true;
return kFakeGetClass;
}
bool IsObjectGetClassMethod(const JavaMethod* method) override {
get_class_called_ = true;
return kFakeGetClass == method;
}
bool get_class_called() const { return get_class_called_; }
private:
static const JavaMethod* kFakeGetClass;
bool get_class_called_;
};
// We don't expect GinJavaMethodInvocationHelper to actually invoke the
// method, since the point of the test is to verify whether calls to
// 'getClass' get blocked.
const JavaMethod* GetClassObjectDelegate::kFakeGetClass =
(JavaMethod*)0xdeadbeef;
} // namespace
TEST_F(GinJavaMethodInvocationHelperTest, HandleGetClassInvocation) {
base::Value::List no_objects;
auto object_delegate_unique = std::make_unique<GetClassObjectDelegate>();
GetClassObjectDelegate* object_delegate = object_delegate_unique.get();
auto helper = base::MakeRefCounted<GinJavaMethodInvocationHelper>(
std::move(object_delegate_unique), "foo", no_objects);
NullDispatcherDelegate dispatcher;
helper->Init(&dispatcher);
EXPECT_FALSE(object_delegate->find_method_called());
EXPECT_FALSE(object_delegate->get_class_called());
EXPECT_EQ(mojom::GinJavaBridgeError::kGinJavaBridgeNoError,
helper->GetInvocationError());
helper->Invoke();
EXPECT_TRUE(object_delegate->find_method_called());
EXPECT_TRUE(object_delegate->get_class_called());
EXPECT_TRUE(helper->HoldsPrimitiveResult());
EXPECT_TRUE(helper->GetPrimitiveResult().empty());
EXPECT_EQ(
mojom::GinJavaBridgeError::kGinJavaBridgeAccessToObjectGetClassIsBlocked,
helper->GetInvocationError());
}
} // namespace content