| 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/browser/android/sandboxed_process_launcher.h" | |
| 6 | |
| 7 #include "base/android/jni_android.h" | |
| 8 #include "base/android/jni_array.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "content/browser/android/media_player_manager_android.h" | |
| 12 #include "content/browser/renderer_host/compositor_impl_android.h" | |
| 13 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 14 #include "content/common/android/scoped_java_surface.h" | |
| 15 #include "content/public/browser/browser_thread.h" | |
| 16 #include "content/public/browser/render_process_host.h" | |
| 17 #include "jni/SandboxedProcessLauncher_jni.h" | |
| 18 #include "media/base/android/media_player_bridge.h" | |
| 19 | |
| 20 using base::android::AttachCurrentThread; | |
| 21 using base::android::ToJavaArrayOfStrings; | |
| 22 using base::android::ScopedJavaGlobalRef; | |
| 23 using base::android::ScopedJavaLocalRef; | |
| 24 using content::StartSandboxedProcessCallback; | |
| 25 | |
| 26 namespace content { | |
| 27 | |
| 28 namespace { | |
| 29 | |
| 30 // Pass a java surface object to the MediaPlayerBridge object | |
| 31 // identified by render process handle, render view ID and player ID. | |
| 32 static void SetSurfacePeer( | |
| 33 const base::android::JavaRef<jobject>& surface, | |
| 34 base::ProcessHandle render_process_handle, | |
| 35 int render_view_id, | |
| 36 int player_id) { | |
| 37 int renderer_id = 0; | |
| 38 RenderProcessHost::iterator it = RenderProcessHost::AllHostsIterator(); | |
| 39 while (!it.IsAtEnd()) { | |
| 40 if (it.GetCurrentValue()->GetHandle() == render_process_handle) { | |
| 41 renderer_id = it.GetCurrentValue()->GetID(); | |
| 42 break; | |
| 43 } | |
| 44 it.Advance(); | |
| 45 } | |
| 46 | |
| 47 if (renderer_id) { | |
| 48 RenderViewHostImpl* host = RenderViewHostImpl::FromID( | |
| 49 renderer_id, render_view_id); | |
| 50 if (host) { | |
| 51 media::MediaPlayerBridge* player = | |
| 52 host->media_player_manager()->GetPlayer(player_id); | |
| 53 if (player && | |
| 54 player != host->media_player_manager()->GetFullscreenPlayer()) { | |
| 55 ScopedJavaSurface scoped_surface(surface); | |
| 56 player->SetVideoSurface(scoped_surface.j_surface().obj()); | |
| 57 } | |
| 58 } | |
| 59 } | |
| 60 } | |
| 61 | |
| 62 } // anonymous namespace | |
| 63 | |
| 64 // Called from SandboxedProcessLauncher.java when the SandboxedProcess was | |
| 65 // started. | |
| 66 // |client_context| is the pointer to StartSandboxedProcessCallback which was | |
| 67 // passed in from StartSandboxedProcess. | |
| 68 // |handle| is the processID of the child process as originated in Java, 0 if | |
| 69 // the SandboxedProcess could not be created. | |
| 70 static void OnSandboxedProcessStarted(JNIEnv*, | |
| 71 jclass, | |
| 72 jint client_context, | |
| 73 jint handle) { | |
| 74 StartSandboxedProcessCallback* callback = | |
| 75 reinterpret_cast<StartSandboxedProcessCallback*>(client_context); | |
| 76 if (handle) | |
| 77 callback->Run(static_cast<base::ProcessHandle>(handle)); | |
| 78 delete callback; | |
| 79 } | |
| 80 | |
| 81 void StartSandboxedProcess( | |
| 82 const CommandLine::StringVector& argv, | |
| 83 const std::vector<content::FileDescriptorInfo>& files_to_register, | |
| 84 const StartSandboxedProcessCallback& callback) { | |
| 85 JNIEnv* env = AttachCurrentThread(); | |
| 86 DCHECK(env); | |
| 87 | |
| 88 // Create the Command line String[] | |
| 89 ScopedJavaLocalRef<jobjectArray> j_argv = ToJavaArrayOfStrings(env, argv); | |
| 90 | |
| 91 size_t file_count = files_to_register.size(); | |
| 92 DCHECK(file_count > 0); | |
| 93 | |
| 94 ScopedJavaLocalRef<jintArray> j_file_ids(env, env->NewIntArray(file_count)); | |
| 95 base::android::CheckException(env); | |
| 96 jint* file_ids = env->GetIntArrayElements(j_file_ids.obj(), NULL); | |
| 97 base::android::CheckException(env); | |
| 98 ScopedJavaLocalRef<jintArray> j_file_fds(env, env->NewIntArray(file_count)); | |
| 99 base::android::CheckException(env); | |
| 100 jint* file_fds = env->GetIntArrayElements(j_file_fds.obj(), NULL); | |
| 101 base::android::CheckException(env); | |
| 102 ScopedJavaLocalRef<jbooleanArray> j_file_auto_close( | |
| 103 env, env->NewBooleanArray(file_count)); | |
| 104 base::android::CheckException(env); | |
| 105 jboolean* file_auto_close = | |
| 106 env->GetBooleanArrayElements(j_file_auto_close.obj(), NULL); | |
| 107 base::android::CheckException(env); | |
| 108 for (size_t i = 0; i < file_count; ++i) { | |
| 109 const content::FileDescriptorInfo& fd_info = files_to_register[i]; | |
| 110 file_ids[i] = fd_info.id; | |
| 111 file_fds[i] = fd_info.fd.fd; | |
| 112 file_auto_close[i] = fd_info.fd.auto_close; | |
| 113 } | |
| 114 env->ReleaseIntArrayElements(j_file_ids.obj(), file_ids, 0); | |
| 115 env->ReleaseIntArrayElements(j_file_fds.obj(), file_fds, 0); | |
| 116 env->ReleaseBooleanArrayElements(j_file_auto_close.obj(), file_auto_close, 0); | |
| 117 | |
| 118 Java_SandboxedProcessLauncher_start(env, | |
| 119 base::android::GetApplicationContext(), | |
| 120 j_argv.obj(), | |
| 121 j_file_ids.obj(), | |
| 122 j_file_fds.obj(), | |
| 123 j_file_auto_close.obj(), | |
| 124 reinterpret_cast<jint>(new StartSandboxedProcessCallback(callback))); | |
| 125 } | |
| 126 | |
| 127 void StopSandboxedProcess(base::ProcessHandle handle) { | |
| 128 JNIEnv* env = AttachCurrentThread(); | |
| 129 DCHECK(env); | |
| 130 Java_SandboxedProcessLauncher_stop(env, static_cast<jint>(handle)); | |
| 131 } | |
| 132 | |
| 133 void EstablishSurfacePeer( | |
| 134 JNIEnv* env, jclass clazz, | |
| 135 jint pid, jobject surface, jint primary_id, jint secondary_id) { | |
| 136 ScopedJavaGlobalRef<jobject> jsurface; | |
| 137 jsurface.Reset(env, surface); | |
| 138 if (jsurface.is_null()) | |
| 139 return; | |
| 140 | |
| 141 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 142 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, base::Bind( | |
| 143 &SetSurfacePeer, jsurface, pid, primary_id, secondary_id)); | |
| 144 } | |
| 145 | |
| 146 jobject GetViewSurface(JNIEnv* env, jclass clazz, jint surface_id) { | |
| 147 // This is a synchronous call from the GPU process and is expected to be | |
| 148 // handled on a binder thread. Handling this on the UI thread will lead | |
| 149 // to deadlocks. | |
| 150 DCHECK(!BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 151 return CompositorImpl::GetSurface(surface_id); | |
| 152 } | |
| 153 | |
| 154 bool RegisterSandboxedProcessLauncher(JNIEnv* env) { | |
| 155 return RegisterNativesImpl(env); | |
| 156 } | |
| 157 | |
| 158 } // namespace content | |
| OLD | NEW |