blob: 2f0d5bc92324edd9dba1efa651b07c78a880d148 [file] [log] [blame]
[email protected]5b6f0692012-06-12 05:00:051// 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]ccf558a2013-03-07 03:58:187#include <android/native_window_jni.h>
[email protected]f2d76062013-01-09 17:09:248#include <cpu-features.h>
9
[email protected]30935362012-06-28 21:26:2310#include "base/android/jni_array.h"
[email protected]5b6f0692012-06-12 05:00:0511#include "base/logging.h"
[email protected]613eef62012-11-09 23:46:5412#include "base/posix/global_descriptors.h"
[email protected]ccf558a2013-03-07 03:58:1813#include "content/common/android/scoped_java_surface.h"
[email protected]5b6f0692012-06-12 05:00:0514#include "content/common/android/surface_texture_peer.h"
[email protected]25be53c2012-11-29 16:51:5215#include "content/common/child_process.h"
16#include "content/common/child_thread.h"
[email protected]ccf558a2013-03-07 03:58:1817#include "content/common/gpu/gpu_surface_lookup.h"
[email protected]5b6f0692012-06-12 05:00:0518#include "content/public/app/android_library_loader_hooks.h"
[email protected]1602cde2012-06-26 15:09:1219#include "content/public/common/content_descriptors.h"
[email protected]5b6f0692012-06-12 05:00:0520#include "ipc/ipc_descriptors.h"
[email protected]e46f66152012-07-19 20:02:5521#include "jni/SandboxedProcessService_jni.h"
[email protected]5b6f0692012-06-12 05:00:0522
23using base::android::AttachCurrentThread;
24using base::android::CheckException;
[email protected]30935362012-06-28 21:26:2325using base::android::JavaIntArrayToIntVector;
[email protected]5b6f0692012-06-12 05:00:0526
[email protected]ccf558a2013-03-07 03:58:1827namespace content {
28
[email protected]5b6f0692012-06-12 05:00:0529namespace {
30
[email protected]ccf558a2013-03-07 03:58:1831class SurfaceTexturePeerSandboxedImpl : public content::SurfaceTexturePeer,
32 public content::GpuSurfaceLookup {
[email protected]5b6f0692012-06-12 05:00:0533 public:
34 // |service| is the instance of
35 // org.chromium.content.app.SandboxedProcessService.
[email protected]ccf558a2013-03-07 03:58:1836 explicit SurfaceTexturePeerSandboxedImpl(
37 const base::android::ScopedJavaLocalRef<jobject>& service)
[email protected]5b6f0692012-06-12 05:00:0538 : service_(service) {
[email protected]ccf558a2013-03-07 03:58:1839 GpuSurfaceLookup::InitInstance(this);
[email protected]5b6f0692012-06-12 05:00:0540 }
41
42 virtual ~SurfaceTexturePeerSandboxedImpl() {
[email protected]ccf558a2013-03-07 03:58:1843 GpuSurfaceLookup::InitInstance(NULL);
[email protected]5b6f0692012-06-12 05:00:0544 }
45
[email protected]11387d282012-10-10 04:38:1746 virtual void EstablishSurfaceTexturePeer(
47 base::ProcessHandle pid,
[email protected]11387d282012-10-10 04:38:1748 scoped_refptr<content::SurfaceTextureBridge> surface_texture_bridge,
49 int primary_id,
50 int secondary_id) {
[email protected]5b6f0692012-06-12 05:00:0551 JNIEnv* env = base::android::AttachCurrentThread();
[email protected]19630022012-06-25 18:58:0752 content::Java_SandboxedProcessService_establishSurfaceTexturePeer(
[email protected]ccf558a2013-03-07 03:58:1853 env, service_.obj(), pid,
[email protected]11387d282012-10-10 04:38:1754 surface_texture_bridge->j_surface_texture().obj(), primary_id,
55 secondary_id);
[email protected]5b6f0692012-06-12 05:00:0556 CheckException(env);
57 }
58
[email protected]ccf558a2013-03-07 03:58:1859 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]5b6f0692012-06-12 05:00:0574 private:
75 // The instance of org.chromium.content.app.SandboxedProcessService.
[email protected]ccf558a2013-03-07 03:58:1876 base::android::ScopedJavaGlobalRef<jobject> service_;
[email protected]5b6f0692012-06-12 05:00:0577
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]c7abd422012-09-25 00:20:0883void InternalInitSandboxedProcess(const std::vector<int>& file_ids,
84 const std::vector<int>& file_fds,
[email protected]5b6f0692012-06-12 05:00:0585 JNIEnv* env,
86 jclass clazz,
87 jobject context,
[email protected]ccf558a2013-03-07 03:58:1888 jobject service_in,
[email protected]f2d76062013-01-09 17:09:2489 jint cpu_count,
90 jlong cpu_features) {
[email protected]ccf558a2013-03-07 03:58:1891 base::android::ScopedJavaLocalRef<jobject> service(env, service_in);
92
[email protected]f2d76062013-01-09 17:09:2493 // Set the CPU properties.
94 android_setCpu(cpu_count, cpu_features);
[email protected]c7abd422012-09-25 00:20:0895 // 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]30935362012-06-28 21:26:23101
[email protected]5b6f0692012-06-12 05:00:05102 content::SurfaceTexturePeer::InitInstance(
103 new SurfaceTexturePeerSandboxedImpl(service));
104
105}
106
[email protected]25be53c2012-11-29 16:51:52107void QuitSandboxMainThreadMessageLoop() {
108 MessageLoop::current()->Quit();
109}
110
[email protected]5b6f0692012-06-12 05:00:05111} // namespace <anonymous>
112
[email protected]19630022012-06-25 18:58:07113void InitSandboxedProcess(JNIEnv* env,
114 jclass clazz,
115 jobject context,
116 jobject service,
[email protected]c7abd422012-09-25 00:20:08117 jintArray j_file_ids,
[email protected]f2d76062013-01-09 17:09:24118 jintArray j_file_fds,
119 jint cpu_count,
120 jlong cpu_features) {
[email protected]c7abd422012-09-25 00:20:08121 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]5b6f0692012-06-12 05:00:05125
[email protected]c7abd422012-09-25 00:20:08126 InternalInitSandboxedProcess(
[email protected]f2d76062013-01-09 17:09:24127 file_ids, file_fds, env, clazz, context, service,
128 cpu_count, cpu_features);
[email protected]5b6f0692012-06-12 05:00:05129}
130
[email protected]19630022012-06-25 18:58:07131void ExitSandboxedProcess(JNIEnv* env, jclass clazz) {
[email protected]5b6f0692012-06-12 05:00:05132 LOG(INFO) << "SandboxedProcessService: Exiting sandboxed process.";
[email protected]19630022012-06-25 18:58:07133 LibraryLoaderExitHook();
[email protected]5b6f0692012-06-12 05:00:05134 _exit(0);
135}
136
[email protected]5b6f0692012-06-12 05:00:05137bool RegisterSandboxedProcessService(JNIEnv* env) {
138 return RegisterNativesImpl(env);
139}
140
[email protected]25be53c2012-11-29 16:51:52141void 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]5b6f0692012-06-12 05:00:05151} // namespace content