Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / content / app / android / child_process_service.cc
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/child_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/android/library_loader/library_loader_hooks.h"
12 #include "base/android/memory_pressure_listener_android.h"
13 #include "base/logging.h"
14 #include "base/posix/global_descriptors.h"
15 #include "content/child/child_thread.h"
16 #include "content/common/android/surface_texture_manager.h"
17 #include "content/common/android/surface_texture_peer.h"
18 #include "content/common/gpu/gpu_surface_lookup.h"
19 #include "content/public/app/android_library_loader_hooks.h"
20 #include "content/public/common/content_descriptors.h"
21 #include "ipc/ipc_descriptors.h"
22 #include "jni/ChildProcessService_jni.h"
23 #include "ui/gl/android/scoped_java_surface.h"
24
25 using base::android::AttachCurrentThread;
26 using base::android::CheckException;
27 using base::android::JavaIntArrayToIntVector;
28
29 namespace content {
30
31 namespace {
32
33 // TODO(sievers): Use two different implementations of this depending on if
34 // we're in a renderer or gpu process.
35 class SurfaceTextureManagerImpl : public SurfaceTextureManager,
36                                   public SurfaceTexturePeer,
37                                   public GpuSurfaceLookup {
38  public:
39   // |service| is the instance of
40   // org.chromium.content.app.ChildProcessService.
41   explicit SurfaceTextureManagerImpl(
42       const base::android::ScopedJavaLocalRef<jobject>& service)
43       : service_(service) {
44     SurfaceTexturePeer::InitInstance(this);
45     GpuSurfaceLookup::InitInstance(this);
46   }
47   virtual ~SurfaceTextureManagerImpl() {
48     SurfaceTexturePeer::InitInstance(NULL);
49     GpuSurfaceLookup::InitInstance(NULL);
50   }
51
52   // Overridden from SurfaceTextureManager:
53   virtual void RegisterSurfaceTexture(
54       int surface_texture_id,
55       int client_id,
56       gfx::SurfaceTexture* surface_texture) override {
57     JNIEnv* env = base::android::AttachCurrentThread();
58     Java_ChildProcessService_createSurfaceTextureSurface(
59         env,
60         service_.obj(),
61         surface_texture_id,
62         client_id,
63         surface_texture->j_surface_texture().obj());
64   }
65   virtual void UnregisterSurfaceTexture(int surface_texture_id,
66                                         int client_id) override {
67     JNIEnv* env = base::android::AttachCurrentThread();
68     Java_ChildProcessService_destroySurfaceTextureSurface(
69         env, service_.obj(), surface_texture_id, client_id);
70   }
71   virtual gfx::AcceleratedWidget AcquireNativeWidgetForSurfaceTexture(
72       int surface_texture_id) override {
73     JNIEnv* env = base::android::AttachCurrentThread();
74     gfx::ScopedJavaSurface surface(
75         Java_ChildProcessService_getSurfaceTextureSurface(env, service_.obj(),
76                                                           surface_texture_id));
77
78     if (surface.j_surface().is_null())
79       return NULL;
80
81     // Note: This ensures that any local references used by
82     // ANativeWindow_fromSurface are released immediately. This is needed as a
83     // workaround for https://code.google.com/p/android/issues/detail?id=68174
84     base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
85     ANativeWindow* native_window =
86         ANativeWindow_fromSurface(env, surface.j_surface().obj());
87
88     return native_window;
89   }
90
91   // Overridden from SurfaceTexturePeer:
92   virtual void EstablishSurfaceTexturePeer(
93       base::ProcessHandle pid,
94       scoped_refptr<gfx::SurfaceTexture> surface_texture,
95       int primary_id,
96       int secondary_id) override {
97     JNIEnv* env = base::android::AttachCurrentThread();
98     content::Java_ChildProcessService_establishSurfaceTexturePeer(
99         env,
100         service_.obj(),
101         pid,
102         surface_texture->j_surface_texture().obj(),
103         primary_id,
104         secondary_id);
105   }
106
107   // Overridden from GpuSurfaceLookup:
108   virtual gfx::AcceleratedWidget AcquireNativeWidget(int surface_id) override {
109     JNIEnv* env = base::android::AttachCurrentThread();
110     gfx::ScopedJavaSurface surface(
111         content::Java_ChildProcessService_getViewSurface(
112             env, service_.obj(), surface_id));
113
114     if (surface.j_surface().is_null())
115       return NULL;
116
117     // Note: This ensures that any local references used by
118     // ANativeWindow_fromSurface are released immediately. This is needed as a
119     // workaround for https://code.google.com/p/android/issues/detail?id=68174
120     base::android::ScopedJavaLocalFrame scoped_local_reference_frame(env);
121     ANativeWindow* native_window =
122         ANativeWindow_fromSurface(env, surface.j_surface().obj());
123
124     return native_window;
125   }
126
127  private:
128   // The instance of org.chromium.content.app.ChildProcessService.
129   base::android::ScopedJavaGlobalRef<jobject> service_;
130
131   DISALLOW_COPY_AND_ASSIGN(SurfaceTextureManagerImpl);
132 };
133
134 // Chrome actually uses the renderer code path for all of its child
135 // processes such as renderers, plugins, etc.
136 void InternalInitChildProcess(const std::vector<int>& file_ids,
137                               const std::vector<int>& file_fds,
138                               JNIEnv* env,
139                               jclass clazz,
140                               jobject context,
141                               jobject service_in,
142                               jint cpu_count,
143                               jlong cpu_features) {
144   base::android::ScopedJavaLocalRef<jobject> service(env, service_in);
145
146   // Set the CPU properties.
147   android_setCpu(cpu_count, cpu_features);
148   // Register the file descriptors.
149   // This includes the IPC channel, the crash dump signals and resource related
150   // files.
151   DCHECK(file_fds.size() == file_ids.size());
152   for (size_t i = 0; i < file_ids.size(); ++i)
153     base::GlobalDescriptors::GetInstance()->Set(file_ids[i], file_fds[i]);
154
155   SurfaceTextureManager::InitInstance(new SurfaceTextureManagerImpl(service));
156
157   base::android::MemoryPressureListenerAndroid::RegisterSystemCallback(env);
158 }
159
160 }  // namespace <anonymous>
161
162 void InitChildProcess(JNIEnv* env,
163                       jclass clazz,
164                       jobject context,
165                       jobject service,
166                       jintArray j_file_ids,
167                       jintArray j_file_fds,
168                       jint cpu_count,
169                       jlong cpu_features) {
170   std::vector<int> file_ids;
171   std::vector<int> file_fds;
172   JavaIntArrayToIntVector(env, j_file_ids, &file_ids);
173   JavaIntArrayToIntVector(env, j_file_fds, &file_fds);
174
175   InternalInitChildProcess(
176       file_ids, file_fds, env, clazz, context, service,
177       cpu_count, cpu_features);
178 }
179
180 void ExitChildProcess(JNIEnv* env, jclass clazz) {
181   VLOG(0) << "ChildProcessService: Exiting child process.";
182   base::android::LibraryLoaderExitHook();
183   _exit(0);
184 }
185
186 bool RegisterChildProcessService(JNIEnv* env) {
187   return RegisterNativesImpl(env);
188 }
189
190 void ShutdownMainThread(JNIEnv* env, jobject obj) {
191   ChildThread::ShutdownThread();
192 }
193
194 }  // namespace content