| OLD | NEW |
| (Empty) |
| 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 | |
| 7 #include <android/native_window_jni.h> | |
| 8 #include <cpu-features.h> | |
| 9 | |
| 10 #include "base/android/jni_array.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "base/posix/global_descriptors.h" | |
| 13 #include "content/common/android/scoped_java_surface.h" | |
| 14 #include "content/common/android/surface_texture_peer.h" | |
| 15 #include "content/common/child_process.h" | |
| 16 #include "content/common/child_thread.h" | |
| 17 #include "content/common/gpu/gpu_surface_lookup.h" | |
| 18 #include "content/public/app/android_library_loader_hooks.h" | |
| 19 #include "content/public/common/content_descriptors.h" | |
| 20 #include "ipc/ipc_descriptors.h" | |
| 21 #include "jni/SandboxedProcessService_jni.h" | |
| 22 | |
| 23 using base::android::AttachCurrentThread; | |
| 24 using base::android::CheckException; | |
| 25 using base::android::JavaIntArrayToIntVector; | |
| 26 | |
| 27 namespace content { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 class SurfaceTexturePeerSandboxedImpl : public content::SurfaceTexturePeer, | |
| 32 public content::GpuSurfaceLookup { | |
| 33 public: | |
| 34 // |service| is the instance of | |
| 35 // org.chromium.content.app.SandboxedProcessService. | |
| 36 explicit SurfaceTexturePeerSandboxedImpl( | |
| 37 const base::android::ScopedJavaLocalRef<jobject>& service) | |
| 38 : service_(service) { | |
| 39 GpuSurfaceLookup::InitInstance(this); | |
| 40 } | |
| 41 | |
| 42 virtual ~SurfaceTexturePeerSandboxedImpl() { | |
| 43 GpuSurfaceLookup::InitInstance(NULL); | |
| 44 } | |
| 45 | |
| 46 virtual void EstablishSurfaceTexturePeer( | |
| 47 base::ProcessHandle pid, | |
| 48 scoped_refptr<content::SurfaceTextureBridge> surface_texture_bridge, | |
| 49 int primary_id, | |
| 50 int secondary_id) { | |
| 51 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 52 content::Java_SandboxedProcessService_establishSurfaceTexturePeer( | |
| 53 env, service_.obj(), pid, | |
| 54 surface_texture_bridge->j_surface_texture().obj(), primary_id, | |
| 55 secondary_id); | |
| 56 CheckException(env); | |
| 57 } | |
| 58 | |
| 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 | |
| 74 private: | |
| 75 // The instance of org.chromium.content.app.SandboxedProcessService. | |
| 76 base::android::ScopedJavaGlobalRef<jobject> service_; | |
| 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. | |
| 83 void InternalInitSandboxedProcess(const std::vector<int>& file_ids, | |
| 84 const std::vector<int>& file_fds, | |
| 85 JNIEnv* env, | |
| 86 jclass clazz, | |
| 87 jobject context, | |
| 88 jobject service_in, | |
| 89 jint cpu_count, | |
| 90 jlong cpu_features) { | |
| 91 base::android::ScopedJavaLocalRef<jobject> service(env, service_in); | |
| 92 | |
| 93 // Set the CPU properties. | |
| 94 android_setCpu(cpu_count, cpu_features); | |
| 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]); | |
| 101 | |
| 102 content::SurfaceTexturePeer::InitInstance( | |
| 103 new SurfaceTexturePeerSandboxedImpl(service)); | |
| 104 | |
| 105 } | |
| 106 | |
| 107 void QuitSandboxMainThreadMessageLoop() { | |
| 108 MessageLoop::current()->Quit(); | |
| 109 } | |
| 110 | |
| 111 } // namespace <anonymous> | |
| 112 | |
| 113 void InitSandboxedProcess(JNIEnv* env, | |
| 114 jclass clazz, | |
| 115 jobject context, | |
| 116 jobject service, | |
| 117 jintArray j_file_ids, | |
| 118 jintArray j_file_fds, | |
| 119 jint cpu_count, | |
| 120 jlong cpu_features) { | |
| 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); | |
| 125 | |
| 126 InternalInitSandboxedProcess( | |
| 127 file_ids, file_fds, env, clazz, context, service, | |
| 128 cpu_count, cpu_features); | |
| 129 } | |
| 130 | |
| 131 void ExitSandboxedProcess(JNIEnv* env, jclass clazz) { | |
| 132 LOG(INFO) << "SandboxedProcessService: Exiting sandboxed process."; | |
| 133 LibraryLoaderExitHook(); | |
| 134 _exit(0); | |
| 135 } | |
| 136 | |
| 137 bool RegisterSandboxedProcessService(JNIEnv* env) { | |
| 138 return RegisterNativesImpl(env); | |
| 139 } | |
| 140 | |
| 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 | |
| 151 } // namespace content | |
| OLD | NEW |