blob: 151ae1c1805daeb936f367349a5057e0604bbbea [file] [log] [blame]
Jay Civelliddcb2492017-06-23 00:30:411// Copyright 2017 The Chromium Authors. All rights reserved.
[email protected]5b6f0692012-06-12 05:00:052// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]30935362012-06-28 21:26:235#include "base/android/jni_array.h"
jcivellidad0cef2017-02-16 18:38:596#include "base/android/jni_string.h"
[email protected]93c9f9b2014-02-10 16:19:227#include "base/android/library_loader/library_loader_hooks.h"
jcivellidad0cef2017-02-16 18:38:598#include "base/file_descriptor_store.h"
[email protected]5b6f0692012-06-12 05:00:059#include "base/logging.h"
avi66a07722015-12-25 23:38:1210#include "base/macros.h"
Jay Civellia15e0a92017-06-20 22:49:1611#include "base/optional.h"
[email protected]613eef62012-11-09 23:46:5412#include "base/posix/global_descriptors.h"
hanxi3f9d47482016-06-25 00:28:1913#include "jni/ChildProcessServiceImpl_jni.h"
[email protected]5b6f0692012-06-12 05:00:0514
[email protected]30935362012-06-28 21:26:2315using base::android::JavaIntArrayToIntVector;
torne86560112016-08-04 15:59:0416using base::android::JavaParamRef;
[email protected]5b6f0692012-06-12 05:00:0517
Jay Civelliddcb2492017-06-23 00:30:4118namespace base {
19namespace android {
[email protected]ccf558a2013-03-07 03:58:1820
Daniel Bratell7aacf952017-11-21 17:51:2521void JNI_ChildProcessServiceImpl_RegisterFileDescriptors(
22 JNIEnv* env,
23 const JavaParamRef<jclass>& clazz,
24 const JavaParamRef<jobjectArray>& j_keys,
25 const JavaParamRef<jintArray>& j_ids,
26 const JavaParamRef<jintArray>& j_fds,
27 const JavaParamRef<jlongArray>& j_offsets,
28 const JavaParamRef<jlongArray>& j_sizes) {
Jay Civellia15e0a92017-06-20 22:49:1629 std::vector<base::Optional<std::string>> keys;
30 jsize keys_size = env->GetArrayLength(j_keys);
31 keys.reserve(keys_size);
32 for (jsize i = 0; i < keys_size; i++) {
33 base::android::ScopedJavaLocalRef<jstring> str(
34 env, static_cast<jstring>(env->GetObjectArrayElement(j_keys, i)));
35 base::Optional<std::string> key;
36 if (!str.is_null()) {
37 key = base::android::ConvertJavaStringToUTF8(env, str);
38 }
39 keys.push_back(std::move(key));
40 }
41
jcivellidad0cef2017-02-16 18:38:5942 std::vector<int> ids;
43 base::android::JavaIntArrayToIntVector(env, j_ids, &ids);
44 std::vector<int> fds;
45 base::android::JavaIntArrayToIntVector(env, j_fds, &fds);
46 std::vector<int64_t> offsets;
47 base::android::JavaLongArrayToInt64Vector(env, j_offsets, &offsets);
48 std::vector<int64_t> sizes;
49 base::android::JavaLongArrayToInt64Vector(env, j_sizes, &sizes);
50
Jay Civellia15e0a92017-06-20 22:49:1651 DCHECK_EQ(keys.size(), ids.size());
jcivellidad0cef2017-02-16 18:38:5952 DCHECK_EQ(ids.size(), fds.size());
53 DCHECK_EQ(fds.size(), offsets.size());
54 DCHECK_EQ(offsets.size(), sizes.size());
55
jcivellidad0cef2017-02-16 18:38:5956 for (size_t i = 0; i < ids.size(); i++) {
57 base::MemoryMappedFile::Region region = {offsets.at(i), sizes.at(i)};
Jay Civellia15e0a92017-06-20 22:49:1658 const base::Optional<std::string>& key = keys.at(i);
jcivellidad0cef2017-02-16 18:38:5959 int id = ids.at(i);
60 int fd = fds.at(i);
Jay Civellia15e0a92017-06-20 22:49:1661 if (key) {
62 base::FileDescriptorStore::GetInstance().Set(*key, base::ScopedFD(fd),
63 region);
jcivellidad0cef2017-02-16 18:38:5964 } else {
65 base::GlobalDescriptors::GetInstance()->Set(id, fd, region);
66 }
67 }
agrieve0f9183132015-06-03 13:41:5368}
69
Daniel Bratell7aacf952017-11-21 17:51:2570void JNI_ChildProcessServiceImpl_ExitChildProcess(
71 JNIEnv* env,
72 const JavaParamRef<jclass>& clazz) {
hanxi3f9d47482016-06-25 00:28:1973 VLOG(0) << "ChildProcessServiceImpl: Exiting child process.";
[email protected]93c9f9b2014-02-10 16:19:2274 base::android::LibraryLoaderExitHook();
[email protected]5b6f0692012-06-12 05:00:0575 _exit(0);
76}
77
Jay Civelliddcb2492017-06-23 00:30:4178} // namespace android
79} // namespace base