[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
| 5 | #include "content/app/android/sandboxed_process_service.h" |
| 6 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame^] | 7 | #include "base/android/jni_array.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 8 | #include "base/global_descriptors_posix.h" |
| 9 | #include "base/logging.h" |
| 10 | #include "content/common/android/surface_texture_peer.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 11 | #include "content/public/app/android_library_loader_hooks.h" |
[email protected] | 1602cde | 2012-06-26 15:09:12 | [diff] [blame] | 12 | #include "content/public/common/content_descriptors.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 13 | #include "ipc/ipc_descriptors.h" |
| 14 | #include "jni/sandboxed_process_service_jni.h" |
| 15 | |
| 16 | using base::android::AttachCurrentThread; |
| 17 | using base::android::CheckException; |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame^] | 18 | using base::android::JavaIntArrayToIntVector; |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 19 | |
| 20 | namespace { |
| 21 | |
| 22 | class SurfaceTexturePeerSandboxedImpl : public content::SurfaceTexturePeer { |
| 23 | public: |
| 24 | // |service| is the instance of |
| 25 | // org.chromium.content.app.SandboxedProcessService. |
| 26 | SurfaceTexturePeerSandboxedImpl(jobject service) |
| 27 | : service_(service) { |
| 28 | } |
| 29 | |
| 30 | virtual ~SurfaceTexturePeerSandboxedImpl() { |
| 31 | } |
| 32 | |
| 33 | virtual void EstablishSurfaceTexturePeer(base::ProcessHandle pid, |
| 34 | SurfaceTextureTarget type, |
| 35 | jobject j_surface_texture, |
| 36 | int primary_id, |
| 37 | int secondary_id) { |
| 38 | JNIEnv* env = base::android::AttachCurrentThread(); |
[email protected] | 1963002 | 2012-06-25 18:58:07 | [diff] [blame] | 39 | content::Java_SandboxedProcessService_establishSurfaceTexturePeer( |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame^] | 40 | env, service_, pid, type, j_surface_texture, primary_id, secondary_id); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 41 | CheckException(env); |
| 42 | } |
| 43 | |
| 44 | private: |
| 45 | // The instance of org.chromium.content.app.SandboxedProcessService. |
| 46 | jobject service_; |
| 47 | |
| 48 | DISALLOW_COPY_AND_ASSIGN(SurfaceTexturePeerSandboxedImpl); |
| 49 | }; |
| 50 | |
| 51 | // Chrome actually uses the renderer code path for all of its sandboxed |
| 52 | // processes such as renderers, plugins, etc. |
| 53 | void InternalInitSandboxedProcess(int ipc_fd, |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame^] | 54 | const std::vector<int>& extra_file_ids, |
| 55 | const std::vector<int>& extra_file_fds, |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 56 | JNIEnv* env, |
| 57 | jclass clazz, |
| 58 | jobject context, |
| 59 | jobject service) { |
| 60 | // Set up the IPC file descriptor mapping. |
| 61 | base::GlobalDescriptors::GetInstance()->Set(kPrimaryIPCChannel, ipc_fd); |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame^] | 62 | // Register the extra file descriptors. |
| 63 | // This usually include the crash dump signals and resource related files. |
| 64 | DCHECK(extra_file_fds.size() == extra_file_ids.size()); |
| 65 | for (size_t i = 0; i < extra_file_ids.size(); ++i) { |
| 66 | base::GlobalDescriptors::GetInstance()->Set(extra_file_ids[i], |
| 67 | extra_file_fds[i]); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 68 | } |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame^] | 69 | |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 70 | content::SurfaceTexturePeer::InitInstance( |
| 71 | new SurfaceTexturePeerSandboxedImpl(service)); |
| 72 | |
| 73 | } |
| 74 | |
| 75 | } // namespace <anonymous> |
| 76 | |
[email protected] | 1963002 | 2012-06-25 18:58:07 | [diff] [blame] | 77 | namespace content { |
| 78 | |
| 79 | void InitSandboxedProcess(JNIEnv* env, |
| 80 | jclass clazz, |
| 81 | jobject context, |
| 82 | jobject service, |
| 83 | jint ipc_fd, |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame^] | 84 | jintArray j_extra_file_ids, |
| 85 | jintArray j_extra_file_fds) { |
| 86 | std::vector<int> extra_file_ids; |
| 87 | std::vector<int> extra_file_fds; |
| 88 | JavaIntArrayToIntVector(env, j_extra_file_ids, &extra_file_ids); |
| 89 | JavaIntArrayToIntVector(env, j_extra_file_fds, &extra_file_fds); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 90 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame^] | 91 | InternalInitSandboxedProcess(static_cast<int>(ipc_fd), |
| 92 | extra_file_ids, extra_file_fds, |
| 93 | env, clazz, context, service); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 94 | } |
| 95 | |
[email protected] | 1963002 | 2012-06-25 18:58:07 | [diff] [blame] | 96 | void ExitSandboxedProcess(JNIEnv* env, jclass clazz) { |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 97 | LOG(INFO) << "SandboxedProcessService: Exiting sandboxed process."; |
[email protected] | 1963002 | 2012-06-25 18:58:07 | [diff] [blame] | 98 | LibraryLoaderExitHook(); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 99 | _exit(0); |
| 100 | } |
| 101 | |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 102 | bool RegisterSandboxedProcessService(JNIEnv* env) { |
| 103 | return RegisterNativesImpl(env); |
| 104 | } |
| 105 | |
| 106 | } // namespace content |