[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] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 7 | #include <android/native_window_jni.h> |
[email protected] | f2d7606 | 2013-01-09 17:09:24 | [diff] [blame] | 8 | #include <cpu-features.h> |
| 9 | |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 10 | #include "base/android/jni_array.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 11 | #include "base/logging.h" |
[email protected] | 613eef6 | 2012-11-09 23:46:54 | [diff] [blame] | 12 | #include "base/posix/global_descriptors.h" |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 13 | #include "content/common/android/scoped_java_surface.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 14 | #include "content/common/android/surface_texture_peer.h" |
[email protected] | 25be53c | 2012-11-29 16:51:52 | [diff] [blame] | 15 | #include "content/common/child_process.h" |
| 16 | #include "content/common/child_thread.h" |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 17 | #include "content/common/gpu/gpu_surface_lookup.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 18 | #include "content/public/app/android_library_loader_hooks.h" |
[email protected] | 1602cde | 2012-06-26 15:09:12 | [diff] [blame] | 19 | #include "content/public/common/content_descriptors.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 20 | #include "ipc/ipc_descriptors.h" |
[email protected] | e46f6615 | 2012-07-19 20:02:55 | [diff] [blame] | 21 | #include "jni/SandboxedProcessService_jni.h" |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 22 | |
| 23 | using base::android::AttachCurrentThread; |
| 24 | using base::android::CheckException; |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 25 | using base::android::JavaIntArrayToIntVector; |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 26 | |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 27 | namespace content { |
| 28 | |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 29 | namespace { |
| 30 | |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 31 | class SurfaceTexturePeerSandboxedImpl : public content::SurfaceTexturePeer, |
| 32 | public content::GpuSurfaceLookup { |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 33 | public: |
| 34 | // |service| is the instance of |
| 35 | // org.chromium.content.app.SandboxedProcessService. |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 36 | explicit SurfaceTexturePeerSandboxedImpl( |
| 37 | const base::android::ScopedJavaLocalRef<jobject>& service) |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 38 | : service_(service) { |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 39 | GpuSurfaceLookup::InitInstance(this); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | virtual ~SurfaceTexturePeerSandboxedImpl() { |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 43 | GpuSurfaceLookup::InitInstance(NULL); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 44 | } |
| 45 | |
[email protected] | 11387d28 | 2012-10-10 04:38:17 | [diff] [blame] | 46 | virtual void EstablishSurfaceTexturePeer( |
| 47 | base::ProcessHandle pid, |
[email protected] | 11387d28 | 2012-10-10 04:38:17 | [diff] [blame] | 48 | scoped_refptr<content::SurfaceTextureBridge> surface_texture_bridge, |
| 49 | int primary_id, |
| 50 | int secondary_id) { |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 51 | JNIEnv* env = base::android::AttachCurrentThread(); |
[email protected] | 1963002 | 2012-06-25 18:58:07 | [diff] [blame] | 52 | content::Java_SandboxedProcessService_establishSurfaceTexturePeer( |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 53 | env, service_.obj(), pid, |
[email protected] | 11387d28 | 2012-10-10 04:38:17 | [diff] [blame] | 54 | surface_texture_bridge->j_surface_texture().obj(), primary_id, |
| 55 | secondary_id); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 56 | CheckException(env); |
| 57 | } |
| 58 | |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 59 | virtual gfx::AcceleratedWidget AcquireNativeWidget(int surface_id) OVERRIDE { |
| 60 | JNIEnv* env = base::android::AttachCurrentThread(); |
| 61 | ScopedJavaSurface surface( |
| 62 | content::Java_SandboxedProcessService_getViewSurface( |
| 63 | env, service_.obj(), surface_id)); |
| 64 | |
| 65 | if (surface.j_surface().is_null()) |
| 66 | return NULL; |
| 67 | |
| 68 | ANativeWindow* native_window = ANativeWindow_fromSurface( |
| 69 | env, surface.j_surface().obj()); |
| 70 | |
| 71 | return native_window; |
| 72 | } |
| 73 | |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 74 | private: |
| 75 | // The instance of org.chromium.content.app.SandboxedProcessService. |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 76 | base::android::ScopedJavaGlobalRef<jobject> service_; |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 77 | |
| 78 | DISALLOW_COPY_AND_ASSIGN(SurfaceTexturePeerSandboxedImpl); |
| 79 | }; |
| 80 | |
| 81 | // Chrome actually uses the renderer code path for all of its sandboxed |
| 82 | // processes such as renderers, plugins, etc. |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 83 | void InternalInitSandboxedProcess(const std::vector<int>& file_ids, |
| 84 | const std::vector<int>& file_fds, |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 85 | JNIEnv* env, |
| 86 | jclass clazz, |
| 87 | jobject context, |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 88 | jobject service_in, |
[email protected] | f2d7606 | 2013-01-09 17:09:24 | [diff] [blame] | 89 | jint cpu_count, |
| 90 | jlong cpu_features) { |
[email protected] | ccf558a | 2013-03-07 03:58:18 | [diff] [blame^] | 91 | base::android::ScopedJavaLocalRef<jobject> service(env, service_in); |
| 92 | |
[email protected] | f2d7606 | 2013-01-09 17:09:24 | [diff] [blame] | 93 | // Set the CPU properties. |
| 94 | android_setCpu(cpu_count, cpu_features); |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 95 | // Register the file descriptors. |
| 96 | // This includes the IPC channel, the crash dump signals and resource related |
| 97 | // files. |
| 98 | DCHECK(file_fds.size() == file_ids.size()); |
| 99 | for (size_t i = 0; i < file_ids.size(); ++i) |
| 100 | base::GlobalDescriptors::GetInstance()->Set(file_ids[i], file_fds[i]); |
[email protected] | 3093536 | 2012-06-28 21:26:23 | [diff] [blame] | 101 | |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 102 | content::SurfaceTexturePeer::InitInstance( |
| 103 | new SurfaceTexturePeerSandboxedImpl(service)); |
| 104 | |
| 105 | } |
| 106 | |
[email protected] | 25be53c | 2012-11-29 16:51:52 | [diff] [blame] | 107 | void QuitSandboxMainThreadMessageLoop() { |
| 108 | MessageLoop::current()->Quit(); |
| 109 | } |
| 110 | |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 111 | } // namespace <anonymous> |
| 112 | |
[email protected] | 1963002 | 2012-06-25 18:58:07 | [diff] [blame] | 113 | void InitSandboxedProcess(JNIEnv* env, |
| 114 | jclass clazz, |
| 115 | jobject context, |
| 116 | jobject service, |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 117 | jintArray j_file_ids, |
[email protected] | f2d7606 | 2013-01-09 17:09:24 | [diff] [blame] | 118 | jintArray j_file_fds, |
| 119 | jint cpu_count, |
| 120 | jlong cpu_features) { |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 121 | std::vector<int> file_ids; |
| 122 | std::vector<int> file_fds; |
| 123 | JavaIntArrayToIntVector(env, j_file_ids, &file_ids); |
| 124 | JavaIntArrayToIntVector(env, j_file_fds, &file_fds); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 125 | |
[email protected] | c7abd42 | 2012-09-25 00:20:08 | [diff] [blame] | 126 | InternalInitSandboxedProcess( |
[email protected] | f2d7606 | 2013-01-09 17:09:24 | [diff] [blame] | 127 | file_ids, file_fds, env, clazz, context, service, |
| 128 | cpu_count, cpu_features); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 129 | } |
| 130 | |
[email protected] | 1963002 | 2012-06-25 18:58:07 | [diff] [blame] | 131 | void ExitSandboxedProcess(JNIEnv* env, jclass clazz) { |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 132 | LOG(INFO) << "SandboxedProcessService: Exiting sandboxed process."; |
[email protected] | 1963002 | 2012-06-25 18:58:07 | [diff] [blame] | 133 | LibraryLoaderExitHook(); |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 134 | _exit(0); |
| 135 | } |
| 136 | |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 137 | bool RegisterSandboxedProcessService(JNIEnv* env) { |
| 138 | return RegisterNativesImpl(env); |
| 139 | } |
| 140 | |
[email protected] | 25be53c | 2012-11-29 16:51:52 | [diff] [blame] | 141 | void ShutdownSandboxMainThread(JNIEnv* env, jobject obj) { |
| 142 | ChildProcess* current_process = ChildProcess::current(); |
| 143 | if (!current_process) |
| 144 | return; |
| 145 | ChildThread* main_child_thread = current_process->main_thread(); |
| 146 | if (main_child_thread && main_child_thread->message_loop()) |
| 147 | main_child_thread->message_loop()->PostTask(FROM_HERE, |
| 148 | base::Bind(&QuitSandboxMainThreadMessageLoop)); |
| 149 | } |
| 150 | |
[email protected] | 5b6f069 | 2012-06-12 05:00:05 | [diff] [blame] | 151 | } // namespace content |